Jump to content

[1.14.2] Creating a custom inventory + capabilities


soravoid

Recommended Posts

I've done my best to try and get through this without turning to the forums, but I've hit a really difficult roadblock. I am trying to make a one slot inventory that will open when a keybinding is pressed. I'm having trouble with using capabilities to store the inventory (which I kinda "completed", but I think it looks kinda weird) and opening the GUI. Here are the files:

 

SparkOn.java

package com.soravoid.sparkon;

import com.soravoid.sparkon.capabilities.CapabilityAlignerInv;
import com.soravoid.sparkon.capabilities.interfaces.IAlignerInv;
import com.soravoid.sparkon.handlers.GuiHandler;
import com.soravoid.sparkon.handlers.SparkPacketHandler;
import com.soravoid.sparkon.proxy.ClientProxy;
import com.soravoid.sparkon.proxy.CommonProxy;
import com.soravoid.sparkon.proxy.IProxy;
import net.minecraft.item.ItemGroup;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.network.FMLPlayMessages;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.soravoid.sparkon.capabilities.CapabilityEB;
import com.soravoid.sparkon.capabilities.CapabilityEB.EBStorage;
import com.soravoid.sparkon.capabilities.CapabilityEF;
import com.soravoid.sparkon.capabilities.CapabilityEF.EFStorage;
import com.soravoid.sparkon.capabilities.CapabilityPlayerStats;
import com.soravoid.sparkon.capabilities.CapabilityPlayerStats.PlayerStatsStorage;
import com.soravoid.sparkon.capabilities.interfaces.IEffervescence;
import com.soravoid.sparkon.capabilities.interfaces.IEffervescentBody;
import com.soravoid.sparkon.capabilities.interfaces.IPlayerStats;
import com.soravoid.sparkon.render.EffervescenceBar;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

@Mod("sparkon")
public class SparkOn 
{
	public static SparkOn instance;
	public static final String MODID = "sparkon";
	private static final Logger logger = LogManager.getLogger(MODID);

	public static final int ALIGNINV_GUI_ID = 0;

	public static final ItemGroup SPARK = new SparkGroup();

	public static final IProxy proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> CommonProxy::new);

	public SparkOn()
	{
		instance = this;
		
		FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
		FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
		
		MinecraftForge.EVENT_BUS.register(this);
	}
	
	private void setup(FMLCommonSetupEvent e)
	{
		CapabilityManager.INSTANCE.register(IEffervescence.class, new EFStorage(), () -> new CapabilityEF());
		CapabilityManager.INSTANCE.register(IPlayerStats.class, new PlayerStatsStorage(), () -> new CapabilityPlayerStats());
		CapabilityManager.INSTANCE.register(IEffervescentBody.class, new EBStorage(), () -> new CapabilityEB(null));
		CapabilityManager.INSTANCE.register(IAlignerInv.class, new CapabilityAlignerInv.AlignerInvStorage(), () -> new CapabilityAlignerInv(null));
		SparkPacketHandler.register();
		logger.info("Setup Complete!");
	}
	
	private void clientSetup(FMLClientSetupEvent e)
	{
		proxy.registerKeyBinds();
		ModLoadingContext.get().registerExtensionPoint(ExtensionPoint.CONFIGGUIFACTORY, () -> GuiHandler::openGui);
		logger.info("Client Setup Complete");
	}
}

 

IAlignerInv.java

package com.soravoid.sparkon.capabilities.interfaces;

import com.soravoid.sparkon.inventory.AlignerInventory;
import net.minecraft.item.Item;
import net.minecraftforge.items.IItemHandler;

public interface IAlignerInv extends IItemHandler
{
    Item getCurrentAligner();
    AlignerInventory getInventory();
    void setInventory(AlignerInventory inv);

}

 

CapabilityAlignerInv.java

package com.soravoid.sparkon.capabilities;

