Jump to content

[1.7.10] [Almost Solved] Can't access player inventory in custom container.


Recommended Posts

Posted

Hi everyone. I recently started work on a steam engine that produces rf (RFApi) using and water. But I have been having problems with it. I will solve the problems one at a time.

 

The first problem I'm having is with the inventory/container. When I right click to open the inventory, it opens fine, but when I try to drag an item from the player inventory I can't. The item jumps back to it's slot which is really weird. I can shift-click items in but then when I click on the item in the fuel slot it disappears. This is REALLY weird.

 

Any idea on why this is happening? Am I doing something wrong? Did I not register something?

 

 

My gui:

public class GuiSteamEngine extends GuiContainer {
    TileEntitySteamEngine tileSteamEngine;

    public GuiSteamEngine(InventoryPlayer inventory, TileEntitySteamEngine tileSteamEngine) {
        super(new ContainerSteamEngine(inventory, tileSteamEngine));
        this.tileSteamEngine = tileSteamEngine;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float par1, int mouseX, int mouseY) {
        TextureUtil.bindTexture(Usus.MODID, "textures/guis/guiSteamEngine.png");
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

        //Progress/quantity bars
        int scaledRf = this.tileSteamEngine.scaleRfForDisplay(30);
        this.drawTexturedModalRect(guiLeft + 111, guiTop + 40, 176, 31, 14, scaledRf);
        int scaledWater = this.tileSteamEngine.scaleWaterForDisplay(30);
        this.drawTexturedModalRect(guiLeft + 39, guiTop + 38, 176, 61, 14, scaledWater);

        if(tileSteamEngine.burningFuel){
            int i1 = tileSteamEngine.getBurnTimeRemainingScaled(13);
            this.drawTexturedModalRect(guiLeft + 56, guiTop + 25 + 12 - i1, 176, 12 - i1, 14, i1 + 1);
            i1 = tileSteamEngine.getCookProgressScaled(24);
            this.drawTexturedModalRect(guiLeft + 80, guiTop + 47, 176, 14, i1 + 1, 16);
        }
    }
}

 

My Container:

public class ContainerSteamEngine extends Container {
    protected TileEntitySteamEngine tileEntity;

    public ContainerSteamEngine (InventoryPlayer inventoryPlayer, TileEntitySteamEngine te){
        tileEntity = te;

        addSlotToContainer(new Slot(tileEntity, 0, 56, 45));//Add fuel slot
        addSlotToContainer(new Slot(tileEntity, 1, 57, 63));//Add ash slot

        //commonly used vanilla code that adds the player's inventory
        bindPlayerInventory(inventoryPlayer);
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return tileEntity.isUseableByPlayer(player);
    }


    protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                        8 + j * 18, 84 + i * 18));
            }
        }

        for (int i = 0; i < 9; i++) {
            addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
        ItemStack stack = null;
        Slot slotObject = (Slot) inventorySlots.get(slot);

        //null checks and checks if the item can be stacked (maxStackSize > 1)
        if (slotObject != null && slotObject.getHasStack()) {
            ItemStack stackInSlot = slotObject.getStack();
            stack = stackInSlot.copy();

            //merges the item into player inventory since its in the tileEntity
            if (slot < tileEntity.getSizeInventory()) {
                if (!this.mergeItemStack(stackInSlot, tileEntity.getSizeInventory(), 36+tileEntity.getSizeInventory(), true)) {
                    return null;
                }
            }
            //places it into the tileEntity is possible since its in the player inventory
            else if (!this.mergeItemStack(stackInSlot, 0, tileEntity.getSizeInventory(), false)) {
                return null;
            }

            if (stackInSlot.stackSize == 0) {
                slotObject.putStack(null);
            } else {
                slotObject.onSlotChanged();
            }

            if (stackInSlot.stackSize == stack.stackSize) {
                return null;
            }
            slotObject.onPickupFromSlot(player, stackInSlot);
        }
        return stack;
    }
}

 

My TileEntity:

