Перемещение в Container

Версия Minecraft
1.6.4
31
1
1
Предмет перемещается из инвентаря в контейнер и наоборот только через шифт, если перекладывать мышкой, то он пропадает на сервере, но визуально остается. Код контейнера:
container:
public class ContainerAdvancedItems extends Container {

    private InventoryAdvancedItems inv;
    
    public ContainerAdvancedItems(IInventory playerinv)
    {
        this.inv = new InventoryAdvancedItems();
        this.addSlotToContainer(new Slot(inv , 0, 146, 18));
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(playerinv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18 + 36));
            }
        }

        for (int k = 0; k < 9; ++k)
        {
            this.addSlotToContainer(new Slot(playerinv, k, 8 + k * 18, 142 + 36));
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer entityplayer) {
        return inv.isUseableByPlayer(entityplayer);
    }
    @Nullable
        public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
        {
            ItemStack itemstack = null;
            Slot slot = (Slot)this.inventorySlots.get(par2);

            if (slot != null && slot.getHasStack())
            {
                ItemStack itemstack1 = slot.getStack();
                itemstack = itemstack1.copy();

                if (par2 == 0)
                {
                    if (!this.mergeItemStack(itemstack1, 1, 37, true))
                    {
                        return null;
                    }
                }
                else
                {
                    if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(itemstack1))
                    {
                        return null;
                    }

                    if (itemstack1.hasTagCompound() && itemstack1.stackSize == 1)
                    {
                        ((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy());
                        itemstack1.stackSize = 0;
                    }
                    else if (itemstack1.stackSize >= 1)
                    {
                        ((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.itemID, 1, itemstack1.getItemDamage()));
                        --itemstack1.stackSize;
                    }
                }

                if (itemstack1.stackSize == 0)
                {
                    slot.putStack((ItemStack)null);
                }
                else
                {
                    slot.onSlotChanged();
                }

                if (itemstack1.stackSize == itemstack.stackSize)
                {
                    return null;
                }

                slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
            }

            return itemstack;
        }
}
Код IInventory:
IInventory:
public class InventoryAdvancedItems implements IInventory {
    
    public ItemStack item;
    private String username;
    public InventoryAdvancedItems() {
    }

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

    @Override
    public ItemStack getStackInSlot(int i) {
        return item;
    }

    @Override
    public ItemStack decrStackSize(int i, int j) {
        item.stackSize -= j;
        return item;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int i) {
        return item;
    }

    @Override
    public void setInventorySlotContents(int i, ItemStack itemstack) {
        item = itemstack;
    }

    @Override
    public String getInvName() {
        return "advancedItemsInv";
    }

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

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

    @Override
    public void onInventoryChanged() {
        
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer entityplayer) {
        return FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().isPlayerOpped(entityplayer.username);
    }

    @Override
    public void openChest() {
        
    }

    @Override
    public void closeChest() {
        
    }

    @Override
    public boolean isItemValidForSlot(int i, ItemStack itemstack) {
        return true;
    }
Самое странное то, что через шифт все работает.
 
28
0
Java:
@Override
    public void setInventorySlotContents(int par1, ItemStack stack) {

        this.stackList[par1] = stack;
    
        if (eventHandler != null)
            this.eventHandler.onCraftMatrixChanged(this);
        syncSlotToClients(par1);
    }
проблема скорее всего с этим методом или с прокси, а может быть и нет, вот код класса можете посравнивать мб найдете причину
Java:
public class InventoryPantheon implements IInventory {
    public ItemStack[] stackList;
    private Container eventHandler;
    public WeakReference<EntityPlayer> player;
    public boolean blockEvents=false;

    public InventoryPantheon(EntityPlayer player) {
        this.stackList = new ItemStack[4];
        this.player = new WeakReference<EntityPlayer>(player);
    }

    public Container getEventHandler() {
        return eventHandler;
    }

    public void setEventHandler(Container eventHandler) {
        this.eventHandler = eventHandler;
    }

    /**
     * Returns the number of slots in the inventory.
     */
    @Override
    public int getSizeInventory() {
        return this.stackList.length;
    }

    /**
     * Returns the stack in slot i
     */
    @Override
    public ItemStack getStackInSlot(int par1) {
        return par1 >= this.getSizeInventory() ? null : this.stackList[par1];
    }

    /**
     * Returns the name of the inventory
     */
    @Override
    public String getInventoryName() {
        return "";
    }

