Jump to content

Elements from inventory aren't dragged


Wasylews

Recommended Posts

I took a code with www.minecraftforge.net/wiki/Containers_and_GUIs, remade coordinates for a texture 256x256. But after that subjects from stock of the player, when clicking them, appear in a pattern, instead of are dragged. I likely somewhere forgot to correct a constant?

[spoiler=MyGui]

package com.mymod;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import org.lwjgl.opengl.GL11;


public class MyGui extends GuiContainer {
    public static final int GUI_ID = 20;
    final int xSize = 256;
    final int ySize = 256;

    public MyGui(InventoryPlayer inventoryPlayer,
                       MyTileEntity tileEntity) {
        //the container is instanciated and passed to the superclass for handling
        super(new MyContainer(inventoryPlayer, tileEntity));
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int param1, int param2) {
        //draw text and stuff here
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float par1, int par2,
                                                   int par3) {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.renderEngine.bindTexture(new ResourceLocation(MyMod.MOD_ID, "textures/gui/sell.png"));
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, xSize, ySize);
    }

    @Override
    public void initGui() {
        super.initGui();
        this.guiLeft = (this.width - this.xSize) / 2 + 40;
        this.guiTop = (this.height - this.ySize) / 2 + 30;
    }

}

 

[spoiler=MyContainer]

package com.mymod;

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

public class MyContainer extends Container {

    protected MyTileEntity tileEntity;

    public MyContainer (InventoryPlayer inventoryPlayer, MyTileEntity te){
        tileEntity = te;

        //the Slot constructor takes the IInventory and the slot number in that it binds to
        //and the x-y coordinates it resides on-screen
        int id, x, y;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                id = j + i * 3;
                x = 9 + j * 18;
                y = 44 + i * 18;
                addSlotToContainer(new MySlot(tileEntity, id, x, y));
            }
        }

        //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) {
        int id, x, y;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                id = j + i * 9 + 9;
                x = 8 + j * 18;
                y = 106 + i * 18;
                addSlotToContainer(new Slot(inventoryPlayer, id,
                        x, y));
            }
        }

        y = 164;

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

    @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 < 9) {
                if (!this.mergeItemStack(stackInSlot, 0, 36, true)) {
                    return null;
                }
            }

            //places it into the tileEntity is possible since its in the player inventory
            else if (!this.mergeItemStack(stackInSlot, 0, 9, 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;
    }
}

 

[spoiler=MyTileEntity]

package com.mymod;


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;

public class MyTileEntity extends TileEntity implements IInventory {

    private ItemStack[] inv;

    public MyTileEntity(){
        inv = new ItemStack[9];
    }

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

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

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

    @Override
    public String getInventoryName() {
        return "My.tileentity";
    }

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

    @Override
    public ItemStack decrStackSize(int slot, int amt) {
        ItemStack stack = getStackInSlot(slot);
        setInventorySlotContents(slot, null);

        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 getStackInSlotOnClosing(int slot) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
            setInventorySlotContents(slot, null);
        }
        return stack;
    }

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

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

    @Override
    public void openInventory() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void closeInventory() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
        return false;
    }

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        super.readFromNBT(tagCompound);

        NBTTagList tagList = tagCompound.getTagList("Inventory", ;
        for (int i = 0; i < tagList.tagCount(); i++) {
            NBTTagCompound tag = tagList.getCompoundTagAt(i);
            byte slot = tag.getByte("Slot");
            if (slot >= 0 && slot < inv.length) {
                inv[slot] = ItemStack.loadItemStackFromNBT(tag);
            }
        }
    }

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

        NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < inv.length; i++) {
            ItemStack stack = inv[i];
            if (stack != null) {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setByte("Slot", (byte) i);
                stack.writeToNBT(tag);
                itemList.appendTag(tag);
            }
        }
        tagCompound.setTag("Inventory", itemList);
    }
}

 

[spoiler=MySlot]