public ForgeDirection facing;

    int fuelSlot = 0;
    int ashSlot = 1;
    ItemStack[] slots = new ItemStack[2];

    public int waterMb = 0;
    int maxWaterMb = 2500;

    public boolean burningFuel = false;
    public ItemStack currentBurningFuel = null;
    int progress = 0;
    int burningCountdown = 0;
    int rfToGenerate = 0;

    IEnergyStorage energyStorage = new IEnergyStorage() {
        int stored = 0;
        int max = 250;

        @Override
        public int receiveEnergy(int maxReceive, boolean simulate) {
            int remainder = maxReceive;
            int accepted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored < max && remainder > 0){
                    remainder --;
                    if (!simulate) stored ++;
                    accepted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return accepted;
        }

        @Override
        public int extractEnergy(int maxExtract, boolean simulate) {//TODO Clean this up for efficiency. Make a class named "TileEntityEnergyCreator" and have this code in there. Then extend that class in other tile entities and call supers.
            int remainder = maxExtract;
            int extracted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored > remainder && remainder > 0){
                    remainder --;
                    if (!simulate) stored --;
                    extracted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return extracted;
        }

        @Override
        public int getEnergyStored() {
            return stored;
        }

        @Override
        public int getMaxEnergyStored() {
            return max;
        }
    };

    @Override
    public void updateEntity(){
        burningFuel = burningCountdown > 0;

        if (getStackInSlot(fuelSlot) != null)
            if (isFuel(getStackInSlot(fuelSlot)) && waterMb > 0) {
                burningCountdown = getFuelBurnTime(getStackInSlot(fuelSlot));
                rfToGenerate = getFuelRf(getStackInSlot(fuelSlot));
                currentBurningFuel = getStackInSlot(fuelSlot);
                decrStackSize(fuelSlot, 1);

                markDirty();
            }

        if (burningFuel){
            burningCountdown --;
            rfToGenerate -= energyStorage.receiveEnergy(1, false);
            waterMb --;

            markDirty();

            if (burningCountdown == 0){
                if (currentBurningFuel != null) {
                    if (doesFuelMakeAsh(currentBurningFuel)) {
                        if (getStackInSlot(ashSlot).getItem() == Usus.ash) {
                            int quantity = getStackInSlot(ashSlot).stackSize;

                            setInventorySlotContents(ashSlot, new ItemStack(Usus.ash, quantity + 1));
                        }
                    }

                    currentBurningFuel = null;
                }
            }
        }
    }

    public boolean isFuel(ItemStack stack){
        return getFuelBurnTime(stack) > 0;
    }

    public boolean doesFuelMakeAsh(ItemStack stack){
        Item item = stack.getItem();
        return item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD") |
                item == Items.stick |
                item == Items.coal |
                item == Item.getItemFromBlock(Blocks.sapling);
    }

    //Taken almost all from tile entity furnace class
    public int getFuelBurnTime(ItemStack stack){
        Item item = stack.getItem();

        if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
            Block block = Block.getBlockFromItem(item);

            if (block == Blocks.wooden_slab) {
                return 150;
            }

            if (block.getMaterial() == Material.wood) {
                return 300;
            }

            if (block == Blocks.coal_block) {
                return 16000;
            }
        }

        if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item == Items.stick) return 100;
        if (item == Items.coal) return 1600;
        if (item == Items.lava_bucket) return 20000;
        if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
        if (item == Items.blaze_rod) return 2400;
        return GameRegistry.getFuelValue(stack);
    }

    public int getFuelRf(ItemStack stack){
        return getFuelBurnTime(stack) / 4;
    }

    public void addWaterBucketToTank(){
        int remainder = 1000;
        int added = 0;
        boolean waterTransferComplete = false;

        while (!waterTransferComplete){
            if (waterMb < maxWaterMb && remainder > 0){
                remainder --;
                waterMb --;
                added ++;
            } else {
                waterTransferComplete = true;
            }
        }
    }

    public int scaleRfForDisplay(int maxNumber){
        return energyStorage.getEnergyStored() * maxNumber / energyStorage.getMaxEnergyStored();
    }

    public int scaleWaterForDisplay(int maxNumber){
        return waterMb * maxNumber / maxWaterMb;
    }

    @SideOnly(Side.CLIENT)
    public int getCookProgressScaled(int maxNumber) {
        return progress * maxNumber / 200;
    }

    @SideOnly(Side.CLIENT)
    public int getBurnTimeRemainingScaled(int maxNumber) {
        if (this.burningCountdown == 0) {
            this.burningCountdown = 200;
        }

        return this.progress * maxNumber / this.burningCountdown;
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        NBTTagList listOfItems = nbt.getTagList("Items", 10);
        slots = new ItemStack[getSizeInventory()];

        for (int i = 0; i < listOfItems.tagCount(); i++)
        {
            NBTTagCompound slotNbt = listOfItems.getCompoundTagAt(i);
            byte b0 = slotNbt.getByte("Slot");

            if (b0 >= 0 && b0 < slots.length)
            {
                slots[b0] = ItemStack.loadItemStackFromNBT(slotNbt);
            }
        }

        waterMb = nbt.getInteger("WaterMb");
        progress = nbt.getInteger("BurnProgress");
        burningCountdown = nbt.getInteger("BurningTimer");
        currentBurningFuel = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("CurrentBurningFuel")); //TODO Confirm that this works.
        rfToGenerate = nbt.getInteger("RfToGenerate");

        energyStorage.receiveEnergy(nbt.getInteger("RF"), false);

        facing = ForgeDirection.getOrientation(nbt.getInteger("Facing"));
    }

    @Override
    public void writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);

        NBTTagList listOfItems = new NBTTagList();

        for (int i = 0; i < slots.length; i++){
            if (slots[i] != null) {
                NBTTagCompound slotNbt = new NBTTagCompound();
                slotNbt.setByte("Slot", (byte) i);
                this.slots[i].writeToNBT(slotNbt);
                listOfItems.appendTag(slotNbt);
            }
        }

        nbt.setTag("Items", listOfItems);

        nbt.setInteger("WaterMb", waterMb);
        nbt.setInteger("BurnProgress", progress);
        nbt.setInteger("BurningTimer", burningCountdown);
        NBTTagCompound currentBurningFuelTag = new NBTTagCompound();
        currentBurningFuel.writeToNBT(currentBurningFuelTag);
        nbt.setTag("CurrentBurningFuel", currentBurningFuelTag);
        nbt.setInteger("RfToGenerate", rfToGenerate);

        nbt.setInteger("RF", energyStorage.getEnergyStored());

        if (facing.equals(ForgeDirection.NORTH)){
            nbt.setInteger("Facing", 3);
        } else if (facing.equals(ForgeDirection.SOUTH)){
            nbt.setInteger("Facing", 4);
        } else if (facing.equals(ForgeDirection.WEST)){
            nbt.setInteger("Facing", 5);
        } else if (facing.equals(ForgeDirection.EAST)){
            nbt.setInteger("Facing", 6);
        }
    }

    @Override
    public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) {
        if (from != ForgeDirection.UP && from != ForgeDirection.DOWN){
            return energyStorage.extractEnergy(maxExtract, simulate);
        } else {
            return 0;
        }
    }

    @Override
    public int getEnergyStored(ForgeDirection from) {
        return energyStorage.getEnergyStored();
    }

    @Override
    public int getMaxEnergyStored(ForgeDirection from) {
        return energyStorage.getMaxEnergyStored();
    }

    @Override
    public boolean canConnectEnergy(ForgeDirection from) {
        return from != ForgeDirection.UP && from != ForgeDirection.DOWN;
    }

    @Override
    public int getSizeInventory() {
        return 2;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
        return slots[slot];
    }

    @Override
    public ItemStack decrStackSize(int slot, int amount) {
        if (slots[slot].stackSize - amount > 0) {
            slots[slot].stackSize -= amount;
        } else {
            slots[slot] = null;
        }

        return slots[slot];
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
        return slots[slot];
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
        slots[slot] = stack;
    }

    @Override
    public String getInventoryName() {
        return "inv.steamEngine.name"; //TODO Confirm that I'm doing this right. Also, if I am, add this to the lang file.
    }

    @Override
    public boolean hasCustomInventoryName() {
        return true;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

    @Override
    public void openInventory() {}

    @Override
    public void closeInventory() {}

    @Override
    public boolean isItemValidForSlot(int slot, ItemStack stack) {
        if (slot == fuelSlot){
            return isFuel(stack);
        } else if (slot == ashSlot){
           return stack.getItem() == Usus.ash;
        } else {
            return true;
        }
    }
}

 

 