import com.soravoid.sparkon.capabilities.interfaces.IAlignerInv;
import com.soravoid.sparkon.inventory.AlignerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CapabilityAlignerInv implements IAlignerInv
{
    private AlignerInventory inventory;

    public CapabilityAlignerInv(AlignerInventory inv)
    {
        this.inventory = inv;
    }

    @Override
    public AlignerInventory getInventory() { return inventory; }

    @Override
    public Item getCurrentAligner() { return this.inventory.getStackInSlot(0).getItem(); }

    public void setInventory(AlignerInventory inventory) { this.inventory = inventory; }

    @Override
    public int getSlots() { return this.inventory.getSizeInventory(); }

   @Override
    public ItemStack getStackInSlot(int slot) { return this.inventory.getStackInSlot(slot); }

    @Nonnull
    @Override
    public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
    {
        if(this.inventory.isItemValidForSlot(slot, stack))
        {
            if(this.inventory.getStackInSlot(slot) == null || this.inventory.getStackInSlot(slot).getCount() <= 0)
            {
                if(!simulate) this.inventory.setInventorySlotContents(slot, stack);
                return ItemStack.EMPTY;
            }
            else
            {
                return stack;
            }
        }
        else
        {
            return stack;
        }
    }

    @Nonnull
    @Override
    public ItemStack extractItem(int slot, int amount, boolean simulate)
    {
        if(this.inventory.getStackInSlot(slot) == null)
        {
            return ItemStack.EMPTY;
        }
        else
        {
            if(!simulate) this.inventory.setInventorySlotContents(slot, ItemStack.EMPTY);
            return this.inventory.getStackInSlot(slot);
        }
    }

    @Override
    public int getSlotLimit(int slot) { return this.inventory.getInventoryStackLimit(); }

    @Override
    public boolean isItemValid(int slot, @Nonnull ItemStack stack) { return this.inventory.isItemValidForSlot(slot, stack); }

    public static class AlignerInvStorage implements Capability.IStorage<IAlignerInv>
    {

        @Override
        public INBT writeNBT(Capability<IAlignerInv> capability, IAlignerInv instance, Direction side)
        {
            CompoundNBT nbt = new CompoundNBT();
            instance.getInventory().writeToNBT(nbt);
            return nbt;
        }

        @Override
        public void readNBT(Capability<IAlignerInv> capability, IAlignerInv instance, Direction side, INBT nbt)
        {
            AlignerInventory inv = new AlignerInventory();
            inv.readFromNBT((CompoundNBT) nbt);
            instance.setInventory(inv);
        }
    }

    public static class AlignerInvProvider implements ICapabilitySerializable<INBT>
    {
        @CapabilityInject(IAlignerInv.class)
        public static Capability<IAlignerInv> ALIGNERINV = null;

        private IAlignerInv instance;

        public AlignerInvProvider(AlignerInventory inv)
        {
            this.instance = new CapabilityAlignerInv(inv);
        }

        @Nonnull
        @Override
        public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side)
        {
            return ALIGNERINV.orEmpty(cap, LazyOptional.of(() -> this.instance));
        }

        @Override
        public INBT serializeNBT()
        {
            return ALIGNERINV.getStorage().writeNBT(ALIGNERINV, instance, null);
        }

        @Override
        public void deserializeNBT(INBT nbt)
        {
            ALIGNERINV.getStorage().readNBT(ALIGNERINV, instance, null, nbt);
        }
    }
}

 

AlignerInventory.java

package com.soravoid.sparkon.inventory;

import com.soravoid.sparkon.lists.SparkItems;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;

public class AlignerInventory implements IInventory
{
    private final String name = "Aligner Slot";
    private final String tagName = "alignerTag";

    public static final int INV_SIZE = 1;

    private ItemStack[] inventory = new ItemStack[INV_SIZE];

    public AlignerInventory() {}

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

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

    @Override
    public ItemStack decrStackSize(int index, int count)
    {
        ItemStack stack = getStackInSlot(index);
        if(stack != null)
        {
            if(stack.getCount() > count)
            {
                stack = stack.split(count);
                this.markDirty();
            }
            else
            {
                setInventorySlotContents(index, null);
            }
        }
        return stack;
    }

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

