diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2016-01-20 14:24:34 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2016-01-20 14:24:34 +1000 |
commit | 869c206c4fcc8001bd2e1d66f704290331813835 (patch) | |
tree | 96735ce8fe4665e2759c3374221d6f06f4527df2 /src/Java/binnie/core/machines | |
parent | ec2c72827f01dd4bb2174137f1ab162f9ddaab62 (diff) | |
download | GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.gz GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.bz2 GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.zip |
Initial Commit
Diffstat (limited to 'src/Java/binnie/core/machines')
76 files changed, 6649 insertions, 0 deletions
diff --git a/src/Java/binnie/core/machines/BlockMachine.java b/src/Java/binnie/core/machines/BlockMachine.java new file mode 100644 index 0000000000..042e213aec --- /dev/null +++ b/src/Java/binnie/core/machines/BlockMachine.java @@ -0,0 +1,181 @@ +package binnie.core.machines; + +import binnie.Binnie; +import binnie.core.BinnieCore; +import binnie.core.machines.component.IRender.RandomDisplayTick; +import binnie.core.proxy.BinnieProxy; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import net.minecraft.block.Block; +import net.minecraft.block.BlockContainer; +import net.minecraft.block.material.Material; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.PlayerCapabilities; +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.IIcon; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +class BlockMachine + extends BlockContainer + implements IBlockMachine +{ + private MachineGroup group; + + public BlockMachine(MachineGroup group, String blockName) + { + super(Material.iron); + this.group = group; + setHardness(1.5F); + setBlockName(blockName); + } + + public void getSubBlocks(Item par1, CreativeTabs par2CreativeTabs, List itemList) + { + for (MachinePackage pack : this.group.getPackages()) { + if (pack.isActive()) { + itemList.add(new ItemStack(this, 1, pack.getMetadata().intValue())); + } + } + } + + public boolean isOpaqueCube() + { + return false; + } + + public boolean renderAsNormalBlock() + { + return !this.group.customRenderer; + } + + public int getRenderType() + { + return Binnie.Machine.getMachineRenderID(); + } + + public TileEntity createTileEntity(World world, int metadata) + { + if (this.group.getPackage(metadata) == null) { + return null; + } + return this.group.getPackage(metadata).createTileEntity(); + } + + public MachinePackage getPackage(int meta) + { + return this.group.getPackage(meta); + } + + public String getMachineName(int meta) + { + return getPackage(meta) == null ? "Unnamed Machine" : getPackage(meta).getDisplayName(); + } + + public int damageDropped(int par1) + { + return par1; + } + + public TileEntity createNewTileEntity(World var1, int meta) + { + return createTileEntity(var1, meta); + } + + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) + { + if (!BinnieCore.proxy.isSimulating(world)) { + return true; + } + if (player.isSneaking()) { + return true; + } + TileEntity entity = world.getTileEntity(x, y, z); + if ((entity instanceof TileEntityMachine)) { + ((TileEntityMachine)entity).getMachine().onRightClick(world, player, x, y, z); + } + return true; + } + + public void onBlockPlacedBy(World world, int i, int j, int k, EntityLivingBase entityliving, ItemStack stack) + { + super.onBlockPlacedBy(world, i, j, k, entityliving, stack); + if (!BinnieCore.proxy.isSimulating(world)) { + return; + } + IMachine machine = Machine.getMachine(world.getTileEntity(i, j, k)); + if (machine == null) { + return; + } + if ((entityliving instanceof EntityPlayer)) { + machine.setOwner(((EntityPlayer)entityliving).getGameProfile()); + } + } + + public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) + { + TileEntity entity = world.getTileEntity(x, y, z); + if (((entity instanceof TileEntityMachine)) && (((TileEntityMachine)entity).getMachine().hasInterface(IMachineTexturedFaces.class))) { + return ((IMachineTexturedFaces)((TileEntityMachine)entity).getMachine().getInterface(IMachineTexturedFaces.class)).getIcon(side); + } + return Blocks.dirt.getIcon(0, 0); + } + + public void breakBlock(World world, int x, int y, int z, Block par5, int par6) + { + TileEntity tileentity = world.getTileEntity(x, y, z); + if (!(tileentity instanceof TileEntityMachine)) { + return; + } + TileEntityMachine entity = (TileEntityMachine)tileentity; + if (entity != null) { + entity.onBlockDestroy(); + } + super.breakBlock(world, x, y, z, par5, par6); + } + + @SideOnly(Side.CLIENT) + public void registerBlockIcons(IIconRegister register) {} + + @SideOnly(Side.CLIENT) + public void randomDisplayTick(World world, int x, int y, int z, Random rand) + { + IMachine machine = Machine.getMachine(world.getTileEntity(x, y, z)); + if (machine != null) { + for (IRender.RandomDisplayTick renders : machine.getInterfaces(IRender.RandomDisplayTick.class)) { + renders.onRandomDisplayTick(world, x, y, z, rand); + } + } + } + + public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) + { + return new ArrayList(); + } + + public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) + { + if ((BinnieCore.proxy.isSimulating(world)) && (canHarvestBlock(player, world.getBlockMetadata(x, y, z))) && + (!player.capabilities.isCreativeMode)) + { + int metadata = world.getBlockMetadata(x, y, z); + ItemStack stack = new ItemStack(Item.getItemFromBlock(this), 1, damageDropped(metadata)); + dropBlockAsItem(world, x, y, z, stack); + } + return world.setBlockToAir(x, y, z); + } + + public static abstract interface IMachineTexturedFaces + { + public abstract IIcon getIcon(int paramInt); + } +} diff --git a/src/Java/binnie/core/machines/IBlockMachine.java b/src/Java/binnie/core/machines/IBlockMachine.java new file mode 100644 index 0000000000..01b7e321a1 --- /dev/null +++ b/src/Java/binnie/core/machines/IBlockMachine.java @@ -0,0 +1,8 @@ +package binnie.core.machines; + +abstract interface IBlockMachine +{ + public abstract MachinePackage getPackage(int paramInt); + + public abstract String getMachineName(int paramInt); +} diff --git a/src/Java/binnie/core/machines/IMachine.java b/src/Java/binnie/core/machines/IMachine.java new file mode 100644 index 0000000000..9a57b0e019 --- /dev/null +++ b/src/Java/binnie/core/machines/IMachine.java @@ -0,0 +1,25 @@ +package binnie.core.machines; + +import java.util.Collection; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public abstract interface IMachine + extends IOwnable +{ + public abstract void addComponent(MachineComponent paramMachineComponent); + + public abstract MachineUtil getMachineUtil(); + + public abstract <T> T getInterface(Class<T> paramClass); + + public abstract void markDirty(); + + public abstract World getWorld(); + + public abstract TileEntity getTileEntity(); + + public abstract <T> Collection<T> getInterfaces(Class<T> paramClass); + + public abstract MachinePackage getPackage(); +} diff --git a/src/Java/binnie/core/machines/IMachineType.java b/src/Java/binnie/core/machines/IMachineType.java new file mode 100644 index 0000000000..06c02218a0 --- /dev/null +++ b/src/Java/binnie/core/machines/IMachineType.java @@ -0,0 +1,11 @@ +package binnie.core.machines; + +import binnie.core.network.IOrdinaled; + +public abstract interface IMachineType + extends IOrdinaled +{ + public abstract Class<? extends MachinePackage> getPackageClass(); + + public abstract boolean isActive(); +} diff --git a/src/Java/binnie/core/machines/IOwnable.java b/src/Java/binnie/core/machines/IOwnable.java new file mode 100644 index 0000000000..78aa1a76f9 --- /dev/null +++ b/src/Java/binnie/core/machines/IOwnable.java @@ -0,0 +1,10 @@ +package binnie.core.machines; + +import com.mojang.authlib.GameProfile; + +abstract interface IOwnable +{ + public abstract GameProfile getOwner(); + + public abstract void setOwner(GameProfile paramGameProfile); +} diff --git a/src/Java/binnie/core/machines/ItemMachine.java b/src/Java/binnie/core/machines/ItemMachine.java new file mode 100644 index 0000000000..08426cdb7a --- /dev/null +++ b/src/Java/binnie/core/machines/ItemMachine.java @@ -0,0 +1,29 @@ +package binnie.core.machines; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; + +public class ItemMachine + extends ItemBlock +{ + private IBlockMachine associatedBlock; + + public ItemMachine(Block block) + { + super(block); + setMaxDamage(0); + setHasSubtypes(true); + this.associatedBlock = ((IBlockMachine)block); + } + + public int getMetadata(int i) + { + return i; + } + + public String getItemStackDisplayName(ItemStack itemstack) + { + return this.associatedBlock.getMachineName(itemstack.getItemDamage()); + } +} diff --git a/src/Java/binnie/core/machines/Machine.java b/src/Java/binnie/core/machines/Machine.java new file mode 100644 index 0000000000..474b32c627 --- /dev/null +++ b/src/Java/binnie/core/machines/Machine.java @@ -0,0 +1,320 @@ +package binnie.core.machines; + +import binnie.core.BinnieCore; +import binnie.core.machines.component.IInteraction.RightClick; +import binnie.core.machines.component.IRender.DisplayTick; +import binnie.core.machines.network.INetwork.GuiNBT; +import binnie.core.machines.network.INetwork.RecieveGuiNBT; +import binnie.core.machines.network.INetwork.SendGuiNBT; +import binnie.core.machines.network.INetwork.TilePacketSync; +import binnie.core.machines.power.ITankMachine; +import binnie.core.network.BinnieCorePacketID; +import binnie.core.network.INetworkedEntity; +import binnie.core.network.packet.MessageTileNBT; +import binnie.core.network.packet.PacketPayload; +import binnie.core.proxy.BinnieProxy; +import com.mojang.authlib.GameProfile; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; +import forestry.api.core.INBTTagable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTUtil; +import net.minecraft.network.Packet; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class Machine + implements INetworkedEntity, INBTTagable, INetwork.TilePacketSync, IMachine, INetwork.GuiNBT +{ + private MachinePackage machinePackage; + private Map<Class, List<MachineComponent>> componentInterfaceMap = new LinkedHashMap(); + private Map<Class<? extends MachineComponent>, MachineComponent> componentMap = new LinkedHashMap(); + private TileEntity tile; + + public Machine(MachinePackage pack, TileEntity tile) + { + this.tile = tile; + + pack.createMachine(this); + this.machinePackage = pack; + } + + public void addComponent(MachineComponent component) + { + if (component == null) { + throw new NullPointerException("Can't have a null machine component!"); + } + component.setMachine(this); + this.componentMap.put(component.getClass(), component); + for (Class inter : component.getComponentInterfaces()) + { + if (!this.componentInterfaceMap.containsKey(inter)) { + this.componentInterfaceMap.put(inter, new ArrayList()); + } + ((List)this.componentInterfaceMap.get(inter)).add(component); + } + } + + public Collection<MachineComponent> getComponents() + { + return this.componentMap.values(); + } + + public <T extends MachineComponent> T getComponent(Class<T> componentClass) + { + return hasComponent(componentClass) ? (MachineComponent)componentClass.cast(this.componentMap.get(componentClass)) : null; + } + + public <T> T getInterface(Class<T> interfaceClass) + { + if (hasInterface(interfaceClass)) { + return getInterfaces(interfaceClass).get(0); + } + if (interfaceClass.isInstance(getPackage())) { + return interfaceClass.cast(getPackage()); + } + for (MachineComponent component : getComponents()) { + if (interfaceClass.isInstance(component)) { + return interfaceClass.cast(component); + } + } + return null; + } + + public <T> List<T> getInterfaces(Class<T> interfaceClass) + { + ArrayList<T> interfaces = new ArrayList(); + if (!hasInterface(interfaceClass)) { + return interfaces; + } + for (MachineComponent component : (List)this.componentInterfaceMap.get(interfaceClass)) { + interfaces.add(interfaceClass.cast(component)); + } + return interfaces; + } + + public boolean hasInterface(Class<?> interfaceClass) + { + return this.componentInterfaceMap.containsKey(interfaceClass); + } + + public boolean hasComponent(Class<? extends MachineComponent> componentClass) + { + return this.componentMap.containsKey(componentClass); + } + + public TileEntity getTileEntity() + { + return this.tile; + } + + public void sendPacket() + { + if (!BinnieCore.proxy.isSimulating(getTileEntity().getWorldObj())) { + return; + } + BinnieCore.proxy.sendNetworkEntityPacket((INetworkedEntity)getTileEntity()); + } + + public Side getSide() + { + return BinnieCore.proxy.isSimulating(getTileEntity().getWorldObj()) ? Side.SERVER : Side.CLIENT; + } + + public void writeToPacket(PacketPayload payload) + { + for (MachineComponent component : getComponents()) { + if ((component instanceof INetworkedEntity)) { + ((INetworkedEntity)component).writeToPacket(payload); + } + } + } + + public void readFromPacket(PacketPayload payload) + { + for (MachineComponent component : getComponents()) { + if ((component instanceof INetworkedEntity)) { + ((INetworkedEntity)component).readFromPacket(payload); + } + } + } + + public void onRightClick(World world, EntityPlayer player, int x, int y, int z) + { + for (IInteraction.RightClick component : getInterfaces(IInteraction.RightClick.class)) { + component.onRightClick(world, player, x, y, z); + } + } + + public void markDirty() + { + this.queuedInventoryUpdate = true; + } + + private boolean queuedInventoryUpdate = false; + + public void onUpdate() + { + if (BinnieCore.proxy.isSimulating(getWorld())) { + for (MachineComponent component : getComponents()) { + component.onUpdate(); + } + } else { + for (IRender.DisplayTick renders : getInterfaces(IRender.DisplayTick.class)) { + renders.onDisplayTick(getWorld(), getTileEntity().xCoord, getTileEntity().yCoord, getTileEntity().zCoord, getWorld().rand); + } + } + if (this.queuedInventoryUpdate) + { + for (MachineComponent component : getComponents()) { + component.onInventoryUpdate(); + } + this.queuedInventoryUpdate = false; + } + } + + public IInventory getInventory() + { + return (IInventory)getInterface(IInventory.class); + } + + public ITankMachine getTankContainer() + { + return (ITankMachine)getInterface(ITankMachine.class); + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + for (MachineComponent component : getComponents()) { + component.readFromNBT(nbttagcompound); + } + this.owner = NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("owner")); + markDirty(); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + for (MachineComponent component : getComponents()) { + component.writeToNBT(nbttagcompound); + } + if (this.owner != null) + { + NBTTagCompound nbt = new NBTTagCompound(); + NBTUtil.func_152460_a(nbt, this.owner); + nbttagcompound.setTag("owner", nbt); + } + } + + public MachinePackage getPackage() + { + return this.machinePackage; + } + + public static IMachine getMachine(Object inventory) + { + if ((inventory != null) && ((inventory instanceof IMachine))) { + return (IMachine)inventory; + } + if ((inventory != null) && ((inventory instanceof TileEntityMachine))) { + return ((TileEntityMachine)inventory).getMachine(); + } + if ((inventory != null) && ((inventory instanceof MachineComponent))) { + return ((MachineComponent)inventory).getMachine(); + } + return null; + } + + public static <T> T getInterface(Class<T> interfac, Object inventory) + { + IMachine machine = getMachine(inventory); + if (machine != null) { + return machine.getInterface(interfac); + } + if (interfac.isInstance(inventory)) { + return interfac.cast(inventory); + } + return null; + } + + public MachineUtil getMachineUtil() + { + return new MachineUtil(this); + } + + public World getWorld() + { + return getTileEntity().getWorldObj(); + } + + public void onBlockDestroy() + { + for (MachineComponent component : getComponents()) { + component.onDestruction(); + } + } + + private int nextProgressBarID = 0; + + public int getUniqueProgressBarID() + { + return this.nextProgressBarID++; + } + + private GameProfile owner = null; + + public GameProfile getOwner() + { + return this.owner; + } + + public void setOwner(GameProfile owner) + { + this.owner = owner; + } + + public Packet getDescriptionPacket() + { + NBTTagCompound nbt = new NBTTagCompound(); + + syncToNBT(nbt); + if (nbt.hasNoTags()) { + return null; + } + return BinnieCore.instance.getNetworkWrapper().getPacketFrom(new MessageTileNBT(BinnieCorePacketID.TileDescriptionSync.ordinal(), getTileEntity(), nbt).GetMessage()); + } + + public void syncToNBT(NBTTagCompound nbt) + { + for (INetwork.TilePacketSync comp : getInterfaces(INetwork.TilePacketSync.class)) { + comp.syncToNBT(nbt); + } + } + + public void syncFromNBT(NBTTagCompound nbt) + { + for (INetwork.TilePacketSync comp : getInterfaces(INetwork.TilePacketSync.class)) { + comp.syncFromNBT(nbt); + } + } + + public void recieveGuiNBT(Side side, EntityPlayer player, String name, NBTTagCompound nbt) + { + for (INetwork.RecieveGuiNBT recieve : getInterfaces(INetwork.RecieveGuiNBT.class)) { + recieve.recieveGuiNBT(side, player, name, nbt); + } + } + + public void sendGuiNBT(Map<String, NBTTagCompound> nbt) + { + for (INetwork.SendGuiNBT recieve : getInterfaces(INetwork.SendGuiNBT.class)) { + recieve.sendGuiNBT(nbt); + } + } +} diff --git a/src/Java/binnie/core/machines/MachineComponent.java b/src/Java/binnie/core/machines/MachineComponent.java new file mode 100644 index 0000000000..3b90497fc3 --- /dev/null +++ b/src/Java/binnie/core/machines/MachineComponent.java @@ -0,0 +1,61 @@ +package binnie.core.machines; + +import binnie.Binnie; +import binnie.core.network.packet.MachinePayload; +import forestry.api.core.INBTTagable; +import net.minecraft.inventory.IInventory; +import net.minecraft.nbt.NBTTagCompound; + +public class MachineComponent + implements INBTTagable +{ + private IMachine machine; + + public MachineComponent(IMachine machine) + { + setMachine(machine); + machine.addComponent(this); + } + + public void setMachine(IMachine machine) + { + this.machine = machine; + } + + public IMachine getMachine() + { + return this.machine; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) {} + + public void writeToNBT(NBTTagCompound nbttagcompound) {} + + public void onUpdate() {} + + public Class[] getComponentInterfaces() + { + return Binnie.Machine.getComponentInterfaces(getClass()); + } + + public void onInventoryUpdate() {} + + public final MachinePayload getPayload() + { + return new MachinePayload(Binnie.Machine.getNetworkID(getClass())); + } + + public void recieveData(MachinePayload payload) {} + + public MachineUtil getUtil() + { + return getMachine().getMachineUtil(); + } + + public void onDestruction() {} + + public IInventory getInventory() + { + return (IInventory)getMachine().getInterface(IInventory.class); + } +} diff --git a/src/Java/binnie/core/machines/MachineGroup.java b/src/Java/binnie/core/machines/MachineGroup.java new file mode 100644 index 0000000000..03984f1f10 --- /dev/null +++ b/src/Java/binnie/core/machines/MachineGroup.java @@ -0,0 +1,113 @@ +package binnie.core.machines; + +import binnie.Binnie; +import binnie.core.AbstractMod; +import cpw.mods.fml.common.registry.GameRegistry; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import net.minecraft.creativetab.CreativeTabs; + +public class MachineGroup +{ + private AbstractMod mod; + private String blockName; + private String uid; + + public MachineGroup(AbstractMod mod, String uid, String blockName, IMachineType[] types) + { + this.mod = mod; + this.uid = uid; + this.blockName = blockName; + for (IMachineType type : types) { + if ((type.getPackageClass() != null) && (type.isActive())) { + try + { + MachinePackage pack = (MachinePackage)type.getPackageClass().newInstance(); + pack.assignMetadata(type.ordinal()); + pack.setActive(type.isActive()); + addPackage(pack); + } + catch (Exception e) + { + throw new RuntimeException("Failed to create machine package " + type.toString(), e); + } + } + } + Binnie.Machine.registerMachineGroup(this); + + this.block = new BlockMachine(this, blockName); + if (this.block != null) + { + GameRegistry.registerBlock(this.block, ItemMachine.class, blockName); + for (MachinePackage pack : getPackages()) { + pack.register(); + } + } + } + + private Map<String, MachinePackage> packages = new LinkedHashMap(); + private Map<Integer, MachinePackage> packagesID = new LinkedHashMap(); + private BlockMachine block; + + private void addPackage(MachinePackage pack) + { + this.packages.put(pack.getUID(), pack); + this.packagesID.put(pack.getMetadata(), pack); + pack.setGroup(this); + } + + public Collection<MachinePackage> getPackages() + { + return this.packages.values(); + } + + public boolean customRenderer = true; + + public BlockMachine getBlock() + { + return this.block; + } + + public MachinePackage getPackage(int metadata) + { + return (MachinePackage)this.packagesID.get(Integer.valueOf(metadata)); + } + + public MachinePackage getPackage(String name) + { + return (MachinePackage)this.packages.get(name); + } + + public String getUID() + { + return this.mod.getModID() + "." + this.uid; + } + + public String getShortUID() + { + return this.uid; + } + + private boolean renderedTileEntity = true; + + boolean isTileEntityRenderered() + { + return this.renderedTileEntity; + } + + public void renderAsBlock() + { + this.renderedTileEntity = false; + } + + public void setCreativeTab(CreativeTabs tab) + { + this.block.setCreativeTab(tab); + } + + public AbstractMod getMod() + { + return this.mod; + } +} diff --git a/src/Java/binnie/core/machines/MachinePackage.java b/src/Java/binnie/core/machines/MachinePackage.java new file mode 100644 index 0000000000..943db084b6 --- /dev/null +++ b/src/Java/binnie/core/machines/MachinePackage.java @@ -0,0 +1,74 @@ +package binnie.core.machines; + +import binnie.Binnie; +import binnie.core.language.ManagerLanguage; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.tileentity.TileEntity; + +public abstract class MachinePackage +{ + private String uid; + private boolean active = true; + boolean powered = false; + private int metadata = -1; + private MachineGroup group; + + public String getUID() + { + return this.uid; + } + + protected MachinePackage(String uid, boolean powered) + { + this.uid = uid; + this.powered = powered; + } + + public abstract void createMachine(Machine paramMachine); + + public abstract TileEntity createTileEntity(); + + public abstract void register(); + + public final String getDisplayName() + { + return Binnie.Language.localise(this.group.getMod(), "machine." + this.group.getShortUID() + "." + getUID()); + } + + public final Integer getMetadata() + { + return Integer.valueOf(this.metadata); + } + + public void assignMetadata(int meta) + { + this.metadata = meta; + } + + public MachineGroup getGroup() + { + return this.group; + } + + public void setGroup(MachineGroup group) + { + this.group = group; + } + + public abstract void renderMachine(Machine paramMachine, double paramDouble1, double paramDouble2, double paramDouble3, float paramFloat, RenderBlocks paramRenderBlocks); + + public boolean isActive() + { + return this.active; + } + + public void setActive(boolean active) + { + this.active = active; + } + + public final String getInformation() + { + return Binnie.Language.localise(this.group.getMod(), "machine." + this.group.getShortUID() + "." + getUID() + ".info"); + } +} diff --git a/src/Java/binnie/core/machines/MachineRendererBlock.java b/src/Java/binnie/core/machines/MachineRendererBlock.java new file mode 100644 index 0000000000..9506746df4 --- /dev/null +++ b/src/Java/binnie/core/machines/MachineRendererBlock.java @@ -0,0 +1,41 @@ +package binnie.core.machines; + +import binnie.core.BinnieCore; +import binnie.core.proxy.BinnieProxy; +import binnie.core.resource.BinnieResource; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import org.lwjgl.opengl.GL11; + +@SideOnly(Side.CLIENT) +public class MachineRendererBlock +{ + public static MachineRendererBlock instance = new MachineRendererBlock(); + private BinnieResource texture; + private ModelBlock model; + + public MachineRendererBlock() + { + this.model = new ModelBlock(); + } + + public void renderMachine(BinnieResource texture, double x, double y, double z, float var8) + { + this.texture = texture; + + GL11.glPushMatrix(); + + GL11.glTranslated(x + 0.5D, y + 1.5D, z + 0.5D); + GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); + + BinnieCore.proxy.bindTexture(texture); + + GL11.glPushMatrix(); + + this.model.render((float)x, (float)y, (float)z, 0.0625F, 0.0625F, 0.0625F); + + GL11.glPopMatrix(); + + GL11.glPopMatrix(); + } +} diff --git a/src/Java/binnie/core/machines/MachineUtil.java b/src/Java/binnie/core/machines/MachineUtil.java new file mode 100644 index 0000000000..97ff515061 --- /dev/null +++ b/src/Java/binnie/core/machines/MachineUtil.java @@ -0,0 +1,216 @@ +package binnie.core.machines; + +import binnie.core.BinnieCore; +import binnie.core.machines.inventory.IChargedSlots; +import binnie.core.machines.power.IPoweredMachine; +import binnie.core.machines.power.IProcess; +import binnie.core.machines.power.ITankMachine; +import binnie.core.machines.power.PowerInterface; +import binnie.core.machines.power.PowerSystem; +import binnie.core.proxy.BinnieProxy; +import binnie.core.util.ItemStackSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidTank; + +public class MachineUtil +{ + private IMachine machine; + + public MachineUtil(IMachine machine) + { + this.machine = machine; + } + + public IInventory getInventory() + { + return (IInventory)this.machine.getInterface(IInventory.class); + } + + public ITankMachine getTankContainer() + { + return (ITankMachine)this.machine.getInterface(ITankMachine.class); + } + + public IPoweredMachine getPoweredMachine() + { + return (IPoweredMachine)this.machine.getInterface(IPoweredMachine.class); + } + + public boolean isSlotEmpty(int slot) + { + return getInventory().getStackInSlot(slot) == null; + } + + public IFluidTank getTank(int id) + { + return getTankContainer().getTanks()[id]; + } + + public boolean spaceInTank(int id, int amount) + { + IFluidTank tank = getTank(id); + int space = tank.getCapacity() - tank.getFluidAmount(); + return amount <= space; + } + + public ItemStack getStack(int slot) + { + return getInventory().getStackInSlot(slot); + } + + public void deleteStack(int slot) + { + setStack(slot, null); + } + + public ItemStack decreaseStack(int slotWood, int amount) + { + return getInventory().decrStackSize(slotWood, amount); + } + + public void setStack(int slot, ItemStack stack) + { + getInventory().setInventorySlotContents(slot, stack); + } + + public void fillTank(int id, FluidStack liquidStack) + { + IFluidTank tank = getTank(id); + tank.fill(liquidStack, true); + } + + public void addStack(int slot, ItemStack addition) + { + if (isSlotEmpty(slot)) + { + setStack(slot, addition); + } + else + { + ItemStack merge = getStack(slot); + if ((merge.isItemEqual(addition)) && (merge.stackSize + addition.stackSize <= merge.getMaxStackSize())) + { + merge.stackSize += addition.stackSize; + setStack(slot, merge); + } + } + } + + public FluidStack drainTank(int tank, int amount) + { + return getTank(tank).drain(amount, true); + } + + public boolean liquidInTank(int tank, int amount) + { + return (getTank(tank).drain(amount, false) != null) && (getTank(tank).drain(amount, false).amount == amount); + } + + public void damageItem(int slot, int damage) + { + ItemStack item = getStack(slot); + if (damage < 0) { + item.setItemDamage(Math.max(0, item.getItemDamage() + damage)); + } else if (item.attemptDamageItem(damage, new Random())) { + setStack(slot, null); + } + setStack(slot, item); + } + + public boolean isTankEmpty(int tankInput) + { + return getTank(tankInput).getFluidAmount() == 0; + } + + public FluidStack getFluid(int tankInput) + { + return getTank(tankInput).getFluid() == null ? null : getTank(tankInput).getFluid(); + } + + public ItemStack[] getStacks(int[] slotGrains) + { + ItemStack[] stacks = new ItemStack[slotGrains.length]; + for (int i = 0; i < slotGrains.length; i++) { + stacks[i] = getStack(slotGrains[i]); + } + return stacks; + } + + public ItemStack hasIngredients(int recipe, int[] inventory) + { + return null; + } + + public boolean hasIngredients(int[] recipe, int[] inventory) + { + ItemStackSet requiredStacks = new ItemStackSet(); + for (ItemStack stack : getStacks(recipe)) { + requiredStacks.add(stack); + } + ItemStackSet inventoryStacks = new ItemStackSet(); + for (ItemStack stack : getStacks(inventory)) { + inventoryStacks.add(stack); + } + requiredStacks.removeAll(inventoryStacks); + + return requiredStacks.isEmpty(); + } + + public void useEnergyMJ(float powerUsage) + { + getPoweredMachine().getInterface().useEnergy(PowerSystem.MJ, powerUsage, true); + } + + public boolean hasEnergyMJ(float powerUsage) + { + return getPoweredMachine().getInterface().useEnergy(PowerSystem.MJ, powerUsage, false) >= powerUsage; + } + + public float getSlotCharge(int slot) + { + return ((IChargedSlots)this.machine.getInterface(IChargedSlots.class)).getCharge(slot); + } + + public void useCharge(int slot, float loss) + { + ((IChargedSlots)this.machine.getInterface(IChargedSlots.class)).alterCharge(slot, -loss); + } + + public Random getRandom() + { + return new Random(); + } + + public void refreshBlock() + { + this.machine.getWorld().markBlockForUpdate(this.machine.getTileEntity().xCoord, this.machine.getTileEntity().yCoord, this.machine.getTileEntity().zCoord); + } + + public IProcess getProcess() + { + return (IProcess)this.machine.getInterface(IProcess.class); + } + + public List<ItemStack> getNonNullStacks(int[] slotacclimatiser) + { + List<ItemStack> stacks = new ArrayList(); + for (ItemStack stack : getStacks(slotacclimatiser)) { + if (stack != null) { + stacks.add(stack); + } + } + return stacks; + } + + public boolean isServer() + { + return BinnieCore.proxy.isSimulating(this.machine.getWorld()); + } +} diff --git a/src/Java/binnie/core/machines/ManagerMachine.java b/src/Java/binnie/core/machines/ManagerMachine.java new file mode 100644 index 0000000000..53920e5496 --- /dev/null +++ b/src/Java/binnie/core/machines/ManagerMachine.java @@ -0,0 +1,108 @@ +package binnie.core.machines; + +import binnie.core.BinnieCore; +import binnie.core.ManagerBase; +import binnie.core.machines.inventory.ValidatorIcon; +import binnie.core.proxy.BinnieProxy; +import forestry.api.core.INBTTagable; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class ManagerMachine + extends ManagerBase +{ + private Map<Class<?>, Class<?>[]> componentInterfaceMap = new HashMap(); + private Map<String, MachineGroup> machineGroups = new HashMap(); + private Map<Integer, Class<?>> networkIDToComponent = new HashMap(); + private Map<Class<?>, Integer> componentToNetworkID = new HashMap(); + private int nextNetworkID = 0; + private int machineRenderID; + + public void registerMachineGroup(MachineGroup group) + { + this.machineGroups.put(group.getUID(), group); + } + + public MachineGroup getGroup(String name) + { + return (MachineGroup)this.machineGroups.get(name); + } + + public MachinePackage getPackage(String group, String name) + { + MachineGroup machineGroup = getGroup(group); + return machineGroup == null ? null : machineGroup.getPackage(name); + } + + private void registerComponentClass(Class<? extends MachineComponent> component) + { + if (this.componentInterfaceMap.containsKey(component)) { + return; + } + Set<Class<?>> interfaces = new HashSet(); + + Class<?> currentClass = component; + while (currentClass != null) + { + for (Class<?> clss : currentClass.getInterfaces()) { + interfaces.add(clss); + } + currentClass = currentClass.getSuperclass(); + if (currentClass == Object.class) { + currentClass = null; + } + } + interfaces.remove(INBTTagable.class); + + this.componentInterfaceMap.put(component, interfaces.toArray(new Class[0])); + + int networkID = this.nextNetworkID++; + + this.networkIDToComponent.put(Integer.valueOf(networkID), component); + this.componentToNetworkID.put(component, Integer.valueOf(networkID)); + } + + public int getNetworkID(Class<?> component) + { + return ((Integer)this.componentToNetworkID.get(component)).intValue(); + } + + public Class<?> getComponentClass(int networkID) + { + return (Class)this.networkIDToComponent.get(Integer.valueOf(networkID)); + } + + public int getMachineRenderID() + { + return this.machineRenderID; + } + + public void init() + { + this.machineRenderID = BinnieCore.proxy.getUniqueRenderID(); + + binnie.core.machines.inventory.SlotValidator.IconBee = new ValidatorIcon(BinnieCore.instance, "validator/bee.0", "validator/bee.1"); + + binnie.core.machines.inventory.SlotValidator.IconFrame = new ValidatorIcon(BinnieCore.instance, "validator/frame.0", "validator/frame.1"); + + binnie.core.machines.inventory.SlotValidator.IconCircuit = new ValidatorIcon(BinnieCore.instance, "validator/circuit.0", "validator/circuit.1"); + + binnie.core.machines.inventory.SlotValidator.IconBlock = new ValidatorIcon(BinnieCore.instance, "validator/block.0", "validator/block.1"); + } + + public void postInit() + { + BinnieCore.proxy.registerBlockRenderer(BinnieCore.proxy.createObject("binnie.core.machines.RendererMachine")); + BinnieCore.proxy.registerTileEntity(TileEntityMachine.class, "binnie.tile.machine", BinnieCore.proxy.createObject("binnie.core.machines.RendererMachine")); + } + + public Class<?>[] getComponentInterfaces(Class<? extends MachineComponent> clss) + { + if (!this.componentInterfaceMap.containsKey(clss)) { + registerComponentClass(clss); + } + return (Class[])this.componentInterfaceMap.get(clss); + } +} diff --git a/src/Java/binnie/core/machines/ModelBlock.java b/src/Java/binnie/core/machines/ModelBlock.java new file mode 100644 index 0000000000..7258ec8654 --- /dev/null +++ b/src/Java/binnie/core/machines/ModelBlock.java @@ -0,0 +1,42 @@ +package binnie.core.machines; + +import net.minecraft.client.model.ModelBase; +import net.minecraft.client.model.ModelRenderer; + +class ModelBlock + extends ModelBase +{ + private ModelRenderer Block; + + public ModelBlock() + { + this.textureWidth = 64; + this.textureHeight = 32; + + this.Block = new ModelRenderer(this, 0, 0); + this.Block.addBox(0.0F, 0.0F, 0.0F, 16, 16, 16); + this.Block.setRotationPoint(-8.0F, 8.0F, -8.0F); + this.Block.setTextureSize(64, 32); + this.Block.mirror = true; + setRotation(this.Block, 0.0F, 0.0F, 0.0F); + } + + public void render(float f, float f1, float f2, float f3, float f4, float f5) + { + super.render(null, f, f1, f2, f3, f4, f5); + setRotationAngles(f, f1, f2, f3, f4, f5); + this.Block.render(f5); + } + + private void setRotation(ModelRenderer model, float x, float y, float z) + { + model.rotateAngleX = x; + model.rotateAngleY = y; + model.rotateAngleZ = z; + } + + public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5) + { + super.setRotationAngles(f, f1, f2, f3, f4, f5, null); + } +} diff --git a/src/Java/binnie/core/machines/RendererMachine.java b/src/Java/binnie/core/machines/RendererMachine.java new file mode 100644 index 0000000000..18ad4afe92 --- /dev/null +++ b/src/Java/binnie/core/machines/RendererMachine.java @@ -0,0 +1,68 @@ +package binnie.core.machines; + +import binnie.Binnie; +import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; +import net.minecraft.block.Block; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +public class RendererMachine + extends TileEntitySpecialRenderer + implements ISimpleBlockRenderingHandler +{ + RenderBlocks blockRenderer; + + public void renderTileEntityAt(TileEntity entity, double x, double y, double z, float var8) + { + renderTileEntity((TileEntityMachine)entity, x, y, z, var8, this.blockRenderer); + } + + public void renderTileEntity(TileEntityMachine entity, double x, double y, double z, float var8, RenderBlocks renderer) + { + if ((entity != null) && (entity.getMachine() != null)) + { + MachinePackage machinePackage = entity.getMachine().getPackage(); + machinePackage.renderMachine(entity.getMachine(), x, y, z, var8, renderer); + } + } + + public void renderInvBlock(RenderBlocks renderblocks, Block block, int i, int j) + { + TileEntity entity = block.createTileEntity(null, i); + renderTileEntity((TileEntityMachine)entity, 0.0D, -0.1D, 0.0D, 0.0625F, renderblocks); + } + + public void renderInventoryBlock(Block block, int metadata, int modelID, RenderBlocks renderer) + { + if (modelID == Binnie.Machine.getMachineRenderID()) { + renderInvBlock(renderer, block, metadata, modelID); + } + } + + public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) + { + TileEntityMachine tile = (TileEntityMachine)world.getTileEntity(x, y, z); + if ((tile != null) && (tile.getMachine() != null) && (tile.getMachine().getPackage() != null) && (tile.getMachine().getPackage().getGroup() != null) && (!tile.getMachine().getPackage().getGroup().customRenderer)) { + renderTileEntity(tile, x, y, z, 1.0F, renderer); + } + return true; + } + + public boolean shouldRender3DInInventory(int i) + { + return true; + } + + public int getRenderId() + { + return Binnie.Machine.getMachineRenderID(); + } + + public void func_147496_a(World par1World) + { + this.blockRenderer = new RenderBlocks(par1World); + } +} diff --git a/src/Java/binnie/core/machines/TileEntityMachine.java b/src/Java/binnie/core/machines/TileEntityMachine.java new file mode 100644 index 0000000000..c6acf1fda8 --- /dev/null +++ b/src/Java/binnie/core/machines/TileEntityMachine.java @@ -0,0 +1,112 @@ +package binnie.core.machines; + +import binnie.Binnie; +import binnie.core.machines.base.TileEntityMachineBase; +import binnie.core.machines.component.IInteraction.ChunkUnload; +import binnie.core.machines.component.IInteraction.Invalidation; +import binnie.core.network.INetworkedEntity; +import binnie.core.network.packet.PacketPayload; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.Packet; + +public class TileEntityMachine + extends TileEntityMachineBase + implements INetworkedEntity +{ + private Machine machine; + + public void updateEntity() + { + super.updateEntity(); + if (this.machine != null) { + this.machine.onUpdate(); + } + } + + public boolean canUpdate() + { + return super.canUpdate(); + } + + public TileEntityMachine(MachinePackage pack) + { + setMachine(pack); + } + + public TileEntityMachine() {} + + public void setMachine(MachinePackage pack) + { + if (pack != null) { + this.machine = new Machine(pack, this); + } + } + + public void readFromNBT(NBTTagCompound nbtTagCompound) + { + super.readFromNBT(nbtTagCompound); + String name = nbtTagCompound.getString("name"); + String group = nbtTagCompound.getString("group"); + MachinePackage pack = Binnie.Machine.getPackage(group, name); + if (pack == null) + { + invalidate(); + return; + } + setMachine(pack); + getMachine().readFromNBT(nbtTagCompound); + } + + public void writeToNBT(NBTTagCompound nbtTagCompound) + { + super.writeToNBT(nbtTagCompound); + String name = this.machine.getPackage().getUID(); + String group = this.machine.getPackage().getGroup().getUID(); + nbtTagCompound.setString("group", group); + nbtTagCompound.setString("name", name); + getMachine().writeToNBT(nbtTagCompound); + } + + public void writeToPacket(PacketPayload payload) + { + this.machine.writeToPacket(payload); + } + + public void readFromPacket(PacketPayload payload) + { + this.machine.readFromPacket(payload); + } + + public Machine getMachine() + { + return this.machine; + } + + public void onBlockDestroy() + { + if (getMachine() != null) { + getMachine().onBlockDestroy(); + } + } + + public final Packet getDescriptionPacket() + { + return getMachine() != null ? getMachine().getDescriptionPacket() : null; + } + + public void invalidate() + { + super.invalidate(); + for (IInteraction.Invalidation c : getMachine().getInterfaces(IInteraction.Invalidation.class)) { + c.onInvalidation(); + } + } + + public void onChunkUnload() + { + super.onChunkUnload(); + for (IInteraction.ChunkUnload c : getMachine().getInterfaces(IInteraction.ChunkUnload.class)) { + c.onChunkUnload(); + } + } +} diff --git a/src/Java/binnie/core/machines/base/DefaultInventory.java b/src/Java/binnie/core/machines/base/DefaultInventory.java new file mode 100644 index 0000000000..b72ebd3b57 --- /dev/null +++ b/src/Java/binnie/core/machines/base/DefaultInventory.java @@ -0,0 +1,82 @@ +package binnie.core.machines.base; + +import binnie.core.machines.inventory.IInventoryMachine; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +class DefaultInventory + implements IInventoryMachine +{ + public int getSizeInventory() + { + return 0; + } + + public ItemStack getStackInSlot(int i) + { + return null; + } + + public ItemStack decrStackSize(int i, int j) + { + return null; + } + + public ItemStack getStackInSlotOnClosing(int i) + { + return null; + } + + public void setInventorySlotContents(int i, ItemStack itemstack) {} + + public int getInventoryStackLimit() + { + return 64; + } + + public boolean isUseableByPlayer(EntityPlayer entityplayer) + { + return false; + } + + public boolean isItemValidForSlot(int i, ItemStack itemstack) + { + return false; + } + + public int[] getAccessibleSlotsFromSide(int var1) + { + return new int[0]; + } + + public boolean canInsertItem(int i, ItemStack itemstack, int j) + { + return false; + } + + public boolean canExtractItem(int i, ItemStack itemstack, int j) + { + return false; + } + + public boolean isReadOnly(int slot) + { + return false; + } + + public String getInventoryName() + { + return ""; + } + + public boolean hasCustomInventoryName() + { + return false; + } + + public void markDirty() {} + + public void openInventory() {} + + public void closeInventory() {} +} diff --git a/src/Java/binnie/core/machines/base/DefaultPower.java b/src/Java/binnie/core/machines/base/DefaultPower.java new file mode 100644 index 0000000000..5d3e143b2b --- /dev/null +++ b/src/Java/binnie/core/machines/base/DefaultPower.java @@ -0,0 +1,71 @@ +package binnie.core.machines.base; + +import binnie.core.machines.power.IPoweredMachine; +import binnie.core.machines.power.PowerInfo; +import binnie.core.machines.power.PowerInterface; +import cpw.mods.fml.common.Optional.Method; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.util.ForgeDirection; + +class DefaultPower + implements IPoweredMachine +{ + public PowerInfo getPowerInfo() + { + return new PowerInfo(this, 0.0F); + } + + @Optional.Method(modid="IC2") + public double getDemandedEnergy() + { + return 0.0D; + } + + @Optional.Method(modid="IC2") + public int getSinkTier() + { + return 0; + } + + @Optional.Method(modid="IC2") + public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) + { + return 0.0D; + } + + @Optional.Method(modid="IC2") + public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) + { + return false; + } + + public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) + { + return 0; + } + + public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) + { + return 0; + } + + public int getEnergyStored(ForgeDirection from) + { + return 0; + } + + public int getMaxEnergyStored(ForgeDirection from) + { + return 0; + } + + public boolean canConnectEnergy(ForgeDirection from) + { + return false; + } + + public PowerInterface getInterface() + { + return null; + } +} diff --git a/src/Java/binnie/core/machines/base/DefaultTankContainer.java b/src/Java/binnie/core/machines/base/DefaultTankContainer.java new file mode 100644 index 0000000000..a0c93b1168 --- /dev/null +++ b/src/Java/binnie/core/machines/base/DefaultTankContainer.java @@ -0,0 +1,79 @@ +package binnie.core.machines.base; + +import binnie.core.machines.inventory.TankSlot; +import binnie.core.machines.power.ITankMachine; +import binnie.core.machines.power.TankInfo; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; + +class DefaultTankContainer + implements ITankMachine +{ + public int fill(ForgeDirection from, FluidStack resource, boolean doFill) + { + return 0; + } + + public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) + { + return null; + } + + public TankInfo[] getTankInfos() + { + return new TankInfo[0]; + } + + public boolean isTankReadOnly(int tank) + { + return false; + } + + public boolean isLiquidValidForTank(FluidStack liquid, int tank) + { + return false; + } + + public TankSlot addTank(int index, String name, int capacity) + { + return null; + } + + public IFluidTank getTank(int index) + { + return null; + } + + public TankSlot getTankSlot(int slot) + { + return null; + } + + public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) + { + return null; + } + + public boolean canFill(ForgeDirection from, Fluid fluid) + { + return false; + } + + public boolean canDrain(ForgeDirection from, Fluid fluid) + { + return false; + } + + public FluidTankInfo[] getTankInfo(ForgeDirection from) + { + return new FluidTankInfo[0]; + } + + public IFluidTank[] getTanks() + { + return new IFluidTank[0]; + } +} diff --git a/src/Java/binnie/core/machines/base/TileEntityMachineBase.java b/src/Java/binnie/core/machines/base/TileEntityMachineBase.java new file mode 100644 index 0000000000..927671b67a --- /dev/null +++ b/src/Java/binnie/core/machines/base/TileEntityMachineBase.java @@ -0,0 +1,262 @@ +package binnie.core.machines.base; + +import binnie.core.machines.Machine; +import binnie.core.machines.inventory.IInventoryMachine; +import binnie.core.machines.inventory.TankSlot; +import binnie.core.machines.power.IPoweredMachine; +import binnie.core.machines.power.ITankMachine; +import binnie.core.machines.power.PowerInfo; +import binnie.core.machines.power.PowerInterface; +import binnie.core.machines.power.TankInfo; +import cpw.mods.fml.common.Optional.Method; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; + +public class TileEntityMachineBase + extends TileEntity + implements IInventoryMachine, ITankMachine, IPoweredMachine +{ + public IInventoryMachine getInventory() + { + IInventoryMachine inv = (IInventoryMachine)Machine.getInterface(IInventoryMachine.class, this); + return (inv == null) || (inv == this) ? new DefaultInventory() : inv; + } + + public ITankMachine getTankContainer() + { + ITankMachine inv = (ITankMachine)Machine.getInterface(ITankMachine.class, this); + return (inv == null) || (inv == this) ? new DefaultTankContainer() : inv; + } + + public IPoweredMachine getPower() + { + IPoweredMachine inv = (IPoweredMachine)Machine.getInterface(IPoweredMachine.class, this); + return (inv == null) || (inv == this) ? new DefaultPower() : inv; + } + + public int getSizeInventory() + { + return getInventory().getSizeInventory(); + } + + public ItemStack getStackInSlot(int index) + { + return getInventory().getStackInSlot(index); + } + + public ItemStack decrStackSize(int index, int amount) + { + return getInventory().decrStackSize(index, amount); + } + + public ItemStack getStackInSlotOnClosing(int var1) + { + return getInventory().getStackInSlotOnClosing(var1); + } + + public void setInventorySlotContents(int index, ItemStack itemStack) + { + getInventory().setInventorySlotContents(index, itemStack); + } + + public String getInventoryName() + { + return getInventory().getInventoryName(); + } + + public int getInventoryStackLimit() + { + return getInventory().getInventoryStackLimit(); + } + + public boolean isUseableByPlayer(EntityPlayer entityplayer) + { + if (isInvalid()) { + return false; + } + if (getWorldObj().getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this) { + return false; + } + if (entityplayer.getDistanceSq(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D) > 64.0D) { + return false; + } + return getInventory().isUseableByPlayer(entityplayer); + } + + public void openInventory() + { + getInventory().openInventory(); + } + + public void closeInventory() + { + getInventory().closeInventory(); + } + + public boolean hasCustomInventoryName() + { + return getInventory().hasCustomInventoryName(); + } + + public void markDirty() + { + super.markDirty(); + getInventory().markDirty(); + } + + public boolean isItemValidForSlot(int slot, ItemStack itemStack) + { + return getInventory().isItemValidForSlot(slot, itemStack); + } + + public int[] getAccessibleSlotsFromSide(int var1) + { + return getInventory().getAccessibleSlotsFromSide(var1); + } + + public boolean canInsertItem(int i, ItemStack itemstack, int j) + { + return getInventory().canInsertItem(i, itemstack, j); + } + + public boolean canExtractItem(int i, ItemStack itemstack, int j) + { + return getInventory().canExtractItem(i, itemstack, j); + } + + public boolean isReadOnly(int slot) + { + return getInventory().isReadOnly(slot); + } + + public PowerInfo getPowerInfo() + { + return getPower().getPowerInfo(); + } + + public TankInfo[] getTankInfos() + { + return getTankContainer().getTankInfos(); + } + + public boolean isTankReadOnly(int tank) + { + return getTankContainer().isTankReadOnly(tank); + } + + public boolean isLiquidValidForTank(FluidStack liquid, int tank) + { + return getTankContainer().isLiquidValidForTank(liquid, tank); + } + + public TankSlot addTank(int index, String name, int capacity) + { + return getTankContainer().addTank(index, name, capacity); + } + + public IFluidTank getTank(int index) + { + return getTankContainer().getTank(index); + } + + public TankSlot getTankSlot(int index) + { + return getTankContainer().getTankSlot(index); + } + + public int fill(ForgeDirection from, FluidStack resource, boolean doFill) + { + return getTankContainer().fill(from, resource, doFill); + } + + public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) + { + return getTankContainer().drain(from, resource, doDrain); + } + + public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) + { + return getTankContainer().drain(from, maxDrain, doDrain); + } + + public boolean canFill(ForgeDirection from, Fluid fluid) + { + return getTankContainer().canFill(from, fluid); + } + + public boolean canDrain(ForgeDirection from, Fluid fluid) + { + return getTankContainer().canDrain(from, fluid); + } + + public FluidTankInfo[] getTankInfo(ForgeDirection from) + { + return getTankContainer().getTankInfo(from); + } + + public IFluidTank[] getTanks() + { + return getTankContainer().getTanks(); + } + + @Optional.Method(modid="IC2") + public double getDemandedEnergy() + { + return getPower().getDemandedEnergy(); + } + + @Optional.Method(modid="IC2") + public int getSinkTier() + { + return getPower().getSinkTier(); + } + + @Optional.Method(modid="IC2") + public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) + { + return getPower().injectEnergy(directionFrom, amount, voltage); + } + + @Optional.Method(modid="IC2") + public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) + { + return getPower().acceptsEnergyFrom(emitter, direction); + } + + public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) + { + return getPower().receiveEnergy(from, maxReceive, simulate); + } + + public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) + { + return getPower().extractEnergy(from, maxExtract, simulate); + } + + public int getEnergyStored(ForgeDirection from) + { + return getPower().getEnergyStored(from); + } + + public int getMaxEnergyStored(ForgeDirection from) + { + return getPower().getMaxEnergyStored(from); + } + + public boolean canConnectEnergy(ForgeDirection from) + { + return getPower().canConnectEnergy(from); + } + + public PowerInterface getInterface() + { + return getPower().getInterface(); + } +} diff --git a/src/Java/binnie/core/machines/component/ComponentRecipe.java b/src/Java/binnie/core/machines/component/ComponentRecipe.java new file mode 100644 index 0000000000..502967e83a --- /dev/null +++ b/src/Java/binnie/core/machines/component/ComponentRecipe.java @@ -0,0 +1,14 @@ +package binnie.core.machines.component; + +import binnie.core.machines.Machine; +import binnie.core.machines.MachineComponent; + +public abstract class ComponentRecipe + extends MachineComponent + implements IComponentRecipe +{ + public ComponentRecipe(Machine machine) + { + super(machine); + } +} diff --git a/src/Java/binnie/core/machines/component/IBuildcraft.java b/src/Java/binnie/core/machines/component/IBuildcraft.java new file mode 100644 index 0000000000..42c4b315e6 --- /dev/null +++ b/src/Java/binnie/core/machines/component/IBuildcraft.java @@ -0,0 +1,20 @@ +package binnie.core.machines.component; + +import binnie.core.triggers.TriggerData; +import buildcraft.api.statements.IActionExternal; +import buildcraft.api.statements.IActionReceptor; +import java.util.List; + +public abstract interface IBuildcraft +{ + public static abstract interface TriggerProvider + { + public abstract void getTriggers(List<TriggerData> paramList); + } + + public static abstract interface ActionProvider + extends IActionReceptor + { + public abstract void getActions(List<IActionExternal> paramList); + } +} diff --git a/src/Java/binnie/core/machines/component/IComponentRecipe.java b/src/Java/binnie/core/machines/component/IComponentRecipe.java new file mode 100644 index 0000000000..f512d1090f --- /dev/null +++ b/src/Java/binnie/core/machines/component/IComponentRecipe.java @@ -0,0 +1,12 @@ +package binnie.core.machines.component; + +import net.minecraft.item.ItemStack; + +public abstract interface IComponentRecipe +{ + public abstract boolean isRecipe(); + + public abstract ItemStack doRecipe(boolean paramBoolean); + + public abstract ItemStack getProduct(); +} diff --git a/src/Java/binnie/core/machines/component/IInteraction.java b/src/Java/binnie/core/machines/component/IInteraction.java new file mode 100644 index 0000000000..7556b5d567 --- /dev/null +++ b/src/Java/binnie/core/machines/component/IInteraction.java @@ -0,0 +1,22 @@ +package binnie.core.machines.component; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +public abstract interface IInteraction +{ + public static abstract interface ChunkUnload + { + public abstract void onChunkUnload(); + } + + public static abstract interface Invalidation + { + public abstract void onInvalidation(); + } + + public static abstract interface RightClick + { + public abstract void onRightClick(World paramWorld, EntityPlayer paramEntityPlayer, int paramInt1, int paramInt2, int paramInt3); + } +} diff --git a/src/Java/binnie/core/machines/component/IRender.java b/src/Java/binnie/core/machines/component/IRender.java new file mode 100644 index 0000000000..0b79e985bb --- /dev/null +++ b/src/Java/binnie/core/machines/component/IRender.java @@ -0,0 +1,28 @@ +package binnie.core.machines.component; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import java.util.Random; +import net.minecraft.client.renderer.entity.RenderItem; +import net.minecraft.world.World; + +public abstract interface IRender +{ + public static abstract interface DisplayTick + { + @SideOnly(Side.CLIENT) + public abstract void onDisplayTick(World paramWorld, int paramInt1, int paramInt2, int paramInt3, Random paramRandom); + } + + public static abstract interface RandomDisplayTick + { + @SideOnly(Side.CLIENT) + public abstract void onRandomDisplayTick(World paramWorld, int paramInt1, int paramInt2, int paramInt3, Random paramRandom); + } + + public static abstract interface Render + { + @SideOnly(Side.CLIENT) + public abstract void renderInWorld(RenderItem paramRenderItem, double paramDouble1, double paramDouble2, double paramDouble3); + } +} diff --git a/src/Java/binnie/core/machines/inventory/AccessDirection.java b/src/Java/binnie/core/machines/inventory/AccessDirection.java new file mode 100644 index 0000000000..10abf8d7f0 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/AccessDirection.java @@ -0,0 +1,97 @@ +package binnie.core.machines.inventory; + + enum AccessDirection +{ + Both, In, Out, Neither; + + private AccessDirection() {} + + boolean canInsert() + { + return (this == Both) || (this == In); + } + + boolean canExtract() + { + return (this == Both) || (this == Out); + } + + boolean canAccess() + { + return this != Neither; + } + + AccessDirection changeInsert(boolean b) + { + if (b) + { + if (this == Out) { + return Both; + } + if (this == Neither) { + return In; + } + } + else + { + if (this == Both) { + return Out; + } + if (this == In) { + return Neither; + } + } + return this; + } + + AccessDirection changeExtract(boolean b) + { + if (b) + { + if (this == In) { + return Both; + } + if (this == Neither) { + return Out; + } + } + else + { + if (this == Both) { + return In; + } + if (this == Out) { + return Neither; + } + } + return this; + } + + public String getTextColour() + { + switch (1.$SwitchMap$binnie$core$machines$inventory$AccessDirection[ordinal()]) + { + case 1: + return "§a"; + case 2: + return "§e"; + case 3: + return "§c"; + } + return "§b"; + } + + public int getShadeColour() + { + switch (1.$SwitchMap$binnie$core$machines$inventory$AccessDirection[ordinal()]) + { + case 1: + return 1431699285; + case 2: + return 1442840405; + case 3: + return 1442796885; + } + return 1431699455; + } +} diff --git a/src/Java/binnie/core/machines/inventory/BaseSlot.java b/src/Java/binnie/core/machines/inventory/BaseSlot.java new file mode 100644 index 0000000000..fe5cde21d3 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/BaseSlot.java @@ -0,0 +1,148 @@ +package binnie.core.machines.inventory; + +import binnie.core.util.IValidator; +import forestry.api.core.INBTTagable; +import java.util.Collection; +import java.util.EnumSet; +import net.minecraftforge.common.util.ForgeDirection; + +abstract class BaseSlot<T> + implements INBTTagable, IValidator<T> +{ + private SidedAccess access = new SidedAccess(); + Validator<T> validator = null; + private boolean readOnly = false; + private int index; + + public BaseSlot(int index, String unlocName) + { + setIndex(index); + setUnlocalisedName(unlocName); + } + + public void setReadOnly() + { + this.readOnly = true; + forbidInsertion(); + } + + public boolean isValid(T item) + { + if (item == null) { + return true; + } + if (this.validator != null) { + return this.validator.isValid(item); + } + return true; + } + + public abstract T getContent(); + + public abstract void setContent(T paramT); + + public void setValidator(Validator<T> val) + { + this.validator = val; + } + + public boolean isEmpty() + { + return getContent() == null; + } + + public boolean isReadOnly() + { + return this.readOnly; + } + + public int getIndex() + { + return this.index; + } + + private void setIndex(int index) + { + this.index = index; + } + + public boolean canInsert() + { + return !this.access.getInsertionSides().isEmpty(); + } + + public boolean canExtract() + { + return !this.access.getExtractionSides().isEmpty(); + } + + public void forbidInteraction() + { + forbidInsertion(); + forbidExtraction(); + } + + public void setInputSides(EnumSet<ForgeDirection> sides) + { + for (ForgeDirection side : EnumSet.complementOf(sides)) { + if (side != ForgeDirection.UNKNOWN) { + this.access.setInsert(side, false); + } + } + } + + public void setOutputSides(EnumSet<ForgeDirection> sides) + { + for (ForgeDirection side : EnumSet.complementOf(sides)) { + if (side != ForgeDirection.UNKNOWN) { + this.access.setExtract(side, false); + } + } + } + + public void forbidExtraction() + { + this.access.setExtract(false); + this.access.forbidExtractChange(); + } + + public void forbidInsertion() + { + this.access.setInsert(false); + this.access.forbidInsertChange(); + } + + public boolean canInsert(ForgeDirection dir) + { + return this.access.canInsert(dir); + } + + public boolean canExtract(ForgeDirection dir) + { + return this.access.canExtract(dir); + } + + public Collection<ForgeDirection> getInputSides() + { + return this.access.getInsertionSides(); + } + + public Collection<ForgeDirection> getOutputSides() + { + return this.access.getExtractionSides(); + } + + protected String unlocName = ""; + + public void setUnlocalisedName(String name) + { + this.unlocName = name; + } + + public abstract String getName(); + + public Validator<T> getValidator() + { + return this.validator; + } +} diff --git a/src/Java/binnie/core/machines/inventory/ComponentChargedSlots.java b/src/Java/binnie/core/machines/inventory/ComponentChargedSlots.java new file mode 100644 index 0000000000..078a2f98d5 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ComponentChargedSlots.java @@ -0,0 +1,117 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.Machine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.MachineUtil; +import binnie.core.machines.network.INetwork.GuiNBT; +import cpw.mods.fml.relauncher.Side; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +public class ComponentChargedSlots + extends MachineComponent + implements INetwork.GuiNBT, IChargedSlots +{ + public ComponentChargedSlots(Machine machine) + { + super(machine); + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + super.readFromNBT(nbttagcompound); + this.charges.clear(); + NBTTagList list = nbttagcompound.getTagList("charges", 10); + for (int i = 0; i < list.tagCount(); i++) + { + NBTTagCompound tag = list.getCompoundTagAt(i); + this.charges.put(Integer.valueOf(tag.getByte("i")), Float.valueOf(tag.getByte("v") / 100.0F)); + } + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + super.writeToNBT(nbttagcompound); + + NBTTagList chargeList = new NBTTagList(); + for (Map.Entry<Integer, Float> entry : this.charges.entrySet()) + { + NBTTagCompound chargesNBT = new NBTTagCompound(); + chargesNBT.setByte("i", (byte)(0 + ((Integer)entry.getKey()).intValue())); + chargesNBT.setByte("v", (byte)(int)(((Float)entry.getValue()).floatValue() * 100.0F)); + chargeList.appendTag(chargesNBT); + } + nbttagcompound.setTag("charges", chargeList); + } + + private Map<Integer, Float> charges = new HashMap(); + + public void addCharge(int slot) + { + this.charges.put(Integer.valueOf(slot), Float.valueOf(0.0F)); + } + + public void recieveGuiNBT(Side side, EntityPlayer player, String name, NBTTagCompound nbt) + { + Iterator i$; + if (name.equals("slot-charges")) { + for (i$ = this.charges.keySet().iterator(); i$.hasNext();) + { + int i = ((Integer)i$.next()).intValue(); + this.charges.put(Integer.valueOf(i), Float.valueOf(nbt.getShort("" + i) / 100.0F)); + } + } + } + + public void sendGuiNBT(Map<String, NBTTagCompound> nbt) + { + NBTTagCompound tag = new NBTTagCompound(); + for (Iterator i$ = this.charges.keySet().iterator(); i$.hasNext();) + { + int i = ((Integer)i$.next()).intValue(); + tag.setShort("" + i, (short)(int)(((Float)this.charges.get(Integer.valueOf(i))).floatValue() * 100.0F)); + } + nbt.put("slot-charges", tag); + } + + public float getCharge(int slot) + { + return this.charges.containsKey(Integer.valueOf(slot)) ? ((Float)this.charges.get(Integer.valueOf(slot))).floatValue() : 0.0F; + } + + public void setCharge(int slot, float charge) + { + if (charge > 1.0F) { + charge = 1.0F; + } + if (charge < 0.0F) { + charge = 0.0F; + } + if (this.charges.containsKey(Integer.valueOf(slot))) { + this.charges.put(Integer.valueOf(slot), Float.valueOf(charge)); + } + } + + public void onUpdate() + { + for (Iterator i$ = this.charges.keySet().iterator(); i$.hasNext();) + { + int slot = ((Integer)i$.next()).intValue(); + if ((getCharge(slot) <= 0.0F) && + (getUtil().decreaseStack(slot, 1) != null)) { + setCharge(slot, 1.0F); + } + } + } + + public void alterCharge(int slot, float charge) + { + setCharge(slot, getCharge(slot) + charge); + } +} diff --git a/src/Java/binnie/core/machines/inventory/ComponentInventory.java b/src/Java/binnie/core/machines/inventory/ComponentInventory.java new file mode 100644 index 0000000000..66566e511c --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ComponentInventory.java @@ -0,0 +1,22 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.IMachine; +import binnie.core.machines.MachineComponent; +import net.minecraft.inventory.IInventory; + +public abstract class ComponentInventory + extends MachineComponent + implements IInventory +{ + public ComponentInventory(IMachine machine) + { + super(machine); + } + + public void markDirty() + { + if (getMachine() != null) { + getMachine().markDirty(); + } + } +} diff --git a/src/Java/binnie/core/machines/inventory/ComponentInventorySlots.java b/src/Java/binnie/core/machines/inventory/ComponentInventorySlots.java new file mode 100644 index 0000000000..42334f4485 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ComponentInventorySlots.java @@ -0,0 +1,247 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.IMachine; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +public class ComponentInventorySlots + extends ComponentInventory + implements IInventoryMachine, IInventorySlots +{ + public ComponentInventorySlots(IMachine machine) + { + super(machine); + } + + private Map<Integer, InventorySlot> inventory = new LinkedHashMap(); + + public int getSizeInventory() + { + int size = 0; + for (Integer index : this.inventory.keySet()) { + size = Math.max(size, index.intValue() + 1); + } + return size; + } + + public ItemStack getStackInSlot(int index) + { + if (this.inventory.containsKey(Integer.valueOf(index))) { + return ((InventorySlot)this.inventory.get(Integer.valueOf(index))).getContent(); + } + return null; + } + + public ItemStack decrStackSize(int index, int amount) + { + if (this.inventory.containsKey(Integer.valueOf(index))) + { + ItemStack stack = ((InventorySlot)this.inventory.get(Integer.valueOf(index))).decrStackSize(amount); + markDirty(); + return stack; + } + return null; + } + + public ItemStack getStackInSlotOnClosing(int var1) + { + return null; + } + + public void setInventorySlotContents(int index, ItemStack itemStack) + { + if ((this.inventory.containsKey(Integer.valueOf(index))) && ((itemStack == null) || (((InventorySlot)this.inventory.get(Integer.valueOf(index))).isValid(itemStack)))) { + ((InventorySlot)this.inventory.get(Integer.valueOf(index))).setContent(itemStack); + } + markDirty(); + } + + protected void transferItem(int indexFrom, int indexTo) + { + if ((this.inventory.containsKey(Integer.valueOf(indexFrom))) && (this.inventory.containsKey(Integer.valueOf(indexTo)))) + { + ItemStack newStack = ((InventorySlot)this.inventory.get(Integer.valueOf(indexFrom))).getContent().copy(); + ((InventorySlot)this.inventory.get(Integer.valueOf(indexFrom))).setContent(null); + ((InventorySlot)this.inventory.get(Integer.valueOf(indexTo))).setContent(newStack); + } + markDirty(); + } + + public String getInventoryName() + { + return ""; + } + + public int getInventoryStackLimit() + { + return 64; + } + + public boolean isUseableByPlayer(EntityPlayer var1) + { + return true; + } + + public void openInventory() {} + + public void closeInventory() {} + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + super.readFromNBT(nbttagcompound); + if (nbttagcompound.hasKey("inventory")) + { + NBTTagList inventoryNBT = nbttagcompound.getTagList("inventory", 10); + for (int i = 0; i < inventoryNBT.tagCount(); i++) + { + NBTTagCompound slotNBT = inventoryNBT.getCompoundTagAt(i); + int index = slotNBT.getInteger("id"); + if (slotNBT.hasKey("Slot")) { + index = slotNBT.getByte("Slot") & 0xFF; + } + if (this.inventory.containsKey(Integer.valueOf(index))) { + ((InventorySlot)this.inventory.get(Integer.valueOf(index))).readFromNBT(slotNBT); + } + } + } + markDirty(); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + super.writeToNBT(nbttagcompound); + + NBTTagList inventoryNBT = new NBTTagList(); + for (Map.Entry<Integer, InventorySlot> entry : this.inventory.entrySet()) + { + NBTTagCompound slotNBT = new NBTTagCompound(); + slotNBT.setInteger("id", ((Integer)entry.getKey()).intValue()); + ((InventorySlot)entry.getValue()).writeToNBT(slotNBT); + inventoryNBT.appendTag(slotNBT); + } + nbttagcompound.setTag("inventory", inventoryNBT); + } + + public final InventorySlot addSlot(int index, String unlocName) + { + this.inventory.put(Integer.valueOf(index), new InventorySlot(index, unlocName)); + return getSlot(index); + } + + public final InventorySlot[] addSlotArray(int[] indexes, String unlocName) + { + for (int k : indexes) { + addSlot(k, unlocName); + } + return getSlots(indexes); + } + + public InventorySlot getSlot(int index) + { + if (this.inventory.containsKey(Integer.valueOf(index))) { + return (InventorySlot)this.inventory.get(Integer.valueOf(index)); + } + return null; + } + + public InventorySlot[] getAllSlots() + { + return (InventorySlot[])this.inventory.values().toArray(new InventorySlot[0]); + } + + public InventorySlot[] getSlots(int[] indexes) + { + List<InventorySlot> list = new ArrayList(); + for (int i : indexes) { + if (getSlot(i) != null) { + list.add(getSlot(i)); + } + } + return (InventorySlot[])list.toArray(new InventorySlot[0]); + } + + public boolean isReadOnly(int slot) + { + InventorySlot iSlot = getSlot(slot); + return iSlot == null ? true : iSlot.isReadOnly(); + } + + public boolean hasCustomInventoryName() + { + return true; + } + + public boolean isItemValidForSlot(int slot, ItemStack itemStack) + { + InventorySlot iSlot = getSlot(slot); + return iSlot != null; + } + + public void onDestruction() + { + for (InventorySlot slot : this.inventory.values()) + { + ItemStack stack = slot.getContent(); + if ((!slot.isRecipe()) && (stack != null)) + { + float f = getMachine().getWorld().rand.nextFloat() * 0.8F + 0.1F; + float f1 = getMachine().getWorld().rand.nextFloat() * 0.8F + 0.1F; + float f2 = getMachine().getWorld().rand.nextFloat() * 0.8F + 0.1F; + if (stack.stackSize == 0) { + stack.stackSize = 1; + } + EntityItem entityitem = new EntityItem(getMachine().getWorld(), getMachine().getTileEntity().xCoord + f, getMachine().getTileEntity().yCoord + f1, getMachine().getTileEntity().zCoord + f2, stack.copy()); + + + + + float accel = 0.05F; + entityitem.motionX = ((float)getMachine().getWorld().rand.nextGaussian() * accel); + + entityitem.motionY = ((float)getMachine().getWorld().rand.nextGaussian() * accel + 0.2F); + + entityitem.motionZ = ((float)getMachine().getWorld().rand.nextGaussian() * accel); + + getMachine().getWorld().spawnEntityInWorld(entityitem); + } + } + } + + public int[] getAccessibleSlotsFromSide(int var1) + { + List<Integer> slots = new ArrayList(); + for (InventorySlot slot : this.inventory.values()) { + if ((slot.canInsert()) || (slot.canExtract())) { + slots.add(Integer.valueOf(slot.getIndex())); + } + } + int[] ids = new int[slots.size()]; + for (int i = 0; i < slots.size(); i++) { + ids[i] = ((Integer)slots.get(i)).intValue(); + } + return ids; + } + + public boolean canInsertItem(int i, ItemStack itemstack, int j) + { + return (isItemValidForSlot(i, itemstack)) && (getSlot(i).canInsert(ForgeDirection.getOrientation(j))); + } + + public boolean canExtractItem(int i, ItemStack itemstack, int j) + { + return getSlot(i).canExtract(ForgeDirection.getOrientation(j)); + } +} diff --git a/src/Java/binnie/core/machines/inventory/ComponentInventoryTransfer.java b/src/Java/binnie/core/machines/inventory/ComponentInventoryTransfer.java new file mode 100644 index 0000000000..8ee10080e8 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ComponentInventoryTransfer.java @@ -0,0 +1,161 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.IMachine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.transfer.TransferRequest; +import java.util.ArrayList; +import java.util.List; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public class ComponentInventoryTransfer + extends MachineComponent +{ + public ComponentInventoryTransfer(IMachine machine) + { + super(machine); + } + + public abstract class Transfer + { + protected ComponentInventoryTransfer.Condition condition; + protected IMachine machine; + + private Transfer(IMachine machine) + { + this.machine = machine; + } + + public final void transfer(IInventory inv) + { + if ((this.condition == null) || (fufilled(inv))) { + doTransfer(inv); + } + } + + protected boolean fufilled(IInventory inv) + { + return true; + } + + protected void doTransfer(IInventory inv) {} + + public final Transfer setCondition(ComponentInventoryTransfer.Condition condition) + { + this.condition = condition; + condition.transfer = this; + return this; + } + + public final IMachine getMachine() + { + return this.machine; + } + } + + private class Restock + extends ComponentInventoryTransfer.Transfer + { + int[] buffer; + int destination; + int limit; + + private Restock(IMachine machine, int[] buffer, int destination, int limit) + { + super(machine, null); + this.buffer = buffer; + this.destination = destination; + this.limit = limit; + } + + private Restock(IMachine machine, int[] buffer, int destination) + { + this(machine, buffer, destination, 64); + } + + protected void doTransfer(IInventory inv) + { + if (inv.getStackInSlot(this.destination) == null) { + for (int i : this.buffer) { + if (inv.getStackInSlot(i) != null) + { + ItemStack newStack = inv.decrStackSize(i, this.limit); + if (newStack != null) + { + inv.setInventorySlotContents(this.destination, newStack); + return; + } + } + } + } + } + } + + private class Storage + extends ComponentInventoryTransfer.Transfer + { + int source; + int[] destination; + + private Storage(IMachine machine, int source, int[] destination) + { + super(machine, null); + this.source = source; + this.destination = destination; + } + + protected void doTransfer(IInventory inv) + { + if (inv.getStackInSlot(this.source) != null) { + inv.setInventorySlotContents(this.source, new TransferRequest(inv.getStackInSlot(this.source), inv).setTargetSlots(this.destination).ignoreValidation().transfer(true)); + } + } + + protected boolean fufilled(IInventory inv) + { + ItemStack stack = inv.getStackInSlot(this.source); + return (stack != null) && (this.condition.fufilled(stack)); + } + } + + private List<Transfer> transfers = new ArrayList(); + + public void addRestock(int[] buffer, int destination, int limit) + { + this.transfers.add(new Restock(getMachine(), buffer, destination, limit, null)); + } + + public void addRestock(int[] buffer, int destination) + { + this.transfers.add(new Restock(getMachine(), buffer, destination, null)); + } + + public void addStorage(int source, int[] destination) + { + this.transfers.add(new Storage(getMachine(), source, destination, null)); + } + + public void performTransfer(int source, int[] destination) + { + new Storage(getMachine(), source, destination, null).transfer((IInventory)getMachine().getInterface(IInventoryMachine.class)); + } + + public void onUpdate() + { + for (Transfer transfer : this.transfers) { + transfer.transfer((IInventory)getMachine().getInterface(IInventoryMachine.class)); + } + } + + public void addStorage(int source, int[] destination, Condition condition) + { + this.transfers.add(new Storage(getMachine(), source, destination, null).setCondition(condition)); + } + + public static abstract class Condition + { + public ComponentInventoryTransfer.Transfer transfer; + + public abstract boolean fufilled(ItemStack paramItemStack); + } +} diff --git a/src/Java/binnie/core/machines/inventory/ComponentTankContainer.java b/src/Java/binnie/core/machines/inventory/ComponentTankContainer.java new file mode 100644 index 0000000000..0dee156475 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ComponentTankContainer.java @@ -0,0 +1,207 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.IMachine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.power.ITankMachine; +import binnie.core.machines.power.TankInfo; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; + +public class ComponentTankContainer + extends MachineComponent + implements ITankMachine +{ + private Map<Integer, TankSlot> tanks = new LinkedHashMap(); + + public ComponentTankContainer(IMachine machine) + { + super(machine); + } + + public final TankSlot addTank(int index, String name, int capacity) + { + TankSlot tank = new TankSlot(index, name, capacity); + this.tanks.put(Integer.valueOf(index), tank); + return tank; + } + + public final int fill(ForgeDirection from, FluidStack resource, boolean doFill) + { + int index = getTankIndexToFill(from, resource); + if (this.tanks.containsKey(Integer.valueOf(index))) { + return fill(index, resource, doFill); + } + return 0; + } + + public final FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) + { + int index = getTankIndexToDrain(from, null); + if (this.tanks.containsKey(Integer.valueOf(index))) { + return drain(index, maxDrain, doDrain); + } + return null; + } + + private final int fill(int tankIndex, FluidStack resource, boolean doFill) + { + if (!this.tanks.containsKey(Integer.valueOf(tankIndex))) { + return 0; + } + if (!isLiquidValidForTank(resource, tankIndex)) { + return 0; + } + TankSlot tank = (TankSlot)this.tanks.get(Integer.valueOf(tankIndex)); + int filled = tank.getTank().fill(resource, doFill); + if (filled > 0) { + markDirty(); + } + return filled; + } + + private final FluidStack drain(int tankIndex, int maxDrain, boolean doDrain) + { + if (!this.tanks.containsKey(Integer.valueOf(tankIndex))) { + return null; + } + TankSlot tank = (TankSlot)this.tanks.get(Integer.valueOf(tankIndex)); + FluidStack drained = tank.getTank().drain(maxDrain, doDrain); + if (drained != null) { + markDirty(); + } + return drained; + } + + private int getTankIndexToFill(ForgeDirection from, FluidStack resource) + { + for (TankSlot tank : this.tanks.values()) { + if ((tank.isValid(resource)) && (tank.canInsert(from)) && ((tank.getContent() == null) || (tank.getContent().isFluidEqual(resource)))) { + return tank.getIndex(); + } + } + return -1; + } + + private int getTankIndexToDrain(ForgeDirection from, FluidStack resource) + { + for (TankSlot tank : this.tanks.values()) { + if ((tank.getContent() != null) && + (tank.canExtract(from)) && ((resource == null) || (resource.isFluidEqual(tank.getContent())))) { + return tank.getIndex(); + } + } + return -1; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + super.readFromNBT(nbttagcompound); + if (nbttagcompound.hasKey("liquidTanks")) + { + NBTTagList tanksNBT = nbttagcompound.getTagList("liquidTanks", 10); + for (int i = 0; i < tanksNBT.tagCount(); i++) + { + NBTTagCompound tankNBT = tanksNBT.getCompoundTagAt(i); + int index = tankNBT.getInteger("index"); + if (this.tanks.containsKey(Integer.valueOf(index))) { + ((TankSlot)this.tanks.get(Integer.valueOf(index))).readFromNBT(tankNBT); + } + } + } + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + super.writeToNBT(nbttagcompound); + + NBTTagList tanksNBT = new NBTTagList(); + for (Map.Entry<Integer, TankSlot> entry : this.tanks.entrySet()) + { + NBTTagCompound tankNBT = new NBTTagCompound(); + tankNBT.setInteger("index", ((Integer)entry.getKey()).intValue()); + ((TankSlot)entry.getValue()).writeToNBT(tankNBT); + tanksNBT.appendTag(tankNBT); + } + nbttagcompound.setTag("liquidTanks", tanksNBT); + } + + public boolean isTankReadOnly(int tank) + { + return ((TankSlot)this.tanks.get(Integer.valueOf(tank))).isReadOnly(); + } + + public boolean isLiquidValidForTank(FluidStack liquid, int tank) + { + TankSlot slot = getTankSlot(tank); + return slot != null; + } + + public TankInfo[] getTankInfos() + { + return TankInfo.get(this); + } + + public IFluidTank getTank(int index) + { + return getTanks()[index]; + } + + public IFluidTank[] getTanks() + { + List<IFluidTank> ltanks = new ArrayList(); + for (TankSlot tank : this.tanks.values()) { + ltanks.add(tank.getTank()); + } + return (IFluidTank[])ltanks.toArray(new IFluidTank[0]); + } + + public TankSlot getTankSlot(int index) + { + return (TankSlot)this.tanks.get(Integer.valueOf(index)); + } + + public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) + { + int index = getTankIndexToDrain(from, null); + if (this.tanks.containsKey(Integer.valueOf(index))) { + return drain(index, resource.amount, doDrain); + } + return null; + } + + public boolean canFill(ForgeDirection from, Fluid fluid) + { + return fill(from, new FluidStack(fluid, 1), false) > 0; + } + + public boolean canDrain(ForgeDirection from, Fluid fluid) + { + return drain(from, new FluidStack(fluid, 1), false) != null; + } + + public FluidTankInfo[] getTankInfo(ForgeDirection from) + { + FluidTankInfo[] info = new FluidTankInfo[getTanks().length]; + for (int i = 0; i < info.length; i++) { + info[i] = new FluidTankInfo(getTanks()[i]); + } + return info; + } + + public void markDirty() + { + if (getMachine() != null) { + getMachine().markDirty(); + } + } +} diff --git a/src/Java/binnie/core/machines/inventory/IChargedSlots.java b/src/Java/binnie/core/machines/inventory/IChargedSlots.java new file mode 100644 index 0000000000..e3fcae1f51 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/IChargedSlots.java @@ -0,0 +1,10 @@ +package binnie.core.machines.inventory; + +public abstract interface IChargedSlots +{ + public abstract float getCharge(int paramInt); + + public abstract void setCharge(int paramInt, float paramFloat); + + public abstract void alterCharge(int paramInt, float paramFloat); +} diff --git a/src/Java/binnie/core/machines/inventory/IInventoryMachine.java b/src/Java/binnie/core/machines/inventory/IInventoryMachine.java new file mode 100644 index 0000000000..5c0c0a997f --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/IInventoryMachine.java @@ -0,0 +1,8 @@ +package binnie.core.machines.inventory; + +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ISidedInventory; + +public abstract interface IInventoryMachine + extends IInventory, ISidedInventory, IValidatedInventory +{} diff --git a/src/Java/binnie/core/machines/inventory/IInventorySlots.java b/src/Java/binnie/core/machines/inventory/IInventorySlots.java new file mode 100644 index 0000000000..c860f00a63 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/IInventorySlots.java @@ -0,0 +1,14 @@ +package binnie.core.machines.inventory; + +public abstract interface IInventorySlots +{ + public abstract InventorySlot addSlot(int paramInt, String paramString); + + public abstract InventorySlot[] addSlotArray(int[] paramArrayOfInt, String paramString); + + public abstract InventorySlot getSlot(int paramInt); + + public abstract InventorySlot[] getSlots(int[] paramArrayOfInt); + + public abstract InventorySlot[] getAllSlots(); +} diff --git a/src/Java/binnie/core/machines/inventory/IValidatedInventory.java b/src/Java/binnie/core/machines/inventory/IValidatedInventory.java new file mode 100644 index 0000000000..56543612c8 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/IValidatedInventory.java @@ -0,0 +1,9 @@ +package binnie.core.machines.inventory; + +import net.minecraft.inventory.IInventory; + +abstract interface IValidatedInventory + extends IInventory +{ + public abstract boolean isReadOnly(int paramInt); +} diff --git a/src/Java/binnie/core/machines/inventory/IValidatedTankContainer.java b/src/Java/binnie/core/machines/inventory/IValidatedTankContainer.java new file mode 100644 index 0000000000..6ca868ce02 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/IValidatedTankContainer.java @@ -0,0 +1,10 @@ +package binnie.core.machines.inventory; + +import net.minecraftforge.fluids.FluidStack; + +public abstract interface IValidatedTankContainer +{ + public abstract boolean isTankReadOnly(int paramInt); + + public abstract boolean isLiquidValidForTank(FluidStack paramFluidStack, int paramInt); +} diff --git a/src/Java/binnie/core/machines/inventory/InventorySlot.java b/src/Java/binnie/core/machines/inventory/InventorySlot.java new file mode 100644 index 0000000000..bfa904176a --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/InventorySlot.java @@ -0,0 +1,115 @@ +package binnie.core.machines.inventory; + +import binnie.Binnie; +import binnie.core.BinnieCore; +import binnie.core.language.ManagerLanguage; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public class InventorySlot + extends BaseSlot<ItemStack> +{ + public static enum Type + { + Standard, Recipe; + + private Type() {} + } + + private ItemStack itemStack = null; + private Type type = Type.Standard; + + public InventorySlot(int index, String unlocName) + { + super(index, unlocName); + } + + public ItemStack getContent() + { + return this.itemStack; + } + + public ItemStack getItemStack() + { + return getContent(); + } + + public void setContent(ItemStack itemStack) + { + this.itemStack = itemStack; + } + + public ItemStack decrStackSize(int amount) + { + if (this.itemStack == null) { + return null; + } + if (this.itemStack.stackSize <= amount) + { + ItemStack returnStack = this.itemStack.copy(); + this.itemStack = null; + return returnStack; + } + ItemStack returnStack = this.itemStack.copy(); + this.itemStack.stackSize -= amount; + returnStack.stackSize = amount; + return returnStack; + } + + public void readFromNBT(NBTTagCompound slotNBT) + { + if (slotNBT.hasKey("item")) + { + NBTTagCompound itemNBT = slotNBT.getCompoundTag("item"); + this.itemStack = ItemStack.loadItemStackFromNBT(itemNBT); + } + else + { + this.itemStack = null; + } + } + + public void writeToNBT(NBTTagCompound slotNBT) + { + NBTTagCompound itemNBT = new NBTTagCompound(); + if (this.itemStack != null) { + this.itemStack.writeToNBT(itemNBT); + } + slotNBT.setTag("item", itemNBT); + } + + public void setItemStack(ItemStack duplicate) + { + setContent(duplicate); + } + + public SlotValidator getValidator() + { + return (SlotValidator)this.validator; + } + + public void setType(Type type) + { + this.type = type; + if (type == Type.Recipe) + { + setReadOnly(); + forbidInteraction(); + } + } + + public Type getType() + { + return this.type; + } + + public boolean isRecipe() + { + return this.type == Type.Recipe; + } + + public String getName() + { + return Binnie.Language.localise(BinnieCore.instance, "gui.slot." + this.unlocName); + } +} diff --git a/src/Java/binnie/core/machines/inventory/MachineSide.java b/src/Java/binnie/core/machines/inventory/MachineSide.java new file mode 100644 index 0000000000..cba1c7417b --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/MachineSide.java @@ -0,0 +1,79 @@ +package binnie.core.machines.inventory; + +import java.util.Collection; +import java.util.EnumSet; +import net.minecraftforge.common.util.ForgeDirection; + +public class MachineSide +{ + private static EnumSet<ForgeDirection> All = EnumSet.of(ForgeDirection.UP, new ForgeDirection[] { ForgeDirection.DOWN, ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.EAST, ForgeDirection.WEST }); + public static EnumSet<ForgeDirection> TopAndBottom = EnumSet.of(ForgeDirection.UP, ForgeDirection.DOWN); + public static EnumSet<ForgeDirection> None = EnumSet.noneOf(ForgeDirection.class); + public static EnumSet<ForgeDirection> Top = EnumSet.of(ForgeDirection.UP); + public static EnumSet<ForgeDirection> Bottom = EnumSet.of(ForgeDirection.DOWN); + public static EnumSet<ForgeDirection> Sides = EnumSet.of(ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.EAST, ForgeDirection.WEST); + + public static String asString(Collection<ForgeDirection> sides) + { + if (sides.containsAll(All)) { + return "Any"; + } + if (sides.isEmpty()) { + return "None"; + } + String text = ""; + if (sides.contains(ForgeDirection.UP)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "Up"; + } + if (sides.contains(ForgeDirection.DOWN)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "Down"; + } + if (sides.containsAll(Sides)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "Sides"; + } + else + { + if (sides.contains(ForgeDirection.NORTH)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "North"; + } + if (sides.contains(ForgeDirection.EAST)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "East"; + } + if (sides.contains(ForgeDirection.SOUTH)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "South"; + } + if (sides.contains(ForgeDirection.WEST)) + { + if (sides.size() > 0) { + text = text + ", "; + } + text = text + "West"; + } + } + return text; + } +} diff --git a/src/Java/binnie/core/machines/inventory/SetList.java b/src/Java/binnie/core/machines/inventory/SetList.java new file mode 100644 index 0000000000..41c455a2fc --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/SetList.java @@ -0,0 +1,36 @@ +package binnie.core.machines.inventory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Set; + +public class SetList<E> + extends ArrayList<E> + implements Set<E> +{ + private static final long serialVersionUID = 1277112003159980135L; + + public boolean add(E e) + { + return contains(e) ? false : super.add(e); + } + + public void add(int index, E e) + { + if (!contains(e)) { + super.add(index, e); + } + } + + public boolean addAll(Collection<? extends E> c) + { + return addAll(size(), c); + } + + public boolean addAll(int index, Collection<? extends E> c) + { + Collection<E> copy = new ArrayList(c); + copy.removeAll(this); + return super.addAll(index, copy); + } +} diff --git a/src/Java/binnie/core/machines/inventory/SidedAccess.java b/src/Java/binnie/core/machines/inventory/SidedAccess.java new file mode 100644 index 0000000000..ee505891e2 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/SidedAccess.java @@ -0,0 +1,106 @@ +package binnie.core.machines.inventory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.minecraftforge.common.util.ForgeDirection; + +class SidedAccess +{ + private Map<ForgeDirection, AccessDirection> accesses = new HashMap(); + private AccessDirection base = AccessDirection.Both; + private boolean insertLocked = false; + private boolean extractLocked = false; + + public AccessDirection getAccess(ForgeDirection side) + { + return this.accesses.containsKey(side) ? (AccessDirection)this.accesses.get(side) : this.base; + } + + public boolean canInsert(ForgeDirection side) + { + return getAccess(side).canInsert(); + } + + public boolean canExtract(ForgeDirection side) + { + return getAccess(side).canExtract(); + } + + public boolean canAccess(ForgeDirection side) + { + return getAccess(side).canAccess(); + } + + public boolean canChangeInsert() + { + return !this.insertLocked; + } + + public boolean canChangeExtract() + { + return !this.extractLocked; + } + + public void forbidInsertChange() + { + this.insertLocked = true; + } + + public void forbidExtractChange() + { + this.extractLocked = true; + } + + public Collection<ForgeDirection> getInsertionSides() + { + List<ForgeDirection> dirs = new ArrayList(); + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { + if (getAccess(dir).canInsert()) { + dirs.add(dir); + } + } + return dirs; + } + + public Collection<ForgeDirection> getExtractionSides() + { + List<ForgeDirection> dirs = new ArrayList(); + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { + if (getAccess(dir).canExtract()) { + dirs.add(dir); + } + } + return dirs; + } + + public void setInsert(ForgeDirection side, boolean b) + { + if (getAccess(side).canInsert() != b) { + this.accesses.put(side, getAccess(side).changeInsert(b)); + } + } + + public void setExtract(ForgeDirection side, boolean b) + { + if (getAccess(side).canExtract() != b) { + this.accesses.put(side, getAccess(side).changeExtract(b)); + } + } + + public void setInsert(boolean b) + { + if (this.base.canInsert() != b) { + this.base = this.base.changeInsert(b); + } + } + + public void setExtract(boolean b) + { + if (this.base.canExtract() != b) { + this.base = this.base.changeExtract(b); + } + } +} diff --git a/src/Java/binnie/core/machines/inventory/SlotValidator.java b/src/Java/binnie/core/machines/inventory/SlotValidator.java new file mode 100644 index 0000000000..952ff99761 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/SlotValidator.java @@ -0,0 +1,68 @@ +package binnie.core.machines.inventory; + +import binnie.core.resource.BinnieIcon; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAlleleRegistry; +import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; + +public abstract class SlotValidator + extends Validator<ItemStack> +{ + public static ValidatorIcon IconBee; + public static ValidatorIcon IconFrame; + public static ValidatorIcon IconCircuit; + public static ValidatorIcon IconBlock; + private ValidatorIcon icon; + + public SlotValidator(ValidatorIcon icon) + { + this.icon = icon; + } + + public IIcon getIcon(boolean input) + { + return this.icon == null ? null : this.icon.getIcon(input).getIcon(); + } + + public static class Item + extends SlotValidator + { + private ItemStack target; + + public Item(ItemStack target, ValidatorIcon icon) + { + super(); + this.target = target; + } + + public boolean isValid(ItemStack itemStack) + { + return itemStack.isItemEqual(this.target); + } + + public String getTooltip() + { + return this.target.getDisplayName(); + } + } + + public static class Individual + extends SlotValidator + { + public Individual() + { + super(); + } + + public boolean isValid(ItemStack itemStack) + { + return AlleleManager.alleleRegistry.getIndividual(itemStack) != null; + } + + public String getTooltip() + { + return "Breedable Individual"; + } + } +} diff --git a/src/Java/binnie/core/machines/inventory/TankSlot.java b/src/Java/binnie/core/machines/inventory/TankSlot.java new file mode 100644 index 0000000000..275ebfd646 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/TankSlot.java @@ -0,0 +1,58 @@ +package binnie.core.machines.inventory; + +import binnie.Binnie; +import binnie.core.BinnieCore; +import binnie.core.language.ManagerLanguage; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; +import net.minecraftforge.fluids.IFluidTank; + +public class TankSlot + extends BaseSlot<FluidStack> +{ + public static final String NameJuice = "Juice Tank"; + public static final String NameWater = "Water Tank"; + public static String NameCupboard = "Cupboard Slot"; + private FluidTank tank; + + public TankSlot(int index, String name, int capacity) + { + super(index, name); + this.tank = new FluidTank(capacity); + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + FluidStack liquid = FluidStack.loadFluidStackFromNBT(nbttagcompound); + + setContent(liquid); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + if (getContent() != null) { + getContent().writeToNBT(nbttagcompound); + } + } + + public FluidStack getContent() + { + return this.tank.getFluid(); + } + + public void setContent(FluidStack itemStack) + { + this.tank.setFluid(itemStack); + } + + public IFluidTank getTank() + { + return this.tank; + } + + public String getName() + { + return Binnie.Language.localise(BinnieCore.instance, "gui.slot." + this.unlocName); + } +} diff --git a/src/Java/binnie/core/machines/inventory/TankValidator.java b/src/Java/binnie/core/machines/inventory/TankValidator.java new file mode 100644 index 0000000000..07c8f2f9c9 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/TankValidator.java @@ -0,0 +1,33 @@ +package binnie.core.machines.inventory; + +import binnie.Binnie; +import binnie.core.liquid.ManagerLiquid; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; + +public abstract class TankValidator + extends Validator<FluidStack> +{ + public abstract boolean isValid(FluidStack paramFluidStack); + + public static class Basic + extends TankValidator + { + private Fluid fluid; + + public Basic(String name) + { + this.fluid = Binnie.Liquid.getLiquidStack(name, 1).getFluid(); + } + + public boolean isValid(FluidStack stack) + { + return new FluidStack(this.fluid, 1).isFluidEqual(stack); + } + + public String getTooltip() + { + return new FluidStack(this.fluid, 1).getLocalizedName(); + } + } +} diff --git a/src/Java/binnie/core/machines/inventory/TransferHandler.java b/src/Java/binnie/core/machines/inventory/TransferHandler.java new file mode 100644 index 0000000000..1fa4b2cac5 --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/TransferHandler.java @@ -0,0 +1,227 @@ +package binnie.core.machines.inventory; + +import binnie.core.machines.Machine; +import binnie.core.machines.power.ITankMachine; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidContainerRegistry; +import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidTank; + +public class TransferHandler +{ + public static ItemStack transfer(ItemStack item, IInventory origin, IInventory destination, boolean doAdd) + { + ItemStack ret = transferItemToInventory(item, destination, doAdd); + if ((destination instanceof ITankMachine)) + { + ret = transferContainerIntoTank(ret, origin, (ITankMachine)destination, doAdd); + ret = transferTankIntoContainer(ret, origin, (ITankMachine)destination, doAdd); + } + return ret; + } + + public static ItemStack transferItemToInventory(ItemStack item, IInventory destination, boolean doAdd) + { + if ((item == null) || (destination == null)) { + return item; + } + ItemStack addition = item.copy(); + for (int i = 0; i < destination.getSizeInventory(); i++) + { + addition = transferToInventory(addition, destination, new int[] { i }, doAdd, false); + if (addition == null) { + return null; + } + } + return addition; + } + + public static ItemStack transferToInventory(ItemStack item, IInventory destination, int[] targetSlots, boolean doAdd, boolean ignoreValidation) + { + for (int i : targetSlots) { + if ((destination.isItemValidForSlot(i, item)) || (ignoreValidation)) + { + if (destination.getStackInSlot(i) == null) + { + if (doAdd) { + destination.setInventorySlotContents(i, item.copy()); + } + return null; + } + if (item.isStackable()) + { + ItemStack merged = destination.getStackInSlot(i).copy(); + ItemStack[] newStacks = mergeStacks(item.copy(), merged.copy()); + item = newStacks[0]; + if (doAdd) { + destination.setInventorySlotContents(i, newStacks[1]); + } + if (item == null) { + return null; + } + } + } + } + return item; + } + + public static ItemStack[] mergeStacks(ItemStack itemstack, ItemStack merged) + { + if ((ItemStack.areItemStackTagsEqual(itemstack, merged)) && (itemstack.isItemEqual(merged))) + { + int space = merged.getMaxStackSize() - merged.stackSize; + if (space > 0) { + if (itemstack.stackSize > space) + { + itemstack.stackSize -= space; + merged.stackSize += space; + } + else if (itemstack.stackSize <= space) + { + merged.stackSize += itemstack.stackSize; + itemstack = null; + } + } + } + return new ItemStack[] { itemstack, merged }; + } + + public static ItemStack transferContainerIntoTank(ItemStack item, IInventory origin, ITankMachine destination, boolean doAdd) + { + if (item == null) { + return null; + } + IFluidTank[] tanks = destination.getTanks(); + ItemStack stack = item.copy(); + for (int i = 0; i < tanks.length; i++) { + stack = transferToTank(stack, origin, destination, i, doAdd); + } + return stack; + } + + public static ItemStack transferTankIntoContainer(ItemStack item, IInventory origin, ITankMachine destination, boolean doAdd) + { + if (item == null) { + return null; + } + IFluidTank[] tanks = destination.getTanks(); + ItemStack stack = item.copy(); + for (int i = 0; i < tanks.length; i++) { + stack = transferFromTank(stack, origin, destination, i, doAdd); + } + return stack; + } + + public static ItemStack transferToTank(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if (item == null) { + return item; + } + FluidStack containerLiquid = null; + FluidContainerRegistry.FluidContainerData containerLiquidData = null; + for (FluidContainerRegistry.FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) { + if (data.filledContainer.isItemEqual(item)) + { + containerLiquidData = data; + containerLiquid = data.fluid.copy(); + break; + } + } + if (containerLiquid == null) { + return item; + } + IFluidTank tank = destination.getTanks()[tankID]; + + IValidatedTankContainer validated = (IValidatedTankContainer)Machine.getInterface(IValidatedTankContainer.class, destination); + if ((validated != null) && + (!validated.isLiquidValidForTank(containerLiquid, tankID))) { + return item; + } + FluidStack largeAmountOfLiquid = containerLiquid.copy(); + largeAmountOfLiquid.amount = tank.getCapacity(); + int amountAdded = tank.fill(largeAmountOfLiquid, false); + + int numberOfContainersToAdd = amountAdded / containerLiquid.amount; + if (numberOfContainersToAdd > item.stackSize) { + numberOfContainersToAdd = item.stackSize; + } + ItemStack leftOverContainers = item.copy(); + leftOverContainers.stackSize -= numberOfContainersToAdd; + if (leftOverContainers.stackSize <= 0) { + leftOverContainers = null; + } + ItemStack emptyContainers = containerLiquidData.emptyContainer.copy(); + emptyContainers.stackSize = 0; + emptyContainers.stackSize += numberOfContainersToAdd; + if (emptyContainers.stackSize <= 0) { + emptyContainers = null; + } + ItemStack containersThatCantBeDumped = transferItemToInventory(emptyContainers, origin, false); + if (containersThatCantBeDumped != null) { + return item; + } + if (doAdd) + { + FluidStack liquidToFillTank = containerLiquid.copy(); + liquidToFillTank.amount *= numberOfContainersToAdd; + tank.fill(liquidToFillTank, true); + transferItemToInventory(emptyContainers, origin, true); + } + return leftOverContainers; + } + + public static ItemStack transferFromTank(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if (item == null) { + return item; + } + IFluidTank tank = destination.getTanks()[tankID]; + FluidStack liquidInTank = tank.getFluid(); + if (liquidInTank == null) { + return item; + } + FluidContainerRegistry.FluidContainerData containerLiquidData = null; + for (FluidContainerRegistry.FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) { + if ((data.emptyContainer.isItemEqual(item)) && (liquidInTank.isFluidEqual(data.fluid))) + { + containerLiquidData = data; + break; + } + } + if (containerLiquidData == null) { + return item; + } + int maximumExtractedLiquid = item.stackSize * containerLiquidData.fluid.amount; + + FluidStack drainedLiquid = tank.drain(maximumExtractedLiquid, false); + int amountInTank = drainedLiquid == null ? 0 : drainedLiquid.amount; + + int numberOfContainersToFill = amountInTank / containerLiquidData.fluid.amount; + if (numberOfContainersToFill > item.stackSize) { + numberOfContainersToFill = item.stackSize; + } + ItemStack leftOverContainers = item.copy(); + leftOverContainers.stackSize -= numberOfContainersToFill; + if (leftOverContainers.stackSize <= 0) { + leftOverContainers = null; + } + ItemStack filledContainers = containerLiquidData.filledContainer.copy(); + filledContainers.stackSize = 0; + filledContainers.stackSize += numberOfContainersToFill; + if (filledContainers.stackSize <= 0) { + filledContainers = null; + } + ItemStack containersThatCantBeDumped = transferItemToInventory(filledContainers, origin, false); + if (containersThatCantBeDumped != null) { + return item; + } + if (doAdd) + { + tank.drain(maximumExtractedLiquid, true); + transferItemToInventory(filledContainers, origin, true); + } + return leftOverContainers; + } +} diff --git a/src/Java/binnie/core/machines/inventory/Validator.java b/src/Java/binnie/core/machines/inventory/Validator.java new file mode 100644 index 0000000000..f8dddf7fae --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/Validator.java @@ -0,0 +1,9 @@ +package binnie.core.machines.inventory; + +import binnie.core.util.IValidator; + +public abstract class Validator<T> + implements IValidator<T> +{ + public abstract String getTooltip(); +} diff --git a/src/Java/binnie/core/machines/inventory/ValidatorIcon.java b/src/Java/binnie/core/machines/inventory/ValidatorIcon.java new file mode 100644 index 0000000000..ff7f1ecb1e --- /dev/null +++ b/src/Java/binnie/core/machines/inventory/ValidatorIcon.java @@ -0,0 +1,25 @@ +package binnie.core.machines.inventory; + +import binnie.Binnie; +import binnie.core.AbstractMod; +import binnie.core.resource.BinnieIcon; +import binnie.core.resource.ManagerResource; +import java.util.ArrayList; +import java.util.List; + +public class ValidatorIcon +{ + private List<BinnieIcon> iconsInput = new ArrayList(); + private List<BinnieIcon> iconsOutput = new ArrayList(); + + public ValidatorIcon(AbstractMod mod, String pathInput, String pathOutput) + { + this.iconsInput.add(Binnie.Resource.getItemIcon(mod, pathInput)); + this.iconsOutput.add(Binnie.Resource.getItemIcon(mod, pathOutput)); + } + + public BinnieIcon getIcon(boolean input) + { + return input ? (BinnieIcon)this.iconsInput.get(0) : (BinnieIcon)this.iconsOutput.get(0); + } +} diff --git a/src/Java/binnie/core/machines/network/INetwork.java b/src/Java/binnie/core/machines/network/INetwork.java new file mode 100644 index 0000000000..90b5621bed --- /dev/null +++ b/src/Java/binnie/core/machines/network/INetwork.java @@ -0,0 +1,30 @@ +package binnie.core.machines.network; + +import cpw.mods.fml.relauncher.Side; +import java.util.Map; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; + +public abstract interface INetwork +{ + public static abstract interface SendGuiNBT + { + public abstract void sendGuiNBT(Map<String, NBTTagCompound> paramMap); + } + + public static abstract interface TilePacketSync + { + public abstract void syncToNBT(NBTTagCompound paramNBTTagCompound); + + public abstract void syncFromNBT(NBTTagCompound paramNBTTagCompound); + } + + public static abstract interface RecieveGuiNBT + { + public abstract void recieveGuiNBT(Side paramSide, EntityPlayer paramEntityPlayer, String paramString, NBTTagCompound paramNBTTagCompound); + } + + public static abstract interface GuiNBT + extends INetwork.RecieveGuiNBT, INetwork.SendGuiNBT + {} +} diff --git a/src/Java/binnie/core/machines/power/ComponentPowerReceptor.java b/src/Java/binnie/core/machines/power/ComponentPowerReceptor.java new file mode 100644 index 0000000000..217af86c43 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ComponentPowerReceptor.java @@ -0,0 +1,191 @@ +package binnie.core.machines.power; + +import binnie.core.Mods; +import binnie.core.Mods.Mod; +import binnie.core.machines.IMachine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.component.IBuildcraft.TriggerProvider; +import binnie.core.machines.component.IInteraction.ChunkUnload; +import binnie.core.machines.component.IInteraction.Invalidation; +import binnie.core.triggers.TriggerData; +import binnie.core.triggers.TriggerPower; +import cpw.mods.fml.common.Optional.Interface; +import cpw.mods.fml.common.Optional.Method; +import cpw.mods.fml.common.eventhandler.EventBus; +import ic2.api.energy.event.EnergyTileLoadEvent; +import ic2.api.energy.event.EnergyTileUnloadEvent; +import ic2.api.energy.tile.IEnergyTile; +import java.util.LinkedList; +import java.util.List; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.util.ForgeDirection; + +@Optional.Interface(iface="binnie.core.machines.component.IBuildcraft.TriggerProvider", modid="BuildCraft|Silicon") +public class ComponentPowerReceptor + extends MachineComponent + implements IPoweredMachine, IBuildcraft.TriggerProvider, IInteraction.ChunkUnload, IInteraction.Invalidation +{ + private boolean registeredToIC2EnergyNet = false; + float previousPower = 0.0F; + LinkedList<Float> inputs = new LinkedList(); + static final int inputAverageTicks = 20; + private PowerInterface container; + + public ComponentPowerReceptor(IMachine machine) + { + this(machine, 1000); + } + + public ComponentPowerReceptor(IMachine machine, int storage) + { + super(machine); + this.container = new PowerInterface(storage); + if (!this.registeredToIC2EnergyNet) { + addToEnergyNet(); + } + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + super.readFromNBT(nbttagcompound); + this.container.readFromNBT(nbttagcompound); + if (!this.registeredToIC2EnergyNet) { + addToEnergyNet(); + } + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + super.writeToNBT(nbttagcompound); + this.container.writeToNBT(nbttagcompound); + } + + public void onUpdate() + { + if ((!this.registeredToIC2EnergyNet) && (!getMachine().getTileEntity().isInvalid())) { + addToEnergyNet(); + } + } + + public PowerInfo getPowerInfo() + { + return new PowerInfo(this, 0.0F); + } + + @Optional.Method(modid="BuildCraft|Silicon") + public final void getTriggers(List<TriggerData> triggers) + { + triggers.add(TriggerPower.powerNone(this)); + triggers.add(TriggerPower.powerLow(this)); + triggers.add(TriggerPower.powerMedium(this)); + triggers.add(TriggerPower.powerHigh(this)); + triggers.add(TriggerPower.powerFull(this)); + } + + @Optional.Method(modid="IC2") + public double getDemandedEnergy() + { + return this.container.getEnergySpace(PowerSystem.EU); + } + + @Optional.Method(modid="IC2") + public int getSinkTier() + { + return 1; + } + + @Optional.Method(modid="IC2") + public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) + { + this.container.addEnergy(PowerSystem.EU, amount, true); + return 0.0D; + } + + @Optional.Method(modid="IC2") + public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) + { + return acceptsPowerSystem(PowerSystem.EU); + } + + public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) + { + return (int)this.container.addEnergy(PowerSystem.RF, maxReceive, !simulate); + } + + public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) + { + return 0; + } + + public int getEnergyStored(ForgeDirection from) + { + return (int)this.container.getEnergy(PowerSystem.RF); + } + + public int getMaxEnergyStored(ForgeDirection from) + { + return (int)this.container.getCapacity(PowerSystem.RF); + } + + public boolean canConnectEnergy(ForgeDirection from) + { + boolean can = acceptsPowerSystem(PowerSystem.RF); + return can; + } + + public PowerInterface getInterface() + { + return this.container; + } + + private boolean acceptsPowerSystem(PowerSystem system) + { + return true; + } + + public void onInvalidation() + { + removeFromEnergyNet(); + } + + public void onChunkUnload() + { + removeFromEnergyNet(); + } + + private void addToEnergyNet() + { + if (getMachine().getWorld() == null) { + return; + } + if (Mods.IC2.active()) { + do_addToEnergyNet(); + } + } + + private void removeFromEnergyNet() + { + if (getMachine().getWorld() == null) { + return; + } + if (Mods.IC2.active()) { + do_removeFromEnergyNet(); + } + } + + @Optional.Method(modid="IC2") + private void do_addToEnergyNet() + { + MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent((IEnergyTile)getMachine().getTileEntity())); + this.registeredToIC2EnergyNet = true; + } + + @Optional.Method(modid="IC2") + private void do_removeFromEnergyNet() + { + MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)getMachine().getTileEntity())); + this.registeredToIC2EnergyNet = false; + } +} diff --git a/src/Java/binnie/core/machines/power/ComponentProcess.java b/src/Java/binnie/core/machines/power/ComponentProcess.java new file mode 100644 index 0000000000..b49e919e2d --- /dev/null +++ b/src/Java/binnie/core/machines/power/ComponentProcess.java @@ -0,0 +1,90 @@ +package binnie.core.machines.power; + +import binnie.core.machines.IMachine; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class ComponentProcess + extends ComponentProcessIndefinate + implements IProcessTimed +{ + private float progressAmount = 0.0F; + + public ComponentProcess(IMachine machine) + { + super(machine, 0.0F); + } + + public float getEnergyPerTick() + { + return getProcessEnergy() / getProcessLength(); + } + + public float getProgressPerTick() + { + return 100.0F / getProcessLength(); + } + + protected void onStartTask() + { + this.progressAmount += 0.01F; + } + + protected void onCancelTask() + { + this.progressAmount = 0.0F; + } + + public void onUpdate() + { + super.onUpdate(); + if (this.progressAmount >= 100.0F) + { + onFinishTask(); + this.progressAmount = 0.0F; + } + } + + public void alterProgress(float f) + { + this.progressAmount += f; + } + + public void setProgress(float f) + { + this.progressAmount = f; + } + + protected void progressTick() + { + super.progressTick(); + alterProgress(getProgressPerTick()); + } + + public boolean inProgress() + { + return this.progressAmount > 0.0F; + } + + public float getProgress() + { + return this.progressAmount; + } + + protected void onFinishTask() {} + + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + this.progressAmount = nbt.getFloat("progress"); + } + + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + nbt.setFloat("progress", this.progressAmount); + } + + public abstract int getProcessLength(); + + public abstract int getProcessEnergy(); +} diff --git a/src/Java/binnie/core/machines/power/ComponentProcessIndefinate.java b/src/Java/binnie/core/machines/power/ComponentProcessIndefinate.java new file mode 100644 index 0000000000..f1442107f4 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ComponentProcessIndefinate.java @@ -0,0 +1,120 @@ +package binnie.core.machines.power; + +import binnie.core.machines.IMachine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.MachineUtil; +import binnie.core.machines.network.INetwork.TilePacketSync; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class ComponentProcessIndefinate + extends MachineComponent + implements IProcess, INetwork.TilePacketSync +{ + private float energyPerTick = 0.1F; + private boolean inProgress; + + public void syncFromNBT(NBTTagCompound nbt) + { + this.inProgress = nbt.getBoolean("progress"); + } + + public void syncToNBT(NBTTagCompound nbt) + { + nbt.setBoolean("progress", this.inProgress); + } + + public ComponentProcessIndefinate(IMachine machine, float energyPerTick) + { + super(machine); + this.energyPerTick = energyPerTick; + } + + protected final IPoweredMachine getPower() + { + return (IPoweredMachine)getMachine().getInterface(IPoweredMachine.class); + } + + public float getEnergyPerTick() + { + return this.energyPerTick; + } + + private float actionPauseProcess = 0.0F; + private float actionCancelTask = 0.0F; + + public void onUpdate() + { + float energyAvailable = (float)getPower().getInterface().useEnergy(PowerSystem.RF, getEnergyPerTick(), false); + if (canWork() == null) + { + if ((!isInProgress()) && (canProgress() == null)) + { + onStartTask(); + } + else if (canProgress() == null) + { + progressTick(); + onTickTask(); + } + } + else if (isInProgress()) { + onCancelTask(); + } + if (this.actionPauseProcess > 0.0F) { + this.actionPauseProcess -= 1.0F; + } + if (this.actionCancelTask > 0.0F) { + this.actionCancelTask -= 1.0F; + } + super.onUpdate(); + if (this.inProgress != inProgress()) + { + this.inProgress = inProgress(); + getUtil().refreshBlock(); + } + } + + protected void progressTick() + { + getPower().getInterface().useEnergy(PowerSystem.RF, getEnergyPerTick(), true); + } + + public ErrorState canWork() + { + return this.actionCancelTask == 0.0F ? null : new ErrorState("Task Cancelled", "Cancelled by Buildcraft Gate"); + } + + public ErrorState canProgress() + { + if (this.actionPauseProcess != 0.0F) { + return new ErrorState("Process Paused", "Paused by Buildcraft Gate"); + } + return getPower().getInterface().getEnergy(PowerSystem.RF) < getEnergyPerTick() ? new ErrorState.InsufficientPower() : null; + } + + public final boolean isInProgress() + { + return this.inProgress; + } + + int clientEnergyPerSecond = 0; + int clientInProgress; + + protected abstract boolean inProgress(); + + protected void onCancelTask() {} + + protected void onStartTask() {} + + protected void onTickTask() {} + + public String getTooltip() + { + return "Processing"; + } + + public final ProcessInfo getInfo() + { + return new ProcessInfo(this); + } +} diff --git a/src/Java/binnie/core/machines/power/ComponentProcessSetCost.java b/src/Java/binnie/core/machines/power/ComponentProcessSetCost.java new file mode 100644 index 0000000000..b269763136 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ComponentProcessSetCost.java @@ -0,0 +1,27 @@ +package binnie.core.machines.power; + +import binnie.core.machines.IMachine; + +public class ComponentProcessSetCost + extends ComponentProcess +{ + private int processLength; + private int processEnergy; + + public ComponentProcessSetCost(IMachine machine, int rfCost, int timePeriod) + { + super(machine); + this.processLength = timePeriod; + this.processEnergy = rfCost; + } + + public int getProcessLength() + { + return this.processLength; + } + + public int getProcessEnergy() + { + return this.processEnergy; + } +} diff --git a/src/Java/binnie/core/machines/power/ErrorState.java b/src/Java/binnie/core/machines/power/ErrorState.java new file mode 100644 index 0000000000..03ed4de1f0 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ErrorState.java @@ -0,0 +1,187 @@ +package binnie.core.machines.power; + +import forestry.api.core.INBTTagable; +import net.minecraft.nbt.NBTTagCompound; + +public class ErrorState + implements INBTTagable +{ + private String name = ""; + private String desc = ""; + private int[] data = new int[0]; + private boolean progress = false; + + public ErrorState(String name, String desc) + { + this.name = name; + this.desc = desc; + } + + public ErrorState(String name, String desc, int[] data) + { + this.name = name; + this.desc = desc; + this.data = data; + } + + public String toString() + { + return this.name; + } + + public String getTooltip() + { + return this.desc; + } + + public int[] getData() + { + return this.data; + } + + public boolean isProgress() + { + return this.progress; + } + + public void setIsProgress() + { + this.progress = true; + } + + public static class Item + extends ErrorState + { + public Item(String name, String desc, int[] slots) + { + super(desc, slots); + } + } + + public static class Tank + extends ErrorState + { + public Tank(String name, String desc, int[] slots) + { + super(desc, slots); + } + } + + public static class NoItem + extends ErrorState.Item + { + public NoItem(String desc, int slot) + { + this(desc, new int[] { slot }); + } + + public NoItem(String desc, int[] slots) + { + super(desc, slots); + } + } + + public static class InvalidItem + extends ErrorState.Item + { + public InvalidItem(String desc, int slot) + { + this("Invalid Item", desc, slot); + } + + public InvalidItem(String name, String desc, int slot) + { + super(desc, new int[] { slot }); + } + } + + public static class NoSpace + extends ErrorState.Item + { + public NoSpace(String desc, int[] slots) + { + super(desc, slots); + } + } + + public static class InsufficientPower + extends ErrorState + { + public InsufficientPower() + { + super("Not enough power to operate"); + } + } + + public static class TankSpace + extends ErrorState.Tank + { + public TankSpace(String desc, int tank) + { + super(desc, new int[] { tank }); + } + } + + public static class InsufficientLiquid + extends ErrorState.Tank + { + public InsufficientLiquid(String desc, int tank) + { + super(desc, new int[] { tank }); + } + } + + public static class InvalidRecipe + extends ErrorState.Item + { + public InvalidRecipe(String string, int[] slots) + { + super(string, slots); + } + } + + public void readFromNBT(NBTTagCompound nbt) + { + this.name = nbt.getString("name"); + this.desc = nbt.getString("desc"); + this.data = nbt.getIntArray("data"); + this.itemError = nbt.getBoolean("item"); + this.tankError = nbt.getBoolean("tank"); + this.powerError = nbt.getBoolean("power"); + } + + public void writeToNBT(NBTTagCompound nbt) + { + nbt.setString("name", toString()); + nbt.setString("desc", getTooltip()); + nbt.setIntArray("data", this.data); + if (isItemError()) { + nbt.setBoolean("item", true); + } + if (isTankError()) { + nbt.setBoolean("tank", true); + } + if (isPowerError()) { + nbt.setBoolean("power", true); + } + } + + private boolean itemError = false; + private boolean tankError = false; + private boolean powerError = false; + + public boolean isItemError() + { + return (this.itemError) || ((this instanceof Item)); + } + + public boolean isTankError() + { + return (this.tankError) || ((this instanceof Tank)); + } + + public boolean isPowerError() + { + return (this.powerError) || ((this instanceof InsufficientPower)); + } +} diff --git a/src/Java/binnie/core/machines/power/IErrorStateSource.java b/src/Java/binnie/core/machines/power/IErrorStateSource.java new file mode 100644 index 0000000000..bd2cd87d24 --- /dev/null +++ b/src/Java/binnie/core/machines/power/IErrorStateSource.java @@ -0,0 +1,8 @@ +package binnie.core.machines.power; + +public abstract interface IErrorStateSource +{ + public abstract ErrorState canWork(); + + public abstract ErrorState canProgress(); +} diff --git a/src/Java/binnie/core/machines/power/IPoweredMachine.java b/src/Java/binnie/core/machines/power/IPoweredMachine.java new file mode 100644 index 0000000000..b5e57aeff0 --- /dev/null +++ b/src/Java/binnie/core/machines/power/IPoweredMachine.java @@ -0,0 +1,14 @@ +package binnie.core.machines.power; + +import cofh.api.energy.IEnergyHandler; +import cpw.mods.fml.common.Optional.Interface; +import ic2.api.energy.tile.IEnergySink; + +@Optional.Interface(iface="ic2.api.energy.tile.IEnergySink", modid="IC2") +public abstract interface IPoweredMachine + extends IEnergySink, IEnergyHandler +{ + public abstract PowerInfo getPowerInfo(); + + public abstract PowerInterface getInterface(); +} diff --git a/src/Java/binnie/core/machines/power/IProcess.java b/src/Java/binnie/core/machines/power/IProcess.java new file mode 100644 index 0000000000..83337cd980 --- /dev/null +++ b/src/Java/binnie/core/machines/power/IProcess.java @@ -0,0 +1,13 @@ +package binnie.core.machines.power; + +public abstract interface IProcess + extends IErrorStateSource +{ + public abstract float getEnergyPerTick(); + + public abstract String getTooltip(); + + public abstract boolean isInProgress(); + + public abstract ProcessInfo getInfo(); +} diff --git a/src/Java/binnie/core/machines/power/IProcessTimed.java b/src/Java/binnie/core/machines/power/IProcessTimed.java new file mode 100644 index 0000000000..1489d8837f --- /dev/null +++ b/src/Java/binnie/core/machines/power/IProcessTimed.java @@ -0,0 +1,13 @@ +package binnie.core.machines.power; + +abstract interface IProcessTimed + extends IProcess, IErrorStateSource +{ + public abstract int getProcessLength(); + + public abstract int getProcessEnergy(); + + public abstract float getProgress(); + + public abstract float getProgressPerTick(); +} diff --git a/src/Java/binnie/core/machines/power/ITankMachine.java b/src/Java/binnie/core/machines/power/ITankMachine.java new file mode 100644 index 0000000000..fa92845cb7 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ITankMachine.java @@ -0,0 +1,20 @@ +package binnie.core.machines.power; + +import binnie.core.machines.inventory.IValidatedTankContainer; +import binnie.core.machines.inventory.TankSlot; +import net.minecraftforge.fluids.IFluidHandler; +import net.minecraftforge.fluids.IFluidTank; + +public abstract interface ITankMachine + extends IFluidHandler, IValidatedTankContainer +{ + public abstract TankInfo[] getTankInfos(); + + public abstract IFluidTank[] getTanks(); + + public abstract TankSlot addTank(int paramInt1, String paramString, int paramInt2); + + public abstract IFluidTank getTank(int paramInt); + + public abstract TankSlot getTankSlot(int paramInt); +} diff --git a/src/Java/binnie/core/machines/power/PowerInfo.java b/src/Java/binnie/core/machines/power/PowerInfo.java new file mode 100644 index 0000000000..2c57f2c898 --- /dev/null +++ b/src/Java/binnie/core/machines/power/PowerInfo.java @@ -0,0 +1,41 @@ +package binnie.core.machines.power; + +import forestry.api.core.INBTTagable; +import net.minecraft.nbt.NBTTagCompound; + +public class PowerInfo + implements INBTTagable +{ + private float currentEnergy = 0.0F; + private float maxEnergy = 0.0F; + + public PowerInfo(IPoweredMachine machine, float currentInput) + { + this.currentEnergy = ((float)machine.getInterface().getEnergy(PowerSystem.RF)); + this.maxEnergy = ((float)machine.getInterface().getCapacity(PowerSystem.RF)); + } + + public PowerInfo() {} + + public int getStoredEnergy() + { + return (int)this.currentEnergy; + } + + public int getMaxEnergy() + { + return (int)this.maxEnergy; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + this.currentEnergy = nbttagcompound.getInteger("current"); + this.maxEnergy = nbttagcompound.getInteger("max"); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + nbttagcompound.setInteger("current", getStoredEnergy()); + nbttagcompound.setInteger("max", getMaxEnergy()); + } +} diff --git a/src/Java/binnie/core/machines/power/PowerInterface.java b/src/Java/binnie/core/machines/power/PowerInterface.java new file mode 100644 index 0000000000..e7e91bc18a --- /dev/null +++ b/src/Java/binnie/core/machines/power/PowerInterface.java @@ -0,0 +1,90 @@ +package binnie.core.machines.power; + +import forestry.api.core.INBTTagable; +import net.minecraft.nbt.NBTTagCompound; + +public class PowerInterface + implements INBTTagable +{ + private int capacity; + private int energy; + + public PowerInterface(int capacity) + { + this.capacity = (capacity * 100); + this.energy = 0; + } + + public int getCapacity() + { + return this.capacity; + } + + public int getEnergy() + { + return this.energy; + } + + public int addEnergy(int amount, boolean shouldDo) + { + int added = Math.min(getEnergySpace(), amount); + if (shouldDo) { + this.energy += added; + } + return added; + } + + public int useEnergy(int amount, boolean simulate) + { + int added = Math.min(getEnergy(), amount); + if (simulate) { + this.energy -= added; + } + return added; + } + + public int getEnergySpace() + { + return getCapacity() - getEnergy(); + } + + public double addEnergy(PowerSystem unit, double amount, boolean simulate) + { + return unit.convertTo(addEnergy(unit.convertFrom(amount), simulate)); + } + + public double useEnergy(PowerSystem unit, double amount, boolean simulate) + { + return unit.convertTo(useEnergy(unit.convertFrom(amount), simulate)); + } + + public double getEnergy(PowerSystem unit) + { + return unit.convertTo(getEnergy()); + } + + public double getCapacity(PowerSystem unit) + { + return unit.convertTo(getCapacity()); + } + + public double getEnergySpace(PowerSystem unit) + { + return unit.convertTo(getEnergySpace()); + } + + public void readFromNBT(NBTTagCompound nbt) + { + this.energy = nbt.getInteger("Energy"); + if (this.energy > this.capacity) { + this.energy = this.capacity; + } else if (this.energy < 0) { + this.energy = 0; + } + } + + public void writeToNBT(NBTTagCompound nbt) + { + nbt.setInteger("Energy", getEnergy()); + } +} diff --git a/src/Java/binnie/core/machines/power/PowerSystem.java b/src/Java/binnie/core/machines/power/PowerSystem.java new file mode 100644 index 0000000000..58c1cf9709 --- /dev/null +++ b/src/Java/binnie/core/machines/power/PowerSystem.java @@ -0,0 +1,44 @@ +package binnie.core.machines.power; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public enum PowerSystem +{ + MJ(100.0D), RF(10.0D), EU(40.0D); + + double conversion; + + private PowerSystem(double conversion) + { + this.conversion = conversion; + } + + public double convertTo(int value) + { + return value / this.conversion; + } + + public int convertFrom(double value) + { + return (int)(value * this.conversion); + } + + public static PowerSystem get(int i) + { + return values()[(i % values().length)]; + } + + public String getUnitName() + { + return name(); + } + + public ItemStack saveTo(ItemStack stack) + { + NBTTagCompound tag = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound(); + tag.setByte("power-system", (byte)ordinal()); + stack.setTagCompound(tag); + return stack; + } +} diff --git a/src/Java/binnie/core/machines/power/ProcessInfo.java b/src/Java/binnie/core/machines/power/ProcessInfo.java new file mode 100644 index 0000000000..7b4a9bd8a4 --- /dev/null +++ b/src/Java/binnie/core/machines/power/ProcessInfo.java @@ -0,0 +1,67 @@ +package binnie.core.machines.power; + +import forestry.api.core.INBTTagable; +import net.minecraft.nbt.NBTTagCompound; + +public class ProcessInfo + implements INBTTagable +{ + private float currentProgress = 0.0F; + private int processEnergy = 0; + private int processTime = 0; + private float energyPerTick = 0.0F; + + public ProcessInfo(IProcess process) + { + this.energyPerTick = process.getEnergyPerTick(); + if ((process instanceof IProcessTimed)) + { + IProcessTimed time = (IProcessTimed)process; + this.currentProgress = time.getProgress(); + this.processEnergy = time.getProcessEnergy(); + this.processTime = time.getProcessLength(); + } + else + { + this.currentProgress = (process.isInProgress() ? 100.0F : 0.0F); + } + } + + public ProcessInfo() {} + + public float getCurrentProgress() + { + return this.currentProgress; + } + + public int getProcessEnergy() + { + return this.processEnergy; + } + + public int getProcessTime() + { + return this.processTime; + } + + public float getEnergyPerTick() + { + return this.energyPerTick; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) + { + this.energyPerTick = nbttagcompound.getFloat("ept"); + this.processEnergy = nbttagcompound.getInteger("e"); + this.processTime = nbttagcompound.getInteger("t"); + this.currentProgress = nbttagcompound.getFloat("p"); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) + { + nbttagcompound.setFloat("ept", this.energyPerTick); + nbttagcompound.setFloat("p", this.currentProgress); + nbttagcompound.setInteger("e", this.processEnergy); + nbttagcompound.setInteger("t", this.processTime); + } +} diff --git a/src/Java/binnie/core/machines/power/TankInfo.java b/src/Java/binnie/core/machines/power/TankInfo.java new file mode 100644 index 0000000000..d9e0b350d7 --- /dev/null +++ b/src/Java/binnie/core/machines/power/TankInfo.java @@ -0,0 +1,76 @@ +package binnie.core.machines.power; + +import forestry.api.core.INBTTagable; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.IIcon; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidTank; + +public class TankInfo + implements INBTTagable +{ + public FluidStack liquid; + private float capacity = 0.0F; + + public TankInfo(IFluidTank tank) + { + this.capacity = tank.getCapacity(); + this.liquid = tank.getFluid(); + } + + public TankInfo() {} + + public float getAmount() + { + return this.liquid == null ? 0.0F : this.liquid.amount; + } + + public float getCapacity() + { + return this.capacity; + } + + public boolean isEmpty() + { + return this.liquid == null; + } + + public IIcon getIcon() + { + return this.liquid.getFluid().getStillIcon(); + } + + public String getName() + { + return this.liquid == null ? "" : this.liquid.getFluid().getLocalizedName(); + } + + public void readFromNBT(NBTTagCompound nbt) + { + this.capacity = nbt.getInteger("capacity"); + if (nbt.hasKey("liquid")) { + this.liquid = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("liquid")); + } + } + + public void writeToNBT(NBTTagCompound nbt) + { + nbt.setInteger("capacity", (int)getCapacity()); + if (this.liquid == null) { + return; + } + NBTTagCompound tag = new NBTTagCompound(); + this.liquid.writeToNBT(tag); + nbt.setTag("liquid", tag); + } + + public static TankInfo[] get(ITankMachine machine) + { + TankInfo[] info = new TankInfo[machine.getTanks().length]; + for (int i = 0; i < info.length; i++) { + info[i] = new TankInfo(machine.getTanks()[i]); + } + return info; + } +} diff --git a/src/Java/binnie/core/machines/storage/Compartment.java b/src/Java/binnie/core/machines/storage/Compartment.java new file mode 100644 index 0000000000..d7694e2502 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/Compartment.java @@ -0,0 +1,65 @@ +package binnie.core.machines.storage; + +import binnie.core.BinnieCore; +import binnie.core.machines.IMachineType; +import binnie.core.machines.Machine; +import binnie.core.machines.MachineGroup; +import binnie.core.machines.MachinePackage; +import binnie.core.machines.TileEntityMachine; +import binnie.core.resource.BinnieResource; +import binnie.core.resource.IBinnieTexture; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; + + enum Compartment + implements IMachineType +{ + Compartment(StandardCompartment.PackageCompartment.class), CompartmentCopper(StandardCompartment.PackageCompartmentCopper.class), CompartmentBronze(StandardCompartment.PackageCompartmentBronze.class), CompartmentIron(StandardCompartment.PackageCompartmentIron.class), CompartmentGold(StandardCompartment.PackageCompartmentGold.class), CompartmentDiamond(StandardCompartment.PackageCompartmentDiamond.class); + + Class<? extends MachinePackage> clss; + + private Compartment(Class<? extends MachinePackage> clss) + { + this.clss = clss; + } + + public Class<? extends MachinePackage> getPackageClass() + { + return this.clss; + } + + public static abstract class PackageCompartment + extends MachinePackage + { + private BinnieResource renderTexture; + + protected PackageCompartment(String uid, IBinnieTexture renderTexture) + { + super(false); + this.renderTexture = renderTexture.getTexture(); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + + public void renderMachine(Machine machine, double x, double y, double z, float var8, RenderBlocks renderer) + { + MachineRendererCompartment.instance.renderMachine(machine, 16777215, this.renderTexture, x, y, z, var8); + } + } + + public boolean isActive() + { + return true; + } + + public ItemStack get(int i) + { + return new ItemStack(BinnieCore.packageCompartment.getBlock(), i, ordinal()); + } +} diff --git a/src/Java/binnie/core/machines/storage/CompartmentTab.java b/src/Java/binnie/core/machines/storage/CompartmentTab.java new file mode 100644 index 0000000000..025f73db31 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/CompartmentTab.java @@ -0,0 +1,77 @@ +package binnie.core.machines.storage; + +import binnie.craftgui.minecraft.EnumColor; +import forestry.api.core.INBTTagable; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +class CompartmentTab + implements INBTTagable +{ + private String name; + private ItemStack icon; + private EnumColor color; + private int id; + + public CompartmentTab(int id) + { + this.name = ""; + this.icon = new ItemStack(Items.paper); + this.color = EnumColor.White; + this.id = id; + } + + public void readFromNBT(NBTTagCompound nbt) + { + this.name = nbt.getString("name"); + this.icon = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("icon")); + this.color = EnumColor.values()[nbt.getByte("color")]; + this.id = nbt.getByte("id"); + } + + public void writeToNBT(NBTTagCompound nbt) + { + nbt.setString("name", this.name); + NBTTagCompound n = new NBTTagCompound(); + this.icon.writeToNBT(n); + nbt.setTag("icon", n); + nbt.setByte("color", (byte)this.color.ordinal()); + nbt.setByte("id", (byte)this.id); + } + + public String getName() + { + return this.name; + } + + public ItemStack getIcon() + { + return this.icon; + } + + public EnumColor getColor() + { + return this.color; + } + + public int getId() + { + return this.id; + } + + public void setName(String name) + { + this.name = (name == null ? "" : name); + } + + public void setIcon(ItemStack icon) + { + this.icon = icon; + } + + public void setColor(EnumColor color) + { + this.color = color; + } +} diff --git a/src/Java/binnie/core/machines/storage/ComponentBinnieCoreGUI.java b/src/Java/binnie/core/machines/storage/ComponentBinnieCoreGUI.java new file mode 100644 index 0000000000..d987129378 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/ComponentBinnieCoreGUI.java @@ -0,0 +1,28 @@ +package binnie.core.machines.storage; + +import binnie.core.BinnieCore; +import binnie.core.gui.BinnieCoreGUI; +import binnie.core.machines.Machine; +import binnie.core.machines.MachineComponent; +import binnie.core.machines.component.IInteraction.RightClick; +import binnie.core.proxy.BinnieProxy; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +class ComponentBinnieCoreGUI + extends MachineComponent + implements IInteraction.RightClick +{ + private BinnieCoreGUI id; + + public ComponentBinnieCoreGUI(Machine machine, BinnieCoreGUI id) + { + super(machine); + this.id = id; + } + + public void onRightClick(World world, EntityPlayer player, int x, int y, int z) + { + BinnieCore.proxy.openGui(this.id, player, x, y, z); + } +} diff --git a/src/Java/binnie/core/machines/storage/ComponentCompartmentInventory.java b/src/Java/binnie/core/machines/storage/ComponentCompartmentInventory.java new file mode 100644 index 0000000000..ef55e9df9e --- /dev/null +++ b/src/Java/binnie/core/machines/storage/ComponentCompartmentInventory.java @@ -0,0 +1,128 @@ +package binnie.core.machines.storage; + +import binnie.core.machines.IMachine; +import binnie.core.machines.inventory.ComponentInventorySlots; +import binnie.core.machines.network.INetwork.GuiNBT; +import cpw.mods.fml.relauncher.Side; +import java.util.HashMap; +import java.util.Map; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.tileentity.TileEntity; + +class ComponentCompartmentInventory + extends ComponentInventorySlots + implements INetwork.GuiNBT +{ + private int numberOfTabs; + private int slotsPerPage; + + public ComponentCompartmentInventory(IMachine machine, int sections) + { + this(machine, sections, 4); + } + + public ComponentCompartmentInventory(IMachine machine, int tabs, int pageSize) + { + super(machine); + + this.numberOfTabs = tabs; + this.slotsPerPage = pageSize; + for (int i = 0; i < this.numberOfTabs * this.slotsPerPage; i++) { + addSlot(i, "compartment"); + } + } + + public int getPageSize() + { + return this.slotsPerPage; + } + + public int getTabNumber() + { + return this.numberOfTabs; + } + + public int[] getSlotsForTab(int currentTab) + { + int[] slots = new int[this.slotsPerPage]; + for (int i = 0; i < this.slotsPerPage; i++) { + slots[i] = (i + currentTab * this.slotsPerPage); + } + return slots; + } + + private Map<Integer, CompartmentTab> tabs = new HashMap(); + + public CompartmentTab getTab(int i) + { + if (!this.tabs.containsKey(Integer.valueOf(i))) { + this.tabs.put(Integer.valueOf(i), new CompartmentTab(i)); + } + return (CompartmentTab)this.tabs.get(Integer.valueOf(i)); + } + + public void sendGuiNBT(Map<String, NBTTagCompound> nbt) + { + NBTTagList list = new NBTTagList(); + for (int i = 0; i < this.numberOfTabs; i++) + { + NBTTagCompound nbt2 = new NBTTagCompound(); + getTab(i).writeToNBT(nbt2); + list.appendTag(nbt2); + } + NBTTagCompound tag = new NBTTagCompound(); + tag.setTag("tabs", list); + nbt.put("comp-tabs", tag); + } + + public void recieveGuiNBT(Side side, EntityPlayer player, String name, NBTTagCompound nbt) + { + if (name.equals("comp-tabs")) + { + NBTTagList tags = nbt.getTagList("tabs", 10); + for (int i = 0; i < tags.tagCount(); i++) + { + NBTTagCompound tag = tags.getCompoundTagAt(i); + CompartmentTab tab = new CompartmentTab(0); + tab.readFromNBT(tag); + this.tabs.put(Integer.valueOf(tab.getId()), tab); + } + } + if (name.equals("comp-change-tab")) + { + NBTTagCompound tag = nbt; + CompartmentTab tab = new CompartmentTab(0); + tab.readFromNBT(tag); + this.tabs.put(Integer.valueOf(tab.getId()), tab); + getMachine().getTileEntity().markDirty(); + } + } + + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + NBTTagList tags = nbt.getTagList("tabs", 10); + for (int i = 0; i < tags.tagCount(); i++) + { + NBTTagCompound tag = tags.getCompoundTagAt(i); + CompartmentTab tab = new CompartmentTab(0); + tab.readFromNBT(tag); + this.tabs.put(Integer.valueOf(tab.getId()), tab); + } + } + + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + NBTTagList list = new NBTTagList(); + for (int i = 0; i < this.numberOfTabs; i++) + { + NBTTagCompound nbt2 = new NBTTagCompound(); + getTab(i).writeToNBT(nbt2); + list.appendTag(nbt2); + } + nbt.setTag("tabs", list); + } +} diff --git a/src/Java/binnie/core/machines/storage/ControlColourSelector.java b/src/Java/binnie/core/machines/storage/ControlColourSelector.java new file mode 100644 index 0000000000..c5320a2629 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/ControlColourSelector.java @@ -0,0 +1,47 @@ +package binnie.core.machines.storage; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.controls.core.IControlValue; +import binnie.craftgui.core.Attribute; +import binnie.craftgui.core.CraftGUI; +import binnie.craftgui.core.ITooltip; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.Tooltip; +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.minecraft.EnumColor; + +class ControlColourSelector + extends Control + implements ITooltip, IControlValue<EnumColor> +{ + private EnumColor value; + + public ControlColourSelector(IWidget parent, float x, float y, float w, float h, EnumColor value) + { + super(parent, x, y, w, h); + setValue(value); + addAttribute(Attribute.MouseOver); + } + + public void getTooltip(Tooltip tooltip) + { + tooltip.add(this.value.toString()); + } + + public EnumColor getValue() + { + return this.value; + } + + public void setValue(EnumColor value) + { + this.value = value; + setColour(getValue().getColour()); + } + + public void onRenderBackground() + { + super.onRenderBackground(); + CraftGUI.Render.gradientRect(getArea(), -16777216 + this.value.getColour(), -16777216 + this.value.getColour()); + } +} diff --git a/src/Java/binnie/core/machines/storage/MachineRendererCompartment.java b/src/Java/binnie/core/machines/storage/MachineRendererCompartment.java new file mode 100644 index 0000000000..133b5293ff --- /dev/null +++ b/src/Java/binnie/core/machines/storage/MachineRendererCompartment.java @@ -0,0 +1,50 @@ +package binnie.core.machines.storage; + +import binnie.core.BinnieCore; +import binnie.core.machines.Machine; +import binnie.core.proxy.BinnieProxy; +import binnie.core.resource.BinnieResource; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.tileentity.TileEntity; +import org.lwjgl.opengl.GL11; + +@SideOnly(Side.CLIENT) +class MachineRendererCompartment +{ + public static MachineRendererCompartment instance = new MachineRendererCompartment(); + private ModelCompartment model; + + public MachineRendererCompartment() + { + this.model = new ModelCompartment(); + } + + public void renderMachine(Machine machine, int colour, BinnieResource texture, double x, double y, double z, float var8) + { + GL11.glPushMatrix(); + + int i1 = 0; + + int ix = machine.getTileEntity().xCoord; + int iy = machine.getTileEntity().yCoord; + int iz = machine.getTileEntity().zCoord; + if (machine.getTileEntity() != null) { + i1 = ix * iy * iz + ix * iy - ix * iz + iy * iz - ix + iy - iz; + } + float phase = (float)Math.max(0.0D, Math.sin((System.currentTimeMillis() + i1) * 0.003D)); + + GL11.glTranslated(x + 0.5D, y + 1.5D, z + 0.5D); + GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); + + BinnieCore.proxy.bindTexture(texture); + + GL11.glPushMatrix(); + + this.model.render(null, (float)x, (float)y, (float)z, 0.0625F, 0.0625F, 0.0625F); + + GL11.glPopMatrix(); + + GL11.glPopMatrix(); + } +} diff --git a/src/Java/binnie/core/machines/storage/ModelCompartment.java b/src/Java/binnie/core/machines/storage/ModelCompartment.java new file mode 100644 index 0000000000..9e5aef450a --- /dev/null +++ b/src/Java/binnie/core/machines/storage/ModelCompartment.java @@ -0,0 +1,171 @@ +package binnie.core.machines.storage; + +import net.minecraft.client.model.ModelBase; +import net.minecraft.client.model.ModelRenderer; +import net.minecraft.entity.Entity; + +class ModelCompartment + extends ModelBase +{ + private ModelRenderer Column1; + private ModelRenderer Column4; + private ModelRenderer Column2; + private ModelRenderer Column3; + private ModelRenderer Lid_1; + private ModelRenderer Body_1; + private ModelRenderer Lock_1; + private ModelRenderer Lid_2; + private ModelRenderer Body_2; + private ModelRenderer Lock_2; + private ModelRenderer Body; + private ModelRenderer Lid_3; + private ModelRenderer Body_3; + private ModelRenderer Lock_3; + private ModelRenderer Lid_4; + private ModelRenderer Body_4; + private ModelRenderer Lock_4; + + public ModelCompartment() + { + this.textureWidth = 128; + this.textureHeight = 128; + + this.Column1 = new ModelRenderer(this, 0, 0); + this.Column1.addBox(-8.0F, 8.0F, -8.0F, 4, 16, 4); + this.Column1.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Column1.setTextureSize(128, 128); + this.Column1.mirror = true; + setRotation(this.Column1, 0.0F, 0.0F, 0.0F); + this.Column4 = new ModelRenderer(this, 0, 0); + this.Column4.addBox(4.0F, 8.0F, -8.0F, 4, 16, 4); + this.Column4.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Column4.setTextureSize(128, 128); + this.Column4.mirror = true; + setRotation(this.Column4, 0.0F, 0.0F, 0.0F); + this.Column2 = new ModelRenderer(this, 0, 0); + this.Column2.addBox(-8.0F, 8.0F, 4.0F, 4, 16, 4); + this.Column2.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Column2.setTextureSize(128, 128); + this.Column2.mirror = true; + setRotation(this.Column2, 0.0F, 0.0F, 0.0F); + this.Column3 = new ModelRenderer(this, 0, 0); + this.Column3.addBox(4.0F, 8.0F, 4.0F, 4, 16, 4); + this.Column3.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Column3.setTextureSize(128, 128); + this.Column3.mirror = true; + setRotation(this.Column3, 0.0F, 0.0F, 0.0F); + this.Lid_1 = new ModelRenderer(this, 48, 0); + this.Lid_1.addBox(-4.0F, 9.0F, -7.0F, 8, 5, 3); + this.Lid_1.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lid_1.setTextureSize(128, 128); + this.Lid_1.mirror = true; + setRotation(this.Lid_1, 0.0F, 0.0F, 0.0F); + this.Body_1 = new ModelRenderer(this, 0, 24); + this.Body_1.addBox(-4.0F, 14.0F, -7.0F, 8, 10, 3); + this.Body_1.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Body_1.setTextureSize(128, 128); + this.Body_1.mirror = true; + setRotation(this.Body_1, 0.0F, 0.0F, 0.0F); + this.Lock_1 = new ModelRenderer(this, 22, 24); + this.Lock_1.addBox(-1.0F, 12.0F, -8.0F, 2, 4, 1); + this.Lock_1.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lock_1.setTextureSize(128, 128); + this.Lock_1.mirror = true; + setRotation(this.Lock_1, 0.0F, 0.0F, 0.0F); + this.Lid_2 = new ModelRenderer(this, 48, 0); + this.Lid_2.addBox(-4.0F, 9.0F, -7.0F, 8, 5, 3); + this.Lid_2.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lid_2.setTextureSize(128, 128); + this.Lid_2.mirror = true; + setRotation(this.Lid_2, 0.0F, 1.570796F, 0.0F); + this.Body_2 = new ModelRenderer(this, 0, 24); + this.Body_2.addBox(-4.0F, 14.0F, -7.0F, 8, 10, 3); + this.Body_2.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Body_2.setTextureSize(128, 128); + this.Body_2.mirror = true; + setRotation(this.Body_2, 0.0F, 1.570796F, 0.0F); + this.Lock_2 = new ModelRenderer(this, 22, 24); + this.Lock_2.addBox(-1.0F, 12.0F, -8.0F, 2, 4, 1); + this.Lock_2.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lock_2.setTextureSize(128, 128); + this.Lock_2.mirror = true; + setRotation(this.Lock_2, 0.0F, 1.570796F, 0.0F); + this.Body = new ModelRenderer(this, 16, 0); + this.Body.addBox(-4.0F, 8.0F, -4.0F, 8, 16, 8); + this.Body.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Body.setTextureSize(128, 128); + this.Body.mirror = true; + setRotation(this.Body, 0.0F, 0.0F, 0.0F); + this.Lid_3 = new ModelRenderer(this, 48, 0); + this.Lid_3.addBox(-4.0F, 9.0F, -7.0F, 8, 5, 3); + this.Lid_3.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lid_3.setTextureSize(128, 128); + this.Lid_3.mirror = true; + setRotation(this.Lid_3, 0.0F, 3.141593F, 0.0F); + this.Body_3 = new ModelRenderer(this, 0, 24); + this.Body_3.addBox(-4.0F, 14.0F, -7.0F, 8, 10, 3); + this.Body_3.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Body_3.setTextureSize(128, 128); + this.Body_3.mirror = true; + setRotation(this.Body_3, 0.0F, 3.141593F, 0.0F); + this.Lock_3 = new ModelRenderer(this, 22, 24); + this.Lock_3.addBox(-1.0F, 12.0F, -8.0F, 2, 4, 1); + this.Lock_3.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lock_3.setTextureSize(128, 128); + this.Lock_3.mirror = true; + setRotation(this.Lock_3, 0.0F, 3.141593F, 0.0F); + this.Lid_4 = new ModelRenderer(this, 48, 0); + this.Lid_4.addBox(-4.0F, 9.0F, -7.0F, 8, 5, 3); + this.Lid_4.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lid_4.setTextureSize(128, 128); + this.Lid_4.mirror = true; + setRotation(this.Lid_4, 0.0F, -1.570796F, 0.0F); + this.Body_4 = new ModelRenderer(this, 0, 24); + this.Body_4.addBox(-4.0F, 14.0F, -7.0F, 8, 10, 3); + this.Body_4.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Body_4.setTextureSize(128, 128); + this.Body_4.mirror = true; + setRotation(this.Body_4, 0.0F, -1.570796F, 0.0F); + this.Lock_4 = new ModelRenderer(this, 22, 24); + this.Lock_4.addBox(-1.0F, 12.0F, -8.0F, 2, 4, 1); + this.Lock_4.setRotationPoint(0.0F, 0.0F, 0.0F); + this.Lock_4.setTextureSize(128, 128); + this.Lock_4.mirror = true; + setRotation(this.Lock_4, 0.0F, -1.570796F, 0.0F); + } + + public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5) + { + super.render(entity, f, f1, f2, f3, f4, f5); + setRotationAngles(f, f1, f2, f3, f4, f5); + this.Column1.render(f5); + this.Column4.render(f5); + this.Column2.render(f5); + this.Column3.render(f5); + this.Lid_1.render(f5); + this.Body_1.render(f5); + this.Lock_1.render(f5); + this.Lid_2.render(f5); + this.Body_2.render(f5); + this.Lock_2.render(f5); + this.Body.render(f5); + this.Lid_3.render(f5); + this.Body_3.render(f5); + this.Lock_3.render(f5); + this.Lid_4.render(f5); + this.Body_4.render(f5); + this.Lock_4.render(f5); + } + + private void setRotation(ModelRenderer model, float x, float y, float z) + { + model.rotateAngleX = x; + model.rotateAngleY = y; + model.rotateAngleZ = z; + } + + public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5) + { + super.setRotationAngles(f, f1, f2, f3, f4, f5, null); + } +} diff --git a/src/Java/binnie/core/machines/storage/ModuleStorage.java b/src/Java/binnie/core/machines/storage/ModuleStorage.java new file mode 100644 index 0000000000..7f8bb38ac4 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/ModuleStorage.java @@ -0,0 +1,39 @@ +package binnie.core.machines.storage; + +import binnie.core.BinnieCore; +import binnie.core.IInitializable; +import binnie.core.machines.MachineGroup; +import cpw.mods.fml.common.registry.GameRegistry; +import java.util.ArrayList; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraftforge.oredict.OreDictionary; +import net.minecraftforge.oredict.ShapedOreRecipe; + +public class ModuleStorage + implements IInitializable +{ + public void preInit() + { + BinnieCore.packageCompartment = new MachineGroup(BinnieCore.instance, "storage", "storage", Compartment.values()); + BinnieCore.packageCompartment.setCreativeTab(CreativeTabs.tabBlock); + } + + public void init() {} + + public void postInit() + { + String ironGear = OreDictionary.getOres("gearIron").isEmpty() ? "ingotIron" : "gearIron"; + String goldGear = OreDictionary.getOres("gearGold").isEmpty() ? "ingotGold" : "gearGold"; + String diamondGear = "gemDiamond"; + + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.Compartment.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Items.book, Character.valueOf('c'), Blocks.chest, Character.valueOf('p'), Blocks.stone_button })); + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.CompartmentCopper.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Compartment.Compartment.get(1), Character.valueOf('c'), "gearCopper", Character.valueOf('p'), Blocks.stone_button })); + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.CompartmentBronze.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Compartment.CompartmentCopper.get(1), Character.valueOf('c'), "gearBronze", Character.valueOf('p'), Items.gold_nugget })); + + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.CompartmentIron.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Compartment.CompartmentCopper.get(1), Character.valueOf('c'), ironGear, Character.valueOf('p'), Items.gold_nugget })); + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.CompartmentGold.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Compartment.CompartmentIron.get(1), Character.valueOf('c'), goldGear, Character.valueOf('p'), Items.emerald })); + GameRegistry.addRecipe(new ShapedOreRecipe(Compartment.CompartmentDiamond.get(1), new Object[] { "pcp", "cbc", "pcp", Character.valueOf('b'), Compartment.CompartmentGold.get(1), Character.valueOf('c'), diamondGear, Character.valueOf('p'), Items.emerald })); + } +} diff --git a/src/Java/binnie/core/machines/storage/StandardCompartment.java b/src/Java/binnie/core/machines/storage/StandardCompartment.java new file mode 100644 index 0000000000..341657cb9e --- /dev/null +++ b/src/Java/binnie/core/machines/storage/StandardCompartment.java @@ -0,0 +1,142 @@ +package binnie.core.machines.storage; + +import binnie.core.gui.BinnieCoreGUI; +import binnie.core.machines.Machine; +import binnie.core.machines.TileEntityMachine; +import binnie.core.texture.BinnieCoreTexture; +import net.minecraft.tileentity.TileEntity; + +class StandardCompartment +{ + public static class PackageCompartment + extends Compartment.PackageCompartment + { + public PackageCompartment() + { + super(BinnieCoreTexture.Compartment); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 4, 25); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } + + public static class PackageCompartmentCopper + extends Compartment.PackageCompartment + { + public PackageCompartmentCopper() + { + super(BinnieCoreTexture.CompartmentCopper); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 6, 25); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } + + public static class PackageCompartmentBronze + extends Compartment.PackageCompartment + { + public PackageCompartmentBronze() + { + super(BinnieCoreTexture.CompartmentBronze); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 8, 25); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } + + public static class PackageCompartmentIron + extends Compartment.PackageCompartment + { + public PackageCompartmentIron() + { + super(BinnieCoreTexture.CompartmentIron); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 4, 50); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } + + public static class PackageCompartmentGold + extends Compartment.PackageCompartment + { + public PackageCompartmentGold() + { + super(BinnieCoreTexture.CompartmentGold); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 6, 50); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } + + public static class PackageCompartmentDiamond + extends Compartment.PackageCompartment + { + public PackageCompartmentDiamond() + { + super(BinnieCoreTexture.CompartmentDiamond); + } + + public void createMachine(Machine machine) + { + new ComponentBinnieCoreGUI(machine, BinnieCoreGUI.Compartment); + new ComponentCompartmentInventory(machine, 8, 50); + } + + public TileEntity createTileEntity() + { + return new TileEntityMachine(this); + } + + public void register() {} + } +} diff --git a/src/Java/binnie/core/machines/storage/WindowCompartment.java b/src/Java/binnie/core/machines/storage/WindowCompartment.java new file mode 100644 index 0000000000..6c28ed0714 --- /dev/null +++ b/src/Java/binnie/core/machines/storage/WindowCompartment.java @@ -0,0 +1,594 @@ +package binnie.core.machines.storage; + +import binnie.core.AbstractMod; +import binnie.core.BinnieCore; +import binnie.core.machines.IMachine; +import binnie.core.machines.Machine; +import binnie.core.machines.MachinePackage; +import binnie.core.machines.transfer.TransferRequest; +import binnie.craftgui.controls.ControlCheckbox; +import binnie.craftgui.controls.ControlText; +import binnie.craftgui.controls.ControlTextEdit; +import binnie.craftgui.controls.button.ControlButton; +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.controls.page.ControlPage; +import binnie.craftgui.controls.page.ControlPages; +import binnie.craftgui.controls.scroll.ControlScrollableContent; +import binnie.craftgui.controls.tab.ControlTab; +import binnie.craftgui.controls.tab.ControlTabBar; +import binnie.craftgui.core.Attribute; +import binnie.craftgui.core.CraftGUI; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.geometry.CraftGUIUtil; +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IBorder; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.geometry.Position; +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.events.EventHandler.Origin; +import binnie.craftgui.events.EventMouse.Down; +import binnie.craftgui.events.EventMouse.Down.Handler; +import binnie.craftgui.events.EventTextEdit; +import binnie.craftgui.events.EventTextEdit.Handler; +import binnie.craftgui.events.EventValueChanged; +import binnie.craftgui.events.EventValueChanged.Handler; +import binnie.craftgui.genetics.machine.WindowMachine; +import binnie.craftgui.minecraft.Dialog; +import binnie.craftgui.minecraft.EnumColor; +import binnie.craftgui.minecraft.IWindowAffectsShiftClick; +import binnie.craftgui.minecraft.MinecraftGUI.PanelType; +import binnie.craftgui.minecraft.Window; +import binnie.craftgui.minecraft.control.ControlItemDisplay; +import binnie.craftgui.minecraft.control.ControlPlayerInventory; +import binnie.craftgui.minecraft.control.ControlSlide; +import binnie.craftgui.minecraft.control.ControlSlot; +import binnie.craftgui.minecraft.control.ControlSlotArray; +import binnie.craftgui.minecraft.control.ControlTabIcon; +import binnie.craftgui.resource.Texture; +import binnie.craftgui.resource.minecraft.CraftGUITexture; +import binnie.craftgui.window.Panel; +import binnie.craftgui.window.Panel.IPanelType; +import cpw.mods.fml.relauncher.Side; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public class WindowCompartment + extends WindowMachine + implements IWindowAffectsShiftClick +{ + public static Window create(EntityPlayer player, IInventory inventory, Side side) + { + return new WindowCompartment(player, inventory, side); + } + + public WindowCompartment(EntityPlayer player, IInventory inventory, Side side) + { + super(320, 226, player, inventory, side); + } + + private final Map<Panel, Integer> panels = new HashMap(); + private ControlTextEdit tabName; + private ControlItemDisplay tabIcon; + private ControlColourSelector tabColour; + boolean dueUpdate; + + public void initialiseClient() + { + setTitle(Machine.getMachine(getInventory()).getPackage().getDisplayName()); + + int x = 16; + int y = 32; + + ComponentCompartmentInventory inv = (ComponentCompartmentInventory)Machine.getMachine(getInventory()).getInterface(ComponentCompartmentInventory.class); + + + + Integer[] tabs1 = new Integer[0]; + Integer[] tabs2 = new Integer[0]; + if (inv.getTabNumber() == 4) + { + tabs1 = new Integer[] { Integer.valueOf(0), Integer.valueOf(1) }; + tabs2 = new Integer[] { Integer.valueOf(2), Integer.valueOf(3) }; + } + if (inv.getTabNumber() == 6) + { + tabs1 = new Integer[] { Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(2) }; + tabs2 = new Integer[] { Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) }; + } + if (inv.getTabNumber() == 8) + { + tabs1 = new Integer[] { Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }; + tabs2 = new Integer[] { Integer.valueOf(4), Integer.valueOf(5), Integer.valueOf(6), Integer.valueOf(7) }; + } + boolean doubleTabbed = tabs2.length > 0; + + int compartmentPageWidth = 16 + 18 * inv.getPageSize() / 5; + int compartmentPageHeight = 106; + int compartmentWidth = compartmentPageWidth + (doubleTabbed ? 48 : 24); + int compartmentHeight = compartmentPageHeight; + + + + Control controlCompartment = new Control(this, x, y, compartmentWidth, compartmentHeight); + + final ControlTabBar<Integer> tab = new ControlTabBar(controlCompartment, 0.0F, 0.0F, 24.0F, compartmentPageHeight, Position.Left) + { + public ControlTab<Integer> createTab(float x, float y, float w, float h, Integer value) + { + new ControlTabIcon(this, x, y, w, h, value) + { + public ItemStack getItemStack() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getIcon(); + } + + public String getName() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getName(); + } + + public int getOutlineColour() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getColor().getColour(); + } + + public boolean hasOutline() + { + return true; + } + }; + } + }; + String[] tabHelp = { "Compartment Tab", "Tabs that divide the inventory into sections. Each one can be labelled seperately." }; + + tab.addHelp(tabHelp); + + tab.setValues(Arrays.asList(tabs1)); + tab.setValue(Integer.valueOf(0)); + + tab.addEventHandler(new EventValueChanged.Handler() + { + public void onEvent(EventValueChanged event) + { + NBTTagCompound nbt = new NBTTagCompound(); + int i = ((Integer)event.getValue()).intValue(); + nbt.setByte("i", (byte)i); + Window.get(tab).sendClientAction("tab-change", nbt); + WindowCompartment.this.currentTab = i; + } + }.setOrigin(EventHandler.Origin.DirectChild, tab)); + + + + + x += 24; + + ControlPages<Integer> compartmentPages = new ControlPages(controlCompartment, 24.0F, 0.0F, compartmentPageWidth, compartmentPageHeight); + + ControlPage[] page = new ControlPage[inv.getTabNumber()]; + for (int p = 0; p < inv.getTabNumber(); p++) { + page[p] = new ControlPage(compartmentPages, Integer.valueOf(p)); + } + CraftGUIUtil.linkWidgets(tab, compartmentPages); + + int i = 0; + for (int p = 0; p < inv.getTabNumber(); p++) + { + ControlPage thisPage = page[p]; + + Panel panel = new Panel(thisPage, 0.0F, 0.0F, thisPage.w(), thisPage.h(), MinecraftGUI.PanelType.Black) + { + public void onRenderForeground() + { + Texture iTexture = CraftGUI.Render.getTexture(CraftGUITexture.TabOutline); + + CraftGUI.Render.colour(WindowCompartment.this.getTab(((Integer)WindowCompartment.this.panels.get(this)).intValue()).getColor().getColour()); + + CraftGUI.Render.texture(iTexture, getArea().inset(3)); + } + }; + this.panels.put(panel, Integer.valueOf(p)); + + int[] slotsIDs = new int[inv.getPageSize()]; + for (int k = 0; k < inv.getPageSize(); k++) { + slotsIDs[k] = (i++); + } + new ControlSlotArray(thisPage, 8, 8, inv.getPageSize() / 5, 5).create(slotsIDs); + } + x += compartmentPageWidth; + if (tabs2.length > 0) + { + ControlTabBar<Integer> tab2 = new ControlTabBar(controlCompartment, 24 + compartmentPageWidth, 0.0F, 24.0F, compartmentPageHeight, Position.Right) + { + public ControlTab<Integer> createTab(float x, float y, float w, float h, Integer value) + { + new ControlTabIcon(this, x, y, w, h, value) + { + public ItemStack getItemStack() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getIcon(); + } + + public String getName() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getName(); + } + + public int getOutlineColour() + { + return WindowCompartment.this.getTab(((Integer)this.value).intValue()).getColor().getColour(); + } + + public boolean hasOutline() + { + return true; + } + }; + } + }; + tab2.setValues(Arrays.asList(tabs2)); + tab2.setValue(Integer.valueOf(0)); + tab2.addHelp(tabHelp); + + tab2.addEventHandler(new EventValueChanged.Handler() + { + public void onEvent(EventValueChanged event) + { + NBTTagCompound nbt = new NBTTagCompound(); + int i = ((Integer)event.getValue()).intValue(); + nbt.setByte("i", (byte)i); + Window.get(tab).sendClientAction("tab-change", nbt); + WindowCompartment.this.currentTab = i; + } + }.setOrigin(EventHandler.Origin.DirectChild, tab2)); + + + + + CraftGUIUtil.linkWidgets(tab2, compartmentPages); + + x += 24; + } + x += 16; + + setSize(new IPoint(Math.max(32 + compartmentWidth, 252), h())); + + controlCompartment.setPosition(new IPoint((w() - controlCompartment.w()) / 2.0F, controlCompartment.y())); + + + + ControlPlayerInventory invent = new ControlPlayerInventory(this, true); + + ControlSlide slide = new ControlSlide(this, 0.0F, 134.0F, 136.0F, 92.0F, Position.Left); + slide.setLabel("Tab Properties"); + slide.setSlide(false); + + slide.addHelp("Tab Properties"); + slide.addHelp("The label, colour and icon of the Tab can be altered here. Clicking on the icon with a held item will change it."); + + Panel tabPropertyPanel = new Panel(slide, 16.0F, 8.0F, 112.0F, 76.0F, MinecraftGUI.PanelType.Gray); + + int y2 = 4; + + new ControlText(tabPropertyPanel, new IPoint(4.0F, y2), "Tab Name:"); + y2 += 12;this.tabName = new ControlTextEdit(tabPropertyPanel, 4.0F, y2, 104.0F, 12.0F); + + this.tabName.addSelfEventHandler(new EventTextEdit.Handler() + { + public void onEvent(EventTextEdit event) + { + CompartmentTab tab = WindowCompartment.this.getCurrentTab(); + tab.setName((String)event.getValue()); + NBTTagCompound nbt = new NBTTagCompound(); + tab.writeToNBT(nbt); + WindowCompartment.this.sendClientAction("comp-change-tab", nbt); + } + }.setOrigin(EventHandler.Origin.Self, this.tabName)); + + + + + y2 += 20; + + new ControlText(tabPropertyPanel, new IPoint(4.0F, y2), "Tab Icon: "); + this.tabIcon = new ControlItemDisplay(tabPropertyPanel, 58.0F, y2 - 4); + this.tabIcon.setItemStack(new ItemStack(Items.paper)); + this.tabIcon.addAttribute(Attribute.MouseOver); + this.tabIcon.addSelfEventHandler(new EventMouse.Down.Handler() + { + public void onEvent(EventMouse.Down event) + { + if (WindowCompartment.this.getHeldItemStack() == null) { + return; + } + CompartmentTab tab = WindowCompartment.this.getCurrentTab(); + ItemStack stack = WindowCompartment.this.getHeldItemStack().copy(); + stack.stackSize = 1; + tab.setIcon(stack); + NBTTagCompound nbt = new NBTTagCompound(); + tab.writeToNBT(nbt); + WindowCompartment.this.sendClientAction("comp-change-tab", nbt); + } + }); + this.tabColour = new ControlColourSelector(tabPropertyPanel, 82.0F, y2 - 4, 16.0F, 16.0F, EnumColor.White); + + this.tabIcon.addHelp("Icon for Current Tab"); + this.tabIcon.addHelp("Click here with an item to change"); + + y2 += 20; + + new ControlText(tabPropertyPanel, new IPoint(4.0F, y2), "Colour: "); + + int cw = 8; + + Panel panelColour = new Panel(tabPropertyPanel, 40.0F, y2 - 4, cw * 8 + 2, cw * 2 + 1, MinecraftGUI.PanelType.Gray); + for (int cc = 0; cc < 16; cc++) + { + final ControlColourSelector color = new ControlColourSelector(panelColour, 1 + cw * (cc % 8), 1 + cw * (cc / 8), cw, cw, EnumColor.values()[cc]); + color.addSelfEventHandler(new EventMouse.Down.Handler() + { + public void onEvent(EventMouse.Down event) + { + CompartmentTab tab = WindowCompartment.this.getCurrentTab(); + tab.setColor(color.getValue()); + NBTTagCompound nbt = new NBTTagCompound(); + tab.writeToNBT(nbt); + WindowCompartment.this.sendClientAction("comp-change-tab", nbt); + } + }); + color.addHelp("Colour Selector"); + color.addHelp("Select a colour to highlight the current tab"); + } + y2 += 20; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ControlButton searchButton = new ControlButton(controlCompartment, compartmentWidth - 24 - 64 - 8, compartmentPageHeight, 64.0F, 16.0F, "Search") + { + protected void onMouseClick(EventMouse.Down event) + { + WindowCompartment.this.createSearchDialog(); + } + + public void onRenderBackground() + { + Object texture = isMouseOver() ? CraftGUITexture.TabHighlighted : CraftGUITexture.Tab; + CraftGUI.Render.texture(CraftGUI.Render.getTexture(texture).crop(Position.Bottom, 8.0F), getArea()); + } + }; + searchButton.addHelp("Search Button"); + searchButton.addHelp("Clicking this will open the Search dialog. This allows you to search the inventory for specific items."); + } + + public void createSearchDialog() + { + new Dialog(this, 252.0F, 192.0F) + { + Control slotGrid; + String textSearch = ""; + boolean sortByName = false; + boolean includeItems = true; + boolean includeBlocks = true; + + public void onClose() {} + + public void initialise() + { + ControlScrollableContent<IWidget> scroll = new ControlScrollableContent(this, 124.0F, 16.0F, 116.0F, 92.0F, 6.0F) + { + public void onRenderBackground() + { + CraftGUI.Render.colour(11184810); + CraftGUI.Render.texture(CraftGUITexture.Outline, getArea().inset(new IBorder(0.0F, 6.0F, 0.0F, 0.0F))); + } + }; + this.slotGrid = new Control(scroll, 1.0F, 1.0F, 108.0F, 18.0F); + + scroll.setScrollableContent(this.slotGrid); + + new ControlPlayerInventory(this, true); + + new ControlTextEdit(this, 16.0F, 16.0F, 100.0F, 14.0F).addEventHandler(new EventTextEdit.Handler() + { + public void onEvent(EventTextEdit event) + { + WindowCompartment.10.this.textSearch = ((String)event.value); + WindowCompartment.10.this.updateSearch(); + } + }); + this.includeItems = true; + this.includeBlocks = true; + + new ControlCheckbox(this, 16.0F, 40.0F, 100.0F, "Sort A-Z", this.sortByName) + { + protected void onValueChanged(boolean value) + { + WindowCompartment.10.this.sortByName = value; + WindowCompartment.10.this.updateSearch(); + } + }; + new ControlCheckbox(this, 16.0F, 64.0F, 100.0F, "Include Items", this.includeItems) + { + protected void onValueChanged(boolean value) + { + WindowCompartment.10.this.includeItems = value; + WindowCompartment.10.this.updateSearch(); + } + }; + new ControlCheckbox(this, 16.0F, 88.0F, 100.0F, "Include Blocks", this.includeBlocks) + { + protected void onValueChanged(boolean value) + { + WindowCompartment.10.this.includeBlocks = value; + WindowCompartment.10.this.updateSearch(); + } + }; + updateSearch(); + } + + private void updateSearch() + { + Map<Integer, String> slotIds = new HashMap(); + IInventory inv = WindowCompartment.this.getInventory(); + for (int i = 0; i < inv.getSizeInventory(); i++) + { + ItemStack stack = inv.getStackInSlot(i); + if (stack != null) + { + String name = stack.getDisplayName().toLowerCase(); + if ((this.textSearch == null) || (name.contains(this.textSearch))) { + if ((this.includeBlocks) || (Block.getBlockFromItem(stack.getItem()) == Blocks.air)) { + if ((this.includeItems) || (Block.getBlockFromItem(stack.getItem()) != Blocks.air)) { + slotIds.put(Integer.valueOf(i), name); + } + } + } + } + } + if (this.sortByName) + { + List list = new LinkedList(slotIds.entrySet()); + Collections.sort(list, new Comparator() + { + public int compare(Object o1, Object o2) + { + return -((Comparable)((Map.Entry)o2).getValue()).compareTo(((Map.Entry)o1).getValue()); + } + }); + Map result = new LinkedHashMap(); + for (Iterator it = list.iterator(); it.hasNext();) + { + Map.Entry entry = (Map.Entry)it.next(); + result.put(entry.getKey(), entry.getValue()); + } + slotIds = result; + } + int y = 0; + int x = 0; + int width = 108; + int height = 2 + 18 * (1 + (slotIds.size() - 1) / 6); + this.slotGrid.deleteAllChildren(); + this.slotGrid.setSize(new IPoint(width, height)); + for (Iterator i$ = slotIds.keySet().iterator(); i$.hasNext();) + { + int k = ((Integer)i$.next()).intValue(); + new ControlSlot(this.slotGrid, x, y).assign(k); + x += 18; + if (x >= 108) + { + x = 0; + y += 18; + } + } + while ((y < 108) || (x != 0)) + { + new ControlSlot(this.slotGrid, x, y); + x += 18; + if (x >= 108) + { + x = 0; + y += 18; + } + } + } + }; + } + + public void onUpdateClient() + { + super.onUpdateClient(); + updateTabs(); + } + + public void updateTabs() + { + this.tabName.setValue(getCurrentTab().getName()); + this.tabIcon.setItemStack(getCurrentTab().getIcon()); + this.tabColour.setValue(getCurrentTab().getColor()); + } + + private int currentTab = 0; + + public void recieveGuiNBT(Side side, EntityPlayer player, String name, NBTTagCompound action) + { + super.recieveGuiNBT(side, player, name, action); + if (name.equals("tab-change")) { + this.currentTab = action.getByte("i"); + } + } + + public String getTitle() + { + return "Compartment"; + } + + protected AbstractMod getMod() + { + return BinnieCore.instance; + } + + protected String getName() + { + return "compartment"; + } + + public void alterRequest(TransferRequest request) + { + if (request.getDestination() == getInventory()) + { + ComponentCompartmentInventory inv = (ComponentCompartmentInventory)Machine.getMachine(getInventory()).getInterface(ComponentCompartmentInventory.class); + request.setTargetSlots(inv.getSlotsForTab(this.currentTab)); + } + } + + public CompartmentTab getTab(int i) + { + return ((ComponentCompartmentInventory)Machine.getInterface(ComponentCompartmentInventory.class, getInventory())).getTab(i); + } + + public CompartmentTab getCurrentTab() + { + return getTab(this.currentTab); + } +} diff --git a/src/Java/binnie/core/machines/storage/WindowTest.java b/src/Java/binnie/core/machines/storage/WindowTest.java new file mode 100644 index 0000000000..55c2a0b9db --- /dev/null +++ b/src/Java/binnie/core/machines/storage/WindowTest.java @@ -0,0 +1,40 @@ +package binnie.core.machines.storage; + +import binnie.core.AbstractMod; +import binnie.core.BinnieCore; +import binnie.craftgui.genetics.machine.WindowMachine; +import binnie.craftgui.minecraft.Window; +import cpw.mods.fml.relauncher.Side; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; + +public class WindowTest + extends WindowMachine +{ + public static Window create(EntityPlayer player, IInventory inventory, Side side) + { + return new WindowCompartment(player, inventory, side); + } + + public WindowTest(EntityPlayer player, IInventory inventory, Side side) + { + super(320, 240, player, inventory, side); + } + + public void initialiseClient() {} + + public String getTitle() + { + return "Test"; + } + + protected AbstractMod getMod() + { + return BinnieCore.instance; + } + + protected String getName() + { + return "Test"; + } +} diff --git a/src/Java/binnie/core/machines/transfer/TransferHandler.java b/src/Java/binnie/core/machines/transfer/TransferHandler.java new file mode 100644 index 0000000000..8d8c2bdb57 --- /dev/null +++ b/src/Java/binnie/core/machines/transfer/TransferHandler.java @@ -0,0 +1,3 @@ +package binnie.core.machines.transfer; + +public class TransferHandler {} diff --git a/src/Java/binnie/core/machines/transfer/TransferRequest.java b/src/Java/binnie/core/machines/transfer/TransferRequest.java new file mode 100644 index 0000000000..8059a6b512 --- /dev/null +++ b/src/Java/binnie/core/machines/transfer/TransferRequest.java @@ -0,0 +1,419 @@ +package binnie.core.machines.transfer; + +import binnie.core.machines.Machine; +import binnie.core.machines.inventory.IInventorySlots; +import binnie.core.machines.inventory.IValidatedTankContainer; +import binnie.core.machines.inventory.InventorySlot; +import binnie.core.machines.power.ITankMachine; +import java.util.ArrayList; +import java.util.List; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidContainerRegistry; +import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidContainerItem; +import net.minecraftforge.fluids.IFluidTank; + +public class TransferRequest +{ + private ItemStack itemToTransfer = null; + private ItemStack returnItem = null; + private IInventory origin; + private IInventory destination; + private int[] targetSlots = new int[0]; + private int[] targetTanks = new int[0]; + private boolean transferLiquids = true; + private boolean ignoreReadOnly = false; + + public TransferRequest(ItemStack toTransfer, IInventory destination) + { + int[] target = new int[destination.getSizeInventory()]; + for (int i = 0; i < target.length; i++) { + target[i] = i; + } + int[] targetTanks = new int[0]; + if ((destination instanceof ITankMachine)) + { + targetTanks = new int[((ITankMachine)destination).getTanks().length]; + for (int i = 0; i < targetTanks.length; i++) { + targetTanks[i] = i; + } + } + if (toTransfer != null) + { + setItemToTransfer(toTransfer.copy()); + setReturnItem(toTransfer.copy()); + } + setOrigin(null); + setDestination(destination); + setTargetSlots(target); + setTargetTanks(targetTanks); + this.transferLiquids = true; + } + + private void setItemToTransfer(ItemStack itemToTransfer) + { + this.itemToTransfer = itemToTransfer; + } + + private void setReturnItem(ItemStack returnItem) + { + this.returnItem = returnItem; + } + + public TransferRequest setOrigin(IInventory origin) + { + this.origin = origin; + return this; + } + + private void setDestination(IInventory destination) + { + this.destination = destination; + } + + public TransferRequest setTargetSlots(int[] targetSlots) + { + this.targetSlots = targetSlots; + return this; + } + + public TransferRequest setTargetTanks(int[] targetTanks) + { + this.targetTanks = targetTanks; + return this; + } + + public TransferRequest ignoreValidation() + { + this.ignoreReadOnly = true; + return this; + } + + public ItemStack getReturnItem() + { + return this.returnItem; + } + + public ItemStack transfer(boolean doAdd) + { + ItemStack item = this.returnItem; + if ((item == null) || (this.destination == null)) { + return null; + } + if ((this.transferLiquids) && ((this.destination instanceof ITankMachine))) { + for (int tankID : this.targetTanks) + { + item = transferToTank(item, this.origin, (ITankMachine)this.destination, tankID, doAdd); + if (item != null) { + item = transferFromTank(item, this.origin, (ITankMachine)this.destination, tankID, doAdd); + } + } + } + if (item != null) { + for (int slot : this.targetSlots) { + if ((this.destination.isItemValidForSlot(slot, item)) || (this.ignoreReadOnly)) { + if ((!(this.destination instanceof IInventorySlots)) || (((IInventorySlots)this.destination).getSlot(slot) == null) || (!((IInventorySlots)this.destination).getSlot(slot).isRecipe())) { + if (this.destination.getStackInSlot(slot) != null) { + if (item.isStackable()) + { + ItemStack merged = this.destination.getStackInSlot(slot).copy(); + ItemStack[] newStacks = mergeStacks(item.copy(), merged.copy()); + item = newStacks[0]; + if (!areItemsEqual(merged, newStacks[1])) { + this.insertedSlots.add(new TransferSlot(slot, this.destination)); + } + if (doAdd) { + this.destination.setInventorySlotContents(slot, newStacks[1]); + } + if (item == null) { + return null; + } + } + } + } + } + } + } + if (item != null) { + for (int slot : this.targetSlots) { + if ((this.destination.isItemValidForSlot(slot, item)) || (this.ignoreReadOnly)) { + if ((!(this.destination instanceof IInventorySlots)) || (((IInventorySlots)this.destination).getSlot(slot) == null) || (!((IInventorySlots)this.destination).getSlot(slot).isRecipe())) { + if ((this.destination.getStackInSlot(slot) == null) && (item != null)) + { + this.insertedSlots.add(new TransferSlot(slot, this.destination)); + if (doAdd) { + this.destination.setInventorySlotContents(slot, item.copy()); + } + return null; + } + } + } + } + } + setReturnItem(item); + return getReturnItem(); + } + + private static boolean areItemsEqual(ItemStack merged, ItemStack itemstack) + { + return (ItemStack.areItemStackTagsEqual(itemstack, merged)) && (itemstack.isItemEqual(merged)); + } + + public static ItemStack[] mergeStacks(ItemStack itemstack, ItemStack merged) + { + if (areItemsEqual(itemstack, merged)) + { + int space = merged.getMaxStackSize() - merged.stackSize; + if (space > 0) { + if (itemstack.stackSize > space) + { + itemstack.stackSize -= space; + merged.stackSize += space; + } + else if (itemstack.stackSize <= space) + { + merged.stackSize += itemstack.stackSize; + itemstack = null; + } + } + } + return new ItemStack[] { itemstack, merged }; + } + + private ItemStack transferToTank(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + item = transferToTankUsingContainerData(item, origin, destination, tankID, doAdd); + item = transferToTankUsingFluidContainer(item, origin, destination, tankID, doAdd); + return item; + } + + private ItemStack transferToTankUsingFluidContainer(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if ((item == null) || (!(item.getItem() instanceof IFluidContainerItem))) { + return item; + } + IFluidContainerItem fluidContainer = (IFluidContainerItem)item.getItem(); + FluidStack fluid = fluidContainer.getFluid(item); + if (fluid == null) { + return item; + } + IFluidTank tank = destination.getTanks()[tankID]; + + IValidatedTankContainer validated = (IValidatedTankContainer)Machine.getInterface(IValidatedTankContainer.class, destination); + if ((validated != null) && ((!validated.isLiquidValidForTank(fluid, tankID)) || (validated.isTankReadOnly(tankID)))) { + return item; + } + int maxFill = tank.fill(fluid, false); + + FluidStack toTake = fluidContainer.drain(item, maxFill, true); + if (doAdd) { + tank.fill(toTake, true); + } + return item; + } + + private ItemStack transferToTankUsingContainerData(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if (item == null) { + return item; + } + FluidStack containerLiquid = null; + FluidContainerRegistry.FluidContainerData containerLiquidData = null; + for (FluidContainerRegistry.FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) { + if (data.filledContainer.isItemEqual(item)) + { + containerLiquidData = data; + containerLiquid = data.fluid.copy(); + break; + } + } + if (containerLiquid == null) { + return item; + } + IFluidTank tank = destination.getTanks()[tankID]; + + IValidatedTankContainer validated = (IValidatedTankContainer)Machine.getInterface(IValidatedTankContainer.class, destination); + if ((validated != null) && ( + (!validated.isLiquidValidForTank(containerLiquid, tankID)) || (validated.isTankReadOnly(tankID)))) { + return item; + } + FluidStack largeAmountOfLiquid = containerLiquid.copy(); + largeAmountOfLiquid.amount = tank.getCapacity(); + int amountAdded = tank.fill(largeAmountOfLiquid, false); + + int numberOfContainersToAdd = amountAdded / containerLiquid.amount; + if (numberOfContainersToAdd > item.stackSize) { + numberOfContainersToAdd = item.stackSize; + } + ItemStack leftOverContainers = item.copy(); + leftOverContainers.stackSize -= numberOfContainersToAdd; + if (leftOverContainers.stackSize <= 0) { + leftOverContainers = null; + } + ItemStack emptyContainers = containerLiquidData.emptyContainer.copy(); + emptyContainers.stackSize = 0; + emptyContainers.stackSize += numberOfContainersToAdd; + if (emptyContainers.stackSize <= 0) { + emptyContainers = null; + } + TransferRequest containersDump = new TransferRequest(emptyContainers, origin); + + ItemStack containersThatCantBeDumped = containersDump.transfer(false); + if (containersThatCantBeDumped != null) { + return item; + } + if (doAdd) + { + FluidStack liquidToFillTank = containerLiquid.copy(); + liquidToFillTank.amount *= numberOfContainersToAdd; + tank.fill(liquidToFillTank, true); + containersDump.transfer(true); + } + return leftOverContainers; + } + + private ItemStack transferFromTank(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + item = transferFromTankUsingContainerData(item, origin, destination, tankID, doAdd); + item = transferFromTankUsingFluidContainer(item, origin, destination, tankID, doAdd); + return item; + } + + private ItemStack transferFromTankUsingFluidContainer(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if ((item == null) || (!(item.getItem() instanceof IFluidContainerItem))) { + return item; + } + IFluidContainerItem fluidContainer = (IFluidContainerItem)item.getItem(); + + IFluidTank tank = destination.getTanks()[tankID]; + + FluidStack fluid = tank.getFluid(); + if (fluid == null) { + return item; + } + int amount = fluidContainer.fill(item, fluid, false); + + amount = Math.min(amount, tank.drain(amount, false) == null ? 0 : tank.drain(amount, false).amount); + if (amount <= 0) { + return item; + } + fluidContainer.fill(item, tank.drain(amount, doAdd), doAdd); + + return item; + } + + private ItemStack transferFromTankUsingContainerData(ItemStack item, IInventory origin, ITankMachine destination, int tankID, boolean doAdd) + { + if (item == null) { + return item; + } + IFluidTank tank = destination.getTanks()[tankID]; + FluidStack liquidInTank = tank.getFluid(); + if (liquidInTank == null) { + return item; + } + FluidContainerRegistry.FluidContainerData containerLiquidData = null; + for (FluidContainerRegistry.FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) { + if ((data.emptyContainer.isItemEqual(item)) && (liquidInTank.isFluidEqual(data.fluid))) + { + containerLiquidData = data; + break; + } + } + FluidStack fluid = null; + ItemStack filled = null; + if (containerLiquidData != null) + { + fluid = containerLiquidData.fluid; + filled = containerLiquidData.filledContainer; + } + if ((fluid == null) || (filled == null)) { + return item; + } + int maximumExtractedLiquid = item.stackSize * fluid.amount; + + FluidStack drainedLiquid = tank.drain(maximumExtractedLiquid, false); + int amountInTank = drainedLiquid == null ? 0 : drainedLiquid.amount; + + int numberOfContainersToFill = amountInTank / fluid.amount; + if (numberOfContainersToFill > item.stackSize) { + numberOfContainersToFill = item.stackSize; + } + ItemStack leftOverContainers = item.copy(); + leftOverContainers.stackSize -= numberOfContainersToFill; + if (leftOverContainers.stackSize <= 0) { + leftOverContainers = null; + } + ItemStack filledContainers = filled.copy(); + filledContainers.stackSize = 0; + filledContainers.stackSize += numberOfContainersToFill; + if (filledContainers.stackSize <= 0) { + filledContainers = null; + } + TransferRequest containersDump = new TransferRequest(filledContainers, origin); + + ItemStack containersThatCantBeDumped = containersDump.transfer(false); + if (containersThatCantBeDumped != null) { + return item; + } + if (doAdd) + { + tank.drain(maximumExtractedLiquid, true); + containersDump.transfer(true); + } + return leftOverContainers; + } + + private List<TransferSlot> insertedSlots = new ArrayList(); + private List<Integer> insertedTanks = new ArrayList(); + + public List<TransferSlot> getInsertedSlots() + { + return this.insertedSlots; + } + + public List<Integer> getInsertedTanks() + { + return this.insertedTanks; + } + + public static class TransferSlot + { + public int id; + public IInventory inventory; + + public TransferSlot(int id, IInventory inventory) + { + this.id = id; + this.inventory = inventory; + } + } + + public IInventory getOrigin() + { + return this.origin; + } + + public IInventory getDestination() + { + return this.destination; + } + + public ItemStack getItemToTransfer() + { + return this.itemToTransfer; + } + + public int[] getTargetSlots() + { + return this.targetSlots; + } + + public int[] getTargetTanks() + { + return this.targetTanks; + } +} |