Posted

Ok, here they are:

My block class (where I open the gui):

public boolean onBlockActivated(World world, int posX, int posY, int posZ, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
        if (player.inventory.getCurrentItem() != null && player.inventory.getCurrentItem().getItem() == Items.water_bucket){
            TileEntitySteamEngine te = (TileEntitySteamEngine) world.getTileEntity(posX, posY, posZ);
            te.addWaterBucketToTank();
            player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(Items.bucket));
        } else {
            player.openGui(Usus.instance, 1, world, posX, posY, posZ);
        }

        return true;
    }

 

My GuiHandler:

public class GuiHandler implements IGuiHandler {
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if (ID == 1){
            return new GuiSteamEngine(player.inventory, (TileEntitySteamEngine) world.getTileEntity(x, y, z));
        }

        return null;
    }
}

Posted

After further testing, I have found items go in fine, but when I try to grab the item out of the slot, they disappear.

 

Any ideas on why this is happening?

 

Here is my updated GuiHandler code:

public class GuiHandler implements IGuiHandler {
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if (ID == 1){
            return new ContainerSteamEngine(player.inventory, (TileEntitySteamEngine) world.getTileEntity(x, y, z));
        }

        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if (ID == 1){
            return new GuiSteamEngine(player.inventory, (TileEntitySteamEngine) world.getTileEntity(x, y, z));
        }

