-
Notifications
You must be signed in to change notification settings - Fork 754
/
LavaTankLogic.java
189 lines (165 loc) · 5.13 KB
/
LavaTankLogic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package tconstruct.smeltery.logic;
import mantle.blocks.abstracts.MultiServantLogic;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.*;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class LavaTankLogic extends MultiServantLogic implements IFluidHandler
{
public FluidTank tank;
public int renderOffset;
public LavaTankLogic()
{
tank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 4);
}
@Override
public int fill (ForgeDirection from, FluidStack resource, boolean doFill)
{
int amount = tank.fill(resource, doFill);
if (amount > 0 && doFill)
{
renderOffset = resource.amount;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, this.getBlockType());
}
return amount;
}
@Override
public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain)
{
FluidStack amount = tank.drain(maxDrain, doDrain);
if (amount != null && doDrain)
{
renderOffset = -maxDrain;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, this.getBlockType());
}
return amount;
}
@Override
public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain)
{
if (tank.getFluidAmount() == 0)
return null;
if (tank.getFluid().getFluid() != resource.getFluid())
return null;
// same fluid, k
return this.drain(from, resource.amount, doDrain);
}
@Override
public boolean canFill (ForgeDirection from, Fluid fluid)
{
return tank.getFluidAmount() == 0 || (tank.getFluid().getFluid() == fluid && tank.getFluidAmount() < tank.getCapacity());
}
@Override
public boolean canDrain (ForgeDirection from, Fluid fluid)
{
return tank.getFluidAmount() > 0;
}
@Override
public FluidTankInfo[] getTankInfo (ForgeDirection from)
{
FluidStack fluid = null;
if (tank.getFluid() != null)
fluid = tank.getFluid().copy();
return new FluidTankInfo[] { new FluidTankInfo(fluid, tank.getCapacity()) };
}
/*
* @Override public IFluidTank[] getTanks (ForgeDirection direction) {
* return new IFluidTank[] { tank }; }
*
* @Override public IFluidTank getTank (ForgeDirection direction, FluidStack
* type) { return tank; }
*/
public float getFluidAmountScaled ()
{
return (float) (tank.getFluid().amount - renderOffset) / (float) (tank.getCapacity() * 1.01F);
}
public boolean containsFluid ()
{
return tank.getFluid() != null;
}
public int getBrightness ()
{
if (containsFluid())
{
return (tank.getFluid().getFluid().getLuminosity() * tank.getFluidAmount()) / tank.getCapacity();
}
return 0;
}
@Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
readCustomNBT(tags);
}
@Override
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
writeCustomNBT(tags);
}
@Override
public void readCustomNBT (NBTTagCompound tags)
{
if (tags.getBoolean("hasFluid"))
{
// TODO: Removed in future Versions, backward compat.
if (tags.getInteger("itemID") != 0)
{
tank.setFluid(new FluidStack(tags.getInteger("itemID"), tags.getInteger("amount")));
}
else
{
tank.setFluid(FluidRegistry.getFluidStack(tags.getString("fluidName"), tags.getInteger("amount")));
}
}
else
tank.setFluid(null);
}
@Override
public void writeCustomNBT (NBTTagCompound tags)
{
FluidStack liquid = tank.getFluid();
tags.setBoolean("hasFluid", liquid != null);
if (liquid != null)
{
tags.setString("fluidName", liquid.getFluid().getName());
tags.setInteger("amount", liquid.amount);
}
}
/* Packets */
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
}
@Override
public void onDataPacket (NetworkManager net, S35PacketUpdateTileEntity packet)
{
readCustomNBT(packet.func_148857_g());
worldObj.func_147479_m(xCoord, yCoord, zCoord);
}
/* Updating */
@Override
public boolean canUpdate ()
{
return true;
}
@Override
public void updateEntity ()
{
if (renderOffset > 0)
{
renderOffset -= 6;
worldObj.func_147479_m(xCoord, yCoord, zCoord);
}
}
public int comparatorStrength ()
{
return 15 * tank.getFluidAmount() / tank.getCapacity();
}
}