Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.7.10]All Tile entities affected by changes to others


grand_mind1
 Share

Recommended Posts

First off, sorry if that title doesn't make any sense whatsoever I couldn't think of anything. Anyways, hopefully I can explain my problem a bit better. Thanks to the help of these forums, my tile entity has two internal tanks. My problem is these tanks are universal for every tile entity of the same type created in the world. So if I deposit two buckets of water in one, two buckets of water will be deposited into every tile entity. I'm fairly certain that the problem is only with the tanks and not the whole tile entity, however I haven't thought of a way to make sure.

 

My Block class:

 

public class BlockCoffeeMaker extends BlockContainer implements ITileEntityProvider
{
    TileEntityCoffeeMaker instance;

    public BlockCoffeeMaker()
    {
        super(Material.rock);
        this.setCreativeTab(CreativeTabACOJ.ACOJ_TAB);
        this.setBlockName(Names.COFFEE_MAKER);
    }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int meta, float hitX, float hitY, float hitZ)
    {
        if (!world.isRemote && world.getTileEntity(x, y, z) instanceof TileEntityCoffeeMaker)
        {
            entityPlayer.openGui(ACOJ.instance, GUIs.COFFEE_MAKER.ordinal(), world, x, y, z);
            ((TileEntityCoffeeMaker) world.getTileEntity(x,y,z)).fill(ForgeDirection.EAST, new FluidStack(ModFluids.LiquidCoffee, 12000), true);
            //((TileEntityCoffeeMaker) world.getTileEntity(x,y,z)).fill(ForgeDirection.EAST, new FluidStack(FluidRegistry.WATER, 12000), true);
        }
        return true;
    }

    @Override
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {
        ArrayList<ItemStack> itemStacks = Lists.newArrayList();
        ItemStack itemStack = new ItemStack(ModBlocks.BlockCoffeeMaker, 1, metadata);
        itemStacks.add(itemStack);
        TileEntityCoffeeMaker coffeeMaker = getInstance();
        if (coffeeMaker.getStackInSlot(0) != null) itemStacks.add(coffeeMaker.getStackInSlot(0));
        if (coffeeMaker.getStackInSlot(1) != null) itemStacks.add(coffeeMaker.getStackInSlot(1));
        if (coffeeMaker.getStackInSlot(2) != null) itemStacks.add(coffeeMaker.getStackInSlot(2));
        if (coffeeMaker.getStackInSlot(3) != null) itemStacks.add(coffeeMaker.getStackInSlot(3));
        return itemStacks;
    }

    @Override
    public TileEntity createNewTileEntity(World world, int meta)
    {
        instance = new TileEntityCoffeeMaker();
        return instance;
    }

    @Override
    public boolean hasTileEntity()
    {
        return true;
    }
    public TileEntityCoffeeMaker getInstance()
    {
        return instance;
    }

    @Override
    public String getUnlocalizedName()
    {
        return String.format("tile.%s%s", Reference.MOD_ID.toLowerCase() + ":", getUnwrappedUnlocalizedName(super.getUnlocalizedName()));
    }

    protected String getUnwrappedUnlocalizedName(String unlocalizedName)
    {
        return unlocalizedName.substring(unlocalizedName.indexOf(".") + 1);
    }
}

 

 

My tile entity class:

 

public class TileEntityCoffeeMaker extends TileEntityInventory implements IFluidHandler
{

    public TileEntityCoffeeMaker()
    {
        super();
        this.size = 4;
        this.invName = Names.COFFEE_MAKER;
    }

    public static String getName()
    {
        return Names.TILE_COFFEE_MAKER;
    }

    int ticker = 100;

    @Override
    public void updateEntity()
    {
        if (getStackInSlot(0) != null)
        {
            if (getStackInSlot(0).isItemEqual(new ItemStack(Items.water_bucket)))
            {
                decrStackSize(0, 1);
                setInventorySlotContents(0, new ItemStack(Items.bucket));
                fill(ForgeDirection.EAST, new FluidStack(FluidRegistry.WATER, 1000), true);
            }
        }
        if (getCoffeeTank().getFluidAmount() >= 1000)
        {
            if (getStackInSlot(3) != null)
            {
                if (getStackInSlot(3).isItemEqual(new ItemStack(ModItems.ItemCoffeeMug)))
                {
                    decrStackSize(3, 1);
                    setInventorySlotContents(3, new ItemStack(ModItems.BeverageBlackCoffee));
                    drain(ForgeDirection.EAST, new FluidStack(ModFluids.LiquidCoffee, 1000), true);
                }
                else if (getStackInSlot(3).isItemEqual(new ItemStack(ModItems.ItemLargeCoffeeMug)))
                {
                    decrStackSize(3, 1);
                    setInventorySlotContents(3, new ItemStack(ModItems.BeverageBlackCoffeeLarge, 1));
                    drain(ForgeDirection.EAST, new FluidStack(ModFluids.LiquidCoffee, 1000), true);
                }
            }
        }
    }



    public static FluidTank waterTank = new FluidTank(12000);
    public static FluidTank coffeeTank = new FluidTank(12000);

