Skip to content

Instantly share code, notes, and snippets.

@AEnterprise
Last active August 29, 2015 14:00
Show Gist options
  • Save AEnterprise/11140606 to your computer and use it in GitHub Desktop.
Save AEnterprise/11140606 to your computer and use it in GitHub Desktop.
package buildcraft.factory.gui;
import org.lwjgl.opengl.GL11;
import buildcraft.core.DefaultProps;
import buildcraft.core.gui.GuiBuildCraft;
import buildcraft.core.render.RenderUtils;
import buildcraft.energy.TileEngineIron;
import buildcraft.energy.TileEngineWithInventory;
import buildcraft.energy.gui.ContainerEngine;
import buildcraft.energy.gui.GuiEngine;
import buildcraft.factory.TileCanner;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
public class GuiCanner extends GuiBuildCraft {
TileCanner canner;
private static final ResourceLocation texture = new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_GUI + "/FluidicCompressorGUI_alt.png");
private static final ResourceLocation BLOCK_TEXTURE = TextureMap.locationBlocksTexture;
private GuiButton mode;
private String modus;
public GuiCanner(InventoryPlayer inventoryplayer, TileCanner tilecanner) {
super(new ContainerCanner(inventoryplayer, tilecanner), tilecanner, texture);
canner = (TileCanner) tile;
}
@Override
public void initGui(){
super.initGui();
int j = (width - xSize) / 2;
int k = (height - ySize) / 2;
modus = "Empty";
if (canner.mode == 1)
modus = "Fill";
mode = new GuiButton(0, j + 125, k + 10, 30, 20, modus);
buttonList.add(mode);
}
@Override
protected void actionPerformed(GuiButton button) {
super.actionPerformed(button);
if (button == mode){
if (modus == "Fill"){
canner.mode = 2;
} else {
canner.mode = 1;
}
canner.sendNetworkUpdate();
}
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int x, int y) {
super.drawGuiContainerBackgroundLayer(f, x, y);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
int j = (width - xSize) / 2;
int k = (height - ySize) / 2;
drawFluid(canner.getFluid(), canner.getScaledLiquid(52), j+44, k+17, 16, 52);
mc.renderEngine.bindTexture(texture);
drawTexturedModalRect(j + 44, k, 176, 0, 16, 70);
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
super.drawGuiContainerForegroundLayer(par1, par2);
}
private void drawFluid(FluidStack fluid, int level, int x, int y, int width, int height){
if(fluid == null || fluid.getFluid() == null) {
return;
}
IIcon icon = fluid.getFluid().getIcon(fluid);
mc.renderEngine.bindTexture(BLOCK_TEXTURE);
RenderUtils.setGLColorFromInt(fluid.getFluid().getColor(fluid));
int fullX = width / 16;
int fullY = height / 16;
int lastX = width - fullX * 16;
int lastY = height - fullY * 16;
int fullLvl = (height - level) / 16;
int lastLvl = (height - level) - fullLvl * 16;
for(int i = 0; i < fullX; i++) {
for(int j = 0; j < fullY; j++) {
if(j >= fullLvl) {
drawCutIcon(icon, x + i * 16, y + j * 16, 16, 16, j == fullLvl ? lastLvl : 0);
}
}
}
for(int i = 0; i < fullX; i++) {
drawCutIcon(icon, x + i * 16, y + fullY * 16, 16, lastY, fullLvl == fullY ? lastLvl : 0);
}
for(int i = 0; i < fullY; i++) {
if(i >= fullLvl) {
drawCutIcon(icon, x + fullX * 16, y + i * 16, lastX, 16, i == fullLvl ? lastLvl : 0);
}
}
drawCutIcon(icon, x + fullX * 16, y + fullY * 16, lastX, lastY, fullLvl == fullY ? lastLvl : 0);
}
//The magic is here
private void drawCutIcon(IIcon icon, int x, int y, int width, int height, int cut){
Tessellator tess = Tessellator.instance;
tess.startDrawingQuads();
tess.addVertexWithUV(x, y + height, zLevel, icon.getMinU(), icon.getInterpolatedV(height));
tess.addVertexWithUV(x + width, y + height, zLevel, icon.getInterpolatedU(width), icon.getInterpolatedV(height));
tess.addVertexWithUV(x + width, y + cut, zLevel, icon.getInterpolatedU(width), icon.getInterpolatedV(cut));
tess.addVertexWithUV(x, y + cut, zLevel, icon.getMinU(), icon.getInterpolatedV(cut));
tess.draw();
}
}
package buildcraft.factory;
import java.io.IOException;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.ItemFluidContainer;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
import buildcraft.api.mj.MjBattery;
import buildcraft.core.GuiIds;
import buildcraft.core.IItemPipe;
import buildcraft.core.ItemDiamondCanister;
import buildcraft.core.ItemGoldCanister;
import buildcraft.core.ItemIronCannister;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.fluids.FluidUtils;
import buildcraft.core.fluids.SingleUseTank;
import buildcraft.core.fluids.TankManager;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.network.ISynchronizedTile;
import buildcraft.core.network.NetworkData;
import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketUpdate;
public class TileCanner extends TileBuildCraft implements IInventory, IFluidHandler{
private final SimpleInventory _inventory = new SimpleInventory(3, "Canner", 1);
public final int maxLiquid = FluidContainerRegistry.BUCKET_VOLUME * 10;
@MjBattery (maxCapacity = 5000.0, maxReceivedPerCycle = 25.0)
public double energyStored = 0;
public SingleUseTank tank = new SingleUseTank("tank", maxLiquid, this);
private TankManager tankManager = new TankManager();
@NetworkData
public int mode = 1;
public TileCanner(){
tankManager.add(tank);
}
@Override
public void updateEntity() {
sendNetworkUpdate();
System.out.println(mode);
if (_inventory.getStackInSlot(0) != null && !tank.isEmpty()){
ItemFluidContainer item = null;
if (_inventory.getStackInSlot(0).getItem() == BuildCraftCore.ironCannister){
item = (ItemIronCannister) _inventory.getStackInSlot(0).getItem();
ItemIronCannister item2 = (ItemIronCannister) item;
}
if (_inventory.getStackInSlot(0).getItem() == BuildCraftCore.goldCanister){
item = (ItemGoldCanister) _inventory.getStackInSlot(0).getItem();
}
if (_inventory.getStackInSlot(0).getItem() == BuildCraftCore.diamondCanister){
item = (ItemDiamondCanister) _inventory.getStackInSlot(0).getItem();
}
if (item != null){
int amount = 10;
if (mode == 1){
if (tank.getFluid().amount <50)
amount = tank.getFluid().amount;
if (energyStored >= amount){
tank.drain(item.fill(_inventory.getStackInSlot(0), new FluidStack(tank.getFluid(), amount), true), true);
energyStored = energyStored - amount;
FluidStack fluid = ItemIronCannister.getFluidStackFromItemStack(_inventory.getStackInSlot(0));
if (fluid != null){
if ((item instanceof ItemIronCannister && fluid.amount == 1000)
|| (item instanceof ItemGoldCanister && fluid.amount == 3000)
|| (item instanceof ItemDiamondCanister && fluid.amount == 9000)){
_inventory.setInventorySlotContents(1, _inventory.getStackInSlot(0));
_inventory.setInventorySlotContents(0, null);
}
}
}
} else {
if (_inventory.getStackInSlot(0) != null && !tank.isFull()){
}
}
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbtTagCompound) {
super.readFromNBT(nbtTagCompound);
NBTTagCompound p = (NBTTagCompound) nbtTagCompound.getTag("inventory");
_inventory.readFromNBT(p);
tankManager.readFromNBT(nbtTagCompound);
mode = nbtTagCompound.getInteger("mode");
}
@Override
public void writeToNBT(NBTTagCompound nbtTagCompound) {
super.writeToNBT(nbtTagCompound);
NBTTagCompound inventoryTag = new NBTTagCompound();
_inventory.writeToNBT(inventoryTag);
nbtTagCompound.setTag("inventory", inventoryTag);
tankManager.writeToNBT(nbtTagCompound);
nbtTagCompound.setInteger("mode", mode);
}
@Override
public int getSizeInventory() {
return _inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slotId) {
return _inventory.getStackInSlot(slotId);
}
@Override
public ItemStack decrStackSize(int slotId, int count) {
return _inventory.decrStackSize(slotId, count);
}
@Override
public ItemStack getStackInSlotOnClosing(int var1) {
return _inventory.getStackInSlotOnClosing(var1);
}
@Override
public void setInventorySlotContents(int slotId, ItemStack itemstack) {
_inventory.setInventorySlotContents(slotId, itemstack);
}
@Override
public String getInventoryName() {
return _inventory.getInventoryName();
}
@Override
public boolean hasCustomInventoryName() {
return _inventory.hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit() {
return _inventory.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && entityPlayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64.0D;
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
@Override
public boolean isItemValidForSlot(int slotid, ItemStack itemStack) {
return _inventory.isItemValidForSlot(slotid, itemStack);
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
int temp = tank.fill(resource, doFill);
this.sendNetworkUpdate();
return temp;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource,
boolean doDrain) {
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
FluidStack temp = tank.drain(maxDrain, doDrain);
sendNetworkUpdate();
return temp;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return tank.getFluidType() == fluid;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return tankManager.getTankInfo(from);
}
public FluidStack getFluid(){
return tank.getFluid();
}
public int getScaledLiquid(int i) {
return tank.getFluid() != null ? (int) (((float) this.tank.getFluid().amount / (float) (maxLiquid)) * i) : 0;
}
@Override
public PacketPayload getPacketPayload() {
PacketPayload payload = new PacketPayload(new PacketPayload.StreamWriter() {
@Override
public void writeData(ByteBuf data) {
tankManager.writeData(data);
data.writeInt(mode);
}
});
return payload;
}
@Override
public void handleUpdatePacket(PacketUpdate packet) throws IOException {
ByteBuf stream = packet.payload.stream;
tankManager.readData(stream);
mode = stream.readInt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment