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

So i met the bug in a pretty simple code, and I completely don't know why it appears.

https://youtu.be/9o534kIYxUc

I want to create Gift item, that after putting it with another item in crafting, it makes it enchanted. And then after right clicking it, it turns into certain item.

BUT
Weird things are doing here. Item disappears or gives result WITHOUT crafting it (just putting and collecting it from crafting table menu).

My code

GiftCrafting - special recipe

Spoiler

package com.matez.wildnature.items.recipes;

import com.matez.wildnature.items.GiftItem;
import com.matez.wildnature.other.Utilities;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.SpecialRecipe;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;

public class GiftCrafting extends SpecialRecipe {

    public GiftCrafting(ResourceLocation idIn) {
        super(idIn);
    }

    public boolean matches(CraftingInventory inv, World worldIn) {
        int i = 0;
        int j = 0;
        for(int k = 0; k < inv.getSizeInventory(); ++k) {
            ItemStack itemstack = inv.getStackInSlot(k);
            if (!itemstack.isEmpty()) {
                if (itemstack.getItem() instanceof GiftItem) {
                    if(itemstack.getOrCreateTag().isEmpty()) {
                        ++i;
                    }
                } else {
                    ++j;
                }

                if (j > 1 || i > 1) {
                    return false;
                }
            }
        }

        return i == 1 && j == 1;
    }

    public ItemStack getCraftingResult(CraftingInventory inv) {
        ItemStack giftItemStack = ItemStack.EMPTY;
        ItemStack gift = ItemStack.EMPTY;



        for(int i = 0; i < inv.getSizeInventory(); ++i) {
            ItemStack itemstack1 = inv.getStackInSlot(i);
            if (!itemstack1.isEmpty()) {
                Item item = itemstack1.getItem();
                if (item instanceof GiftItem) {
                    giftItemStack = itemstack1;
                } else {
                    gift = itemstack1;
                }
            }
        }

        ItemStack result = giftItemStack;
        CompoundNBT nbt = giftItemStack.getOrCreateTag();
        if(nbt.isEmpty() && giftItemStack != ItemStack.EMPTY && gift != ItemStack.EMPTY) {
            Utilities.saveItem(nbt, new ItemStack(gift.getItem()));
            result.setTag(nbt);
        }else{
            return null;
        }

        return result;
    }

    public boolean canFit(int width, int height) {
        return width * height >= 2;
    }


    @Override
    public IRecipeSerializer<?> getSerializer() {
        return Registry.RECIPE_SERIALIZER.getOrDefault(new ResourceLocation("wildnature:gift_crafting"));
    }
}

Gift Item class
 

Spoiler


package com.matez.wildnature.items;

import com.matez.wildnature.other.Utilities;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.*;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class GiftItem extends Item {
    private GiftColor color;
    public GiftItem(Properties properties, GiftColor color) {
        super(properties.maxStackSize(1));
        this.color=color;
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context) {
        System.out.println("click");
        ItemStack i = context.getPlayer().getHeldItem(context.getHand());
        ItemStack gift = Utilities.loadItem(i.getTag());

        if(gift == ItemStack.EMPTY) {
            return ActionResultType.PASS;
        }else{
            context.getPlayer().setHeldItem(context.getHand(),gift);
            return ActionResultType.SUCCESS;
        }
    }

    @OnlyIn(Dist.CLIENT)
    public boolean hasEffect(ItemStack stack) {
        ItemStack gift = Utilities.loadItem(stack.getTag());
        return gift != ItemStack.EMPTY;
    }


    public enum GiftColor implements IStringSerializable {
        CYAN_RED("cyan_red"),
        RED_YELLOW("red_yellow"),
        BLUE_PINK("blue_pink");

        public static final GiftColor[] VALUES = values();
        private final String name;

        private GiftColor(String name) {
            this.name = name;
        }

        @Override
        public String getName() {
            return null;
        }
    }

}

Special Recipe Registry
  

Spoiler


@SubscribeEvent
public static void registerRecipes(final RegistryEvent.Register<IRecipeSerializer<?>> event){
    LOGGER.info("Registering recipes...");
    event.getRegistry().register(new SpecialRecipeSerializer<>(GiftCrafting::new).setRegistryName("wildnature:gift_crafting"));
}

 

 

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.