    /**
     * Returns if the inventory is named
     */
    @Override
    public boolean hasCustomInventoryName() {
        return false;
    }

    /**
     * When some containers are closed they call this on each slot, then drop
     * whatever it returns as an EntityItem - like when you close a workbench
     * GUI.
     */
    @Override
    public ItemStack getStackInSlotOnClosing(int par1) {
        if (this.stackList[par1] != null) {
            ItemStack itemstack = this.stackList[par1];
            this.stackList[par1] = null;
            return itemstack;
        } else {
            return null;
        }
    }

    /**
     * Removes from an inventory slot (first arg) up to a specified number
     * (second arg) of items and returns them in a new stack.
     */
    @Override
    public ItemStack decrStackSize(int par1, int par2) {
        if (this.stackList[par1] != null) {
            ItemStack itemstack;

            if (this.stackList[par1].stackSize <= par2) {
                itemstack = this.stackList[par1];

                if (itemstack != null && itemstack.getItem() instanceof IPantheon) {
                    ((IPantheon) itemstack.getItem()).onUnequipped(itemstack,
                            player.get());
                }
                
                this.stackList[par1] = null;

                if (eventHandler != null)
                    this.eventHandler.onCraftMatrixChanged(this);
                syncSlotToClients(par1);
                return itemstack;
            } else {
                itemstack = this.stackList[par1].splitStack(par2);
                
                if (itemstack != null && itemstack.getItem() instanceof IPantheon) {
                    ((IPantheon) itemstack.getItem()).onUnequipped(itemstack,
                            player.get());
                }
                
                if (this.stackList[par1].stackSize == 0) {
                    this.stackList[par1] = null;
                }
                
                if (eventHandler != null)
                    this.eventHandler.onCraftMatrixChanged(this);
                syncSlotToClients(par1);
                return itemstack;
            }
        } else {
            return null;
        }
    }

    /** игра крашилась закоментил строчки, позволяющие использовать функционал руки
     * отключенные строчки блокировали возможность вытащить предмет из руки
     */
    @Override
    public void setInventorySlotContents(int par1, ItemStack stack) { // метод перетаскивания !!!!!
        
        //if(!blockEvents && this.stackList[par1] != null) {
              //  ((IPantheon)stackList[par1].getItem()).onUnequipped(stackList[par1], player.get());
    //    }
        this.stackList[par1] = stack;
        //if (!blockEvents && stack != null && stack.getItem() instanceof IPantheon) {
            //((IPantheon) stack.getItem()).onEquipped(stack, player.get());
    //    }
        if (eventHandler != null)
            this.eventHandler.onCraftMatrixChanged(this);
        syncSlotToClients(par1);
    }

    /**
     * Returns the maximum stack size for a inventory slot.
     */
    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    /**
     * For tile entities, ensures the chunk containing the tile entity is saved
     * to disk later - the game won't think it hasn't changed and skip it.
     */
    @Override
    public void markDirty() {
        try {
            player.get().inventory.markDirty();
        } catch (Exception e) {
        }
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes
     * with Container
     */
    @Override
    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
        return true;
    }

    @Override
    public void openInventory() {

    }

    @Override
    public void closeInventory() {

    }

    /**
     * Returns true if automation is allowed to insert the given stack (ignoring
     * stack size) into the given slot.
     */
    @Override
    public boolean isItemValidForSlot(int i, ItemStack stack) {
        if (stack == null || !(stack.getItem() instanceof IPantheon)
                || !((IPantheon) stack.getItem()).canEquip(stack, player.get()))
            return false;
        if (i == 0
                && ((IPantheon) stack.getItem()).getBaubleType(stack) == PantheonType.MASK)
            return true;
        if (i == 1
                && ((IPantheon) stack.getItem()).getBaubleType(stack) == PantheonType.AMULET)
            return true;
        if (i == 2
                && ((IPantheon) stack.getItem()).getBaubleType(stack) == PantheonType.ARTIFACT)
            return true;
        if (i == 3
                && ((IPantheon) stack.getItem()).getBaubleType(stack) == PantheonType.HAND)
            return true;
        return false;
    }

    public void saveNBT(EntityPlayer player) {
        NBTTagCompound tags = player.getEntityData();
        saveNBT(tags);
    }