        this.markDirty();
    }

    public String getName() { return name; }

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

    @Override
    public void markDirty()
    {
        for(int i = 0; i < getSizeInventory(); ++i)
        {
            if(getStackInSlot(i) != null && getStackInSlot(i).getCount() <= 0)
            {
                this.inventory[i] = null;
            }
        }
    }

    @Override
    public boolean isUsableByPlayer(PlayerEntity player) { return true; }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack)
    {
        return stack.getItem() == SparkItems.ADAPTIVE_ALIGNER || stack.getItem() == SparkItems.STRONG_WILLED_ALIGNER || stack.getItem() == SparkItems.WAVE_CASTER_ALIGNER || stack.getItem() == SparkItems.WAVE_MANIPULATOR_ALIGNER;
    }

    public void writeToNBT(CompoundNBT nbt)
    {
        ListNBT items = new ListNBT();

        for(int i = 0; i < this.getSizeInventory(); ++i) {
            if (getStackInSlot(i) != null) {
                CompoundNBT item = new CompoundNBT();
                item.putByte("Slot", (byte) i);
                getStackInSlot(i).write(item);
                items.add(item);
            }
        }
        nbt.put(tagName, items);
    }

    public void readFromNBT(CompoundNBT nbt)
    {
        ListNBT items = (ListNBT) nbt.get(tagName);
        for(int i = 0; i < items.size(); ++i)
        {
            CompoundNBT item = (CompoundNBT) items.get(i);
            byte slot = item.getByte("Slot");

            if(slot >= 0 && slot < getSizeInventory())
            {
                inventory[slot] = ItemStack.read(item);
            }
        }
    }

    @Override
    public boolean isEmpty()
    {
        for(int i = 0; i < getSizeInventory(); ++i)
        {
            if(this.inventory[i] != null)
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public ItemStack removeStackFromSlot(int index)
    {
        ItemStack stack = getStackInSlot(index);
        setInventorySlotContents(index, null);
        return stack;
    }

    @Override
    public void clear()
    {
        for(int i = 0; i < inventory.length; ++i)
        {
            this.inventory[i] = null;
        }
    }
}

 

AlignerContainer.java

package com.soravoid.sparkon.inventory.containers;

import com.soravoid.sparkon.inventory.AlignerInventory;
import com.soravoid.sparkon.inventory.slots.AlignerSlot;
import com.soravoid.sparkon.lists.SparkItems;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;

public class AlignerContainer extends Container
{
    private static final int ARMOR_START = AlignerInventory.INV_SIZE, ARMOR_END = ARMOR_START+3,
            INV_START = ARMOR_END+1, INV_END = INV_START+26, HOTBAR_START = INV_END+1,
            HOTBAR_END = HOTBAR_START+8;

    public AlignerContainer(PlayerInventory inventory, AlignerInventory customInv)
    {
        super(null, 0);
        int i;

        this.addSlot(new AlignerSlot(customInv, 0, 80, 23));

        for (i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (i = 0; i < 9; ++i)
        {
            this.addSlot(new Slot(inventory, i, 8 + i * 18, 142));
        }
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) { return true; }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = this.inventorySlots.get(index);

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

            // Either armor slot or custom item slot was clicked
            if (index < INV_START)
            {
                // try to place in player inventory / action bar
                if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            // Item is in inventory / hotbar, try to place either in custom or armor slots
            else
            {
                // if item is our custom item
                if (itemstack1.getItem() == SparkItems.ADAPTIVE_ALIGNER || itemstack1.getItem() == SparkItems.STRONG_WILLED_ALIGNER || itemstack1.getItem() == SparkItems.WAVE_CASTER_ALIGNER || itemstack1.getItem() == SparkItems.WAVE_MANIPULATOR_ALIGNER)
                {
                    if (!this.mergeItemStack(itemstack1, 0, AlignerInventory.INV_SIZE, false))
                    {
                        return null;
                    }
                }
                else if (index >= INV_START && index < HOTBAR_START)
                {
                    if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_START + 1, false))
                    {
                        return null;
                    }
                }
                else if (index >= HOTBAR_START && index < HOTBAR_END + 1)
                {
                    if (!this.mergeItemStack(itemstack1, INV_START, INV_END + 1, false))
                    {
                        return null;
                    }
                }
            }

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

            if (itemstack1.getCount() == itemstack.getCount())
            {
                return null;
            }
            slot.onTake(playerIn, itemstack1);
        }

        return itemstack;
    }
}

 