        return null;
    }
}

 

My updated block code (where I open my gui):

public boolean onBlockActivated(World world, int posX, int posY, int posZ, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
        if (player.inventory.getCurrentItem() != null && player.inventory.getCurrentItem().getItem() == Items.water_bucket){
            TileEntitySteamEngine te = (TileEntitySteamEngine) world.getTileEntity(posX, posY, posZ);
            te.addWaterBucketToTank();
            player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(Items.bucket));
        } else {
            if (!world.isRemote) player.openGui(Usus.instance, 1, world, posX, posY, posZ);
        }

        return true;
    }

Posted

Also you should replace your slot for The Ashe with a slot that is preventing a player from placing stuff in there, and maybe replace your fuel slot with a slot that only allows fuel input

Posted

Your implementation of getStackInSlotOnClosing and decrStackSize is faulty.

 

What do mean by faulty? How do I fix them?

Posted

I did what you said and looked at the chest code, I put the code into getStackInSlotOnClosing and decrStackSize. The item doesn't disappear anymore, :). But now it does something different. When I click on the item to take it out of the gui, the item doesn't come out. Instead it produces a ghost item on my cursor and the real item stays in the steam engines inventory. It's really weird.

 

Here is my new tile entity code:

public class TileEntitySteamEngine extends TileEntity implements IEnergyProvider, IInventory {
    public ForgeDirection facing;

    int fuelSlot = 0;
    int ashSlot = 1;
    ItemStack[] slots = new ItemStack[2];

    public int waterMb = 0;
    int maxWaterMb = 2500;

    public boolean burningFuel = false;
    public ItemStack currentBurningFuel = null;
    int progress = 0;
    int burningCountdown = 0;
    int rfToGenerate = 0;

    IEnergyStorage energyStorage = new IEnergyStorage() {
        int stored = 0;
        int max = 250;

        @Override
        public int receiveEnergy(int maxReceive, boolean simulate) {
            int remainder = maxReceive;
            int accepted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored < max && remainder > 0){
                    remainder --;
                    if (!simulate) stored ++;
                    accepted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return accepted;
        }

        @Override
        public int extractEnergy(int maxExtract, boolean simulate) {//TODO Clean this up for efficiency. Make a class named "TileEntityEnergyCreator" and have this code in there. Then extend that class in other tile entities and call supers.
            int remainder = maxExtract;
            int extracted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored > remainder && remainder > 0){
                    remainder --;
                    if (!simulate) stored --;
                    extracted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return extracted;
        }

        @Override
        public int getEnergyStored() {
            return stored;
        }

        @Override
        public int getMaxEnergyStored() {
            return max;
        }
    };