    public void saveNBT(NBTTagCompound tags) {
        NBTTagList tagList = new NBTTagList();
        NBTTagCompound invSlot;
        for (int i = 0; i < this.stackList.length; ++i) {
            if (this.stackList[i] != null) {
                invSlot = new NBTTagCompound();
                invSlot.setByte("Slot", (byte) i);
                this.stackList[i].writeToNBT(invSlot);
                tagList.appendTag(invSlot);
            }
        }
        tags.setTag("Baubles.Inventory", tagList);
    }

    public void readNBT(EntityPlayer player) {
        NBTTagCompound tags = player.getEntityData();
        readNBT(tags);
    }

    public void readNBT(NBTTagCompound tags) {
        NBTTagList tagList = tags.getTagList("Baubles.Inventory", 10);
        for (int i = 0; i < tagList.tagCount(); ++i) {
            NBTTagCompound nbttagcompound = (NBTTagCompound) tagList
                    .getCompoundTagAt(i);
            int j = nbttagcompound.getByte("Slot") & 255;
            ItemStack itemstack = ItemStack
                    .loadItemStackFromNBT(nbttagcompound);
            if (itemstack != null) {
                this.stackList[j] = itemstack;
            }
        }
    }

    public void dropItems(ArrayList<EntityItem> drops) {
        for (int i = 0; i < 4; ++i) {
            if (this.stackList[i] != null) {
                EntityItem ei = new EntityItem(player.get().worldObj,
                        player.get().posX, player.get().posY
                                + player.get().eyeHeight, player.get().posZ,
                        this.stackList[i].copy());
                ei.delayBeforeCanPickup = 40;
                float f1 = player.get().worldObj.rand.nextFloat() * 0.5F;
                float f2 = player.get().worldObj.rand.nextFloat()
                        * (float) Math.PI * 2.0F;
                ei.motionX = (double) (-MathHelper.sin(f2) * f1);
                ei.motionZ = (double) (MathHelper.cos(f2) * f1);
                ei.motionY = 0.20000000298023224D;
                drops.add(ei);
                this.stackList[i] = null;
                syncSlotToClients(i);
            }
        }
    }
    
    public void dropItemsAt(ArrayList<EntityItem> drops, Entity e) {
        for (int i = 0; i < 4; ++i) {
            if (this.stackList[i] != null) {
                EntityItem ei = new EntityItem(e.worldObj,
                        e.posX, e.posY + e.getEyeHeight(), e.posZ,
                        this.stackList[i].copy());
                ei.delayBeforeCanPickup = 40;
                float f1 = e.worldObj.rand.nextFloat() * 0.5F;
                float f2 = e.worldObj.rand.nextFloat() * (float) Math.PI * 2.0F;
                ei.motionX = (double) (-MathHelper.sin(f2) * f1);
                ei.motionZ = (double) (MathHelper.cos(f2) * f1);
                ei.motionY = 0.20000000298023224D;
                drops.add(ei);
                this.stackList[i] = null;
                syncSlotToClients(i);
            }
        }
    }

    public void syncSlotToClients(int slot) {
        try {
            if (Pantheon.proxy.getClientWorld() == null) {
                PacketHandler.INSTANCE.sendToAll(new PacketSyncPantheon(player
                        .get(), slot));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Последнее редактирование:
31
1
1
@NetworkMod(clientSideRequired=true, serverSideRequired=true, channels = {NetworkHandler.CHANNEL}, packetHandler = NetworkHandler.class)

При нажатии на кнопку отправляю пакет на сервер:
Отправляю:
        public static final void sendOpenGuiPacket(int guiId)
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream outputStream = new DataOutputStream(bos);
            
            try {
                outputStream.writeByte(OPEN_SERVER_GUI);
                outputStream.writeInt(guiId);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            
            PacketDispatcher.sendPacketToServer(PacketDispatcher.getPacket(CHANNEL, bos.toByteArray()));
        }
Принимаю пакет:
Принимаю:
        private void handleOpenGuiPacket(Packet250CustomPayload packet, EntityPlayer player, DataInputStream inputStream)
        {
            int guiID;
            try {
                guiID = inputStream.readInt();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            
            player.openGui(TestyMod.instance, guiID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
        }
 
31
1
1
Вот:
public class GuiHandler implements IGuiHandler {
    
    public static final int ADVANCEDITEMSGUI = 0;

    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if(ID == ADVANCEDITEMSGUI)
        {
            return new ContainerAdvancedItems(player.inventory);
        }
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if(ID == ADVANCEDITEMSGUI)
        {
            return new GuiAdvancedItems(player.inventory);
        }       
        return null;
    }

}
 
Сверху