Skip to content

Commit 67abd1e

Browse files
committed
add buffer
1 parent 2d585bd commit 67abd1e

21 files changed

+1895
-9
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.glodblock.github.client.gui;
2+
3+
import appeng.api.storage.data.IAEFluidStack;
4+
import appeng.client.gui.AEBaseGui;
5+
import appeng.core.localization.GuiText;
6+
import com.glodblock.github.FluidCraft;
7+
import com.glodblock.github.client.gui.container.ContainerIngredientBuffer;
8+
import com.glodblock.github.common.tile.TileIngredientBuffer;
9+
import com.glodblock.github.inventory.IAEFluidTank;
10+
import com.glodblock.github.inventory.gui.ButtonMouseHandler;
11+
import com.glodblock.github.inventory.gui.MouseRegionManager;
12+
import com.glodblock.github.inventory.gui.TankMouseHandler;
13+
import com.glodblock.github.util.GlStateManager;
14+
import com.glodblock.github.util.NameConst;
15+
import net.minecraft.client.Minecraft;
16+
import net.minecraft.client.renderer.Tessellator;
17+
import net.minecraft.client.renderer.texture.TextureMap;
18+
import net.minecraft.client.resources.I18n;
19+
import net.minecraft.entity.player.InventoryPlayer;
20+
import net.minecraft.util.IIcon;
21+
import net.minecraft.util.ResourceLocation;
22+
import net.minecraftforge.common.util.ForgeDirection;
23+
import org.lwjgl.opengl.GL11;
24+
25+
import javax.annotation.Nullable;
26+
27+
public class GuiIngredientBuffer extends AEBaseGui {
28+
29+
private static final ResourceLocation TEX_BG = FluidCraft.resource("textures/gui/ingredient_buffer.png");
30+
private static final int TANK_X = 47, TANK_X_OFF = 22, TANK_Y = 18;
31+
private static final int TANK_WIDTH = 16, TANK_HEIGHT = 74;
32+
33+
private final ContainerIngredientBuffer cont;
34+
private final MouseRegionManager mouseRegions = new MouseRegionManager(this, this.fontRendererObj);
35+
36+
public GuiIngredientBuffer(InventoryPlayer ipl, TileIngredientBuffer tile) {
37+
super(new ContainerIngredientBuffer(ipl, tile));
38+
this.cont = (ContainerIngredientBuffer) inventorySlots;
39+
this.ySize = 222;
40+
for (int i = 0; i < 4; i++) {
41+
mouseRegions.addRegion(TANK_X + TANK_X_OFF * i, TANK_Y, TANK_WIDTH, TANK_HEIGHT,
42+
new TankMouseHandler(cont.getTile().getFluidInventory(), i));
43+
mouseRegions.addRegion(TANK_X + 10 + 22 * i, TANK_Y + TANK_HEIGHT + 2, 7, 7,
44+
ButtonMouseHandler.dumpTank(cont, i));
45+
}
46+
}
47+
48+
@Override
49+
protected void mouseClicked(int xCoord, int yCoord, int btn) {
50+
if (mouseRegions.onClick(xCoord, yCoord, btn)) {
51+
super.mouseClicked(xCoord, yCoord, btn);
52+
}
53+
}
54+
55+
@Override
56+
public void drawBG(int offsetX, int offsetY, int mouseX, int mouseY) {
57+
mc.getTextureManager().bindTexture(TEX_BG);
58+
drawTexturedModalRect(offsetX, offsetY, 0, 0, 176, ySize);
59+
}
60+
61+
@Override
62+
public void drawFG(int offsetX, int offsetY, int mouseX, int mouseY) {
63+
fontRendererObj.drawString(getGuiDisplayName(I18n.format(NameConst.GUI_INGREDIENT_BUFFER)), 8, 6, 0x404040);
64+
fontRendererObj.drawString(GuiText.inventory.getLocal(), 8, ySize - 94, 0x404040);
65+
GlStateManager.color(1F, 1F, 1F, 1F);
66+
67+
IAEFluidTank fluidInv = cont.getTile().getFluidInventory();
68+
mc.getTextureManager().bindTexture(TextureMap.locationBlocksTexture);
69+
Tessellator tess = Tessellator.instance;
70+
for (int i = 0; i < 4; i++) {
71+
renderFluidIntoGui(tess, TANK_X + i * TANK_X_OFF, TANK_Y, TANK_WIDTH, TANK_HEIGHT,
72+
fluidInv.getFluidInSlot(i), fluidInv.getTankInfo(ForgeDirection.UNKNOWN)[i].capacity);
73+
}
74+
GlStateManager.color(1F, 1F, 1F, 1F);
75+
76+
mouseRegions.render(mouseX, mouseY);
77+
}
78+
79+
public void renderFluidIntoGui(Tessellator tess, int x, int y, int width, int height,
80+
@Nullable IAEFluidStack aeFluidStack, int capacity) {
81+
if (aeFluidStack != null) {
82+
GL11.glDisable(2896);
83+
GL11.glColor3f(1.0F, 1.0F, 1.0F);
84+
int hi = (int) (height * ((double) aeFluidStack.getStackSize() / capacity));
85+
if (aeFluidStack.getStackSize() > 0 && hi > 0) {
86+
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.locationBlocksTexture);
87+
IIcon fluidIcon = aeFluidStack.getFluid().getStillIcon();
88+
GL11.glColor3f((float)(aeFluidStack.getFluid().getColor() >> 16 & 255) / 255.0F, (float)(aeFluidStack.getFluid().getColor() >> 8 & 255) / 255.0F, (float)(aeFluidStack.getFluid().getColor() & 255) / 255.0F);
89+
for (int th = 0; th <= hi; th += 16) {
90+
if (hi - th <= 0) break;
91+
this.drawTexturedModelRectFromIcon(x, y + height - Math.min(16, hi - th) - th, fluidIcon, width, Math.min(16, hi - th));
92+
}
93+
GL11.glColor3f(1.0F, 1.0F, 1.0F);
94+
}
95+
}
96+
}
97+
98+
public void update(int slot, IAEFluidStack aeFluidStack) {
99+
cont.getTile().getFluidInventory().setFluidInSlot(slot, aeFluidStack);
100+
}
101+
102+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.glodblock.github.client.gui;
2+
3+
public interface TankDumpable {
4+
5+
boolean canDumpTank(int index);
6+
7+
void dumpTank(int index);
8+
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.glodblock.github.client.gui.container;
2+
3+
import appeng.api.storage.data.IAEFluidStack;
4+
import appeng.container.AEBaseContainer;
5+
import appeng.container.slot.SlotNormal;
6+
import com.glodblock.github.FluidCraft;
7+
import com.glodblock.github.client.gui.TankDumpable;
8+
import com.glodblock.github.common.tile.TileIngredientBuffer;
9+
import com.glodblock.github.network.SPacketFluidUpdate;
10+
import net.minecraft.entity.player.EntityPlayer;
11+
import net.minecraft.entity.player.EntityPlayerMP;
12+
import net.minecraft.entity.player.InventoryPlayer;
13+
import net.minecraft.inventory.IInventory;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
public class ContainerIngredientBuffer extends AEBaseContainer implements TankDumpable {
19+
20+
private final TileIngredientBuffer tile;
21+
22+
public ContainerIngredientBuffer(InventoryPlayer ipl, TileIngredientBuffer tile) {
23+
super(ipl, tile);
24+
this.tile = tile;
25+
IInventory inv = tile.getInternalInventory();
26+
for (int i = 0; i < 9; i++) {
27+
addSlotToContainer(new SlotNormal(inv, i, 8 + 18 * i, 108));
28+
}
29+
bindPlayerInventory(ipl, 0, 140);
30+
}
31+
32+
public TileIngredientBuffer getTile() {
33+
return tile;
34+
}
35+
36+
@Override
37+
public boolean canDumpTank(int index) {
38+
return tile.getFluidInventory().getFluidInSlot(index) != null;
39+
}
40+
41+
@Override
42+
public void dumpTank(int index) {
43+
if (index >= 0 && index < tile.getFluidInventory().getSlots()) {
44+
tile.getFluidInventory().setFluidInSlot(index, null);
45+
}
46+
}
47+
48+
@Override
49+
public void detectAndSendChanges()
50+
{
51+
super.detectAndSendChanges();
52+
Map<Integer, IAEFluidStack> tmp = new HashMap<>();
53+
for (int i = 0; i < tile.getFluidInventory().getSlots(); i ++) {
54+
tmp.put(i, tile.getFluidInventory().getFluidInSlot(i));
55+
}
56+
for( final Object g : this.crafters )
57+
{
58+
if( g instanceof EntityPlayer)
59+
{
60+
FluidCraft.proxy.netHandler.sendTo(new SPacketFluidUpdate(tmp), (EntityPlayerMP) g);
61+
}
62+
}
63+
}
64+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.glodblock.github.common.block;
2+
3+
import appeng.block.AEBaseItemBlock;
4+
import com.glodblock.github.common.tile.TileIngredientBuffer;
5+
import com.glodblock.github.inventory.InventoryHandler;
6+
import com.glodblock.github.inventory.gui.GuiType;
7+
import com.glodblock.github.util.BlockPos;
8+
import com.glodblock.github.util.NameConst;
9+
import cpw.mods.fml.common.registry.GameRegistry;
10+
import net.minecraft.block.material.Material;
11+
import net.minecraft.entity.player.EntityPlayer;
12+
import net.minecraft.item.ItemStack;
13+
import net.minecraft.util.EnumFacing;
14+
import net.minecraft.world.World;
15+
16+
public class BlockIngredientBuffer extends FCBaseBlock {
17+
18+
public BlockIngredientBuffer() {
19+
super(Material.iron, NameConst.BLOCK_INGREDIENT_BUFFER);
20+
setTileEntity(TileIngredientBuffer.class);
21+
setOpaque(false);
22+
setFullBlock(false);
23+
this.lightOpacity = 4;
24+
}
25+
26+
@Override
27+
public boolean onActivated(World world, int x, int y, int z, EntityPlayer player, int facing, float hitX, float hitY, float hitZ) {
28+
if (player.isSneaking()) {
29+
return false;
30+
}
31+
TileIngredientBuffer tile = getTileEntity(world, x, y, z);
32+
if (tile != null) {
33+
if (!world.isRemote) {
34+
InventoryHandler.openGui(player, world, new BlockPos(x, y, z), EnumFacing.getFront(facing), GuiType.INGREDIENT_BUFFER);
35+
}
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
public BlockIngredientBuffer register() {
42+
GameRegistry.registerBlock(this, AEBaseItemBlock.class, NameConst.BLOCK_INGREDIENT_BUFFER);
43+
GameRegistry.registerTileEntity(TileIngredientBuffer.class, NameConst.BLOCK_INGREDIENT_BUFFER);
44+
return this;
45+
}
46+
47+
public ItemStack stack(int size) {
48+
return new ItemStack(this, size);
49+
}
50+
51+
public ItemStack stack() {
52+
return new ItemStack(this, 1);
53+
}
54+
55+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.glodblock.github.common.tile;
2+
3+
import appeng.api.storage.data.IAEFluidStack;
4+
import appeng.tile.AEBaseInvTile;
5+
import appeng.tile.TileEvent;
6+
import appeng.tile.events.TileEventType;
7+
import appeng.tile.inventory.AppEngInternalInventory;
8+
import appeng.tile.inventory.InvOperation;
9+
import appeng.util.item.AEFluidStack;
10+
import com.glodblock.github.inventory.AEFluidInventory;
11+
import com.glodblock.github.inventory.IAEFluidInventory;
12+
import com.glodblock.github.inventory.IAEFluidTank;
13+
import cpw.mods.fml.common.network.ByteBufUtils;
14+
import io.netty.buffer.ByteBuf;
15+
import net.minecraft.inventory.IInventory;
16+
import net.minecraft.item.ItemStack;
17+
import net.minecraft.nbt.NBTTagCompound;
18+
import net.minecraftforge.common.util.ForgeDirection;
19+
import net.minecraftforge.fluids.Fluid;
20+
import net.minecraftforge.fluids.FluidStack;
21+
import net.minecraftforge.fluids.FluidTankInfo;
22+
import net.minecraftforge.fluids.IFluidHandler;
23+
24+
import javax.annotation.Nonnull;
25+
import java.io.IOException;
26+
27+
public class TileIngredientBuffer extends AEBaseInvTile implements IAEFluidInventory, IFluidHandler {
28+
29+
private final AppEngInternalInventory invItems = new AppEngInternalInventory(this, 9);
30+
private final AEFluidInventory invFluids = new AEFluidInventory(this, 4, 64000);
31+
32+
@Nonnull
33+
@Override
34+
public IInventory getInternalInventory() {
35+
return invItems;
36+
}
37+
38+
public IAEFluidTank getFluidInventory() {
39+
return invFluids;
40+
}
41+
42+
@Override
43+
public boolean canBeRotated() {
44+
return false;
45+
}
46+
47+
@Override
48+
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added) {
49+
markForUpdate();
50+
}
51+
52+
@Override
53+
public int[] getAccessibleSlotsBySide(ForgeDirection whichSide) {
54+
return new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8};
55+
}
56+
57+
@Override
58+
public void onFluidInventoryChanged(IAEFluidTank inv, int slot) {
59+
saveChanges();
60+
markForUpdate();
61+
}
62+
63+
@TileEvent( TileEventType.NETWORK_WRITE )
64+
protected void writeToStream(ByteBuf data) throws IOException {
65+
for (int i = 0; i < invItems.getSizeInventory(); i++) {
66+
ByteBufUtils.writeItemStack(data, invItems.getStackInSlot(i));
67+
}
68+
int fluidMask = 0;
69+
for (int i = 0; i < invFluids.getSlots(); i++) {
70+
if (invFluids.getFluidInSlot(i) != null) {
71+
fluidMask |= 1 << i;
72+
}
73+
}
74+
data.writeByte(fluidMask);
75+
for (int i = 0; i < invFluids.getSlots(); i++) {
76+
IAEFluidStack fluid = invFluids.getFluidInSlot(i);
77+
if (fluid != null) {
78+
fluid.writeToPacket(data);
79+
}
80+
}
81+
}
82+
83+
@TileEvent( TileEventType.NETWORK_READ )
84+
protected boolean readFromStream(ByteBuf data) throws IOException {
85+
boolean changed = false;
86+
for (int i = 0; i < invItems.getSizeInventory(); i++) {
87+
ItemStack stack = ByteBufUtils.readItemStack(data);
88+
if (!ItemStack.areItemStacksEqual(stack, invItems.getStackInSlot(i))) {
89+
invItems.setInventorySlotContents(i, stack);
90+
changed = true;
91+
}
92+
}
93+
int fluidMask = data.readByte();
94+
for (int i = 0; i < invFluids.getSlots(); i++) {
95+
if ((fluidMask & (1 << i)) != 0) {
96+
IAEFluidStack fluid = AEFluidStack.loadFluidStackFromPacket(data);
97+
if (fluid != null) { // this shouldn't happen, but better safe than sorry
98+
IAEFluidStack origFluid = invFluids.getFluidInSlot(i);
99+
if (!fluid.equals(origFluid) || fluid.getStackSize() != origFluid.getStackSize()) {
100+
invFluids.setFluidInSlot(i, fluid);
101+
changed = true;
102+
}
103+
}
104+
} else if (invFluids.getFluidInSlot(i) != null) {
105+
invFluids.setFluidInSlot(i, null);
106+
changed = true;
107+
}
108+
}
109+
return changed;
110+
}
111+
112+
@TileEvent( TileEventType.WORLD_NBT_READ )
113+
public void readFromNBTEvent(NBTTagCompound data) {
114+
invItems.readFromNBT(data, "ItemInv");
115+
invFluids.readFromNBT(data, "FluidInv");
116+
}
117+
118+
@TileEvent( TileEventType.WORLD_NBT_WRITE )
119+
public NBTTagCompound writeToNBTEvent(NBTTagCompound data) {
120+
invItems.writeToNBT(data, "ItemInv");
121+
invFluids.writeToNBT(data, "FluidInv");
122+
return data;
123+
}
124+
125+
@Override
126+
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
127+
return invFluids.fill(from, resource, doFill);
128+
}
129+
130+
@Override
131+
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
132+
return invFluids.drain(from, resource, doDrain);
133+
}
134+
135+
@Override
136+
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
137+
return invFluids.drain(from, maxDrain, doDrain);
138+
}
139+
140+
@Override
141+
public boolean canFill(ForgeDirection from, Fluid fluid) {
142+
return invFluids.canFill(from, fluid);
143+
}
144+
145+
@Override
146+
public boolean canDrain(ForgeDirection from, Fluid fluid) {
147+
return invFluids.canDrain(from, fluid);
148+
}
149+
150+
@Override
151+
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
152+
return invFluids.getTankInfo(ForgeDirection.UNKNOWN);
153+
}
154+
}

0 commit comments

Comments
 (0)