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
|
package gtPlusPlus.core.tileentities.general;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
public class TileEntityInfiniteFluid extends TileEntity implements IFluidHandler {
public FluidTank tank = new FluidTank(Integer.MAX_VALUE);
private boolean needsUpdate = false;
private int updateTimer = 0;
public TileEntityInfiniteFluid() {}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
needsUpdate = true;
return this.tank.fill(resource, doFill);
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
needsUpdate = true;
return this.tank.drain(resource.amount, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
needsUpdate = true;
FluidStack fluid = this.tank.getFluid();
// return this.tank.drain(maxDrain, doDrain);
if (fluid == null) {
return null;
}
int drained = maxDrain;
if (fluid.amount < drained) {
drained = fluid.amount;
}
FluidStack stack = new FluidStack(fluid, drained);
if (doDrain) {
fluid.amount -= drained;
if (fluid.amount <= 0) {
fluid = null;
}
FluidEvent.fireEvent(
new FluidEvent.FluidDrainingEvent(
fluid,
this.getWorldObj(),
this.xCoord,
this.yCoord,
this.zCoord,
this.tank,
0));
}
return stack;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return new FluidTankInfo[] { this.tank.getInfo() };
}
public float getAdjustedVolume() {
needsUpdate = true;
float amount = tank.getFluidAmount();
float capacity = tank.getCapacity();
return (amount / capacity) * 0.8F;
}
@Override
public void updateEntity() {
if (this.tank.getFluid() != null) {
FluidStack bigStorage = this.tank.getFluid();
bigStorage.amount = this.tank.getCapacity();
this.tank.setFluid(bigStorage);
}
if (needsUpdate) {
if (this.tank.getFluid() != null) {
FluidStack bigStorage = this.tank.getFluid();
bigStorage.amount = this.tank.getCapacity();
this.tank.setFluid(bigStorage);
}
if (updateTimer == 0) {
updateTimer = 10; // every 10 ticks it will send an update
} else {
--updateTimer;
if (updateTimer == 0) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
needsUpdate = false;
}
}
}
}
@Override
public void readFromNBT(NBTTagCompound tag) {
tank.readFromNBT(tag);
super.readFromNBT(tag);
}
@Override
public void writeToNBT(NBTTagCompound tag) {
tank.writeToNBT(tag);
super.writeToNBT(tag);
}
@Override
public Packet getDescriptionPacket() {
NBTTagCompound tag = new NBTTagCompound();
writeToNBT(tag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.blockMetadata, tag);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.func_148857_g();
readFromNBT(tag);
}
}
|