Jump to content

kiou.23

Members
  • Posts

    444
  • Joined

  • Last visited

  • Days Won

    5

Posts posted by kiou.23

  1. I've implemented a workbench that works just like the vanilla one, however when I get the recipe result to output, it always returns a stack with 64 items. I have no idea why this is happening

     

    public class ModWorkbenchTileEntity extends TileEntity implements INamedContainerProvider {
    
        public static final int CRAFT_MATRIX_SLOT_START = 0;
        public static final int CRAFT_MATRIX_SLOT_END = 8;
        public static final int OUTPUT_SLOT = 9;
    
        public final ItemStackHandler inventory = new ItemStackHandler(10) {
            @Override
            protected void onContentsChanged(int slot) {
                super.onContentsChanged(slot);
                switch (slot) {
                    case OUTPUT_SLOT:
                        // this doesn't handle the cases were the ingredient has a container item, eg. Milk Bucket
                        if (getStackInSlot(slot).isEmpty())
                            for (int i = CRAFT_MATRIX_SLOT_START; i <= CRAFT_MATRIX_SLOT_END; i++)
                                setStackInSlot(i, ItemStack.EMPTY);
                    default:
                        ItemStack res = getResult().orElse(ItemStack.EMPTY);
                        if (res.isEmpty()) return;
    
                        // the Crafting Result is outputed into the output slot, and the crafting matrix is cleared
                        // but the Item Stack in the output slot has 64 items instead of the correct count based on the recipe
                        insertItem(OUTPUT_SLOT, res, false);
                }
            }
        };
    
        public ModWorkbenchTileEntity() {
            super(ModTileEntityTypes.MOD_WORKBENCH.get());
        }
    
        // ModWorkbenchInventory is a subclass of CraftingInventory
        // which delegates the getStackInSlot method to the ItemStackHandler passed in
        // it's purpose is so I can call RecipeManager#getRecipe()
        private ModWorkbenchInventory getCraftingInventory() {
            IItemHandler craftMatrix = new ItemStackHandler(9);
            for (int slot = CRAFT_MATRIX_SLOT_START; slot <= CRAFT_MATRIX_SLOT_END; slot++)
                craftMatrix.insertItem(slot, inventory.getStackInSlot(slot), false);
    
            return new ModWorkbenchInventory(craftMatrix);
        }
    
        private Optional<ICraftingRecipe> getRecipe() {
            return world.getRecipeManager().getRecipe(
                IRecipeType.CRAFTING,
                getCraftingInventory(),
                world
            );
        }
    
        private Optional<ItemStack> getResult() {
            return getRecipe().map(recipe -> recipe.getCraftingResult(getCraftingInventory()));
        }
    
        @Override
        public ITextComponent getDisplayName() {
            return new TranslationTextComponent(ModBlocks.MOD_WORKBENCH.get().getTranslationKey());
        }
    
        @Override
        public Container createMenu(int windowId, PlayerInventory playerInv, PlayerEntity player) {
            return new ModWorkbenchContainer(windowId, playerInv, this);
        }
    }

     

  2. 3 minutes ago, Turnrp said:

    So i would put IForgeItem.onItemRightClick?

     

    no, you override it

    your Backpack is an implementation of that interface (check out encapsulation and interfaces in Object Oriented Programming for more guidance)

     

    so in your Backpack class, you can override it

    (if you don't know what this means search for overriding methods in Java)

  3. 9 minutes ago, Turnrp said:

    I found something called IForgeContainerType? Will that help with a container im a little new to this

    That's something different

     

    So, okay

    you have your BackpackBase class right?

    it extends the net.minecraft.Item class, which in turn extends the IForgeItem interface

     

    the interface provides a few methods you can override, in the case of doing something upon right-click you have: onItemUse, and onItemRightClick

     

    onItemRightClick detects any right click

    onItemUse detects right clicks on blocks

     

  4. 1 minute ago, Jaden0922 said:

    I don't know what u mean so what do I type?

    4- And when replying please quote the comment you're replying, so we don't have to guess

     

    I guess you're talking about the MOD_ID, just pass it

    like

    your main class has a static final String which stands for your mod Id, whenever you need the Mod Id, just use YourModClass.MOD_ID

    eg:

    instead of @Mod("DoggieLandMod") use @Mod(DoggieLandMod.MOD_ID)

  5. 5 minutes ago, Jaden0922 said:

    main mod:

    
    package com.jadenwong.doggie_land;
    
    import com.jadenwong.doggie_land.util.RegistryHandler;
    import net.minecraftforge.common.MinecraftForge;
    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;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    @Mod("DoggieLandMod")
    public class DoggieLandMod
    {
        private static final Logger LOGGER = LogManager.getLogger();
        public static final String MOD_ID = "DoggieLandMod";
    
        public DoggieLandMod() {
            FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
            FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
    
            RegistryHandler.init();
    
            MinecraftForge.EVENT_BUS.register(this);
        }
    
        private void setup(final FMLCommonSetupEvent event) { }
    
        private void doClientStuff(final FMLClientSetupEvent event) { }
    }
    

    and in here, you have MOD_ID as a static final var, so whenever you need your mod id in a .java file, you should reference DoggieLandMod.MOD_ID, instead of stringly typing it

    for example: in the @Mod() annotation

     

    this way is less error prone and easier to update and make changes

  6. 2 hours ago, diesieben07 said:

    You can make a subclass of CraftingInventory that overrides all methods to delegate to your IItemHandler instead.

    like so?

    public class ModWorkbenchInventory extends CraftingInventory {
    
        private final ModWorkbenchItemStackHandler itemStackHandler;
    
        public ModWorkbenchInventory(ModWorkbenchItemStackHandler itemStackHandler) {
            super(null, 3, 3);
    
            this.itemStackHandler = itemStackHandler;
        }
    
        @Override public int getHeight() { return 3; }
    
        @Override public int getWidth() { return 3; }
    
        @Override public ItemStack getStackInSlot(int index) { return itemStackHandler.getStackInSlot(index); }
    
        //
    
        @Override public int getSizeInventory() { return itemStackHandler.getSlots(); }
    
        @Override
        public boolean isEmpty() {
            for(int slot = 0; slot < itemStackHandler.getSlots(); slot++) {
                if (!itemStackHandler.getStackInSlot(slot).isEmpty()) {
                    return false;
                }
            }
    
            return true;
        }
    
        @Override public ItemStack removeStackFromSlot(int index) { return ItemStack.EMPTY; }
    
        @Override public ItemStack decrStackSize(int index, int count) { return ItemStack.EMPTY; }
    
        @Override public void setInventorySlotContents(int index, ItemStack stack) {}
    
        @Override public void markDirty() {}
    
        @Override public void clear() {}
    
        @Override public void fillStackedContents(RecipeItemHelper helper) {}
    }

     

    public class ModWorkbenchItemStackHandler extends ItemStackHandler {
    
        public ModWorkbenchItemStackHandler() {
            super(9);
        }
    
        @Override
        public boolean isItemValid(int slot, ItemStack stack) {
            if (slot < 0 || slot >= 9)
                throw new IllegalArgumentException("Invalid slot number: " + slot);
    
            if (stack.isEmpty()) return false;
    
            return !(stack.getItem() instanceof BackpackItem);
        }
    }

     

    public class ModWorkbenchTileEntity extends TileEntity implements INamedContainerProvider {
    
        final ModWorkbenchItemStackHandler craftMatrix = new ModWorkbenchItemStackHandler();
    
        public ModWorkbenchTileEntity() {
            super(ModTileEntityTypes.MOD_WORKBENCH.get());
        }
    
        private Optional<ICraftingRecipe> getRecipe() {
            ItemStack[] stacks = new ItemStack[9];
            for (int i = 0; i < craftMatrix.getSlots(); i++)
                stacks[i] = (craftMatrix.getStackInSlot(i));
    
            return world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, new ModWorkbenchInventory(craftMatrix), world);
        }
    
    
        @Override
        public ITextComponent getDisplayName() {
            return null;
        }
    
        @Override
        public Container createMenu(int windowId, PlayerInventory playerInv, PlayerEntity player) {
            return null;
        }
    }

     

  7. 5 hours ago, Turnrp said:

    I looked and nothing shows up when i do that i type it in and it shows up but when a place a dot nothing shows up after that

    your Item extends the Item class, right? the Item class implements the IForgeItem interface

    and that interface provides some methods, go to the interface implementation (you can do that in your IDE) and look at the methods the interface provides

    Also, don't try coding when you're really tired, go get some rest

    • Like 1
×
×
  • Create New...

Important Information

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