Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

In my mod, I have a sword which when sneak right clicked opens a gui, the gui, container, and iinventory work just fine but I am having trouble figuring out how to save the item in the slot of the gui to the sword as nbt data.

 

Here is my code right now:

 

Sword Class:

public class ItemRaveSword extends SwordElectricFactory{

    private boolean lightning;
    private boolean fireball;

    public ItemRaveSword() {

        super(ToolMaterials.RAVE);

        this.setUnlocalizedName("swordRave");

        lightning = false;
        fireball = false;

    }

    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {

        if(player.isSneaking()) {

            player.openGui(ElectricFactory.instance, 0, world, (int) player.posX, (int) player.posY, (int) player.posZ);

        }
        else {

            if (lightning)
                spawnLightning(world, player);
            else if (fireball)
                spawnFireball(world, player);

        }

        return stack;

    }

}

 

GUI Handler:

public class GUIHandler implements IGuiHandler{

    public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {

        if(id == 0)
            return new RaveSwordContainer(player, player.getHeldItem());

        return null;

    }

    public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {

        if(id == 0)
            return new RaveSwordGUI(player, player.getHeldItem());

        return null;

    }

}

 

GUI:

@SideOnly(Side.CLIENT)
public class RaveSwordGUI extends GuiContainer{

    public RaveSwordGUI(EntityPlayer player, ItemStack sword) {

        super(new RaveSwordContainer(player, sword));

    }

    protected void drawGuiContainerForegroundLayer(int x, int y) {

    }

    protected void drawGuiContainerBackgroundLayer(float opacity, int x, int y) {

        mc.getTextureManager().bindTexture(new ResourceLocation(Reference.MODID.toLowerCase(), "textures/gui/ravesword.png"));

        int xStart = (width - xSize) / 2;
        int yStart = (height - ySize) / 2;

        drawTexturedModalRect(xStart, yStart, 0, 0, xSize, ySize);

    }

}

 

Container:

public class RaveSwordContainer extends Container{

    public RaveSwordContainer(EntityPlayer player, ItemStack sword) {

        addSlots(player, sword);

    }

    public ItemStack transferStackInSlot(EntityPlayer player, int index) {

        ItemStack newStack = null;

        Slot slot = (Slot) inventorySlots.get(index);

        if(slot != null && slot.getHasStack()) {

            ItemStack stack = slot.getStack();
            newStack = stack.copy();

            if(index < 1) {

                if(!this.mergeItemStack(stack, 1, inventorySlots.size(), false))
                    return null;

            }
            else if(!this.mergeItemStack(stack, 0, 1, false))
                return null;

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

        }

        return newStack;

    }

    public boolean canInteractWith(EntityPlayer player) {

        return true;

    }

    private void addSlots(EntityPlayer player, ItemStack sword) {

        addSlotToContainer(new Slot(new RaveSwordInventory(sword), 0, 80, 35));

        for(int i = 0; i < 3; i++) {

            for(int j = 0; j < 9; j++) {

                addSlotToContainer(new Slot(player.inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));

            }

        }

        for(int i = 0; i < 9; i++) {

            addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 142));

        }

    }

}

 

IInventory:

public class RaveSwordInventory implements IInventory{

    private ItemStack[] inventory;

    private ItemStack sword;

    public RaveSwordInventory(ItemStack sword) {

        inventory = new ItemStack[1];

        this.sword = sword;

    }

    public String getInventoryName() {

        return "swordRave";

    }

    public boolean hasCustomInventoryName() {

        return false;

    }

    public int getSizeInventory() {

       return inventory.length;

    }

    public int getInventoryStackLimit() {

        return 1;

    }

    public void markDirty() {

    }

    public void openInventory() {

    }

    public void closeInventory() {

    }

    public boolean isUseableByPlayer(EntityPlayer player) {

        return true;

    }

    public boolean isItemValidForSlot(int index, ItemStack stack) {

        return true;

    }

    public ItemStack getStackInSlot(int index) {

        return inventory[index];

    }

    public ItemStack getStackInSlotOnClosing(int index) {

        if(inventory[index] != null) {

            ItemStack stack = inventory[index];

            inventory[index] = null;

            return stack;

        }
        else {

            return null;

        }

    }

    public void setInventorySlotContents(int index, ItemStack stack) {

        inventory[index] = stack;

    }

    public ItemStack decrStackSize(int index, int amount) {

        ItemStack stack = getStackInSlot(index);

        if(stack != null) {

            if(stack.stackSize <= amount) {

                setInventorySlotContents(index, null);

            }
            else {

                stack = stack.splitStack(amount);

                if(stack.stackSize == 0)
                    setInventorySlotContents(index, null);

            }

        }

        return stack;

    }

}

 

This all works just fine I just need to figure out how to use nbt to save and read what item is stored in the gui

ItemStacks can store NBT, I would suggest passing the ItemStack to the classes where you need to store NBT, and then stack.stackTagCompound = new NBTTagCompound(); and edit it from there

  • Author

ItemStacks can store NBT, I would suggest passing the ItemStack to the classes where you need to store NBT, and then stack.stackTagCompound = new NBTTagCompound(); and edit it from there

thank you, I tried something similar to this and it works great!

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.