Skip to content

Commit 9f3ac38

Browse files
committed
Fixed all stuff with fluid slots
Closes #84 and closes #100 P.S yay closed issue #100!!! <3
1 parent 50785bb commit 9f3ac38

File tree

13 files changed

+78
-152
lines changed

13 files changed

+78
-152
lines changed

src/main/java/gregtechmod/api/enums/GT_Items.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ public enum GT_Items implements IItemContainer {
123123
Upgrade_Transformer,
124124
Upgrade_Battery,
125125
McGuffium_239,
126-
Display_Fluid,
127126
NC_SensorCard,
128127
NC_SensorKit,
129128
Tool_Mortar_Iron,

src/main/java/gregtechmod/api/gui/GT_Container.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package gregtechmod.api.gui;
22

33
import gregtechmod.api.interfaces.IGregTechTileEntity;
4-
import gregtechmod.api.interfaces.IMetaTileEntity;
5-
import gregtechmod.api.metatileentity.implementations.GT_MetaTileEntity_BasicTank;
64
import gregtechmod.api.util.GT_Log;
75
import gregtechmod.api.util.GT_Utility;
86
import gregtechmod.common.network.GT_NetworkHandler;
@@ -32,11 +30,15 @@ public class GT_Container extends Container {
3230
public IGregTechTileEntity mTileEntity;
3331
public InventoryPlayer mPlayerInventory;
3432
public List<GT_FluidSlot> fluidSlots;
33+
public List<FluidStack> fluidInventory;
3534

36-
public GT_Container (InventoryPlayer aPlayerInventory, IGregTechTileEntity aTileEntityInventory) {
35+
protected int updateTimer;
36+
37+
public GT_Container(InventoryPlayer aPlayerInventory, IGregTechTileEntity aTileEntityInventory) {
3738
mTileEntity = aTileEntityInventory;
3839
mPlayerInventory = aPlayerInventory;
3940
fluidSlots = new ArrayList<>();
41+
fluidInventory = new ArrayList<>();
4042
}
4143

4244
/**
@@ -48,6 +50,7 @@ public void addSlots(InventoryPlayer aPlayerInventory) {
4850

4951
public void addFluidSlot(GT_FluidSlot slot) {
5052
addSlotToContainer(slot);
53+
fluidInventory.add(null);
5154
fluidSlots.add(slot);
5255
}
5356

@@ -118,7 +121,10 @@ public ItemStack slotClick(int aSlotIndex, int aMouseclick, int aShifthold, Enti
118121
aSlot = (Slot) inventorySlots.get(aSlotIndex);
119122

120123
if (aSlot == null || aSlot instanceof GT_Slot_Holo) return null;
121-
if (aSlot instanceof GT_FluidSlot) if(((GT_FluidSlot)aSlot).onClick(aMouseclick, aShifthold, aPlayer)) return null;
124+
if (aSlot instanceof GT_FluidSlot) if(((GT_FluidSlot)aSlot).onClick(aMouseclick, aShifthold, aPlayer)) {
125+
mTileEntity.decrStackSize(-1, 0); // Some shitcode here, this IS a problem from anywhre already p.s marking inventory dirty to recipe check
126+
return null;
127+
}
122128
if (!(aSlot instanceof GT_Slot_Armor)) if (aSlotIndex < getAllSlotCount()) if (aSlotIndex < getSlotStartIndex() || aSlotIndex >= getSlotStartIndex() + getSlotCount()) return null;
123129
}
124130

@@ -400,16 +406,10 @@ public void addCraftingToCrafters(ICrafting par1ICrafting) {
400406
} else {
401407
this.crafters.add(par1ICrafting);
402408
par1ICrafting.sendContainerAndContentsToPlayer(this, this.getInventory());
403-
IMetaTileEntity mte = mTileEntity.getMetaTileEntity();
404-
if (!fluidSlots.isEmpty() && mte instanceof GT_MetaTileEntity_BasicTank) {
405-
GT_MetaTileEntity_BasicTank mte1 = (GT_MetaTileEntity_BasicTank)mte;
409+
if (!fluidSlots.isEmpty()) {
406410
Map<Integer, GT_FluidSlot> toUpdate = new HashMap<>();
407-
for (int i = 0; i < fluidSlots.size(); ++i) {
408-
GT_FluidSlot slot = fluidSlots.get(i);
409-
slot.fluid = mte1.mFluid[i] == null ? null : mte1.mFluid[i].copy();
410-
toUpdate.put(i, slot);
411-
}
412-
411+
for (int i = 0; i < fluidSlots.size(); ++i)
412+
toUpdate.put(i, fluidSlots.get(i));
413413
GT_NetworkHandler.sendPacket(new FluidInventoryPacket(toUpdate, this.windowId), (EntityPlayerMP)par1ICrafting);
414414
}
415415
this.detectAndSendChanges();
@@ -443,20 +443,17 @@ public void removeCraftingFromCrafters(ICrafting par1ICrafting) {
443443
public void detectAndSendChanges() {
444444
try {
445445
super.detectAndSendChanges();
446-
447-
IMetaTileEntity mte = mTileEntity.getMetaTileEntity();
448-
if (!fluidSlots.isEmpty() && mte instanceof GT_MetaTileEntity_BasicTank) {
446+
447+
if (!fluidSlots.isEmpty() && ++updateTimer % 2 == 0) { // Added timer to update only 10 times per second
449448
Map<Integer, GT_FluidSlot> toUpdate = new HashMap<>();
450-
GT_MetaTileEntity_BasicTank tank = (GT_MetaTileEntity_BasicTank) mte;
451-
452449
for (int i = 0; i < fluidSlots.size(); ++i) {
453450
GT_FluidSlot slot = fluidSlots.get(i);
454-
FluidStack f1 = tank.mFluid[slot.fluidIdx];
455-
FluidStack f2 = slot.fluid;
451+
FluidStack f1 = fluidInventory.get(i);
452+
FluidStack f2 = slot.getFluid();
456453

457454
if (!GT_Utility.areFluidStackSame(f1, f2)) {
458455
f1 = f2 == null ? null : f2.copy();
459-
tank.mFluid[slot.fluidIdx] = f1;
456+
fluidInventory.set(i, f1);
460457
toUpdate.put(i, slot);
461458
}
462459
}

src/main/java/gregtechmod/api/gui/GT_Container_BasicTank.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public GT_Container_BasicTank(InventoryPlayer aInventoryPlayer, IGregTechTileEnt
2626
public void addSlots(InventoryPlayer aInventoryPlayer) {
2727
addSlotToContainer(new Slot(mTileEntity, 0, 80, 17));
2828
addSlotToContainer(new GT_Slot_Output(mTileEntity, 1, 80, 53));
29-
addSlotToContainer(new GT_Slot_Render(mTileEntity, 2, 59, 42));
29+
GT_MetaTileEntity_BasicTank tank = (GT_MetaTileEntity_BasicTank) mTileEntity.getMetaTileEntity();
30+
addFluidSlot(new GT_FluidSlot(mTileEntity, 2, 59, 42, 0, tank.canTankBeEmptied(), tank.canTankBeFilled()).setRenderAmount(false).setRenderOverlay(false));
3031
}
3132

3233
public SyncedField<Integer> mContent = new SyncedField<>("mContent", Integer.valueOf(0));

src/main/java/gregtechmod/api/gui/GT_FluidSlot.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gregtechmod.api.gui;
22

3+
import java.util.List;
34
import java.util.Objects;
45

56
import org.lwjgl.opengl.GL11;
@@ -10,7 +11,9 @@
1011

1112
import gregtechmod.api.GregTech_API;
1213
import gregtechmod.api.interfaces.IGregTechTileEntity;
14+
import gregtechmod.api.metatileentity.implementations.GT_MetaTileEntity_BasicTank;
1315
import gregtechmod.api.util.GT_Utility;
16+
import gregtechmod.api.util.ListAdapter;
1417
import gregtechmod.common.render.GTRenderHelper;
1518

1619
import net.minecraft.client.renderer.texture.TextureMap;
@@ -37,9 +40,11 @@ public class GT_FluidSlot extends Slot {
3740

3841
@SideOnly(Side.CLIENT)
3942
protected ResourceLocation customOverlay;
43+
protected List<FluidStack> fluidInvenotry;
4044
protected boolean renderOverlay;
41-
protected FluidStack fluid;
45+
protected boolean renderAmount;
4246

47+
/** Index of internal mFluid array */
4348
public final int fluidIdx;
4449
public final boolean canDrain;
4550
public final boolean canFill;
@@ -51,15 +56,17 @@ public class GT_FluidSlot extends Slot {
5156
}
5257

5358
public GT_FluidSlot(IGregTechTileEntity te, int slotIndex, int x, int y, int fluidIdx) {
54-
this(te, slotIndex, x, y, fluidIdx, true, true, true);
59+
this(te, slotIndex, x, y, fluidIdx, true, true);
5560
}
5661

57-
public GT_FluidSlot(IGregTechTileEntity te, int slotIndex, int x, int y, int fluidIdx, boolean canDrain, boolean canFill, boolean renderOverlay) {
62+
public GT_FluidSlot(IGregTechTileEntity te, int slotIndex, int x, int y, int fluidIdx, boolean canDrain, boolean canFill) {
5863
super(te, slotIndex, x, y);
5964
this.fluidIdx = fluidIdx;
6065
this.canDrain = canDrain;
6166
this.canFill = canFill;
62-
this.renderOverlay = renderOverlay;
67+
this.renderOverlay = true;
68+
this.renderAmount = true;
69+
this.fluidInvenotry = new ListAdapter<>(((GT_MetaTileEntity_BasicTank)te.getMetaTileEntity()).mFluid);
6370
}
6471

6572
/*
@@ -105,6 +112,7 @@ public void draw(int uiLeft, int uiTop, boolean isMouseOver) {
105112
}
106113

107114
// Fluid render
115+
FluidStack fluid = getFluid();
108116
if (GT_Utility.isFluidStackValid(fluid)) {
109117
IIcon text = fluid.getFluid().getIcon(fluid);
110118
if (text != null) {
@@ -113,9 +121,11 @@ public void draw(int uiLeft, int uiTop, boolean isMouseOver) {
113121
}
114122

115123
// Amount render
124+
if (renderAmount) {
116125
int amount = fluid.amount / 1000;
117126
if (amount > 0)
118127
GTRenderHelper.drawStackAmount(posX, posY, 0xFFFFFF, Integer.toString(amount));
128+
}
119129
}
120130

121131
if (isMouseOver)
@@ -138,30 +148,31 @@ public boolean onClick(int mouseClick, int shiftHold, EntityPlayer player) {
138148

139149
if (GT_Utility.isStackValid(held)) {
140150
FluidStack fluid = GT_Utility.getFluidForFilledItem(held);
151+
FluidStack fluidInv = getFluid();
141152
int amount;
142153
if (mouseClick == 1 && canDrain && fluid != null) { // Fill (Right click to fill container with liquid)
143154
amount = Math.min(getSpace(), fluid.amount);
144155
if (amount > 0) {
145-
if (this.fluid == null) { // Fill empty slot
156+
if (!GT_Utility.isFluidStackValid(fluidInv)) { // Fill empty slot
146157
FluidStack actual = fluid.copy();
147158
actual.amount = amount;
148159

149160
if (this.useFluidContainer(inv, held, amount)) {
150-
this.fluid = actual;
161+
setFluid(actual);
151162
return true;
152163
}
153-
} else if (this.fluid.isFluidEqual(fluid)) { // Add fluid if same
164+
} else if (fluidInv.isFluidEqual(fluid)) { // Add fluid if same
154165
if (this.useFluidContainer(inv, held, amount)) {
155-
this.fluid.amount += amount;
166+
fluidInv.amount += amount;
156167
return true;
157168
}
158169
}
159170
}
160-
} else if (mouseClick == 0 && canFill && this.fluid != null) { // Epmty (Left click to drain liquid from container)
161-
if ((amount = this.tryFill(inv, held, this.fluid.copy())) > 0) {
162-
this.fluid.amount -= amount;
163-
if (this.fluid.amount <= 0)
164-
this.fluid = null;
171+
} else if (mouseClick == 0 && canFill && GT_Utility.isFluidStackValid(fluidInv)) { // Epmty (Left click to drain liquid from container)
172+
if ((amount = this.tryFill(inv, held, fluidInv.copy())) > 0) {
173+
fluidInv.amount -= amount;
174+
if (fluidInv.amount <= 0)
175+
setFluid(null);
165176

166177
return true;
167178
}
@@ -243,9 +254,17 @@ protected boolean exchangeStacks(InventoryPlayer inv, ItemStack stack) {
243254
* @return free space to fill
244255
*/
245256
protected int getSpace() {
246-
int availableSpace = this.getSlotStackLimit() * 1000;
247-
if (this.fluid != null)
248-
availableSpace -= this.fluid.amount;
257+
IGregTechTileEntity te = (IGregTechTileEntity)inventory;
258+
int availableSpace = 0;
259+
260+
if (te.getMetaTileEntity() instanceof GT_MetaTileEntity_BasicTank) {
261+
availableSpace = ((GT_MetaTileEntity_BasicTank)te.getMetaTileEntity()).getCapacity();
262+
} else
263+
availableSpace = this.getSlotStackLimit() * 1000;
264+
265+
FluidStack fluid = getFluid();
266+
if (GT_Utility.isFluidStackValid(fluid))
267+
availableSpace -= fluid.amount;
249268
return availableSpace;
250269
}
251270

@@ -258,6 +277,15 @@ public GT_FluidSlot setRenderOverlay(boolean value) {
258277
return this;
259278
}
260279

280+
/**
281+
* Disable or enable amount string rendering
282+
* Default value set to true
283+
*/
284+
public GT_FluidSlot setRenderAmount(boolean value) {
285+
this.renderAmount = value;
286+
return this;
287+
}
288+
261289
/**
262290
* Set render overlay image
263291
* @param overlay
@@ -270,10 +298,10 @@ public GT_FluidSlot setRenderOverlay(ResourceLocation overlay) {
270298
}
271299

272300
public FluidStack getFluid() {
273-
return fluid;
301+
return fluidInvenotry.get(fluidIdx);
274302
}
275303

276304
public void setFluid(FluidStack fluid) {
277-
this.fluid = fluid;
305+
fluidInvenotry.set(fluidIdx, fluid);
278306
}
279307
}

src/main/java/gregtechmod/api/gui/GT_GUIContainer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.minecraft.inventory.Container;
1717
import net.minecraft.util.EnumChatFormatting;
1818
import net.minecraft.util.ResourceLocation;
19+
import net.minecraftforge.fluids.FluidStack;
1920

2021
/**
2122
* NEVER INCLUDE THIS FILE IN YOUR MOD!!!
@@ -41,9 +42,10 @@ protected void drawTooltips(int posX, int posY) {
4142
GL11.glPushAttrib(GL11.GL_COLOR_BUFFER_BIT);
4243
{
4344
List<String> tooltipData = Lists.newArrayList();
44-
if (slot.fluid != null) {
45-
tooltipData.add(slot.fluid.getLocalizedName());
46-
tooltipData.add(EnumChatFormatting.GRAY + I18n.format("metatileentity.fluid.amount", GT_Utility.parseNumberToString(slot.fluid.amount)));
45+
FluidStack fluid = slot.getFluid();
46+
if (GT_Utility.isFluidStackValid(fluid)) {
47+
tooltipData.add(fluid.getLocalizedName());
48+
tooltipData.add(EnumChatFormatting.GRAY + I18n.format("metatileentity.fluid.amount", GT_Utility.parseNumberToString(fluid.amount)));
4749
} else {
4850
tooltipData.add(I18n.format("metatileentity.fluid.empty"));
4951
}

src/main/java/gregtechmod/api/metatileentity/implementations/BasicFluidWorkable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
public abstract class BasicFluidWorkable extends GT_MetaTileEntity_BasicTank implements IRecipeWorkable {
2727
protected RecipeLogic recipeLogic;
2828

29-
public int MAX_FLUID_STACK = 16_000;
30-
3129
public BasicFluidWorkable(int aID, String aName, RecipeMap<?> recipeMap) {
3230
super(aID, aName);
3331
initRecipeLogic(recipeMap);
@@ -62,7 +60,7 @@ public BasicFluidWorkable(RecipeMap<?> recipeMap) {
6260

6361
@Override public int getInputSlot() {return 1;}
6462
@Override public int getOutputSlot() {return 2;}
65-
@Override public int getStackDisplaySlot() {return 6;}
63+
@Override public int getCapacity() {return 16_000;}
6664

6765
protected void initRecipeLogic(RecipeMap<?> recipeMap) {
6866
recipeLogic = new RecipeLogic(recipeMap, this);
@@ -135,7 +133,7 @@ public boolean spaceForOutput(Recipe recipe) {
135133
for (int i = 0; amount > 0 && i < fluidOutputs.size(); i++) {
136134
FluidStack stackInSlot = fluidOutputs.get(i);
137135
if (GT_Utility.isFluidStackValid(stackInSlot) && stackInSlot.isFluidEqual(fluid)) {
138-
int tmp = Math.min(MAX_FLUID_STACK, stackInSlot.amount + fluid.amount);
136+
int tmp = Math.min(getCapacity(), stackInSlot.amount + fluid.amount);
139137
amount -= tmp - stackInSlot.amount;
140138
} else if (stackInSlot == null) amount = 0;
141139
}

src/main/java/gregtechmod/api/metatileentity/implementations/GT_MetaTileEntity_BasicGenerator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public GT_MetaTileEntity_BasicGenerator(RecipeMap<?> recipeMap, int efficiency)
5151
@Override public boolean canTankBeEmptied() {return getBaseMetaTileEntity().isAllowedToWork();}
5252
@Override public int getInputSlot() {return 0;}
5353
@Override public int getOutputSlot() {return 1;}
54-
@Override public int getStackDisplaySlot() {return 2;}
5554
@Override public List<ItemStack> getInputItems() { return new ListAdapter<>(mInventory, 0, 0); }
5655
@Override public List<ItemStack> getOutputItems() { return new ListAdapter<>(mInventory, 1, 1); }
5756
@Override public boolean displaysItemStack() {return true;}

src/main/java/gregtechmod/api/metatileentity/implementations/GT_MetaTileEntity_BasicTank.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package gregtechmod.api.metatileentity.implementations;
22

3-
import gregtechmod.api.enums.GT_Items;
43
import gregtechmod.api.metatileentity.MetaTileEntity;
54
import gregtechmod.api.util.GT_Utility;
65

@@ -28,7 +27,7 @@ public GT_MetaTileEntity_BasicTank() {
2827

2928
@Override public boolean isSimpleMachine() {return false;}
3029
@Override public boolean isValidSlot(int aIndex) {return aIndex < 2;}
31-
@Override public int getInvSize() {return 3;}
30+
@Override public int getInvSize() {return 2;}
3231

3332
@Override
3433
public void saveNBTData(NBTTagCompound aNBT) {
@@ -59,7 +58,6 @@ public int getTextureIndex(byte aSide, byte aFacing, boolean aActive, boolean aR
5958
public abstract boolean displaysStackSize();
6059
public int getInputSlot() {return 0;}
6160
public int getOutputSlot() {return 1;}
62-
public int getStackDisplaySlot() {return 2;}
6361

6462
public boolean isFluidInputAllowed(FluidStack aFluid) {return true;}
6563
public boolean isFluidChangingAllowed() {return true;}
@@ -74,19 +72,6 @@ public void onPreTick() {
7472
if (getBaseMetaTileEntity().isServerSide()) {
7573
if (isFluidChangingAllowed() && mFluid[0] != null && mFluid[0].amount <= 0) mFluid[0] = null;
7674

77-
if (displaysItemStack()) {
78-
if (getDrainableStack() != null) {
79-
ItemStack fluidDisplay = GT_Items.Display_Fluid.getWithDamage(displaysStackSize()?Math.max(1, Math.min(getDrainableStack().amount/1000, 64)):1, getDrainableStack().getFluidID());;
80-
NBTTagCompound data = new NBTTagCompound();
81-
data.setInteger("amount", mFluid[0].amount);
82-
fluidDisplay.setTagCompound(data);
83-
mInventory[getStackDisplaySlot()] = fluidDisplay;
84-
85-
} else {
86-
if (GT_Items.Display_Fluid.isStackEqual(mInventory[getStackDisplaySlot()], true, true)) mInventory[getStackDisplaySlot()] = null;
87-
}
88-
}
89-
9075
if (doesEmptyContainers()) {
9176
FluidStack tFluid = GT_Utility.getFluidForFilledItem(mInventory[getInputSlot()]);
9277
if (tFluid != null && isFluidInputAllowed(tFluid)) {

0 commit comments

Comments
 (0)