Jump to content

1.18.2 stack.getItem().is import problem


DelviousCrafts

Recommended Posts

I've built a GUI for an item object that I'm trying to limit the items you can insert into it to specific forge tags using the simplified .is(anyCustomTag) method. I have the container built and working with no issues. The issue I'm running into is when I change from the functioning placeholder:
return (Items.STICK == stack.getItem());
to
return (stack.getItem().is(anyCustomTag));
for a boolean of true or false as to weather the slot can accept the item in the code. It errors out saying cannot find symbol, and symbol variable is anyCustomTag. I know it's a simple matter of missing imports as I completed this successfully with this exact same method once before by adding a specific set of imports and then my computer froze causing me to loose all my work. I can't for the life of me remember what imports I used to get this working correctly and it's driving me crazy and eclipse doesn't give me auto imports for .is() functionality as it seems to be to common a word for it to find the imports to use or something.

current imports in working code with placeholder: return (Items.STICK == stack.getItem()); are:

Spoiler
Spoiler

import net.minecraftforge.items.SlotItemHandler;

import net.minecraftforge.items.ItemStackHandler;

import net.minecraftforge.items.IItemHandler;

import net.minecraftforge.items.CapabilityItemHandler;

 

import net.minecraft.world.level.block.entity.BlockEntity;

import net.minecraft.world.level.Level;

import net.minecraft.world.item.Items;

import net.minecraft.world.item.ItemStack;

import net.minecraft.world.inventory.Slot;

import net.minecraft.world.inventory.AbstractContainerMenu;

import net.minecraft.world.entity.player.Player;

import net.minecraft.world.entity.player.Inventory;

import net.minecraft.world.entity.Entity;

import net.minecraft.server.level.ServerPlayer;

import net.minecraft.network.FriendlyByteBuf;

import net.minecraft.core.BlockPos;

I've poured through forge tags documentation attempting to resolve myself, studying out of the documentation thoroughly. Forge documentation states that Tag-holding registry objects contain a method called #is in either their registry object or state-aware class to check whether the object belongs to a certain tag. From this I concluded that importing ForgeRegistries (with obvious file structure proceeding it) would work. It did not. Then tried importing Item (from minecraft.world.item (as it's my understanding this is the state-aware class)) with no success.

I've also scoured the internet. It seems use for the .is() function is not very commonly used considering most people (understandably) don't want to limit what items can be placed into a slot, so not much luck.

Imports I've tried:

Spoiler
Spoiler

I started one at a time with what I believed to be the most likely location:

import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.ForgeRegistryEntry;
Import net.minecraftforge.registries.RegistryManager;
import net.minecraftforge.registriestry.RegistryObject;
import net.minecraftforge.registries.ObjectHolderRegistry;
import net.minecraftforge.registries.ObjectHolder;

Then I began to get desperate trying things I was less sure had what I was looking for:

import net.minecraftforge.registries.ITag;
Import net.minecraftforge.registries.tags.ITagManager;
import net.minecraftforge.registries.tags.ITagDeferredManager;

then out of further desperation began to try anything mentioned in the documentation:

import net.minecraftforge.registries.DeferredRegister;
import net.minecraft.world.item.Item; (as the state-aware class)

I then moved on to countless combinations of these same imports, including importing everything on the list at the same time.

 

I then attempted a different method of creating a new comparison approach creating a new direct registry in the beginning of the class using:

Spoiler
Spoiler


public static final TagKey<Item> SLOT0TAG = ItemTags.create(new ResourceLocation("myModName", "anyCustomTag"));
(of course importing the proper classes using eclipse auto import function), then pointing back to it using both:
stack.getItem().is(SLOT0TAG) and stack.getItem().is(anyCustomTag)
with the same error that .is() symbol cant be found.

Then attempted to use "" around anyCustomTag giving the same .is() symbol doesn't exist, including in the above approach.

Then attempted this "" around anyCustomTag approach in the same manner as before, trying each import individually, then combinations of said imports.

I then began to dig through forge's 1.18.x code on GitHub, looking through literally every class for mention of "boolean is" with the find functionality of my internet browser, and couldn't find it anywhere in the GitHub repository. Plenty of "boolean isSomething" but no plain old "boolean is". Truly I have made (in my opinion) a more than valiant attempt at resolving this on my own with no results. At this point I'm ready to throw in the towel. Someone please assist with what import I'm missing for my class to work, or how I need to adjust the code for it to function as desired.

 

Full code if it helps (erroring code color is changed to yellow for an easier find):

Spoiler


package net.dccoinpurse.world.inventory;

import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.registries.ForgeRegistries;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.Item;

import net.dccoinpurse.network.DCCOINPURSESlotMessage;
import net.dccoinpurse.init.DccoinpurseModMenus;
import net.dccoinpurse.DccoinpurseMod;

import java.util.function.Supplier;
import java.util.Map;
import java.util.HashMap;

public class DCCOINPURSEMenu extends AbstractContainerMenu implements Supplier<Map<Integer, Slot>> {
    public final static HashMap<String, Object> guistate = new HashMap<>();
    public final Level world;
    public final Player entity;
    public int x, y, z;
    private IItemHandler internal;
    private final Map<Integer, Slot> customSlots = new HashMap<>();
    private boolean bound = false;