AlignerGui.java

package com.soravoid.sparkon.guis;

import com.soravoid.sparkon.SparkOn;
import com.soravoid.sparkon.inventory.AlignerInventory;
import com.soravoid.sparkon.inventory.containers.AlignerContainer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import org.lwjgl.opengl.GL11;

public class AlignerGui extends ContainerScreen<AlignerContainer>
{

    private static final ResourceLocation location = new ResourceLocation(SparkOn.MODID, "textures/gui/aligner.png");

    private final AlignerInventory inventory;

    public AlignerGui(PlayerInventory inv, AlignerInventory cusInv)
    {
        super(new AlignerContainer(inv, cusInv), inv, new StringTextComponent(""));
        this.inventory = cusInv;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        Minecraft.getInstance().getTextureManager().bindTexture(location);
        this.blit(guiLeft, guiTop, 0, 0, xSize, ySize);
    }
}

 

GuiHandler.java (For this one, I wasn't sure how to get the AlignerInventory from the player bc getting it from the Minecraft.getInstance().player AlignerInv capability would return null I think because I don't have packets for this capability set up, so null is a placeholder)

package com.soravoid.sparkon.handlers;

import com.soravoid.sparkon.SparkOn;
import com.soravoid.sparkon.guis.AlignerGui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraftforge.fml.network.FMLPlayMessages;

public class GuiHandler
{
    public static Screen openGui(FMLPlayMessages.OpenContainer openContainer)
    {
        if(openContainer.getWindowId() == SparkOn.ALIGNINV_GUI_ID) return new AlignerGui(Minecraft.getInstance().player.inventory, null);
        return null;
    }
}

 

The capabilities are properly set up in a Capability Handler.

 

Edited by soravoid
Link to comment
Share on other sites

31 minutes ago, diesieben07 said:
  • Do not use IInventory. You already have IItemHandler.
  • You probably don't want to implement IItemHandler on your capability, because that means you cannot use the default ItemStackHandler implementation (composition over inheritance).

What are you actually having trouble with?

