Jump to content

Recommended Posts

Posted (edited)

I Wanted to create a oxidation function for my own items. But i get Errors when I start minecraft.
Here the code for the oxidation:

package de.stynxyxy.armorplus.oxidationevents;

import de.stynxyxy.armorplus.item.copperlightoxidated;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import de.stynxyxy.armorplus.item.kupferarmorItem;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import de.stynxyxy.armorplus.init.moditems;
import org.apache.commons.lang3.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Mod.EventBusSubscriber
public class oxidation {
    private static final int TRANSFORM_DELAY = 100; // Number of ticks after which the item should transform
    private static final Map<ItemStack, Integer> transformCounts = new HashMap<>();
    private static final Map<Item, Item> TRANSFORM_MAP = new HashMap<>();

    static {
        //TRANSFORM_MAP.put(moditems.copper_boots.get(),moditems.exposed_copper_boots.get());

    }

    @SubscribeEvent
    public static void onServerTick(TickEvent.ServerTickEvent event) {
        if (event.phase == TickEvent.Phase.END && event.side.isServer()) {
            for (ServerPlayer player : event.getServer().getPlayerList().getPlayers()) {
                Inventory inventory = player.getInventory();

                for (int i = 0; i < inventory.getContainerSize(); i++) {
                    ItemStack itemStack = inventory.getItem(i);

                    if (shouldTransform(itemStack)) {
                        int tickCount = transformCounts.getOrDefault(itemStack, 0);

                        if (tickCount >= TRANSFORM_DELAY) {
                            Item targetItem = TRANSFORM_MAP.get(itemStack.getItem());

                            if (targetItem != null) {
                                ItemStack newItemStack = new ItemStack(targetItem, itemStack.getCount());
                                // Transfer enchantments to the new item
                                Map<Enchantment, Integer> enchantments = itemStack.getAllEnchantments();
                                for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
                                    Enchantment enchantment = entry.getKey();
                                    int level = entry.getValue();
                                    newItemStack.enchant(enchantment, level);
                                }
                                // Set the name of the new item
                                newItemStack.setHoverName(itemStack.getHoverName());
                                inventory.setItem(i, newItemStack);
                                transformCounts.remove(itemStack);
                            }
                        } else {
                            transformCounts.put(itemStack, tickCount + 1);
                        }
                    }
                }
            }
        }
    }

    private static boolean shouldTransform(ItemStack itemStack) {
        return TRANSFORM_MAP.containsKey(itemStack.getItem());
    }
}

Thank you (;

Edited by Stynxyxy
Posted (edited)
Quote

//TRANSFORM_MAP.put(moditems.copper_boots.get(),moditems.exposed_copper_boots.get());

This cannot possibly work.

You are asking for objects with RegistryObject.get() that won't be registered yet.

The code in that static initaliser is run during mod construction.

https://forge.gemwire.uk/wiki/Stages_of_Modloading

 

You should initialise that map during the FMLCommonSetupEvent which is after all the objects have been registered.

Since you are not using a concurrent map, you should use enqueueWork() to run the setup on the main thread. See that link.

 

Why not use PlayerTickEvent instead of iterating over all the players yourself?

And i would throttle the checks so you are not doing it every tick e.g.

if (player.tickCount % 100 == 0) {
   // Do stuff every 100 ticks
}

 

Edited by warjort

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.

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.