Jump to content

Recommended Posts

Posted

I have created an item which ticks when it is in a player inventory. It has a timer that applies a potion effect to the player whenever a certain time has been exceeded. And so it loops. 

This item has 2 important variables, a timer, as well as a boolean which is used to determine the item that should be ticking whenever a player has more than one instance in the inventory. I determine this value everytime a player drops or picks this item up.

The problem now is that the data doesn't seem to be saving. Any help would be very much appreciated.

    @Override
    public void inventoryTick(ItemStack itemStack, World world, Entity entity, int p_77663_4_, boolean p_77663_5_)
    {
        super.inventoryTick(itemStack, world, entity, p_77663_4_, p_77663_5_);

        if (entity instanceof ServerPlayerEntity)
        {
            readItemStackInfo(itemStack);

            if (GetIsActiveStack(itemStack)){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) entity;
                incrementCurrentTime(itemStack);

                if (this.currentTime >= deterInterval + random.nextInt(50))
                {
                    resetCurrentTime(itemStack);
                    int deterValue = random.nextInt(5) + 1;

                    addPositivePotionEffect(positiveEffects, serverPlayer);

                    itemStack.hurt(deterValue, random, serverPlayer);

                    if (itemStack.getMaxDamage() - itemStack.getDamageValue() <= 0){
                        SetIsActiveStack(itemStack, false);

                        SetNewActiveStack(itemStack, serverPlayer);
                    }
                }
            }
        }
    }
    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        readItemStackInfo(itemStack);

        return this.isActiveStack;
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        this.isActiveStack = active;
        writeItemStackInfo(itemStack);
    }

    public void readItemStackInfo(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getTag();

        if (nbt == null){
            writeItemStackInfo(itemStack);
        }

        this.isActiveStack = nbt.getBoolean("active");
    }

    public CompoundNBT writeItemStackInfo(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("active", this.isActiveStack);
        itemStack.setTag(nbt);

        return nbt;
    }

 

Posted
  On 8/6/2021 at 11:11 AM, diesieben07 said:

You cannot store data in your Item class like that. There is only one instance of your item class, as such you cannot store stack-specific data as instance fields.

Expand  

Ah, right. So like this?

    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            SetIsActiveStack(itemStack, false);
        }

        return nbt.getBoolean("active");
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("active", active);
        itemStack.setTag(nbt);
    }

 

  On 8/6/2021 at 11:11 AM, diesieben07 said:

Additionally you should really avoid modifying the stack NBT every single tick. Instead of counting down a value in the stack, you can store a target "game time" (World#getGameTime) and then check if it has been reached. This was you do not need to modify the NBT.

Expand  

Ahh, that is a very solid suggestion!

    private int getNextPotionTime(ItemStack itemStack){
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            setNextPotionTime(itemStack, (int) itemStack.getEntityRepresentation().level.getGameTime() + deterInterval + random.nextInt(50));
        }

        return nbt.getInt("time");
    }

    private void setNextPotionTime(ItemStack itemStack, int time){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putInt("time", time);
        itemStack.setTag(nbt);
    }

This still doesn't seem to work. Through my logging, all the values are as default and doesn't seem to get saved.

Posted
  On 8/6/2021 at 2:47 PM, diesieben07 said:

Yes, except you need to realize that the ItemStack tag is global ground. You should store your data either prefixed with your ModID or store it in a sub-compound named after your ModID.

Expand  

So, like this?

    public boolean GetIsActiveStack(ItemStack itemStack)
    {
        CompoundNBT nbt = itemStack.getTag();
        if (nbt == null){
            SetIsActiveStack(itemStack, false);
        }

        return nbt.getBoolean("tnw.active");
    }

    public void SetIsActiveStack(ItemStack itemStack, boolean active){
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("tnw.active", active);
        itemStack.setTag(nbt);
    }

I log this every tick, only for testing this specific occurence, this will be removed promptly after I have this working. 

            LogManager.getLogger().info(GetIsActiveStack(itemStack));
            LogManager.getLogger().info(getNextPotionTime(itemStack));

They both log false and 0 respectively.

Posted
  On 8/6/2021 at 3:02 PM, diesieben07 said:

Why is your ModID "tnw"? That is way too short, it should be something unique. Do not use abbrevations.

Expand  