    @Override
    public void updateEntity() {
        burningFuel = burningCountdown > 0;

        if (getStackInSlot(fuelSlot) != null)
           if (isFuel(getStackInSlot(fuelSlot)) && waterMb > 0) {
               burningCountdown = getFuelBurnTime(getStackInSlot(fuelSlot));
               rfToGenerate = getFuelRf(getStackInSlot(fuelSlot));
               currentBurningFuel = getStackInSlot(fuelSlot);
               decrStackSize(fuelSlot, 1);

               markDirty();
           }

        if (burningFuel) {
            burningCountdown--;
            rfToGenerate -= energyStorage.receiveEnergy(1, false);
            waterMb--;

            markDirty();

            if (burningCountdown == 0) {
                if (currentBurningFuel != null) {
                    if (doesFuelMakeAsh(currentBurningFuel)) {
                        if (getStackInSlot(ashSlot).getItem() == Usus.ash) {
                            int quantity = getStackInSlot(ashSlot).stackSize;

                            setInventorySlotContents(ashSlot, new ItemStack(Usus.ash, quantity + 1));
                        }
                    }

                    currentBurningFuel = null;
                }
            }
        }
    }

    public boolean isFuel(ItemStack stack){
        return getFuelBurnTime(stack) > 0;
    }

    public boolean doesFuelMakeAsh(ItemStack stack){
        Item item = stack.getItem();
        return item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD") |
                item == Items.stick |
                item == Items.coal |
                item == Item.getItemFromBlock(Blocks.sapling);
    }

    //Taken almost all from tile entity furnace class
    public int getFuelBurnTime(ItemStack stack){
        Item item = stack.getItem();

        if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
            Block block = Block.getBlockFromItem(item);

            if (block == Blocks.wooden_slab) {
                return 150;
            }

            if (block.getMaterial() == Material.wood) {
                return 300;
            }

            if (block == Blocks.coal_block) {
                return 16000;
            }
        }