What should I do instead of having IInventory? Would I extend a different class and then declare the ItemHandler capability in there? Also, what should openGui(OpenContainer) in GuiHandler return in order for me to register it as GuiHandler? 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • hola thanks for answering  i set some system outs to test and see whats going on      @Override     protected void saveAdditional( CompoundTag nbt ){                  nbt.put("itemhandler", itemhandler.serializeNBT() );         nbt.putInt("progress", this.progress );         System.out.println("saveAdditional(nbt)");                 System.out.println(NbtUtils.prettyPrint(nbt)); //<-- this is whats getting saved         super.saveAdditional( nbt );     }     @Override     public void load( CompoundTag nbt ){         itemhandler.deserializeNBT( nbt.getCompound("itemhandler") );         this.progress = nbt.getInt("progress" );         System.out.println(NbtUtils.prettyPrint(nbt)); //<-- this is whats minecraft is giving back to the entity          System.out.println("load(nbt)");         super.load( nbt );     }   This dead adventurer body  has two parts a block and a block Item  both has their own item-handlers and their own menus all of that seems fine  Testing remarks : *when i change some element inside the gui  it triggers the saveAdditional(nbt) system out and shows a pretty print of the data being saved  *when the world map loads it triggers the load(nbt) system out and shows a pretty print of the data minecraft is giving to this Block Entity For the testing i gonna set mi BlockEntityBodyBlock, save something in it, close minecraft, fireup the game again and check if the items remain      ################ first try set the block entity (dead adventurer body like shaped) and left only a weath seed  in the slot 10 the console says its all right    close minecraft, launch it again i see the system out whit the data returned to the entity and its look good theres is only a weed seed  ################ Second try, now i gonna change the seed for two arrows  the console says its all right the two arrows are being saved    ######### Now close and run again bam Error  minecraft returns the original data whit only a wheat seed the two arrows has been loss   ########## try again now i gonna put 3 wheat seeds Console system out says it gets it and is saving the data   ############## but after close fire again  wheat seed,  data losed once more time  and it just throw the original   ####################################################################################### sorry for the long post and the crappy video  this is the reason why i say its saving the Block Entity data only when it feels like  coze its saves data when it feels like  ¿ its something wrong whit mi BlockEntity.saveAdditional()  if  i keep trying eventually it updates and the data chages      i alredy try to  level.removeBlockEntity(pos); level.setBlockEntity(pos, bebb);     BlockEntityBodyBlock.class   BlockEntityInit.class     body_blockitem.class                  
    • Yesterday I buy a minecraft server to play with my friends you know... One of my friends send me the list of mods to add and then i start to dowload them, when i add them to the server it start crashing but i don't know about this things because it's my first time buying a minecraft server This is the error: https://paste.ee/p/sJf7B
    • Discovering that a trusted colleague had accessed my Bitcoin account and transferred $30,000 worth of bitcoins was a devastating blow. It shattered the trust I had placed in them and left me feeling vulnerable and betrayed. However, in the face of adversity, I turned to MUYERN TRUST HACKER for assistance in reclaiming control over my finances and holding the perpetrators accountable for their actions. One of the standout features of MUYERN TRUST HACKER was its ability to provide real-time alerts and notifications, keeping me informed every step of the way. With the support of MUYERN TRUST HACKER, I was able to gather irrefutable evidence of the perpetrator's actions and hold them accountable for their crimes. Armed with the information provided by the platform, I pursued legal recourse and ensured that justice was served. The perpetrator faced severe consequences for their actions, including legal penalties and financial restitution. Beyond its role in facilitating the recovery process, MUYERN TRUST HACKER provided me with a sense of empowerment and resilience in the face of adversity. While the experience was undoubtedly challenging, it ultimately served as a testament to the importance of vigilance and the power of technology in safeguarding our digital assets. Thanks to MUYERN TRUST HACKER, I emerged stronger and more resilient, ready to face whatever challenges the future may hold. Mail; muyerntrusted[At] mail-me .c o m
    • Discovering that a trusted colleague had accessed my Bitcoin account and transferred $30,000 worth of bitcoins was a devastating blow. It shattered the trust I had placed in them and left me feeling vulnerable and betrayed. However, in the face of adversity, I turned to MUYERN TRUST HACKER web [ ht tps:// muyerntrusthack.solutions/ ] for assistance in reclaiming control over my finances and holding the perpetrators accountable for their actions. One of the standout features of MUYERN TRUST HACKER was its ability to provide real-time alerts and notifications, keeping me informed every step of the way. With the support of MUYERN TRUST HACKER, I was able to gather irrefutable evidence of the perpetrator's actions and hold them accountable for their crimes. Armed with the information provided by the platform, I pursued legal recourse and ensured that justice was served. The perpetrator faced severe consequences for their actions, including legal penalties and financial restitution. Beyond its role in facilitating the recovery process, MUYERN TRUST HACKER provided me with a sense of empowerment and resilience in the face of adversity. While the experience was undoubtedly challenging, it ultimately served as a testament to the importance of vigilance and the power of technology in safeguarding our digital assets. Thanks to MUYERN TRUST HACKER, I emerged stronger and more resilient, ready to face whatever challenges the future may hold. Mail; muyerntrusted[At] mail-me .c o m  
    • Do you have still this problem? (I have it too so I want to know if you did something with it)  
  • Topics

×
×
  • Create New...

Important Information

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