Пропадают вещи в контейнере

1,015
9
102
Как сделать так, что бы в контейнере не пропадали вещи, когда ты его закрываешь?
 
НУ как бы в ванильном майне нет у нас контейнера, так что код кидай)
 
Почему нету то? Сундуки, инвентарь вроде. Ладн.
Код:
package com.minebot.statics.gui;

import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerMagnet extends Container
{

    private static List<ItemStack> slots = null;
    private TileEntityMagnet te;
    
    public ContainerMagnet(IInventory playerInv, TileEntityMagnet te) {
        this.te = te;
        
        // Tile Entity, Slot 0-4, Slot IDs 0-4
        for(int i = 0; i <= 4; i++)
        {
            int x = 44 + i * 18;
            int y = 20;
            addSlotToContainer(new Slot((IInventory) te, i, x, y));
        }

        // Player Inventory, Slot 9-35, Slot IDs 9-35
        for (int y = 0; y < 3; ++y) {
            for (int x = 0; x < 9; ++x) {
                this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 51 + y * 18));
            }
        }

        // Player Inventory, Slot 0-8, Slot IDs 36-44
        for (int x = 0; x < 9; ++x) {
            this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 109));
        }
        
    }

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
         return te.isUseableByPlayer(playerIn);
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) 
    {
        ItemStack previous = null;
        Slot slot = (Slot) this.inventorySlots.get(fromSlot);

        if (slot != null && slot.getHasStack()) {
            ItemStack current = slot.getStack();
            previous = current.copy();

            if (fromSlot < 6) {
                if (!this.mergeItemStack(current, 5, 45, true))
                    return null;
            } else {
                if (!this.mergeItemStack(current, 0, 5, false))
                    return null;
            }

            if (current.stackSize == 0)
                slot.putStack((ItemStack) null);
            else
                slot.onSlotChanged();

            if (current.stackSize == previous.stackSize)
                return null;
            slot.onPickupFromSlot(playerIn, current);
        }
        return previous;
    }
    
}
[merge_posts_bbcode]Добавлено: 06.03.2016 19:48:25[/merge_posts_bbcode]

Ммм, а как? В onContainerClosed в переменную?

[merge_posts_bbcode]Добавлено: 06.03.2016 19:49:45[/merge_posts_bbcode]

А как потом его загрузить?
 
Помогите. Написал в тайле вот это, но не работает. Где ошибка? Я не могу найти
Код:
    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
        this.inv = new ItemStack[this.getSizeInventory()];

        if (compound.hasKey("CustomName", 8))
        {
            this.customName = compound.getString("CustomName");
        }

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound.getByte("Slot") & 255;

            if (j >= 0 && j < this.inv.length)
            {
                this.inv[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
            }
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        NBTTagList nbttaglist = new NBTTagList();

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

        compound.setTag("Items", nbttaglist);

        if (this.hasCustomName())
        {
            compound.setString("CustomName", this.customName);
        }
    }
 
Minebot написал(а):
Угу. А его надо регистрировать?
Тайл?Конечно
В клиент прокси
Код:
        GameRegistry.registerTileEntity(TEBlockDecorMonitor.class, "TEBlockDecorMonitor");
        ClientRegistry.bindTileEntitySpecialRenderer(TEBlockDecorMonitor.class, new TEMonitorRenderer());
 
Лолчик, я по своему делаю.
[merge_posts_bbcode]Добавлено: 06.03.2016 22:46:26[/merge_posts_bbcode]

А вообще кинь ВЕСЬ тайл.
 
Код:
package com.minebot.statics.gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IChatComponent;

public class TileEntityMagnet extends TileEntity implements IInventory
{
    private String customName;
    private ItemStack[] inv;

    public TileEntityMagnet(){ inv = new ItemStack[5]; }
    
    @Override
    public String getName() {
        return "gui.tileentitymagnet";
    }

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

    @Override
    public IChatComponent getDisplayName() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getSizeInventory() {
        return inv.length;
    }

    @Override
    public ItemStack getStackInSlot(int index) {
        return inv[index];
    }

    @Override
    public ItemStack decrStackSize(int slot, int amt) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
                if (stack.stackSize <= amt) {
                        setInventorySlotContents(slot, null);
                } else {
                        stack = stack.splitStack(amt);
                        if (stack.stackSize == 0) {
                                setInventorySlotContents(slot, null);
                        }
                }
        }
        return stack;
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
        if (this.inv[index] != null)
        {
            ItemStack itemstack = this.inv[index];
            this.inv[index] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        inv[index] = stack;
        if (stack != null && stack.stackSize > getInventoryStackLimit()) {
            stack.stackSize = getInventoryStackLimit();
        }    
    }

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

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return true;
    }

    @Override
    public void openInventory(EntityPlayer player) {

    }

    @Override
    public void closeInventory(EntityPlayer player) {

    }
    
    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
        this.inv = new ItemStack[this.getSizeInventory()];

        if (compound.hasKey("CustomName", 8))
        {
            this.customName = compound.getString("CustomName");
        }

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound.getByte("Slot") & 255;

            if (j >= 0 && j < this.inv.length)
            {
                this.inv[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
            }
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        NBTTagList nbttaglist = new NBTTagList();

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

        compound.setTag("Items", nbttaglist);

        if (this.hasCustomName())
        {
            compound.setString("CustomName", this.customName);
        }
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return true;
    }

    @Override
    public int getField(int id) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void setField(int id, int value) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public int getFieldCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.inv.length; ++i)
        {
            this.inv[i] = null;
        }
        
    }
    
}
 
GameRegistry.registerTileEntity(TileEntityMagnet.class, "TileEntityMagnet"); 
Ну вот зарегал и ни чё. Открываю контейнер, ложу, закрываю, открываю и нету (
 
Тут же типа две строчки. не?
Код:
 GameRegistry.registerTileEntity(TEBlockDecorMonitor.class, "TEBlockDecorMonitor");
        ClientRegistry.bindTileEntitySpecialRenderer(TEBlockDecorMonitor.class, new TEMonitorRenderer());
 
Назад
Сверху