From 58fc306bf29b002855c2397898d8caeec41c6d3d Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 10 Nov 2012 21:23:16 -0500 Subject: [PATCH] As agreed, liquid API from BuildCraft has been migrated to minecraft forge. There will be cleanups to follow, including sided handling and client rendering tweaks. Stay tuned! Tweak forgedirection for a couple of naming constants --- .../minecraftforge/common/ForgeDirection.java | 8 +- .../net/minecraftforge/liquids/ILiquid.java | 20 ++++ .../minecraftforge/liquids/ILiquidTank.java | 27 +++++ .../liquids/ITankContainer.java | 46 ++++++++ .../minecraftforge/liquids/LiquidData.java | 45 ++++++++ .../liquids/LiquidDictionary.java | 57 +++++++++ .../minecraftforge/liquids/LiquidManager.java | 64 ++++++++++ .../minecraftforge/liquids/LiquidStack.java | 109 ++++++++++++++++++ .../minecraftforge/liquids/LiquidTank.java | 100 ++++++++++++++++ eclipse/Forge-Client/.project | 2 +- 10 files changed, 473 insertions(+), 5 deletions(-) create mode 100644 common/net/minecraftforge/liquids/ILiquid.java create mode 100644 common/net/minecraftforge/liquids/ILiquidTank.java create mode 100644 common/net/minecraftforge/liquids/ITankContainer.java create mode 100644 common/net/minecraftforge/liquids/LiquidData.java create mode 100644 common/net/minecraftforge/liquids/LiquidDictionary.java create mode 100644 common/net/minecraftforge/liquids/LiquidManager.java create mode 100644 common/net/minecraftforge/liquids/LiquidStack.java create mode 100644 common/net/minecraftforge/liquids/LiquidTank.java diff --git a/common/net/minecraftforge/common/ForgeDirection.java b/common/net/minecraftforge/common/ForgeDirection.java index 8f07d6b060..948e7b676c 100644 --- a/common/net/minecraftforge/common/ForgeDirection.java +++ b/common/net/minecraftforge/common/ForgeDirection.java @@ -30,8 +30,8 @@ public enum ForgeDirection public final int offsetZ; public final int flag; public static final ForgeDirection[] VALID_DIRECTIONS = {DOWN, UP, NORTH, SOUTH, WEST, EAST}; - public static final int[] opposite = {1, 0, 3, 2, 5, 4, 6}; - public static final int[] toLeft = {0, 1, 5, 4, 3, 2, 6}; + public static final int[] OPPOSITES = {1, 0, 3, 2, 5, 4, 6}; + public static final int[] TO_LEFT = {0, 1, 5, 4, 3, 2, 6}; private ForgeDirection(int x, int y, int z) { @@ -52,11 +52,11 @@ public enum ForgeDirection public ForgeDirection getOpposite() { - return getOrientation(opposite[ordinal()]); + return getOrientation(OPPOSITES[ordinal()]); } public ForgeDirection getLeftRotation() { - return getOrientation(toLeft[ordinal()]); + return getOrientation(TO_LEFT[ordinal()]); } } diff --git a/common/net/minecraftforge/liquids/ILiquid.java b/common/net/minecraftforge/liquids/ILiquid.java new file mode 100644 index 0000000000..3d33f3d03b --- /dev/null +++ b/common/net/minecraftforge/liquids/ILiquid.java @@ -0,0 +1,20 @@ +/** + * Copyright (c) SpaceToad, 2011 + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ + +package net.minecraftforge.liquids; + +public interface ILiquid { + + public int stillLiquidId(); + + public boolean isMetaSensitive(); + + public int stillLiquidMeta(); + +} diff --git a/common/net/minecraftforge/liquids/ILiquidTank.java b/common/net/minecraftforge/liquids/ILiquidTank.java new file mode 100644 index 0000000000..562874c456 --- /dev/null +++ b/common/net/minecraftforge/liquids/ILiquidTank.java @@ -0,0 +1,27 @@ +package net.minecraftforge.liquids; + +public interface ILiquidTank { + + /** + * @return LiquidStack representing the liquid contained in the tank, null if empty. + */ + LiquidStack getLiquid(); + void setLiquid(LiquidStack liquid); + void setCapacity(int capacity); + int getCapacity(); + + /** + * + * @param resource + * @param doFill + * @return Amount of liquid used for filling. + */ + int fill(LiquidStack resource, boolean doFill); + /** + * + * @param maxDrain + * @param doDrain + * @return Null if nothing was drained, otherwise a LiquidStack containing the drained. + */ + LiquidStack drain(int maxDrain, boolean doDrain); +} diff --git a/common/net/minecraftforge/liquids/ITankContainer.java b/common/net/minecraftforge/liquids/ITankContainer.java new file mode 100644 index 0000000000..0f0d5b882e --- /dev/null +++ b/common/net/minecraftforge/liquids/ITankContainer.java @@ -0,0 +1,46 @@ +package net.minecraftforge.liquids; + +import net.minecraftforge.common.ForgeDirection; + +public interface ITankContainer { + + /** + * Fills liquid into internal tanks, distribution is left to the ITankContainer. + * @param from Orientation the liquid is pumped in from. + * @param resource LiquidStack representing the maximum amount of liquid filled into the ITankContainer + * @param doFill If false filling will only be simulated. + * @return Amount of resource that was filled into internal tanks. + */ + int fill(ForgeDirection from, LiquidStack resource, boolean doFill); + /** + * Fills liquid into the specified internal tank. + * @param from Orientation the liquid is pumped in from. + * @param resource LiquidStack representing the maximum amount of liquid filled into the ITankContainer + * @param doFill If false filling will only be simulated. + * @return Amount of resource that was filled into internal tanks. + */ + int fill(int tankIndex, LiquidStack resource, boolean doFill); + + /** + * Drains liquid out of internal tanks, distribution is left to the ITankContainer. + * @param from Orientation the liquid is drained to. + * @param maxDrain Maximum amount of liquid to drain. + * @param doDrain If false draining will only be simulated. + * @return LiquidStack representing the liquid and amount actually drained from the ITankContainer + */ + LiquidStack drain(ForgeDirection from, int maxDrain, boolean doDrain); + /** + * Drains liquid out of the specified internal tank. + * @param from Orientation the liquid is drained to. + * @param maxDrain Maximum amount of liquid to drain. + * @param doDrain If false draining will only be simulated. + * @return LiquidStack representing the liquid and amount actually drained from the ITankContainer + */ + LiquidStack drain(int tankIndex, int maxDrain, boolean doDrain); + + /** + * @return Array of {@link LiquidTank}s contained in this ITankContainer + */ + ILiquidTank[] getTanks(); + +} diff --git a/common/net/minecraftforge/liquids/LiquidData.java b/common/net/minecraftforge/liquids/LiquidData.java new file mode 100644 index 0000000000..feff1ec4c0 --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidData.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) SpaceToad, 2011 + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ + +package net.minecraftforge.liquids; + +import net.minecraft.src.Item; +import net.minecraft.src.ItemStack; + +public class LiquidData { + + public final LiquidStack stillLiquid; + public final LiquidStack movingLiquid; + + public final ItemStack filled; + public final ItemStack container; + + public LiquidData(int stillLiquidId, int movingLiquidId, Item filled) { + this(new LiquidStack(stillLiquidId, LiquidManager.BUCKET_VOLUME), new LiquidStack(movingLiquidId, LiquidManager.BUCKET_VOLUME), new ItemStack(filled, 1), new ItemStack(Item.bucketEmpty)); + } + + public LiquidData(int stillLiquidId, int movingLiquidId, ItemStack filled) { + this(new LiquidStack(stillLiquidId, LiquidManager.BUCKET_VOLUME), new LiquidStack(movingLiquidId, LiquidManager.BUCKET_VOLUME), filled, new ItemStack(Item.bucketEmpty)); + } + + public LiquidData(LiquidStack stillLiquid, ItemStack filled, ItemStack container) { + this(stillLiquid, stillLiquid, filled, container); + } + + public LiquidData(LiquidStack stillLiquid, LiquidStack movingLiquid, ItemStack filled, ItemStack container) { + this.stillLiquid = stillLiquid; + this.movingLiquid = movingLiquid; + this.filled = filled; + this.container = container; + + if(stillLiquid == null || filled == null || container == null) + throw new RuntimeException("stillLiquid, filled, or container is null, this is an error"); + } + +} diff --git a/common/net/minecraftforge/liquids/LiquidDictionary.java b/common/net/minecraftforge/liquids/LiquidDictionary.java new file mode 100644 index 0000000000..cac8e909e2 --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidDictionary.java @@ -0,0 +1,57 @@ +package net.minecraftforge.liquids; + +import java.util.HashMap; +import java.util.Map; + +/** + * When creating liquids you should register them with this class. + * + * @author CovertJaguar + */ +public abstract class LiquidDictionary +{ + + private static Map liquids = new HashMap(); + + /** + * When creating liquids you should call this function. + * + * Upon passing it a name and liquid item it will return either + * a preexisting implementation of that liquid or the liquid passed in. + * + * + * @param name the name of the liquid + * @param liquid the liquid to use if one doesn't exist + * @return + */ + public static LiquidStack getOrCreateLiquid(String name, LiquidStack liquid) + { + LiquidStack existing = liquids.get(name); + if(existing != null) { + return existing.copy(); + } + liquids.put(name, liquid.copy()); + return liquid; + } + + /** + * Returns the liquid matching the name, + * if such a liquid exists. + * + * Can return null. + * + * @param name the name of the liquid + * @param amount the amout of liquid + * @return + */ + public static LiquidStack getLiquid(String name, int amount) + { + LiquidStack liquid = liquids.get(name); + if(liquid == null) + return null; + + liquid = liquid.copy(); + liquid.amount = amount; + return liquid; + } +} diff --git a/common/net/minecraftforge/liquids/LiquidManager.java b/common/net/minecraftforge/liquids/LiquidManager.java new file mode 100644 index 0000000000..1805a50143 --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidManager.java @@ -0,0 +1,64 @@ +package net.minecraftforge.liquids; + +import java.util.LinkedList; + +import net.minecraft.src.ItemStack; + +public class LiquidManager { + + public static final int BUCKET_VOLUME = 1000; + public static LinkedList liquids = new LinkedList(); + + public static LiquidStack getLiquidForFilledItem(ItemStack filledItem) { + if (filledItem == null) + return null; + + for (LiquidData liquid : liquids) + if (liquid.filled.isItemEqual(filledItem)) + return liquid.stillLiquid; + + return null; + } + + public static int getLiquidIDForFilledItem(ItemStack filledItem) { + LiquidStack liquidForFilledItem = getLiquidForFilledItem(filledItem); + + if (liquidForFilledItem == null) + return 0; + + return liquidForFilledItem.itemID; + } + + public static ItemStack getFilledItemForLiquid(LiquidStack liquid) { + for (LiquidData data : liquids) + if(data.stillLiquid.isLiquidEqual(liquid)) + return data.filled.copy(); + + return null; + } + + public static ItemStack fillLiquidContainer(int liquidId, int quantity, ItemStack emptyContainer) { + return fillLiquidContainer(new LiquidStack(liquidId, quantity, 0), emptyContainer); + } + + public static ItemStack fillLiquidContainer(LiquidStack liquid, ItemStack emptyContainer) { + for(LiquidData data : liquids) + if(liquid.containsLiquid(data.stillLiquid) + && data.container.isItemEqual(emptyContainer)) + return data.filled.copy(); + return null; + } + + public static boolean isLiquid(ItemStack block) { + if (block.itemID == 0) + return false; + + for (LiquidData liquid : liquids) + if (liquid.stillLiquid.isLiquidEqual(block) || liquid.movingLiquid.isLiquidEqual(block)) + return true; + + return false; + } + + +} diff --git a/common/net/minecraftforge/liquids/LiquidStack.java b/common/net/minecraftforge/liquids/LiquidStack.java new file mode 100644 index 0000000000..08a02c4922 --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidStack.java @@ -0,0 +1,109 @@ +package net.minecraftforge.liquids; + +import net.minecraft.src.Block; +import net.minecraft.src.Item; +import net.minecraft.src.ItemStack; +import net.minecraft.src.NBTTagCompound; + +/** + * ItemStack substitute for liquids + * @author SirSengir + */ +public class LiquidStack { + public int itemID; + public int amount; + public int itemMeta; + + private LiquidStack() { + } + + public LiquidStack(int itemID, int amount) { + this(itemID, amount, 0); + } + + public LiquidStack(Item item, int amount) { + this(item.shiftedIndex, amount, 0); + } + + public LiquidStack(Block block, int amount) { + this(block.blockID, amount, 0); + } + + public LiquidStack(int itemID, int amount, int itemDamage) { + this.itemID = itemID; + this.amount = amount; + this.itemMeta = itemDamage; + } + + public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) { + nbttagcompound.setShort("Id", (short) itemID); + nbttagcompound.setInteger("Amount", amount); + nbttagcompound.setShort("Meta", (short) itemMeta); + return nbttagcompound; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) { + itemID = nbttagcompound.getShort("Id"); + amount = nbttagcompound.getInteger("Amount"); + itemMeta = nbttagcompound.getShort("Meta"); + } + + /** + * @return A copy of this LiquidStack + */ + public LiquidStack copy() { + return new LiquidStack(itemID, amount, itemMeta); + } + + /** + * @param other + * @return true if this LiquidStack contains the same liquid as the one passed in. + */ + public boolean isLiquidEqual(LiquidStack other) { + if(other == null) + return false; + + return itemID == other.itemID && itemMeta == other.itemMeta; + } + + /** + * @param other + * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount). + */ + public boolean containsLiquid(LiquidStack other) { + if(!isLiquidEqual(other)) + return false; + + return amount >= other.amount; + } + + /** + * @param other ItemStack containing liquids. + * @return true if this LiquidStack contains the same liquid as the one passed in. + */ + public boolean isLiquidEqual(ItemStack other) { + if(other == null) + return false; + + return itemID == other.itemID && itemMeta == other.getItemDamage(); + } + + /** + * @return ItemStack representation of this LiquidStack + */ + public ItemStack asItemStack() { + return new ItemStack(itemID, 1, itemMeta); + } + + /** + * Reads a liquid stack from the passed nbttagcompound and returns it. + * + * @param nbttagcompound + * @return + */ + public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbttagcompound) { + LiquidStack liquidstack = new LiquidStack(); + liquidstack.readFromNBT(nbttagcompound); + return liquidstack.itemID == 0 ? null : liquidstack; + } +} diff --git a/common/net/minecraftforge/liquids/LiquidTank.java b/common/net/minecraftforge/liquids/LiquidTank.java new file mode 100644 index 0000000000..97e9c4ea8f --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidTank.java @@ -0,0 +1,100 @@ +package net.minecraftforge.liquids; + +/** + * Reference implementation of ILiquidTank. Use this or implement your own. + */ +public class LiquidTank implements ILiquidTank { + private LiquidStack liquid; + private int capacity; + + public LiquidTank(int capacity) { + this(null, capacity); + } + + public LiquidTank(int liquidId, int quantity, int capacity) { + this(new LiquidStack(liquidId, quantity), capacity); + } + public LiquidTank(LiquidStack liquid, int capacity) { + this.liquid = liquid; + this.capacity = capacity; + } + + @Override + public LiquidStack getLiquid() { + return this.liquid; + } + + @Override + public void setLiquid(LiquidStack liquid) { + this.liquid = liquid; + } + + @Override + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + @Override + public int getCapacity() { + return this.capacity; + } + + @Override + public int fill(LiquidStack resource, boolean doFill) { + if(resource == null || resource.itemID <= 0) + return 0; + + if(liquid == null || liquid.itemID <= 0) { + if(resource.amount <= capacity) { + if(doFill) + this.liquid = resource.copy(); + return resource.amount; + } else { + if(doFill) { + this.liquid = resource.copy(); + this.liquid.amount = capacity; + } + return capacity; + } + } + + if(!liquid.isLiquidEqual(resource)) + return 0; + + int space = capacity - liquid.amount; + if(resource.amount <= space) { + if(doFill) + this.liquid.amount += resource.amount; + return resource.amount; + } else { + + if(doFill) + this.liquid.amount = capacity; + return space; + } + + } + @Override + public LiquidStack drain(int maxDrain, boolean doDrain) { + if(liquid == null || liquid.itemID <= 0) + return null; + if(liquid.amount <= 0) + return null; + + int used = maxDrain; + if(liquid.amount < used) + used = liquid.amount; + + if(doDrain) { + liquid.amount -= used; + } + + LiquidStack drained = new LiquidStack(liquid.itemID, used, liquid.itemMeta); + + // Reset liquid if emptied + if(liquid.amount <= 0) + liquid = null; + + return drained; + } +} diff --git a/eclipse/Forge-Client/.project b/eclipse/Forge-Client/.project index c182e8a8f6..a499d093b3 100644 --- a/eclipse/Forge-Client/.project +++ b/eclipse/Forge-Client/.project @@ -78,7 +78,7 @@ MCP_LOC - $%7BPARENT-2-WORKSPACE_LOC%7D + $%7BPARENT-3-PROJECT_LOC%7D