diff options
author | kekzdealer <kekzdealer@gmail.com> | 2020-05-17 16:33:44 +0200 |
---|---|---|
committer | kekzdealer <kekzdealer@gmail.com> | 2020-05-17 16:33:44 +0200 |
commit | 6045fc76bf08eeaf1e74c0a86200bf0288d8e3fa (patch) | |
tree | cf2f05b36c3104548a95119f08468ef5e076481e | |
parent | 695e5ab73f82e33598c96e6c59ca7fd18eb4259a (diff) | |
download | GT5-Unofficial-6045fc76bf08eeaf1e74c0a86200bf0288d8e3fa.tar.gz GT5-Unofficial-6045fc76bf08eeaf1e74c0a86200bf0288d8e3fa.tar.bz2 GT5-Unofficial-6045fc76bf08eeaf1e74c0a86200bf0288d8e3fa.zip |
Massive code cleanup and rewrite for Item Proxy tech
5 files changed, 135 insertions, 126 deletions
diff --git a/src/main/java/common/blocks/Block_ItemProxyEndpoint.java b/src/main/java/common/blocks/Block_ItemProxyEndpoint.java index 5b4888a1f5..ec1bc6b827 100644 --- a/src/main/java/common/blocks/Block_ItemProxyEndpoint.java +++ b/src/main/java/common/blocks/Block_ItemProxyEndpoint.java @@ -18,7 +18,7 @@ import net.minecraft.world.World; public class Block_ItemProxyEndpoint extends Block { - private static Block_ItemProxyEndpoint instance = new Block_ItemProxyEndpoint(); + private static final Block_ItemProxyEndpoint instance = new Block_ItemProxyEndpoint(); private Block_ItemProxyEndpoint() { super(Material.glass); @@ -45,19 +45,11 @@ public class Block_ItemProxyEndpoint extends Block { final TileEntity te = world.getTileEntity(x, y, z); if(te instanceof TE_ItemProxyEndpoint) { - final TE_ItemProxyEndpoint endpoint = (TE_ItemProxyEndpoint) te; - if(player.inventory.getCurrentItem() != null && player.inventory.getCurrentItem().getItem() instanceof Item_Configurator) { - - final ItemStack held = player.inventory.getCurrentItem(); - if(held.hasTagCompound() && held.getTagCompound().hasKey("config")) { - endpoint.setChannel(UUID.fromString(held.getTagCompound().getString("config"))); - } - } else { - player.openGui(KekzCore.instance, GuiHandler.ITEM_PROXY_ENDPOINT, world, x, y, z); - } + player.openGui(KekzCore.instance, GuiHandler.ITEM_PROXY_ENDPOINT, world, x, y, z); return true; + } else { + return false; } - return false; } @Override diff --git a/src/main/java/common/blocks/Block_ItemProxySource.java b/src/main/java/common/blocks/Block_ItemProxySource.java index 25d9de482b..7291c15e6f 100644 --- a/src/main/java/common/blocks/Block_ItemProxySource.java +++ b/src/main/java/common/blocks/Block_ItemProxySource.java @@ -3,21 +3,18 @@ package common.blocks; import common.itemBlocks.IB_ItemProxySource; import common.tileentities.TE_ItemProxySource; import cpw.mods.fml.common.registry.GameRegistry; -import items.Item_Configurator; import kekztech.GuiHandler; import kekztech.KekzCore; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class Block_ItemProxySource extends Block { - private static Block_ItemProxySource instance = new Block_ItemProxySource(); + private static final Block_ItemProxySource instance = new Block_ItemProxySource(); private Block_ItemProxySource() { super(Material.glass); @@ -44,20 +41,11 @@ public class Block_ItemProxySource extends Block { final TileEntity te = world.getTileEntity(x, y, z); if(te instanceof TE_ItemProxySource) { - final TE_ItemProxySource source = (TE_ItemProxySource) te; - if(player.inventory.getCurrentItem() != null && player.inventory.getCurrentItem().getItem() instanceof Item_Configurator) { - - final NBTTagCompound configNBT = new NBTTagCompound(); - configNBT.setString("config", source.getChannel().toString()); - final ItemStack held = player.inventory.getCurrentItem(); - held.setTagCompound(configNBT); - - } else { - player.openGui(KekzCore.instance, GuiHandler.ITEM_PROXY_SOURCE, world, x, y, z); - } + player.openGui(KekzCore.instance, GuiHandler.ITEM_PROXY_SOURCE, world, x, y, z); return true; + } else { + return false; } - return false; } @Override diff --git a/src/main/java/common/tileentities/TE_ItemProxyCable.java b/src/main/java/common/tileentities/TE_ItemProxyCable.java index c65ba7be02..f4caab3d36 100644 --- a/src/main/java/common/tileentities/TE_ItemProxyCable.java +++ b/src/main/java/common/tileentities/TE_ItemProxyCable.java @@ -5,13 +5,30 @@ import net.minecraftforge.common.util.ForgeDirection; public class TE_ItemProxyCable extends TileEntity { - private static final float THICKNESS = 0.3f; - private byte connections; + private static final float THICKNESS = 0.5F; + private byte connections = 0; + private byte connectionAllowed = 63; private String idCache = null; public TE_ItemProxyCable() { - connections = 63; - this.setConnection(ForgeDirection.DOWN, true); + + } + + @Override + public void updateEntity() { + // Check all 6 sides and connect the conduit if it is allowed to + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) { + final TileEntity te = super.getWorldObj().getTileEntity( + super.xCoord + side.offsetX, + super.yCoord + side.offsetY, + super.zCoord + side.offsetZ); + if(te instanceof TE_ItemProxyCable) { + final TE_ItemProxyCable cable = (TE_ItemProxyCable) te; + setConnection(side, cable.isConnectionAllowed(side.getOpposite())); + } else { + setConnection(side, false); + } + } } public static float getThickness() { @@ -46,33 +63,59 @@ public class TE_ItemProxyCable extends TileEntity { * The side for which to set the connection status. * @param connected * Whether this side should be connected or not + * @return + * True if the connection was allowed */ - public void setConnection(ForgeDirection side, boolean connected) { - switch(side) { - case DOWN: connections = (byte) ((connected) ? connections | 1 : connections ^ 1); break; - case UP: connections = (byte) ((connected) ? connections | 2 : connections ^ 2); break; - case NORTH: connections = (byte) ((connected) ? connections | 4 : connections ^ 4); break; - case SOUTH: connections = (byte) ((connected) ? connections | 8 : connections ^ 8); break; - case WEST: connections = (byte) ((connected) ? connections | 16 : connections ^ 16); break; - case EAST: connections = (byte) ((connected) ? connections | 32 : connections ^ 32); break; - default: break; + public boolean setConnection(ForgeDirection side, boolean connected) { + if(isConnectionAllowed(side)){ + switch(side) { + case DOWN: connections = (byte) ((connected) ? connections | 1 : connections ^ 1); break; + case UP: connections = (byte) ((connected) ? connections | 2 : connections ^ 2); break; + case NORTH: connections = (byte) ((connected) ? connections | 4 : connections ^ 4); break; + case SOUTH: connections = (byte) ((connected) ? connections | 8 : connections ^ 8); break; + case WEST: connections = (byte) ((connected) ? connections | 16 : connections ^ 16); break; + case EAST: connections = (byte) ((connected) ? connections | 32 : connections ^ 32); break; + default: return false; + } + return true; + } else { + return false; } } public boolean isConnected(ForgeDirection side) { switch(side) { - case DOWN: return (connections & 1) == connections; - case UP: return (connections & 2) == connections; - case NORTH: return (connections & 4) == connections; - case SOUTH: return (connections & 8) == connections; - case WEST: return (connections & 16) == connections; - case EAST: return (connections & 32) == connections; + case DOWN: return (connections & 1) == 1; + case UP: return (connections & 2) == 2; + case NORTH: return (connections & 4) == 4; + case SOUTH: return (connections & 8) == 8; + case WEST: return (connections & 16) == 16; + case EAST: return (connections & 32) == 32; default: return false; } } - - public byte getConnections() { - return connections; + + public void setConnectionAllowed(ForgeDirection side, boolean allowed) { + switch(side) { + case DOWN: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 1 : connectionAllowed ^ 1); break; + case UP: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 2 : connectionAllowed ^ 2); break; + case NORTH: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 4 : connectionAllowed ^ 4); break; + case SOUTH: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 8 : connectionAllowed ^ 8); break; + case WEST: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 16 : connectionAllowed ^ 16); break; + case EAST: connectionAllowed = (byte) ((allowed) ? connectionAllowed | 32 : connectionAllowed ^ 32); break; + default: break; + } + } + + public boolean isConnectionAllowed(ForgeDirection side) { + switch(side) { + case DOWN: return (connectionAllowed & 1) == 1; + case UP: return (connectionAllowed & 2) == 2; + case NORTH: return (connectionAllowed & 4) == 4; + case SOUTH: return (connectionAllowed & 8) == 8; + case WEST: return (connectionAllowed & 16) == 16; + case EAST: return (connectionAllowed & 32) == 32; + default: return false; + } } - } diff --git a/src/main/java/common/tileentities/TE_ItemProxyEndpoint.java b/src/main/java/common/tileentities/TE_ItemProxyEndpoint.java index 95bf32c52a..9587dacfe9 100644 --- a/src/main/java/common/tileentities/TE_ItemProxyEndpoint.java +++ b/src/main/java/common/tileentities/TE_ItemProxyEndpoint.java @@ -1,7 +1,6 @@ package common.tileentities; import java.util.HashSet; -import java.util.UUID; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -11,45 +10,33 @@ import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory { - - private UUID channel = null; - private int subChannel = -1; + + private byte channel = -1; private IInventory proxyInventory = null; private int tickCounter = 0; - private ItemStack[] slots = new ItemStack[2]; - - public void setChannel(UUID channel) { - this.channel = channel; + + public TE_ItemProxyEndpoint() { + channel = 0; } - - public void setSubChannel(int subChannel) { - this.subChannel = subChannel; + + public void setChannel(byte channel) { + this.channel = channel; } + + public int getChannel() { return channel; } @Override public void updateEntity() { if(tickCounter == 20) { - - if(slots[1] == null || !slots[1].getUnlocalizedName().equals("gt.integrated_circuit") || slots[1].getItemDamage() >= 16) { - setSubChannel(-1); - } - - if(slots[1] != null && slots[1].getUnlocalizedName().equals("gt.integrated_circuit") && slots[1].getItemDamage() < 16) { - setSubChannel(slots[1].getItemDamage()); - } - - if(channel != null && subChannel != -1) { - TE_ItemProxySource source = searchSource(channel); - if(source != null) { - proxyInventory = source; - } + if(channel != -1) { + proxyInventory = searchSource(); } tickCounter = 0; } tickCounter++; } - public TE_ItemProxySource searchSource(UUID channel) { + private TE_ItemProxySource searchSource() { final HashSet<TE_ItemProxySource> sources = new HashSet<>(); final HashSet<String> visited = new HashSet<>(); @@ -62,7 +49,7 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory if(te instanceof TE_ItemProxyCable) { final TE_ItemProxyCable cable = (TE_ItemProxyCable) te; if(cable.isConnected(next.getOpposite())) { - searchSourceRecursive(sources, visited, next.getOpposite(), cable, channel); + searchSourceRecursive(sources, visited, next.getOpposite(), cable); } } } @@ -76,30 +63,24 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory } private void searchSourceRecursive(HashSet<TE_ItemProxySource> sources, HashSet<String> visited, - ForgeDirection from, TE_ItemProxyCable nextTarget, UUID channel) { + ForgeDirection from, TE_ItemProxyCable nextTarget) { - if(visited.contains(nextTarget.getIdentifier())) { - return; - } else { + if(!visited.contains(nextTarget.getIdentifier())) { visited.add(nextTarget.getIdentifier()); - + for(ForgeDirection next : ForgeDirection.VALID_DIRECTIONS) { - if(next == from || !nextTarget.isConnected(next)) { - continue; - } - final TileEntity te = super.getWorldObj().getTileEntity( - nextTarget.xCoord + next.offsetX, - nextTarget.yCoord + next.offsetY, - nextTarget.zCoord + next.offsetZ); - if(te instanceof TE_ItemProxyCable) { - final TE_ItemProxyCable cable = (TE_ItemProxyCable) te; - if(cable.isConnected(next.getOpposite())) { - searchSourceRecursive(sources, visited, next.getOpposite(), cable, channel); - } - } else if (te instanceof TE_ItemProxySource) { - final TE_ItemProxySource source = (TE_ItemProxySource) te; - if(source.getChannel().equals(channel)) { - sources.add((TE_ItemProxySource) te); + if(next != from) { + final TileEntity te = super.getWorldObj().getTileEntity( + nextTarget.xCoord + next.offsetX, + nextTarget.yCoord + next.offsetY, + nextTarget.zCoord + next.offsetZ); + if(te instanceof TE_ItemProxyCable) { + final TE_ItemProxyCable cable = (TE_ItemProxyCable) te; + if(cable.isConnected(next.getOpposite())) { + searchSourceRecursive(sources, visited, next.getOpposite(), cable); + } + } else if (te instanceof TE_ItemProxySource) { + sources.add((TE_ItemProxySource) te); } } } @@ -108,41 +89,36 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory @Override public int getSizeInventory() { - return slots.length; + return 1; } @Override public ItemStack getStackInSlot(int slot) { - if(slot == 0) { - return (proxyInventory != null) ? proxyInventory.getStackInSlot(subChannel) : null; + if(proxyInventory != null && slot == 0) { + return proxyInventory.getStackInSlot(channel); } else { - return slots[slot]; + return null; } } @Override public ItemStack decrStackSize(int slot, int amount) { - if(slot == 0) { - return (proxyInventory != null) ? proxyInventory.decrStackSize(subChannel, amount) : null; + if(proxyInventory != null && slot == 0) { + return proxyInventory.decrStackSize(channel, amount); } else { - final ItemStack copy = slots[1].copy(); - slots[1] = null; - super.markDirty(); - return copy; + return null; } } @Override public ItemStack getStackInSlotOnClosing(int slot) { - return (proxyInventory != null) ? proxyInventory.getStackInSlotOnClosing(subChannel) : null; + return (proxyInventory != null) ? proxyInventory.getStackInSlotOnClosing(channel) : null; } @Override public void setInventorySlotContents(int slot, ItemStack itemStack) { - if(slot == 0 && proxyInventory != null) { - proxyInventory.setInventorySlotContents(subChannel, itemStack); - } else { - slots[slot] = itemStack; + if(proxyInventory != null && slot == 0) { + proxyInventory.setInventorySlotContents(channel, itemStack); } } @@ -158,7 +134,7 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory @Override public int getInventoryStackLimit() { - return (proxyInventory != null) ? proxyInventory.getInventoryStackLimit() : 1; + return (proxyInventory != null) ? proxyInventory.getInventoryStackLimit() : 0; } @Override @@ -178,17 +154,16 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory @Override public boolean isItemValidForSlot(int slot, ItemStack itemStack) { - if(slot == 0 && proxyInventory != null) { - return proxyInventory.isItemValidForSlot(subChannel, itemStack); + if(proxyInventory != null && slot == 0) { + return proxyInventory.isItemValidForSlot(channel, itemStack); } else { - return itemStack != null && itemStack.getUnlocalizedName().equals("gt.integrated_circuit"); + return false; } } @Override public int[] getAccessibleSlotsFromSide(int side) { - final int[] as = {0}; - return as; + return new int[]{0}; } @Override @@ -198,7 +173,7 @@ public class TE_ItemProxyEndpoint extends TileEntity implements ISidedInventory @Override public boolean canExtractItem(int slot, ItemStack itemStack, int side) { - return (slot == 0) ? true : false; + return slot == 0; } } diff --git a/src/main/java/common/tileentities/TE_ItemProxySource.java b/src/main/java/common/tileentities/TE_ItemProxySource.java index e514afe524..2a5cc7b4d1 100644 --- a/src/main/java/common/tileentities/TE_ItemProxySource.java +++ b/src/main/java/common/tileentities/TE_ItemProxySource.java @@ -8,12 +8,23 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; public class TE_ItemProxySource extends TileEntity implements IInventory { - - private final UUID channel = UUID.randomUUID(); + private ItemStack[] slots = new ItemStack[16]; - - public UUID getChannel() { - return channel; + private String idCache = null; + + /** + * Builds a simple unique identifier for this TileEntity by appending + * the x, y, and z coordinates in a string. + * + * @return unique identifier for this TileEntity + */ + public String getIdentifier() { + if(idCache == null) { + idCache = "" + super.xCoord + super.yCoord + super.zCoord; + return idCache; + } else { + return idCache; + } } @Override |