-
Notifications
You must be signed in to change notification settings - Fork 754
/
FaucetLogic.java
247 lines (219 loc) · 6.27 KB
/
FaucetLogic.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package tconstruct.blocks.logic;
import tconstruct.TConstruct;
import tconstruct.library.util.IActiveLogic;
import tconstruct.library.util.IFacingLogic;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
public class FaucetLogic extends TileEntity implements IFacingLogic, IActiveLogic, IFluidHandler
{
byte direction;
boolean active;
public FluidStack liquid;
public boolean activateFaucet ()
{
if (liquid == null && active)
{
int x = xCoord, z = zCoord;
switch (getRenderDirection())
{
case 2:
z++;
break;
case 3:
z--;
break;
case 4:
x++;
break;
case 5:
x--;
break;
}
TileEntity drainte = worldObj.getBlockTileEntity(x, yCoord, z);
TileEntity tankte = worldObj.getBlockTileEntity(xCoord, yCoord - 1, zCoord);
if (drainte != null && drainte instanceof IFluidHandler && tankte != null && tankte instanceof IFluidHandler)
{
FluidStack templiquid = ((IFluidHandler) drainte).drain(getForgeDirection(), TConstruct.ingotLiquidValue, false);
if (templiquid != null)
{
int drained = ((IFluidHandler) tankte).fill(ForgeDirection.UP, templiquid, false);
if (drained > 0)
{
liquid = ((IFluidHandler) drainte).drain(getForgeDirection(), drained, true);
((IFluidHandler) tankte).fill(ForgeDirection.UP, liquid, true);
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
return true;
}
else
{
return false;
}
}
}
}
return false;
}
@Override
public void updateEntity ()
{
if (liquid != null)
{
liquid.amount -= TConstruct.liquidUpdateAmount;
if (liquid.amount <= 0)
{
liquid = null;
if (!activateFaucet())
{
active = false;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
}
}
@Override
public byte getRenderDirection ()
{
return direction;
}
@Override
public ForgeDirection getForgeDirection ()
{
return ForgeDirection.VALID_DIRECTIONS[direction];
}
@Override
public void setDirection (int side)
{
//direction = (byte) side;
}
@Override
public void setDirection (float yaw, float pitch, EntityLivingBase player)
{
int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3;
switch (facing)
{
case 0:
direction = 2;
break;
case 1:
direction = 5;
break;
case 2:
direction = 3;
break;
case 3:
direction = 4;
break;
}
}
@Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
readCustomNBT(tags);
}
public void readCustomNBT (NBTTagCompound tags)
{
direction = tags.getByte("Direction");
if (tags.getBoolean("hasLiquid"))
{
this.liquid = FluidStack.loadFluidStackFromNBT(tags.getCompoundTag("Fluid"));
}
else
this.liquid = null;
}
@Override
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
writeCustomNBT(tags);
}
public void writeCustomNBT (NBTTagCompound tags)
{
tags.setByte("Direction", direction);
tags.setBoolean("hasLiquid", liquid != null);
if (liquid != null)
{
NBTTagCompound nbt = new NBTTagCompound();
liquid.writeToNBT(nbt);
tags.setCompoundTag("Fluid", nbt);
}
}
/* Packets */
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
}
@Override
public void onDataPacket (INetworkManager net, Packet132TileEntityData packet)
{
readCustomNBT(packet.data);
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
}
@Override
public boolean getActive ()
{
return active;
}
@Override
public void setActive (boolean flag)
{
if (!active)
{
active = true;
activateFaucet();
}
else
{
active = false;
}
}
@Override
public int fill (ForgeDirection from, FluidStack resource, boolean doFill)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain)
{
// TODO Auto-generated method stub
return null;
}
@Override
public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain)
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean canFill (ForgeDirection from, Fluid fluid)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean canDrain (ForgeDirection from, Fluid fluid)
{
// TODO Auto-generated method stub
return false;
}
@Override
public FluidTankInfo[] getTankInfo (ForgeDirection from)
{
// TODO Auto-generated method stub
return null;
}
}