    public DCCOINPURSEMenu(int id, Inventory inv, FriendlyByteBuf extraData) {
        super(DccoinpurseModMenus.DCCOINPURSE, id);
        this.entity = inv.player;
        this.world = inv.player.level;
        this.internal = new ItemStackHandler(16);
        BlockPos pos = null;
        if (extraData != null) {
            pos = extraData.readBlockPos();
            this.x = pos.getX();
            this.y = pos.getY();
            this.z = pos.getZ();
        }
        if (pos != null) {
            if (extraData.readableBytes() == 1) { // bound to item
                byte hand = extraData.readByte();
                ItemStack itemstack;
                if (hand == 0)
                    itemstack = this.entity.getMainHandItem();
                else
                    itemstack = this.entity.getOffhandItem();
                itemstack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
                    this.internal = capability;
                    this.bound = true;
                });
            } else if (extraData.readableBytes() > 1) {
                extraData.readByte(); // drop padding
                Entity entity = world.getEntity(extraData.readVarInt());
                if (entity != null)
                    entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
                        this.internal = capability;
                        this.bound = true;
                    });
            } else { // might be bound to block
                BlockEntity ent = inv.player != null ? inv.player.level.getBlockEntity(pos) : null;
                if (ent != null) {
                    ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
                        this.internal = capability;
                        this.bound = true;
                    });
                }
            }
        }
        this.customSlots.put(0, this.addSlot(new SlotItemHandler(internal, 0, 25, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(0, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (stack.getItem().is(coins/copper));
                
            }

        }));
        this.customSlots.put(1, this.addSlot(new SlotItemHandler(internal, 1, 43, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(1, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(2, this.addSlot(new SlotItemHandler(internal, 2, 61, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(2, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(3, this.addSlot(new SlotItemHandler(internal, 3, 79, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(3, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(4, this.addSlot(new SlotItemHandler(internal, 4, 97, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(4, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(5, this.addSlot(new SlotItemHandler(internal, 5, 115, 60) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(5, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(6, this.addSlot(new SlotItemHandler(internal, 6, 25, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(6, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(7, this.addSlot(new SlotItemHandler(internal, 7, 43, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(7, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(8, this.addSlot(new SlotItemHandler(internal, 8, 61, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(8, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(9, this.addSlot(new SlotItemHandler(internal, 9, 79, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(9, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(10, this.addSlot(new SlotItemHandler(internal, 10, 97, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(10, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(11, this.addSlot(new SlotItemHandler(internal, 11, 115, 78) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(11, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(12, this.addSlot(new SlotItemHandler(internal, 12, 43, 96) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(12, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(13, this.addSlot(new SlotItemHandler(internal, 13, 61, 96) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(13, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(14, this.addSlot(new SlotItemHandler(internal, 14, 79, 96) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(14, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        this.customSlots.put(15, this.addSlot(new SlotItemHandler(internal, 15, 97, 96) {
            @Override
            public void setChanged() {
                super.setChanged();
                slotChanged(15, 0, 0);
            }

            @Override
            public boolean mayPlace(ItemStack stack) {
                return (Items.STICK == stack.getItem());
            }
        }));
        for (int si = 0; si < 3; ++si)
            for (int sj = 0; sj < 9; ++sj)
                this.addSlot(new Slot(inv, sj + (si + 1) * 9, 0 + 8 + sj * 18, 54 + 84 + si * 18));
        for (int si = 0; si < 9; ++si)
            this.addSlot(new Slot(inv, si, 0 + 8 + si * 18, 54 + 142));
    }

 

Link to comment
Share on other sites

  • DelviousCrafts changed the title to 1.18.2 stack.getItem().is import problem

There is no

Item.is(TagKey<Item>) // does not exist

  but there is an

ItemStack.is(TagKey<Item>)

 

Just remove your getItem() 🙂 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

3 hours ago, warjort said:

There is no

Item.is(TagKey<Item>) // does not exist

  but there is an

ItemStack.is(TagKey<Item>)

 

Just remove your getItem() 🙂 

Thanks for the reply!!

I did attempt what you suggested...

Attempted exactly as suggested:
return (ItemStack.is(TagKeyReference));
requiring a implimentation of:
public static final TagKey<Item> TagKeyReference = ItemTags.create(new ResourceLocation("modName", "anyCustomTag")); (with proper imports of course)

As well as
return (itemStack.is(anyCustomTag));
and
return (ItemStack.is("anyCustomTag"));
All of which returned an error of: non-static method cannot be referenced from a static context.
It seems that the ItemStack.is is from a static boolean in vanilla minecraft code which can't be referenced by the non-static field required by AbstractContainerMenu Supplier. (as otherwise it goes way wonkey with a mess of errors, but just to be sure I did attempt to make this static as well.)

Also attempted as:
public final TagKey<Item> ItemTagReference = ItemTags.create(new ResourceLocation("modName", "anyCustomTag"));
to remove this "static" field just to be sure along with all methods of return listed above. All of which returned the same error as above.

Also, correct me if I'm wrong, but as I understand it stack.getItem() is referencing the ItemStack that you're attempting to provide to the slot, therefore I'm not calling Item.is(TagKey<Item>), I'm already referencing ItemStack.is(TagKey<Item>) with the stack.getItem().is code. stack.getItem() is found in the SlotItemHandler of Forge being called by the customSlots reference of new SlotItemHandler which, according to the Forge code, references an itemStack, not an Item direct.

Link to comment
Share on other sites

Look at the method definition in ItemStack. It is not a static method.

Then look at BeaconMenu.PaymentSlot.mayPlace() for an example usage.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.