    public static FluidTank getWaterTank()
    {
        return waterTank;
    }

    public static FluidTank getCoffeeTank()
    {
        return coffeeTank;
    }

    @Override
    public void readFromNBT(NBTTagCompound tag)
    {
        super.readFromNBT(tag);
        waterTank.readFromNBT(tag);
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        super.writeToNBT(tag);
        waterTank.writeToNBT(tag);
    }

    @Override
    public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
    {
        if (resource.isFluidEqual(new FluidStack(FluidRegistry.WATER, 1)))
        {
            return waterTank.fill(resource, doFill);
        } else if (resource.isFluidEqual(new FluidStack(ModFluids.LiquidCoffee, 1)))
        {
            return coffeeTank.fill(resource, doFill);
        }
        return 0;
    }

    @Override
    public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain)
    {
        if (resource == null || !resource.isFluidEqual(waterTank.getFluid()) || !resource.isFluidEqual(coffeeTank.getFluid()))
        {
            return null;
        } else if (resource.isFluidEqual(waterTank.getFluid()))
        {
            return waterTank.drain(resource.amount, doDrain);
        } else if (resource.isFluidEqual(coffeeTank.getFluid()))
        {
            return coffeeTank.drain(resource.amount, doDrain);
        }
        return null;
    }

    @Override
    public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
    {
        if (from.equals(ForgeDirection.EAST))
        {
            return waterTank.drain(maxDrain, doDrain);
        }
        return coffeeTank.drain(maxDrain, doDrain);
    }

    @Override
    public boolean canFill(ForgeDirection from, Fluid fluid)
    {
        if (fluid == FluidRegistry.WATER || fluid == ModFluids.LiquidCoffee)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean canDrain(ForgeDirection from, Fluid fluid)
    {
        return true;
    }

    @Override
    public FluidTankInfo[] getTankInfo(ForgeDirection from)
    {
        return new FluidTankInfo[]{waterTank.getInfo(), coffeeTank.getInfo()};
    }
}

 

 

If I've forgotten to include any relevant code, please tell me.

Thanks! :D

Link to comment
Share on other sites

Hey, I've been trying to get this to work for a bit now, but I can't seem to get it. For my Gui class to draw the fluid in each tank, it must be passed each of the tanks. To do this, I need to somehow get the instance of the tile entity in the Gui class. Do you have any suggestions?

 

Gui Class:

 

public class GuiCoffeeMaker extends GuiContainer
{
    private int x, y, z;
    private EntityPlayer entityPlayer;
    private World world;
    private int xSize, ySize;
    private ResourceLocation backgroundImage = new ResourceLocation(Reference.MOD_ID.toLowerCase() + ":textures/gui/GuiCoffeeMaker.png");

    public GuiCoffeeMaker(EntityPlayer entityPlayer, World world, int x, int y, int z)
    {
        super(new ContainerCofffeeMaker(entityPlayer, (IInventory) world.getTileEntity(x, y, z), 176, 170));
        this.x = x;
        this.y = y;
        this.z = z;
        this.entityPlayer = entityPlayer;
        this.world = world;
        xSize = 176;
        ySize = 170;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(backgroundImage);
        int x = (this.width - xSize) / 2;
        int y = (this.height - ySize) / 2;
        drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
        drawFluidTank(TileEntityCoffeeMaker.getWaterTank(), x + 35, y + 17); //Lines that must utilize the instance
        drawFluidTank(TileEntityCoffeeMaker.getCoffeeTank(), x + 126, y + 17); //------
        fontRendererObj.drawString(StatCollector.translateToLocal("acoj.inv.coffeeMaker"), x + 56, y + 4, 0x313131);
        fontRendererObj.drawString(StatCollector.translateToLocal("acoj.inv.Inventory"), x + 9, y + 79, 0x313131);
    }

    @Override
    public boolean doesGuiPauseGame()
    {
        return false;
    }

    public void drawFluidTank(IFluidTank tank, int x, int y) //Method that must be passed the tank
    {
        FluidStack fluid = tank.getFluid();
        TextureManager manager = Minecraft.getMinecraft().renderEngine;
        if (fluid != null)
        {
            manager.bindTexture(manager.getResourceLocation(0));
            float amount = fluid.amount;
            float capacity = tank.getCapacity();
            float scale = amount / capacity;
            int fluidTankHeight = 60;
            int fluidAmount = (int) (scale * fluidTankHeight);
            drawFluid(x, y + fluidTankHeight - fluidAmount, fluid.getFluid().getIcon(fluid), 16, fluidAmount);
        }
    }

    private void drawFluid(int x, int y, IIcon icon, int width, int height)
    {
        int i = 0;
        int j = 0;

        int drawHeight = 0;
        int drawWidth = 0;

        for (i = 0; i < width; i += 16)
        {
            for (j = 0; j < height; j += 16)
            {
                drawWidth = Math.min(width - i, 16);
                drawHeight = Math.min(height - j, 16);
                drawRectangleFromIcon(x + i, y + j, icon, drawWidth, drawHeight);
            }
        }
    }