The ModID is not permanent in this scenario. I don't yet have a good name for the mod, so tnw is just temporary. 

    @Override
    public void inventoryTick(ItemStack itemStack, World world, Entity entity, int p_77663_4_, boolean p_77663_5_)
    {
        super.inventoryTick(itemStack, world, entity, p_77663_4_, p_77663_5_);

        if (entity instanceof ServerPlayerEntity)
        {
            LogManager.getLogger().info(GetIsActiveStack(itemStack));
            LogManager.getLogger().info(getNextPotionTime(itemStack));

            if (GetIsActiveStack(itemStack)){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) entity;

                if (hasTimeBeenReached(itemStack))
                {
                    setNextPotionTime(itemStack, (int) (world.getGameTime() + deterInterval + random.nextInt(50)));
                    int deterValue = random.nextInt(5) + 1;

                    addPositivePotionEffect(positiveEffects, serverPlayer);

                    itemStack.hurt(deterValue, random, serverPlayer);

                    if (itemStack.getMaxDamage() - itemStack.getDamageValue() <= 0){
                        SetIsActiveStack(itemStack, false);

                        SetNewActiveStack(itemStack, serverPlayer);
                    }
                }
            }
        }
    }
    private boolean hasTimeBeenReached(ItemStack itemStack){
        if (itemStack.getEntityRepresentation().level.getGameTime() >= getNextPotionTime(itemStack)){
            LogManager.getLogger().info(getNextPotionTime(itemStack));
            return true;
        }
        return false;
    }

 

Posted (edited)
  On 8/6/2021 at 3:12 PM, diesieben07 said:

Where do you set the stack as active?

Expand  

In ItemPickupEvent.

    @SubscribeEvent
    public static void onItemPickup(PlayerEvent.ItemPickupEvent event){
        if (event.getPlayer() instanceof ServerPlayerEntity){
            if (event.getStack().getItem().getItem() == TnwItems.WISP_BOTTLE.get()){
                ServerPlayerEntity serverPlayer = (ServerPlayerEntity) event.getPlayer();
                WispBottleItem item = (WispBottleItem) event.getStack().getItem();

                boolean flag = false;

                Iterator<ItemStack> iterator = WispBottleItem.getWispsOnPlayer(serverPlayer).iterator();

                for (Iterator<ItemStack> it = iterator; it.hasNext(); ) {
                    ItemStack stack = it.next();

                    if (((WispBottleItem)stack.getItem()).GetIsActiveStack(stack)){
                        flag = true;
                    }
                }

                if (!flag){
                    item.SetIsActiveStack(event.getStack(), true);
                    LogManager.getLogger().info("Picked up!" + item.GetIsActiveStack(event.getStack()));
                }
            }
        }
    }

The logger at the end always reports as true since the value never saves.

Edited by Triphion
Posted
  On 8/6/2021 at 3:24 PM, diesieben07 said:

ItemPickupEvent fires after the stack has already been picked up, you cannot modify it anymore.

You can use EntityItemPickupEvent, which fires before the stack has been added to the player's inventory and thus you can modify the stack. Note however that if the item is stackable, only parts may be picked up. And also note that EntityItemPickupEvent fires even if the item doesn't actually fit in the player's inventory. Check ItemEntity#playerTouch for details of how this works.

Expand  

That seems to have done the trick! Thanks!

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

    • Hello, I want to change some things about a specific mod. I made some small mods for myself before, but I don't know how to use other mods as a dependencie and add the repositories. My build.gradle file looks something like this:   repositories { flatDir { dirs 'libs' } } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation fg.deobf("libs:create-1.20.1:0.5.1.f") implementation fg.deobf("libs:create-stuff-additions1.20.1:2.0.4a") } I have the folder libs in my root folder. If I start the gradlew.exe, I get the following output: > Configure project : Java: 17.0.15, JVM: 17.0.15+6 (Eclipse Adoptium), Arch: amd64 files(...) dependencies are not deobfuscated. Use a flatDir repository instead: https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver Cannot deobfuscate dependency of type DefaultSelfResolvingDependency_Decorated, using obfuscated version! files(...) dependencies are not deobfuscated. Use a flatDir repository instead: https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver Cannot deobfuscate dependency of type DefaultSelfResolvingDependency_Decorated, using obfuscated version! I don't know why this warning occurs or how to fix it. If I try to build the whole thing, I get the same message, but this time as an error and the build process crashes. I looked at the gradle dependencies website (https://docs.gradle.org/current/userguide/dependency_management_for_java_projects.html), but everything I tried did not work.   I use the Minecaft version 1.20.1 and the Forge version 47.2.17. Both mods don't have other dependencies and work perfectly fine if I start the game normaly.   Can someone explain to me why it does not work and/or where I can get help? Something like a documentation would be enough.
    • im just going to play without shaders(my friends will be dissapointed)
    • crashed the game // Embeddium instance tainted by mods: [oculus] // Please do not reach out for Embeddium support without removing these mods first. // ------- // Why did you do that? flywheel is needed for oculus but when i go into mc it then says sodium is 0.5.3(rubidum fork of sodium ported to forge)
    • oh wait i think i found the error  rubidium wasnt installed for oculus . IT WAS RUBIDIUM????????????????????
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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