From ca16b4b1bd60ab03580784c7116d7959ea53251c Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 16 Nov 2012 01:28:29 -0500 Subject: [PATCH] Some liquid events. Non-API breaking. Add them at your leisure. This helps liquids know what's happening to them. In case they're volatile or something ;) --- .../minecraftforge/liquids/LiquidEvent.java | 87 +++++++++++++++++++ .../minecraftforge/liquids/LiquidTank.java | 19 ++++ 2 files changed, 106 insertions(+) create mode 100644 common/net/minecraftforge/liquids/LiquidEvent.java diff --git a/common/net/minecraftforge/liquids/LiquidEvent.java b/common/net/minecraftforge/liquids/LiquidEvent.java new file mode 100644 index 000000000..cb517d4a4 --- /dev/null +++ b/common/net/minecraftforge/liquids/LiquidEvent.java @@ -0,0 +1,87 @@ +package net.minecraftforge.liquids; + +import net.minecraft.src.TileEntity; +import net.minecraft.src.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.Event; + +public class LiquidEvent extends Event { + public final LiquidStack liquid; + public final int x; + public final int y; + public final int z; + public final World world; + + public LiquidEvent(LiquidStack liquid, World world, int x, int y, int z) + { + this.liquid = liquid; + this.world = world; + this.x = x; + this.y = y; + this.z = z; + } + + /** + * Mods should fire this event when they move liquids around (pipe networks etc) + * + * @author cpw + * + */ + public static class LiquidMotionEvent extends LiquidEvent { + public LiquidMotionEvent(LiquidStack liquid, World world, int x, int y, int z) { + super(liquid, world, x, y, z); + } + } + + /** + * Mods should fire this event when a liquid is {@link ILiquidTank#fill(LiquidStack, boolean)} their tank implementation. + * {@link LiquidTank} does. + * + * @author cpw + * + */ + public static class LiquidFillingEvent extends LiquidEvent { + public final ILiquidTank tank; + + public LiquidFillingEvent(LiquidStack liquid, World world, int x, int y, int z, ILiquidTank tank) { + super(liquid, world, x, y, z); + this.tank = tank; + } + } + + /** + * Mods should fire this event when a liquid is {@link ILiquidTank#drain(int, boolean)} from their tank. + * @author cpw + * + */ + public static class LiquidDrainingEvent extends LiquidEvent { + public final ILiquidTank tank; + + public LiquidDrainingEvent(LiquidStack liquid, World world, int x, int y, int z, ILiquidTank tank) { + super(liquid, world, x, y, z); + this.tank = tank; + } + } + + + /** + * Mods should fire this event when a liquid "spills", for example, if a block containing liquid is broken. + * + * @author cpw + * + */ + public static class LiquidSpilledEvent extends LiquidEvent { + public LiquidSpilledEvent(LiquidStack liquid, World world, int x, int y, int z) { + super(liquid, world, x, y, z); + } + } + + /** + * A handy shortcut for firing the various liquid events + * + * @param event + */ + public static final void fireEvent(LiquidEvent event) { + MinecraftForge.EVENT_BUS.post(event); + } +} diff --git a/common/net/minecraftforge/liquids/LiquidTank.java b/common/net/minecraftforge/liquids/LiquidTank.java index cd450c953..fe624f716 100644 --- a/common/net/minecraftforge/liquids/LiquidTank.java +++ b/common/net/minecraftforge/liquids/LiquidTank.java @@ -1,5 +1,7 @@ package net.minecraftforge.liquids; +import net.minecraft.src.TileEntity; + /** * Reference implementation of ILiquidTank. Use this or implement your own. */ @@ -7,6 +9,7 @@ public class LiquidTank implements ILiquidTank { private LiquidStack liquid; private int capacity; private int tankPressure; + private TileEntity tile; public LiquidTank(int capacity) { this(null, capacity); @@ -15,11 +18,22 @@ public class LiquidTank implements ILiquidTank { public LiquidTank(int liquidId, int quantity, int capacity) { this(new LiquidStack(liquidId, quantity), capacity); } + + public LiquidTank(int liquidId, int quantity, int capacity, TileEntity tile) { + this(liquidId, quantity, capacity); + this.tile = tile; + } + public LiquidTank(LiquidStack liquid, int capacity) { this.liquid = liquid; this.capacity = capacity; } + public LiquidTank(LiquidStack liquid, int capacity, TileEntity tile) + { + this(liquid, capacity); + this.tile = tile; + } @Override public LiquidStack getLiquid() { return this.liquid; @@ -54,6 +68,8 @@ public class LiquidTank implements ILiquidTank { if(doFill) { this.liquid = resource.copy(); this.liquid.amount = capacity; + if (tile!=null) + LiquidEvent.fireEvent(new LiquidEvent.LiquidFillingEvent(liquid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this)); } return capacity; } @@ -96,6 +112,9 @@ public class LiquidTank implements ILiquidTank { if(liquid.amount <= 0) liquid = null; + if (doDrain && tile!=null) + LiquidEvent.fireEvent(new LiquidEvent.LiquidDrainingEvent(drained, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this)); + return drained; }