    private void drawRectangleFromIcon(int x, int y, IIcon icon, int width, int height)
    {
        if (icon == null)
        {
            LogHelper.info("Null");
            return;
        }
        double minU = icon.getMinU();
        double maxU = icon.getMaxU();
        double minV = icon.getMinV();
        double maxV = icon.getMaxV();

        Tessellator tessellator = Tessellator.instance;
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV(x, y + height, 0, minU, minV + (maxV - minV) * height / 16.0D);
        tessellator.addVertexWithUV(x + width, y + height, 0, minU + (maxU - minU) * width / 16.0D, minV + (maxV - minV) * height / 16.0D);
        tessellator.addVertexWithUV(x + width, y, 0, minU + (maxU - minU) * width / 16.0D, minV);
        tessellator.addVertexWithUV(x, y, 10, minU, minV);
        tessellator.draw();
    }
}

 

 

Tile Entity Class:

 

public class TileEntityCoffeeMaker extends TileEntityInventory implements IFluidHandler
{

    public TileEntityCoffeeMaker()
    {
        super();
        this.size = 4;
        this.invName = Names.COFFEE_MAKER;
    }

    public static String getName()
    {
        return Names.TILE_COFFEE_MAKER;
    }

    @Override
    public void updateEntity()
    {
        if (getStackInSlot(0) != null)
        {
            if (getStackInSlot(0).isItemEqual(new ItemStack(Items.water_bucket)))
            {
                decrStackSize(0, 1);
                setInventorySlotContents(0, new ItemStack(Items.bucket));
                fill(ForgeDirection.EAST, new FluidStack(FluidRegistry.WATER, 1000), true);
            }
        }
        if (getCoffeeTank().getFluidAmount() >= 1000)
        {
            if (getStackInSlot(3) != null)
            {
                if (getStackInSlot(3).isItemEqual(new ItemStack(ModItems.ItemCoffeeMug)))
                {
                    decrStackSize(3, 1);
                    setInventorySlotContents(3, new ItemStack(ModItems.BeverageBlackCoffee));
                    drain(ForgeDirection.EAST, new FluidStack(ModFluids.LiquidCoffee, 1000), true);
                }
                else if (getStackInSlot(3).isItemEqual(new ItemStack(ModItems.ItemLargeCoffeeMug)))
                {
                    decrStackSize(3, 1);
                    setInventorySlotContents(3, new ItemStack(ModItems.BeverageBlackCoffeeLarge, 1));
                    drain(ForgeDirection.EAST, new FluidStack(ModFluids.LiquidCoffee, 1000), true);
                }
            }
        }
    }



    public FluidTank waterTank = new FluidTank(12000);
    public FluidTank coffeeTank = new FluidTank(12000);

    public FluidTank getWaterTank()
    {
        return waterTank;
    }

    public FluidTank getCoffeeTank()
    {
        return coffeeTank;
    }

    @Override
    public void readFromNBT(NBTTagCompound tag)
    {
        super.readFromNBT(tag);
        waterTank.readFromNBT(tag);
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        super.writeToNBT(tag);
        waterTank.writeToNBT(tag);
    }

    @Override
    public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
    {
        if (resource.isFluidEqual(new FluidStack(FluidRegistry.WATER, 1)))
        {
            return waterTank.fill(resource, doFill);
        } else if (resource.isFluidEqual(new FluidStack(ModFluids.LiquidCoffee, 1)))
        {
            return coffeeTank.fill(resource, doFill);
        }
        return 0;
    }

    @Override
    public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain)
    {
        if (resource == null || !resource.isFluidEqual(waterTank.getFluid()) || !resource.isFluidEqual(coffeeTank.getFluid()))
        {
            return null;
        } else if (resource.isFluidEqual(waterTank.getFluid()))
        {
            return waterTank.drain(resource.amount, doDrain);
        } else if (resource.isFluidEqual(coffeeTank.getFluid()))
        {
            return coffeeTank.drain(resource.amount, doDrain);
        }
        return null;
    }

    @Override
    public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
    {
        if (from.equals(ForgeDirection.EAST))
        {
            return waterTank.drain(maxDrain, doDrain);
        }
        return coffeeTank.drain(maxDrain, doDrain);
    }

    @Override
    public boolean canFill(ForgeDirection from, Fluid fluid)
    {
        if (fluid == FluidRegistry.WATER || fluid == ModFluids.LiquidCoffee)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean canDrain(ForgeDirection from, Fluid fluid)
    {
        return true;
    }

    @Override
    public FluidTankInfo[] getTankInfo(ForgeDirection from)
    {
        return new FluidTankInfo[]{waterTank.getInfo(), coffeeTank.getInfo()};
    }
}

 

Link to comment
Share on other sites

By the way, you also have this in your block class:

 

TileEntityCoffeeMaker instance;

 

That's a no-no, as the Block class is a singleton and you want to have more than one TileEntity instance.  That's why world.getTileEntity(x,y,z) exists.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.