Compare commits

...
2 Commits
5 changed files with 84 additions and 88 deletions

No files matched your search

@@ -1,7 +1,5 @@
package crazypants.enderio;
import java.text.DecimalFormat;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
@@ -21,15 +19,15 @@ import crazypants.enderio.conduit.IConduit;
import crazypants.enderio.conduit.render.ConduitRenderer;
import crazypants.enderio.machine.hypercube.HyperCubeRegister;
import crazypants.enderio.machine.transceiver.ServerChannelRegister;
import crazypants.enderio.machine.wireless.WirelessChargerController;
public class CommonProxy {
private static final DecimalFormat FORMAT = new DecimalFormat("########0.000");
protected long serverTickCount = 0;
protected long clientTickCount = 0;
protected final TickTimer tickTimer = new TickTimer();
public ConduitNetworkTickHandler conduitNetworkTickHandler = null;
public WirelessChargerController wirelessChargerController = null;
public CommonProxy() {}
@@ -86,6 +84,8 @@ public class CommonProxy {
public void onServerAboutToStart(FMLServerAboutToStartEvent event) {
conduitNetworkTickHandler = new ConduitNetworkTickHandler();
FMLCommonHandler.instance().bus().register(conduitNetworkTickHandler);
wirelessChargerController = new WirelessChargerController();
FMLCommonHandler.instance().bus().register(wirelessChargerController);
}
public void onServerStarted(FMLServerStartedEvent event) {
@@ -97,6 +97,8 @@ public class CommonProxy {
HyperCubeRegister.unload();
FMLCommonHandler.instance().bus().unregister(conduitNetworkTickHandler);
conduitNetworkTickHandler = null;
FMLCommonHandler.instance().bus().unregister(wirelessChargerController);
wirelessChargerController = null;
}
public final class TickTimer {
@@ -10,7 +10,6 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
@@ -41,8 +40,8 @@ public class NetworkedInventory {
int tickDeficit;
// work around for a vanilla chest changing into a double chest without doing unneeded checks all the time
boolean recheckInv = false;
private TileEntity connectedTileEntity;
// Hack for TiC crafting station not working correctly when setting output slot to null
boolean ticHack = false;
@@ -61,19 +60,13 @@ public class NetworkedInventory {
this.location = location;
world = con.getBundle().getWorld();
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if (te.getClass().getName().equals("tconstruct.tools.logic.CraftingStationLogic")) {
ticHack = true;
} else if (te.getClass().getName().contains("cpw.mods.ironchest")) {
recheckInv = true;
} else if (te instanceof TileEntityChest) {
recheckInv = true;
} else if (te instanceof TileInventoryPanel) {
inventoryPanel = true;
}
updateInventory();
}
public boolean isInvalid() {
return connectedTileEntity == null || connectedTileEntity.isInvalid();
}
public boolean hasTarget(IItemConduit conduit, ForgeDirection dir) {
for (Target t : sendPriority) {
if (t.inv.con == conduit && t.inv.conDir == dir) {
@@ -110,6 +103,13 @@ public class NetworkedInventory {
}
public void onTick() {
if (isInvalid()) {
updateInventory();
if (isInvalid()) {
return;
}
}
if (tickDeficit > 0 || !canExtract() || !con.isExtractionRedstoneConditionMet(conDir)) {
// do nothing
} else {
@@ -144,11 +144,6 @@ public class NetworkedInventory {
}
private boolean transferItems() {
if (recheckInv) {
updateInventory();
}
int[] slotIndices = getInventory().getAccessibleSlotsFromSide(inventorySide);
if (slotIndices == null) {
return false;
@@ -257,9 +252,6 @@ public class NetworkedInventory {
matchedStickyInput = of != null && of.isValid() && of.doesItemPassFilter(this, toExtract);
}
if (target.stickyInput || !matchedStickyInput) {
if (target.inv.recheckInv) {
target.inv.updateInventory();
}
int inserted = target.inv.insertItem(toExtract);
if (inserted > 0) {
toExtract.stackSize -= inserted;
@@ -281,15 +273,38 @@ public class NetworkedInventory {
}
public final void updateInventory() {
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if (te instanceof ISidedInventory) {
inv = (ISidedInventory) te;
} else if (te instanceof IInventory) {
inv = new InventoryWrapper((IInventory) te);
TileEntity newTe = world.getTileEntity(location.x, location.y, location.z);
if (newTe != connectedTileEntity) {
connectedTileEntity = newTe;
ticHack = false;
inventoryPanel = false;
if (connectedTileEntity != null) {
if (connectedTileEntity.getClass().getName().equals("tconstruct.tools.logic.CraftingStationLogic")) {
ticHack = true;
} else if (connectedTileEntity instanceof TileInventoryPanel) {
inventoryPanel = true;
}
}
network.routesChanged();
}
if (connectedTileEntity instanceof ISidedInventory) {
inv = (ISidedInventory) connectedTileEntity;
} else if (connectedTileEntity instanceof IInventory) {
inv = new InventoryWrapper((IInventory) connectedTileEntity);
} else {
inv = null;
}
}
private int insertItem(ItemStack item) {
if (isInvalid()) {
updateInventory();
if (isInvalid()) {
return 0;
}
}
if (!canInsert() || item == null) {
return 0;
}
@@ -410,7 +425,7 @@ public class NetworkedInventory {
}
public ISidedInventory getInventoryRecheck() {
if (recheckInv) {
if (isInvalid()) {
updateInventory();
}
return inv;
@@ -6,6 +6,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyContainerItem;
import crazypants.enderio.EnderIO;
import crazypants.enderio.TileEntityEio;
import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.power.IInternalPowerReceiver;
@@ -28,7 +29,7 @@ public class TileWirelessCharger extends TileEntityEio implements IInternalPower
@Override
public void invalidate() {
super.invalidate();
WirelessChargerController.instance.deregisterCharger(this);
EnderIO.proxy.wirelessChargerController.deregisterCharger(this);
registered = false;
}
@@ -40,7 +41,7 @@ public class TileWirelessCharger extends TileEntityEio implements IInternalPower
}
if (!registered) {
WirelessChargerController.instance.registerCharger(this);
EnderIO.proxy.wirelessChargerController.registerCharger(this);
registered = true;
}
@@ -1,8 +1,6 @@
package crazypants.enderio.machine.wireless;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.minecraft.item.ItemStack;
@@ -10,6 +8,8 @@ import net.minecraft.tileentity.TileEntity;
import com.enderio.core.common.util.BlockCoord;
import crazypants.enderio.EnderIO;
public class WirelessChargedLocation {
private final TileEntity te;
@@ -18,34 +18,30 @@ public class WirelessChargedLocation {
public WirelessChargedLocation(TileEntity te) {
this.te = te;
this.chargers = new ArrayList<IWirelessCharger>();
this.chargers = new ArrayList<>();
/*
* Set lastChangeCount to one less than the current change count so that the charger list is updated on first
* use (unless ~4B changes happen in between). Do this instead directly updating so that WirelessChargedLocation
* can be used while the TileEntity is still in construction (or loading).
*/
this.lastChangeCount = WirelessChargerController.instance.getChangeCount() - 1;
this.lastChangeCount = EnderIO.proxy.wirelessChargerController.getChangeCount() - 1;
}
private void updateChargers() {
WirelessChargerController wcc = WirelessChargerController.instance;
WirelessChargerController wcc = EnderIO.proxy.wirelessChargerController;
chargers.clear();
lastChangeCount = wcc.getChangeCount();
final BlockCoord bc = new BlockCoord(te);
wcc.getChargers(te.getWorldObj(), bc, chargers);
Collections.sort(chargers, new Comparator<IWirelessCharger>() {
@Override
public int compare(IWirelessCharger o1, IWirelessCharger o2) {
int dist1 = o1.getLocation().getDistSq(bc);
int dist2 = o2.getLocation().getDistSq(bc);
return dist1 - dist2;
}
chargers.sort((o1, o2) -> {
int dist1 = o1.getLocation().getDistSq(bc);
int dist2 = o2.getLocation().getDistSq(bc);
return dist1 - dist2;
});
}
private void checkChangeCount() {
if (lastChangeCount != WirelessChargerController.instance.getChangeCount()) {
if (lastChangeCount != EnderIO.proxy.wirelessChargerController.getChangeCount()) {
updateChargers();
}
}
@@ -8,11 +8,9 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import com.enderio.core.common.util.BlockCoord;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
@@ -21,20 +19,17 @@ import crazypants.util.BaublesUtil;
public class WirelessChargerController {
public static WirelessChargerController instance = new WirelessChargerController();
public static final int RANGE = Config.wirelessChargerRange;
public static final int RANGE_SQ = RANGE * RANGE;
static {
FMLCommonHandler.instance().bus().register(WirelessChargerController.instance);
MinecraftForge.EVENT_BUS.register(WirelessChargerController.instance);
}
private static final int RANGE = Config.wirelessChargerRange;
private static final int RANGE_SQ = RANGE * RANGE;
private final Map<Integer, Map<BlockCoord, IWirelessCharger>> perWorldChargers = new HashMap<Integer, Map<BlockCoord, IWirelessCharger>>();
private int changeCount;
private WirelessChargerController() {}
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.side != Side.CLIENT && event.phase == TickEvent.Phase.END) {
chargePlayersItems(event.player);
}
}
public void registerCharger(IWirelessCharger charger) {
if (charger == null) {
@@ -54,28 +49,7 @@ public class WirelessChargerController {
changeCount++;
}
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.side == Side.CLIENT || event.phase != TickEvent.Phase.END) {
return;
}
chargePlayersItems(event.player);
}
public int getChangeCount() {
return changeCount;
}
public void getChargers(World world, BlockCoord bc, Collection<IWirelessCharger> res) {
Map<BlockCoord, IWirelessCharger> chargers = getChargersForWorld(world);
for (IWirelessCharger wc : chargers.values()) {
if (wc.getLocation().getDistSq(bc) <= RANGE_SQ) {
res.add(wc);
}
}
}
public void chargePlayersItems(EntityPlayer player) {
private void chargePlayersItems(EntityPlayer player) {
Map<BlockCoord, IWirelessCharger> chargers = getChargersForWorld(player.worldObj);
if (chargers.isEmpty()) {
return;
@@ -108,13 +82,21 @@ public class WirelessChargerController {
return res;
}
private Map<BlockCoord, IWirelessCharger> getChargersForWorld(World world) {
Map<BlockCoord, IWirelessCharger> res = perWorldChargers.get(world.provider.dimensionId);
if (res == null) {
res = new HashMap<BlockCoord, IWirelessCharger>();
perWorldChargers.put(world.provider.dimensionId, res);
public int getChangeCount() {
return changeCount;
}
public void getChargers(World world, BlockCoord bc, Collection<IWirelessCharger> res) {
Map<BlockCoord, IWirelessCharger> chargers = getChargersForWorld(world);
for (IWirelessCharger wc : chargers.values()) {
if (wc.getLocation().getDistSq(bc) <= RANGE_SQ) {
res.add(wc);
}
}
return res;
}
private Map<BlockCoord, IWirelessCharger> getChargersForWorld(World world) {
return perWorldChargers.computeIfAbsent(world.provider.dimensionId, k -> new HashMap<>());
}
public Collection<IWirelessCharger> getChargers(World world) {