        if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return 200;
        if (item == Items.stick) return 100;
        if (item == Items.coal) return 1600;
        if (item == Items.lava_bucket) return 20000;
        if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
        if (item == Items.blaze_rod) return 2400;
        return GameRegistry.getFuelValue(stack);
    }

    public int getFuelRf(ItemStack stack){
        return getFuelBurnTime(stack) / 4;
    }

    public void addWaterBucketToTank(){
        int remainder = 1000;
        int added = 0;
        boolean waterTransferComplete = false;

        while (!waterTransferComplete){
            if (waterMb < maxWaterMb && remainder > 0){
                remainder --;
                waterMb --;
                added ++;
            } else {
                waterTransferComplete = true;
            }
        }
    }

    public int scaleRfForDisplay(int maxNumber){
        return energyStorage.getEnergyStored() * maxNumber / energyStorage.getMaxEnergyStored();
    }

    public int scaleWaterForDisplay(int maxNumber){
        return waterMb * maxNumber / maxWaterMb;
    }

    @SideOnly(Side.CLIENT)
    public int getCookProgressScaled(int maxNumber) {
        return progress * maxNumber / 200;
    }

    @SideOnly(Side.CLIENT)
    public int getBurnTimeRemainingScaled(int maxNumber) {
        if (this.burningCountdown == 0) {
            this.burningCountdown = 200;
        }

        return this.progress * maxNumber / this.burningCountdown;
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        NBTTagList listOfItems = nbt.getTagList("Items", 10);
        slots = new ItemStack[getSizeInventory()];

        for (int i = 0; i < listOfItems.tagCount(); i++)
        {
            NBTTagCompound slotNbt = listOfItems.getCompoundTagAt(i);
            byte b0 = slotNbt.getByte("Slot");

            if (b0 >= 0 && b0 < slots.length)
            {
                slots[b0] = ItemStack.loadItemStackFromNBT(slotNbt);
            }
        }

        waterMb = nbt.getInteger("WaterMb");
        progress = nbt.getInteger("BurnProgress");
        burningCountdown = nbt.getInteger("BurningTimer");
        currentBurningFuel = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("CurrentBurningFuel")); //TODO Confirm that this works.
        rfToGenerate = nbt.getInteger("RfToGenerate");

        energyStorage.receiveEnergy(nbt.getInteger("RF"), false);

        facing = ForgeDirection.getOrientation(nbt.getInteger("Facing"));
    }

    @Override
    public void writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);

        NBTTagList listOfItems = new NBTTagList();

        for (int i = 0; i < slots.length; i++){
            if (slots[i] != null) {
                NBTTagCompound slotNbt = new NBTTagCompound();
                slotNbt.setByte("Slot", (byte) i);
                this.slots[i].writeToNBT(slotNbt);
                listOfItems.appendTag(slotNbt);
            }
        }

        nbt.setTag("Items", listOfItems);

        nbt.setInteger("WaterMb", waterMb);
        nbt.setInteger("BurnProgress", progress);
        nbt.setInteger("BurningTimer", burningCountdown);
        NBTTagCompound currentBurningFuelTag = new NBTTagCompound();
        currentBurningFuel.writeToNBT(currentBurningFuelTag);
        nbt.setTag("CurrentBurningFuel", currentBurningFuelTag);
        nbt.setInteger("RfToGenerate", rfToGenerate);

        nbt.setInteger("RF", energyStorage.getEnergyStored());

        if (facing.equals(ForgeDirection.NORTH)){
            nbt.setInteger("Facing", 3);
        } else if (facing.equals(ForgeDirection.SOUTH)){
            nbt.setInteger("Facing", 4);
        } else if (facing.equals(ForgeDirection.WEST)){
            nbt.setInteger("Facing", 5);
        } else if (facing.equals(ForgeDirection.EAST)){
            nbt.setInteger("Facing", 6);
        }
    }

    @Override
    public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) {
        if (from != ForgeDirection.UP && from != ForgeDirection.DOWN){
            return energyStorage.extractEnergy(maxExtract, simulate);
        } else {
            return 0;
        }
    }

    @Override
    public int getEnergyStored(ForgeDirection from) {
        return energyStorage.getEnergyStored();
    }

    @Override
    public int getMaxEnergyStored(ForgeDirection from) {
        return energyStorage.getMaxEnergyStored();
    }

    @Override
    public boolean canConnectEnergy(ForgeDirection from) {
        return from != ForgeDirection.UP && from != ForgeDirection.DOWN;
    }

    @Override
    public int getSizeInventory() {
        return 2;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
        return slots[slot];
    }

    @Override
    public ItemStack decrStackSize(int slot, int amount) {
        if (this.slots[slot] != null) {
            ItemStack itemstack;

            if (this.slots[slot].stackSize <= slot) {
                itemstack = this.slots[slot];
                this.slots[slot] = null;
                this.markDirty();
                return itemstack;
            }
            else {
                itemstack = this.slots[slot].splitStack(slot);

                if (this.slots[slot].stackSize == 0)
                {
                    this.slots[slot] = null;
                }

                this.markDirty();
                return itemstack;
            }
        }
        else {
            return null;
        }
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
        if (this.slots[slot] != null) {
            ItemStack itemstack = this.slots[slot];
            this.slots[slot] = null;
            return itemstack;
        }
        else {
            return null;
        }
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
        slots[slot] = stack;

        markDirty();
    }

    @Override
    public String getInventoryName() {
        return "inv.steamEngine.name"; //TODO Confirm that I'm doing this right. Also, if I am, add this to the lang file.
    }

    @Override
    public boolean hasCustomInventoryName() {
        return true;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

    @Override
    public void openInventory() {}

    @Override
    public void closeInventory() {}

    @Override
    public boolean isItemValidForSlot(int slot, ItemStack stack) {
        if (slot == fuelSlot){
            return isFuel(stack);
        } else if (slot == ashSlot){
           return false;
        } else {
            return true;
        }
    }
}

Posted

K, I'll put it on github. :) I was going to do that anyway, I just didn't have the time.

Posted

I must have been tired when I was coding that day. I fixed the problems except for the nbt one. I don't know how to fix that one.

 

Also, I made the changes you suggested in my repository. If you need anything else in there let me know.

Posted

Well, the issue is that

currentBurningFuel

can be null, in which case that line will throw a NPE.

 

I tried to fix it by putting an if statement checking if it isn't null. That didn't fix it.

 

Might want to include your .gitignore, other than that it seems fine now.

 

Ok, I'll do that.

Posted

Oh lol, oops. I took it off for some reason... I'll put it back.

Posted

It still isn't saving it's inventory after I close the world.

Posted

Well, my computer is having hard drive problems and just crashed. Besides that we are going on a month vacation. I won't on these forums very much until I get back and replace this pc's faulty hard drive. Don't expect me to reply anymore on this topic until things settle down. :)

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.