package com.mymod;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class MySlot extends Slot {
    private static ItemStack validType;
    private IInventory inv;

    public MySlot(IInventory p_i1824_1_, int p_i1824_2_, int p_i1824_3_, int p_i1824_4_) {
        super(p_i1824_1_, p_i1824_2_, p_i1824_3_, p_i1824_4_);
        inv = p_i1824_1_;
    }

    @Override
    public boolean isItemValid(ItemStack stack) {
        if (validType == null) {
            validType = stack.copy();
        }
        return (stack.isItemEqual(MySlot.validType));
    }

    @Override
    public void onSlotChanged() {
        super.onSlotChanged();
        if (invIsEmpty()) {
            MySlot.validType = null;
        }
    }

    private boolean invIsEmpty() {
        for (int i = 0; i < inv.getSizeInventory(); ++i) {
            if (inv.getStackInSlot(i) != null) {
                return false;
            }
        }
        return true;
    }

}

 

[spoiler=MyBlock]

package com.mymod;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import java.util.Random;

public class MyBlock extends BlockContainer {
    private IIcon[] icons = new IIcon[6];

    public MyBlock() {
        super(Material.iron);

        this.setBlockName("Myblock");
        this.setCreativeTab(CreativeTabs.tabMisc);
        this.setBlockTextureName("mymod:Myblock");
    }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
        TileEntity tileEntity = world.getTileEntity(x, y, z);

        if (tileEntity == null || player.isSneaking()) {
            return false;
        }
        player.openGui(My.instance, MyGui.GUI_ID, world, x, y, z);

        return true;
    }

    @Override
    public void breakBlock(World world, int x, int y, int z, Block par5, int par6) {
        dropItems(world, x, y, z);
        super.breakBlock(world, x, y, z, par5, par6);
    }

    private void dropItems(World world, int x, int y, int z){
        Random rand = new Random();

        TileEntity tileEntity = world.getTileEntity(x, y, z);
        if (!(tileEntity instanceof IInventory)) {
            return;
        }
        IInventory inventory = (IInventory) tileEntity;

        for (int i = 0; i < inventory.getSizeInventory(); i++) {
            ItemStack item = inventory.getStackInSlot(i);

            if (item != null && item.stackSize > 0) {
                float rx = rand.nextFloat() * 0.8F + 0.1F;
                float ry = rand.nextFloat() * 0.8F + 0.1F;
                float rz = rand.nextFloat() * 0.8F + 0.1F;

                EntityItem entityItem = new EntityItem(world,
                        x + rx, y + ry, z + rz,
                        new ItemStack(item.getItem(), item.stackSize, item.getItemDamage()));

                if (item.hasTagCompound()) {
                    entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
                }

                float factor = 0.05F;
                entityItem.motionX = rand.nextGaussian() * factor;
                entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
                entityItem.motionZ = rand.nextGaussian() * factor;
                world.spawnEntityInWorld(entityItem);
                item.stackSize = 0;
            }
        }
    }

    @Override
    public void registerBlockIcons(IIconRegister reg) {
        for (int i = 0; i < 6; i ++) {
            this.icons[i] = reg.registerIcon(this.textureName + "_" + i);
        }
    }

    @Override
    public IIcon getIcon(int side, int meta) {
        return this.icons[side];
    }

    @Override
    public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
        return new MyTileEntity();
    }
}

 

Link to comment
Share on other sites

I had this in 1.6 using mcp,

 

in you container you need

 

public boolean canDragIntoSlot(Slot slt){

//System.out.println("canDragIntoSlot "+slt.slotNumber);

if (slt.slotNumber<34) return true;

if (slt.slotNumber>36) return true;

return false;

}

 

it might be called something else now but this should point you in the right direction

 

Link to comment
Share on other sites

Thanks, but it doesn't work. The code from wiki worked while I didn't change layout of a texture and slots.

Problem, likely, in this code

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

How i can calculate this constant for coordinates?

No, not here. Anybody can help?

Link to comment
Share on other sites

firstly, by "drag items in my inventory" i assume you mean , picking up a stack then dragging around to place them into multiple slots

 

if this is the case then i cant see anything that would stop the dragging

add the function i said , and add a diagnostic line to see if it is being called ,

 

System.out.println("Candrag is being called");

 

then watch the consol when you try dradging

 

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
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.