From fbdbb388c369fae549cfde3eed38b536f7461d90 Mon Sep 17 00:00:00 2001 From: Connor-Colenso <52056774+Connor-Colenso@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:03:23 +0100 Subject: Cleaning up (#767) * Kill playerAPI * Gut more events and compat that is now obsolete * Remove commented out code * Remove final modifier from methods * Make more stuff final * Remove slow building ring * Protected -> private in final classes * More cleaning * More cleaning v2 * Purging * Clean DevHelper * Clean DevHelper 2 * delete DevHelper * remove useless IFMLLoadingPlugin.MCVersion annotation from @Mod files * check for obfuscation only once in the IFMLloadingPlugin.injectData * don't instantiate the static class ASMConfig * delete unused and empty implementation of IFMLCallHook * delete empty class * delete ClassesToTransform class that just holds the class names constants * delete unused methods and unsless logging in dummymod container * delete unused transformer ClassTransformer_TT_ThaumicRestorer * spotless * Clean * Clean utils * Build checkpoint * Purge * Stage 2 * Stage 3 * Stage 4 * Stage 5 * Stage 6 * Spotless * Imports * Stage idk * Stage 1 * Fix issue * Spotless * Format numbers --------- Co-authored-by: GTNH-Colen <54497873+GTNH-Colen@users.noreply.github.com> Co-authored-by: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Co-authored-by: Connor Colenso --- .../core/tileentities/ModTileEntities.java | 4 - .../core/tileentities/base/TileEntityBase.java | 2 +- .../general/TileEntityCircuitProgrammer.java | 2 +- .../general/TileEntityDecayablesChest.java | 5 +- .../general/TileEntityPlayerDoorBase.java | 281 --------- .../general/TileEntityVolumetricFlaskSetter.java | 40 +- .../machines/TileEntityAdvPooCollector.java | 2 +- .../machines/TileEntityPestKiller.java | 20 +- .../machines/TileEntityPooCollector.java | 2 +- .../machines/TileEntityProjectTable.java | 6 - .../machines/TileEntityRoundRobinator.java | 687 --------------------- 11 files changed, 36 insertions(+), 1015 deletions(-) delete mode 100644 src/main/java/gtPlusPlus/core/tileentities/general/TileEntityPlayerDoorBase.java delete mode 100644 src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityRoundRobinator.java (limited to 'src/main/java/gtPlusPlus/core/tileentities') diff --git a/src/main/java/gtPlusPlus/core/tileentities/ModTileEntities.java b/src/main/java/gtPlusPlus/core/tileentities/ModTileEntities.java index 1cb6e5699c..56cf2fdbab 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/ModTileEntities.java +++ b/src/main/java/gtPlusPlus/core/tileentities/ModTileEntities.java @@ -8,13 +8,11 @@ import gtPlusPlus.core.tileentities.general.TileEntityCircuitProgrammer; import gtPlusPlus.core.tileentities.general.TileEntityDecayablesChest; import gtPlusPlus.core.tileentities.general.TileEntityFishTrap; import gtPlusPlus.core.tileentities.general.TileEntityInfiniteFluid; -import gtPlusPlus.core.tileentities.general.TileEntityPlayerDoorBase; import gtPlusPlus.core.tileentities.general.TileEntityVolumetricFlaskSetter; import gtPlusPlus.core.tileentities.machines.TileEntityAdvPooCollector; import gtPlusPlus.core.tileentities.machines.TileEntityPestKiller; import gtPlusPlus.core.tileentities.machines.TileEntityPooCollector; import gtPlusPlus.core.tileentities.machines.TileEntityProjectTable; -import gtPlusPlus.core.tileentities.machines.TileEntityRoundRobinator; public class ModTileEntities { @@ -26,12 +24,10 @@ public class ModTileEntities { GameRegistry.registerTileEntity(TileEntityInfiniteFluid.class, "TileInfiniteFluid"); GameRegistry.registerTileEntity(TileEntityProjectTable.class, "TileProjectTable"); GameRegistry.registerTileEntity(TileEntityCircuitProgrammer.class, "TileCircuitProgrammer"); - GameRegistry.registerTileEntity(TileEntityPlayerDoorBase.class, "TilePlayerDoorBase"); GameRegistry.registerTileEntity(TileEntityDecayablesChest.class, "TileDecayablesChest"); GameRegistry.registerTileEntity(TileEntitySuperJukebox.class, "TileEntitySuperJukebox"); GameRegistry.registerTileEntity(TileEntitySuperLight.class, "TileEntitySuperLight"); GameRegistry.registerTileEntity(TileEntityPestKiller.class, "TileEntityPestKiller"); - GameRegistry.registerTileEntity(TileEntityRoundRobinator.class, "TileEntityRoundRobinator"); GameRegistry.registerTileEntity(TileEntityVolumetricFlaskSetter.class, "TileEntityVolumetricFlaskSetter"); } diff --git a/src/main/java/gtPlusPlus/core/tileentities/base/TileEntityBase.java b/src/main/java/gtPlusPlus/core/tileentities/base/TileEntityBase.java index 762d5a4d61..56197098c6 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/base/TileEntityBase.java +++ b/src/main/java/gtPlusPlus/core/tileentities/base/TileEntityBase.java @@ -473,7 +473,7 @@ public class TileEntityBase extends TileEntity implements ILazyCoverable, IGregT public boolean ignoreUnloadedChunks = true; public boolean isDead = false; - private final void clearNullMarkersFromTileEntityBuffer() { + private void clearNullMarkersFromTileEntityBuffer() { for (int i = 0; i < this.mBufferedTileEntities.length; ++i) { if (this.mBufferedTileEntities[i] == this) { this.mBufferedTileEntities[i] = null; diff --git a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityCircuitProgrammer.java b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityCircuitProgrammer.java index a170f71af3..895c68029d 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityCircuitProgrammer.java +++ b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityCircuitProgrammer.java @@ -65,7 +65,7 @@ public class TileEntityCircuitProgrammer extends TileEntity implements ISidedInv if (aInputs[25] != null) { hasOutput = true; } - AutoMap aValidSlots = new AutoMap(); + AutoMap aValidSlots = new AutoMap<>(); int aSlotCount = 0; for (ItemStack i : aInputs) { if (i != null) { diff --git a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityDecayablesChest.java b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityDecayablesChest.java index 5da2560cde..36df996eb0 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityDecayablesChest.java +++ b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityDecayablesChest.java @@ -68,8 +68,7 @@ public class TileEntityDecayablesChest extends TileEntity implements ISidedInven if (inv == null) { continue; } - if (inv.getItem() instanceof DustDecayable) { - DustDecayable D = (DustDecayable) inv.getItem(); + if (inv.getItem() instanceof DustDecayable D) { tryUpdateDecayable(D, inv, this.worldObj); } } @@ -353,7 +352,7 @@ public class TileEntityDecayablesChest extends TileEntity implements ISidedInven this.checkForAdjacentChests(); } - private final int updateSlots() { + private int updateSlots() { // Have slots changed? if (cachedChestType == 0) { return 0; diff --git a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityPlayerDoorBase.java b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityPlayerDoorBase.java deleted file mode 100644 index 86e5454b80..0000000000 --- a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityPlayerDoorBase.java +++ /dev/null @@ -1,281 +0,0 @@ -package gtPlusPlus.core.tileentities.general; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.block.Block; -import net.minecraft.block.BlockDoor; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; - -import gtPlusPlus.api.objects.data.AutoMap; -import gtPlusPlus.api.objects.minecraft.BlockPos; -import gtPlusPlus.core.util.minecraft.EntityUtils; - -public class TileEntityPlayerDoorBase extends TileEntity { - - public boolean mIsOpen = false; - private short mMeta = 0; - private long mTickCounter = 0; - private final Block mBlockType; - private BlockPos mNeighbourDoor; - - public TileEntityPlayerDoorBase(Block aBlock, int meta) { - mBlockType = aBlock; - } - - @Override - public void readFromNBT(NBTTagCompound aNBT) { - super.readFromNBT(aNBT); - this.mIsOpen = aNBT.getBoolean("mIsOpen"); - } - - @Override - public void writeToNBT(NBTTagCompound aNBT) { - super.writeToNBT(aNBT); - aNBT.setBoolean("mIsOpen", mIsOpen); - } - - public int getNeighbourState() { - - if (mNeighbourDoor != null) { - World aWorld = this.worldObj; - if (aWorld != null) { - TileEntity t = aWorld.getTileEntity(mNeighbourDoor.xPos, mNeighbourDoor.yPos, mNeighbourDoor.zPos); - // Custom Door - if (t != null) { - if (t instanceof TileEntityPlayerDoorBase) { - TileEntityPlayerDoorBase d = (TileEntityPlayerDoorBase) t; - if (d.mIsOpen) { - return 100; - } else { - return -100; - } - } else return -100; - } - // Vanilla Door - else { - Block aBlock = mNeighbourDoor.getBlockAtPos(); - BlockDoor aDoor = (aBlock instanceof BlockDoor ? (BlockDoor) aBlock : null); - if (aDoor != null) { - int i1 = aDoor.func_150012_g( - mNeighbourDoor.world, - mNeighbourDoor.xPos, - mNeighbourDoor.yPos, - mNeighbourDoor.zPos); - if ((i1 & 4) != 0) { - return 100; - } else { - return -100; - } - } - } - } - } - return 0; - } - - AutoMap mNearbyEntityCache = new AutoMap(); - - @Override - public void updateEntity() { - - if (this.getWorldObj().isRemote) { - return; - } - - // Look For Neighbours - if (mTickCounter % 100 == 0 || mTickCounter == 0) { - World aWorld = this.getWorldObj(); - BlockPos aThisPos = new BlockPos(xCoord, yCoord, zCoord, aWorld); - BlockPos[] aNeighbors = new BlockPos[4]; - aNeighbors[0] = aThisPos.getXNeg(); - aNeighbors[1] = aThisPos.getXPos(); - aNeighbors[2] = aThisPos.getZNeg(); - aNeighbors[3] = aThisPos.getZPos(); - boolean aFoundDoor = false; - for (BlockPos b : aNeighbors) { - Block aBlock = aWorld.getBlock(b.xPos, b.yPos, b.zPos); - BlockDoor aDoor = (aBlock instanceof BlockDoor ? (BlockDoor) aBlock : null); - if (aDoor != null) { - mNeighbourDoor = b; - aFoundDoor = true; - if (mMeta == 0) { - TileEntity t = aWorld.getTileEntity(b.xPos, b.yPos, b.zPos); - if (t != null) { - if (t instanceof TileEntityPlayerDoorBase) { - TileEntityPlayerDoorBase d = (TileEntityPlayerDoorBase) t; - if (d.mMeta != 0) { - // Logger.INFO("Found Door with Mode set other than 0, assuming slave role."); - mMeta = -1; - } else { - // Logger.INFO("Found door with no mode set, assuming we are master."); - mMeta = 1; - } - } else { - // Logger.INFO("Custom door from another mod, assuming slave role."); - mMeta = -1; - } - } else { - // Logger.INFO("No Tile Entity found, Door is probably vanilla, assuming slave role."); - mMeta = -1; - } - } - break; - } - } - if (mMeta < 1 && !aFoundDoor) { - // Logger.INFO("Found No Valid Doors around, setting this one to master mode."); - mMeta = 1; - } - } - - World aWorld = this.getWorldObj(); - Block aBlock = aWorld.getBlock(xCoord, yCoord, zCoord); - BlockPos aThisPos = new BlockPos(xCoord, yCoord, zCoord, this.worldObj); - - if (mTickCounter % 20 == 0) { - int x = 0, y = 0, z = 0; - x = this.xCoord; - y = this.yCoord; - z = this.zCoord; - // List aEntityList = aWorld.loadedEntityList; - List aEntityList = new ArrayList(); - Chunk aThisChunk = aWorld.getChunkFromBlockCoords(x, z); - for (List l : aThisChunk.entityLists) { - aEntityList.addAll(l); - } - for (Object o : aEntityList) { - if (o != null) { - if (o instanceof Entity) { - if (o instanceof EntityPlayer) { - continue; - } else { - Entity e = (Entity) o; - BlockPos p = EntityUtils.findBlockPosUnderEntity(e); - if (p != null) { - int newY = p.yPos + 1; - if (e.getDistance(xCoord, yCoord, zCoord) <= 2) { - mNearbyEntityCache.put(e); - } else if (aThisPos.distanceFrom(p.xPos, newY, p.zPos) <= 2) { - mNearbyEntityCache.put(e); - } - } - } - } - } - } - } - - if (mTickCounter % 4 == 0) { - for (Entity y : mNearbyEntityCache) { - if (y.getDistance(xCoord, yCoord, zCoord) > 2) { - mNearbyEntityCache.remove(y); - } - } - - boolean foundMonster = mNearbyEntityCache.size() > 0; - int aNeighbourDoorState = 0; - if (mNeighbourDoor != null) { - aNeighbourDoorState = getNeighbourState(); - } - BlockDoor aDoor = (aBlock instanceof BlockDoor ? (BlockDoor) aBlock : null); - boolean aPlayers = checkForPlayers(this.getWorldObj()); - - if (aDoor != null) { - // If neighbour state != 0 and we are in slave mode - if (aNeighbourDoorState != 0 && mMeta == -1) { - if (aNeighbourDoorState == 100) { - if (!mIsOpen && !foundMonster) { - // Logger.INFO("Opening Door (Slave)"); - aDoor.func_150014_a(aWorld, this.xCoord, this.yCoord, this.zCoord, true); - mIsOpen = true; - } - } else if (aNeighbourDoorState == -100 || foundMonster) { - if (mIsOpen) { - // Logger.INFO("Closing Door (Slave)"); - aDoor.func_150014_a(aWorld, this.xCoord, this.yCoord, this.zCoord, false); - mIsOpen = false; - } - } - // We are master, proceed - } else { - // No redstone found, allow automatic handling - if (aDoor != null && !hasRedstone()) { - // Found a nearby player - if (aPlayers) { - // If we are closed and there are no monsters nearby, open - if (!mIsOpen && !foundMonster) { - // Logger.INFO("Opening Door (Mstr)"); - aDoor.func_150014_a(aWorld, this.xCoord, this.yCoord, this.zCoord, true); - mIsOpen = true; - } else { - // Logger.INFO("Doing Nothing, Door is in correct state."); - } - // Did not find nearby player - } else { - // If we are open or there is a monster nearby, close. - if (mIsOpen || foundMonster) { - // Logger.INFO("Closing Door (Mstr)"); - aDoor.func_150014_a(aWorld, this.xCoord, this.yCoord, this.zCoord, false); - mIsOpen = false; - } else { - // Logger.INFO("Doing Nothing, Door is in correct state."); - } - } - } - } - } - } - super.updateEntity(); - mTickCounter++; - } - - @Override - public int getBlockMetadata() { - return this.mMeta; - } - - public boolean hasRedstone() { - World aWorld = this.worldObj; - if (aWorld != null && !aWorld.isRemote) { - return aWorld.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) - || aWorld.isBlockIndirectlyGettingPowered(xCoord, yCoord + 1, zCoord); - } - return false; - } - - @Override - public Block getBlockType() { - return mBlockType; - } - - @Override - public boolean canUpdate() { - return true; - } - - private boolean checkForPlayers(World aWorld) { - int x = 0, y = 0, z = 0; - x = this.xCoord; - y = this.yCoord; - z = this.zCoord; - EntityPlayer aPlayer = aWorld.getClosestPlayer(x, y, z, 3.5D); - if (aPlayer != null) { - return true; - } - return false; - } - - private short getClosedMeta() { - return 0; - } - - private short getOpenMeta() { - return 1; - } -} diff --git a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityVolumetricFlaskSetter.java b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityVolumetricFlaskSetter.java index 9e829e0c16..a7fb226411 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityVolumetricFlaskSetter.java +++ b/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityVolumetricFlaskSetter.java @@ -87,25 +87,25 @@ public class TileEntityVolumetricFlaskSetter extends TileEntity implements ISide } private int getCapacityForSlot(int aSlot) { - switch (aSlot) { - case 0: // 16 - return 16; - case 1: // 36 - return 36; - case 2: // 144 - return 144; - case 3: // 432 - return 432; - case 4: // 576 - return 576; - case 5: // 720 - return 720; - case 6: // 864 - return 864; - case 7: // Custom - return getCustomValue(); - } - return 1000; + return switch (aSlot) { + case 0 -> // 16 + 16; + case 1 -> // 36 + 36; + case 2 -> // 144 + 144; + case 3 -> // 432 + 432; + case 4 -> // 576 + 576; + case 5 -> // 720 + 720; + case 6 -> // 864 + 864; + case 7 -> // Custom + getCustomValue(); + default -> 1000; + }; } public boolean addOutput() { @@ -126,7 +126,7 @@ public class TileEntityVolumetricFlaskSetter extends TileEntity implements ISide return false; } } - AutoMap aValidSlots = new AutoMap(); + AutoMap aValidSlots = new AutoMap<>(); int aSlotCount = 0; for (ItemStack i : aInputs) { if (i != null) { diff --git a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityAdvPooCollector.java b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityAdvPooCollector.java index c256f73b4c..d3add73de7 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityAdvPooCollector.java +++ b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityAdvPooCollector.java @@ -63,7 +63,7 @@ public class TileEntityAdvPooCollector extends TileEntityBaseFluidCollector { return false; } - private static AutoMap aEntityToDrain = new AutoMap(); + private static AutoMap aEntityToDrain = new AutoMap<>(); @Override public AutoMap aThingsToLookFor() { diff --git a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPestKiller.java b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPestKiller.java index 54014103cd..ac78658403 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPestKiller.java +++ b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPestKiller.java @@ -52,7 +52,7 @@ public class TileEntityPestKiller extends TileEntity implements ISidedInventory, private boolean mNeedsUpdate = false; private String mCustomName; - private static final AutoMap> mEntityMap = new AutoMap>(); + private static final AutoMap> mEntityMap = new AutoMap<>(); static { mEntityMap.put(EntityBat.class); @@ -74,7 +74,7 @@ public class TileEntityPestKiller extends TileEntity implements ISidedInventory, return mTank; } - private final void setup() { + private void setup() { World w = this.worldObj; if (w != null) { Chunk c = w.getChunkFromBlockCoords(this.xCoord, this.zCoord); @@ -91,19 +91,19 @@ public class TileEntityPestKiller extends TileEntity implements ISidedInventory, int min = 0; int max = 0; switch (getTier()) { - case 1: + case 1 -> { min = -2; max = 3; - break; - case 2: + } + case 2 -> { min = -4; max = 5; - break; - default: - // code block + } + default -> {} + // code block } int aChunkCount = 0; - AutoMap entities = new AutoMap(); + AutoMap entities = new AutoMap<>(); if (min != 0 && max != 0) { for (int x = min; x < max; x++) { for (int z = min; z < max; z++) { @@ -491,7 +491,7 @@ public class TileEntityPestKiller extends TileEntity implements ISidedInventory, this.mNeedsUpdate = true; } - private final void updateTick() { + private void updateTick() { if (mNeedsUpdate) { if (mUpdateTick == 0) { mUpdateTick = 4; // every 4 ticks it will send an update diff --git a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPooCollector.java b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPooCollector.java index ceff9d8528..d0d4ee3661 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPooCollector.java +++ b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityPooCollector.java @@ -67,7 +67,7 @@ public class TileEntityPooCollector extends TileEntityBaseFluidCollector { return false; } - private static AutoMap aEntityToDrain = new AutoMap(); + private static AutoMap aEntityToDrain = new AutoMap<>(); @Override public AutoMap aThingsToLookFor() { diff --git a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityProjectTable.java b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityProjectTable.java index 0229f89402..a287d8c33a 100644 --- a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityProjectTable.java +++ b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityProjectTable.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.Vector; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -30,10 +28,6 @@ public class TileEntityProjectTable extends TileEntity public InventoryProjectMain inventoryGrid; public InventoryProjectOutput inventoryOutputs; - /** The crafting matrix inventory (3x3). */ - public InventoryCrafting craftMatrix; - - public IInventory craftResult; private Container_ProjectTable container; public TileEntityProjectTable() { diff --git a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityRoundRobinator.java b/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityRoundRobinator.java deleted file mode 100644 index 366251941b..0000000000 --- a/src/main/java/gtPlusPlus/core/tileentities/machines/TileEntityRoundRobinator.java +++ /dev/null @@ -1,687 +0,0 @@ -package gtPlusPlus.core.tileentities.machines; - -import java.util.EnumSet; -import java.util.List; - -import net.minecraft.block.Block; -import net.minecraft.block.BlockChest; -import net.minecraft.command.IEntitySelector; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.ISidedInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.network.NetworkManager; -import net.minecraft.network.Packet; -import net.minecraft.network.play.server.S35PacketUpdateTileEntity; -import net.minecraft.tileentity.IHopper; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.tileentity.TileEntityChest; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.MathHelper; -import net.minecraft.world.World; -import net.minecraftforge.common.util.ForgeDirection; - -import gtPlusPlus.GTplusplus; -import gtPlusPlus.api.objects.Logger; -import gtPlusPlus.core.handler.GuiHandler; -import gtPlusPlus.core.inventories.Inventory_RoundRobinator; -import gtPlusPlus.core.util.minecraft.ItemUtils; -import gtPlusPlus.core.util.minecraft.PlayerUtils; -import gtPlusPlus.core.util.sys.KeyboardUtils; - -public class TileEntityRoundRobinator extends TileEntity implements ISidedInventory, IHopper { - - private int tickCount = 0; - private final Inventory_RoundRobinator inventoryContents; - private String customName; - public int locationX; - public int locationY; - public int locationZ; - private int aData = 1111; - private int aTier = 1; - private int aTickRate = 100; - - public TileEntityRoundRobinator() { - this.inventoryContents = new Inventory_RoundRobinator(); - this.setTileLocation(); - } - - public boolean setTileLocation() { - if (this.hasWorldObj()) { - if (!this.getWorldObj().isRemote) { - this.locationX = this.xCoord; - this.locationY = this.yCoord; - this.locationZ = this.zCoord; - this.aTier = this.getWorldObj().getBlockMetadata(locationX, locationY, locationZ) + 1; - return true; - } - } - return false; - } - - // Rename to hasCircuitToConfigure - public final boolean hasInventoryContents() { - for (ItemStack i : this.aHopperInventory) { - if (i == null) { - continue; - } else { - return true; - } - } - return false; - } - - public Inventory_RoundRobinator getInventory() { - return this.inventoryContents; - } - - public int getTier() { - return this.aTier; - } - - public int getTickRate() { - return this.aTickRate; - } - - @Override - public void updateEntity() { - try { - // TODO - if (this.worldObj != null && !this.worldObj.isRemote) { - setTileLocation(); - aTickRate = (60 - (aTier * 10)); - if (this.getTier() == 1) { - // 20 s - aTickRate = 400; - } else if (this.getTier() == 2) { - // 5 - aTickRate = 100; - } else if (this.getTier() == 3) { - // 1 - aTickRate = 20; - } else if (this.getTier() == 4) { - // 1/5 - aTickRate = 10; - } else if (this.getTier() == 5) { - // 1/20 - aTickRate = 1; - } else { - aTickRate = 999999; - } - - if (tickCount % getTickRate() == 0) { - if (hasInventoryContents()) { - Logger.WARNING("Trying to move items. " + aTickRate); - this.tryProcessItems(); - } - } - this.tickCount++; - } - } catch (final Throwable t) { - t.printStackTrace(); - } - } - - public boolean anyPlayerInRange() { - return this.worldObj.getClosestPlayer(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, 32) != null; - } - - public NBTTagCompound getTag(final NBTTagCompound nbt, final String tag) { - if (!nbt.hasKey(tag)) { - nbt.setTag(tag, new NBTTagCompound()); - } - return nbt.getCompoundTag(tag); - } - - @Override - public void writeToNBT(final NBTTagCompound nbt) { - super.writeToNBT(nbt); - if (this.hasCustomInventoryName()) { - nbt.setString("CustomName", this.getCustomName()); - } - nbt.setInteger("aCurrentMode", aData); - this.writeToNBT2(nbt); - } - - @Override - public void readFromNBT(final NBTTagCompound nbt) { - super.readFromNBT(nbt); - if (nbt.hasKey("CustomName", 8)) { - this.setCustomName(nbt.getString("CustomName")); - } - aData = nbt.getInteger("aCurrentMode"); - this.readFromNBT2(nbt); - } - - @Override - public int getInventoryStackLimit() { - return 64; - } - - @Override - public boolean isUseableByPlayer(final EntityPlayer entityplayer) { - return this.getInventory().isUseableByPlayer(entityplayer); - } - - @Override - public void openInventory() { - this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1); - this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType()); - this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType()); - // this.getInventory().openInventory(); - } - - @Override - public void closeInventory() { - this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1); - this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType()); - this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType()); - // this.getInventory().closeInventory(); - } - - @Override - public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) { - return true; - } - - @Override - public int[] getAccessibleSlotsFromSide(final int aSide) { - return new int[] { 0, 1, 2, 3, 4 }; - } - - @Override - public boolean canInsertItem(final int aSlot, final ItemStack aStack, final int aSide) { - return aSide < 2; - } - - @Override - public boolean canExtractItem(final int aSlot, final ItemStack aStack, final int aSide) { - return false; - } - - public String getCustomName() { - return this.customName; - } - - public void setCustomName(final String customName) { - this.customName = customName; - } - - @Override - public String getInventoryName() { - return this.hasCustomInventoryName() ? this.customName : "container.roundrobinator"; - } - - @Override - public boolean hasCustomInventoryName() { - return (this.customName != null) && !this.customName.equals(""); - } - - @Override - public Packet getDescriptionPacket() { - final NBTTagCompound tag = new NBTTagCompound(); - this.writeToNBT(tag); - return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.blockMetadata, tag); - } - - @Override - public void onDataPacket(final NetworkManager net, final S35PacketUpdateTileEntity pkt) { - final NBTTagCompound tag = pkt.func_148857_g(); - this.readFromNBT(tag); - } - - public boolean onRightClick(ForgeDirection side, EntityPlayer player, int x, int y, int z) { - if (player != null && player.getHeldItem() == null) { - if (!player.isSneaking() && !KeyboardUtils.isShiftKeyDown()) { - player.openGui(GTplusplus.instance, GuiHandler.GUI16, player.getEntityWorld(), x, y, z); - } else { - String InventoryContents = ItemUtils.getArrayStackNames(this.aHopperInventory); - PlayerUtils.messagePlayer(player, "Contents: " + InventoryContents + " | " + getDataString()); - } - return true; - } else { - return false; - } - } - - public boolean onScrewdriverRightClick(ForgeDirection side, EntityPlayer player, int x, int y, int z) { - try { - if (side.offsetY != 0) { - // Top/Bottom - } else { - if (toggleSide(side)) { - PlayerUtils.messagePlayer(player, "Enabling side " + side + "."); - } else { - PlayerUtils.messagePlayer(player, "Disabling side " + side + "."); - } - PlayerUtils.messagePlayer(player, "Mode String: " + aData + ""); - } - return true; - } catch (Throwable t) { - t.printStackTrace(); - return false; - } - } - - public int getDataString() { - return aData; - } - - public boolean[] getActiveSides() { - this.markDirty(); - String s = String.valueOf(aData); - if (s == null || s.length() != 4) { - s = "1111"; - } - boolean[] aActiveSides = new boolean[4]; - for (int i = 0; i < 4; i++) { - char ch = s.charAt(i); - if (ch == '1') { - aActiveSides[i] = true; - } else { - aActiveSides[i] = false; - } - } - return aActiveSides; - } - - /** - * Toggle active state of side - * - * @param side - Forge Direction / Side - * @return - True if the side is now Active, false if now disabled. - */ - public boolean toggleSide(ForgeDirection side) { - setSideActive(!getSideActive(side), side); - return getSideActive(side); - } - - public void setSideActive(boolean aActive, ForgeDirection side) { - try { - if (side.offsetY != 0) {} else { - if (aData < 1111) { - aData = 1111; - } else if (aData > 2222) { - aData = 2222; - } - String s = String.valueOf(aData); - StringBuilder aDataString = new StringBuilder(s); - int aIndex = side.ordinal() - 2; - if (aActive) { - aDataString.setCharAt(aIndex, '1'); - } else { - aDataString.setCharAt(aIndex, '2'); - } - aData = Integer.valueOf(aDataString.toString()); - this.markDirty(); - } - } catch (Throwable t) { - t.printStackTrace(); - } - } - - public boolean getSideActive(ForgeDirection side) { - this.markDirty(); - try { - if (side.offsetY != 0) { - return false; - } else { - if (aData < 1111) { - aData = 1111; - } else if (aData > 2222) { - aData = 2222; - } - String s = String.valueOf(aData); - int aIndex = side.ordinal() - 2; - char ch = s.charAt(aIndex); - if (ch == '1') { - return true; - } else { - return false; - } - } - } catch (Throwable t) { - t.printStackTrace(); - return false; - } - } - - @Override - public double getXPos() { - return this.locationX; - } - - @Override - public double getYPos() { - return this.locationY; - } - - @Override - public double getZPos() { - return this.locationZ; - } - - // TODO - - /* - * Hopper Code - */ - - private ItemStack[] aHopperInventory = new ItemStack[5]; - - @Override - public int getSizeInventory() { - return this.aHopperInventory.length; - } - - @Override - public ItemStack getStackInSlot(int aSlot) { - return this.aHopperInventory[aSlot]; - } - - /** - * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a - * new stack. - */ - @Override - public ItemStack decrStackSize(int aSlot, int aMinimumSizeOfExistingStack) { - if (this.aHopperInventory[aSlot] != null) { - ItemStack itemstack; - - if (this.aHopperInventory[aSlot].stackSize <= aMinimumSizeOfExistingStack) { - itemstack = this.aHopperInventory[aSlot]; - this.aHopperInventory[aSlot] = null; - return itemstack; - } else { - itemstack = this.aHopperInventory[aSlot].splitStack(aMinimumSizeOfExistingStack); - - if (this.aHopperInventory[aSlot].stackSize == 0) { - this.aHopperInventory[aSlot] = null; - } - - return itemstack; - } - } else { - return null; - } - } - - /** - * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - - * like when you close a workbench GUI. - */ - @Override - public ItemStack getStackInSlotOnClosing(int aSlot) { - if (this.aHopperInventory[aSlot] != null) { - ItemStack itemstack = this.aHopperInventory[aSlot]; - this.aHopperInventory[aSlot] = null; - return itemstack; - } else { - return null; - } - } - - /** - * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). - */ - @Override - public void setInventorySlotContents(int aSlot, ItemStack aStack) { - this.aHopperInventory[aSlot] = aStack; - - if (aStack != null && aStack.stackSize > this.getInventoryStackLimit()) { - aStack.stackSize = this.getInventoryStackLimit(); - } - } - - public boolean tryProcessItems() { - if (this.worldObj != null && !this.worldObj.isRemote) { - boolean didSomething = false; - if (!this.isEmpty()) { - Logger.WARNING("Has Items, Trying to push to all active directions."); - didSomething = this.tryPushItemsIntoNeighbours(); - } - if (didSomething) { - this.markDirty(); - return true; - } - } - return false; - } - - /** - * Is Empty - * - * @return - */ - private boolean isEmpty() { - ItemStack[] aitemstack = this.aHopperInventory; - int i = aitemstack.length; - - for (int j = 0; j < i; ++j) { - ItemStack itemstack = aitemstack[j]; - - if (itemstack != null) { - return false; - } - } - - return true; - } - - private static final EnumSet VALID_SIDES = EnumSet - .of(ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.EAST); - - private boolean tryPushItemsIntoNeighbours() { - - boolean aDidPush = false; - - for (final ForgeDirection side : VALID_SIDES) { - final int ordinalSide = side.ordinal(); - if (!this.getSideActive(side)) { - Logger.WARNING("Not pushing on side " + side); - continue; - } - - Logger.WARNING("Pushing on side " + side); - IInventory iinventory = this.getInventoryFromFacing(side); - - if (iinventory == null) { - Logger.WARNING("No inventory found."); - continue; - } else { - final ForgeDirection oppositeSide = side.getOpposite(); - - // int i = Facing.oppositeSide[u]; - Logger.WARNING("Using Opposite direction: " + oppositeSide); - - if (this.isInventoryFull(iinventory, oppositeSide.ordinal())) { - Logger.WARNING("Target is full, skipping."); - continue; - } else { - Logger.WARNING("Target has space, let's move a single item."); - for (int j = 0; j < this.getSizeInventory(); ++j) { - if (this.getStackInSlot(j) != null) { - ItemStack itemstack = this.getStackInSlot(j).copy(); - ItemStack itemstack1 = setStackInNeighbour( - iinventory, - this.decrStackSize(j, 1), - ordinalSide); - if (itemstack1 == null || itemstack1.stackSize == 0) { - iinventory.markDirty(); - aDidPush = true; - continue; - } - this.setInventorySlotContents(j, itemstack); - } - } - } - } - } - - return aDidPush; - } - - private boolean isInventoryFull(IInventory aInv, int ordinalSide) { - if (aInv instanceof ISidedInventory && ordinalSide > -1) { - ISidedInventory isidedinventory = (ISidedInventory) aInv; - int[] aint = isidedinventory.getAccessibleSlotsFromSide(ordinalSide); - - for (int l = 0; l < aint.length; ++l) { - ItemStack itemstack1 = isidedinventory.getStackInSlot(aint[l]); - - if (itemstack1 == null || itemstack1.stackSize != itemstack1.getMaxStackSize()) { - return false; - } - } - } else { - int j = aInv.getSizeInventory(); - - for (int k = 0; k < j; ++k) { - ItemStack itemstack = aInv.getStackInSlot(k); - - if (itemstack == null || itemstack.stackSize != itemstack.getMaxStackSize()) { - return false; - } - } - } - return true; - } - - public static ItemStack setStackInNeighbour(IInventory aNeighbour, ItemStack aStack, int ordinalSide) { - if (aNeighbour instanceof ISidedInventory && ordinalSide > -1) { - ISidedInventory isidedinventory = (ISidedInventory) aNeighbour; - int[] aint = isidedinventory.getAccessibleSlotsFromSide(ordinalSide); - - for (int l = 0; l < aint.length && aStack != null && aStack.stackSize > 0; ++l) { - aStack = tryMoveStack(aNeighbour, aStack, aint[l], ordinalSide); - } - } else { - int j = aNeighbour.getSizeInventory(); - - for (int k = 0; k < j && aStack != null && aStack.stackSize > 0; ++k) { - aStack = tryMoveStack(aNeighbour, aStack, k, ordinalSide); - } - } - - if (aStack != null && aStack.stackSize == 0) { - aStack = null; - } - - return aStack; - } - - private static boolean canInsertItemIntoNeighbour(IInventory aNeighbour, ItemStack aStack, int aSlot, int aSide) { - return !aNeighbour.isItemValidForSlot(aSlot, aStack) ? false - : !(aNeighbour instanceof ISidedInventory) - || ((ISidedInventory) aNeighbour).canInsertItem(aSlot, aStack, aSide); - } - - private static ItemStack tryMoveStack(IInventory aNeighbour, ItemStack aStack, int aSlot, int aSide) { - ItemStack itemstack1 = aNeighbour.getStackInSlot(aSlot); - if (canInsertItemIntoNeighbour(aNeighbour, aStack, aSlot, aSide)) { - boolean aDidSomething = false; - if (itemstack1 == null) { - // Forge: BUGFIX: Again, make things respect max stack sizes. - int max = Math.min(aStack.getMaxStackSize(), aNeighbour.getInventoryStackLimit()); - if (max >= aStack.stackSize) { - aNeighbour.setInventorySlotContents(aSlot, aStack); - aStack = null; - } else { - aNeighbour.setInventorySlotContents(aSlot, aStack.splitStack(max)); - } - aDidSomething = true; - } else if (areItemStacksEqual(itemstack1, aStack)) { - // Forge: BUGFIX: Again, make things respect max stack sizes. - int max = Math.min(aStack.getMaxStackSize(), aNeighbour.getInventoryStackLimit()); - if (max > itemstack1.stackSize) { - int l = Math.min(aStack.stackSize, max - itemstack1.stackSize); - aStack.stackSize -= l; - itemstack1.stackSize += l; - aDidSomething = l > 0; - } - } - if (aDidSomething) { - aNeighbour.markDirty(); - } - } - return aStack; - } - - private IInventory getInventoryFromFacing(ForgeDirection side) { - return tryFindInvetoryAtXYZ( - this.getWorldObj(), - this.xCoord + side.offsetX, - this.yCoord + side.offsetY, - this.zCoord + side.offsetZ); - } - - public static IInventory tryFindInvetoryAtXYZ(World aWorld, double aX, double aY, double aZ) { - IInventory iinventory = null; - int sX = MathHelper.floor_double(aX); - int sY = MathHelper.floor_double(aY); - int sZ = MathHelper.floor_double(aZ); - TileEntity tileentity = aWorld.getTileEntity(sX, sY, sZ); - - if (tileentity != null && tileentity instanceof IInventory) { - iinventory = (IInventory) tileentity; - - if (iinventory instanceof TileEntityChest) { - Block block = aWorld.getBlock(sX, sY, sZ); - - if (block instanceof BlockChest) { - iinventory = ((BlockChest) block).func_149951_m(aWorld, sX, sY, sZ); - } - } - } - - if (iinventory == null) { - List list = aWorld.getEntitiesWithinAABBExcludingEntity( - (Entity) null, - AxisAlignedBB.getBoundingBox(aX, aY, aZ, aX + 1.0D, aY + 1.0D, aZ + 1.0D), - IEntitySelector.selectInventories); - - if (list != null && list.size() > 0) { - iinventory = (IInventory) list.get(aWorld.rand.nextInt(list.size())); - } - } - - return iinventory; - } - - private static boolean areItemStacksEqual(ItemStack aStack, ItemStack aStack2) { - return aStack.getItem() != aStack2.getItem() ? false - : (aStack.getItemDamage() != aStack2.getItemDamage() ? false - : (aStack.stackSize > aStack.getMaxStackSize() ? false - : ItemStack.areItemStackTagsEqual(aStack, aStack2))); - } - - public void readFromNBT2(NBTTagCompound p_145839_1_) { - super.readFromNBT(p_145839_1_); - NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10); - this.aHopperInventory = new ItemStack[this.getSizeInventory()]; - for (int i = 0; i < nbttaglist.tagCount(); ++i) { - NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); - byte b0 = nbttagcompound1.getByte("Slot"); - if (b0 >= 0 && b0 < this.aHopperInventory.length) { - this.aHopperInventory[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1); - } - } - } - - public void writeToNBT2(NBTTagCompound aNBT) { - super.writeToNBT(aNBT); - NBTTagList nbttaglist = new NBTTagList(); - for (int i = 0; i < this.aHopperInventory.length; ++i) { - if (this.aHopperInventory[i] != null) { - NBTTagCompound nbttagcompound1 = new NBTTagCompound(); - nbttagcompound1.setByte("Slot", (byte) i); - this.aHopperInventory[i].writeToNBT(nbttagcompound1); - nbttaglist.appendTag(nbttagcompound1); - } - } - aNBT.setTag("Items", nbttaglist); - } -} -- cgit