Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to override registries (change name of item's registries) in minecraft?


CrossForz
 Share

Recommended Posts

Hello everyone, I recently ran into one problem when I started doing forge mods. I have already tried to find a solution to my problem on this site, but I have not been able to fix it.

The main problem: I created a custom minecraft item (ak-47) and I wanted to change the first-person view of the weapon when clicking on the right mouse button (another json file was connected). Json files are loaded to item by the name specified in the file ModItems.java. I want that when clicking on the mouse button, the registry name is overrided to the desired name of the json file, which is responsible for a different first-person view, but I do not know how to do it correctly so that no errors occur.

 

In file ModItems.java I created 1 registry, wich connect a json model and add item in creative tab.

package net.battleroyale.modbtlr.item;

import net.battleroyale.modbtlr.BattleRoyaleMod;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraftforge.registries.*;

public class ModItems{

    public static DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, BattleRoyaleMod.MOD_ID);

    public static RegistryObject<Item> AK47GUN = ITEMS.register("ak-47", () -> new AK47(new Item.Properties().tab(CreativeModeTab.TAB_COMBAT)));
}

 

The file AK47.java containes right mouse click handler and registries overriding (Changing the register name from ak-47 to zoomx1).

package net.battleroyale.modbtlr.item;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
public class AK47 extends Item{


    @Override
    public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
        if(!world.isClientSide()){
            ModItems.AK47GUN = ModItems.ITEMS.register("zoomx1", () -> new AK47(new Item.Properties().tab(CreativeModeTab.TAB_COMBAT)));
        }
        return super.use(world, player, hand);
    }
    public AK47(Item.Properties properties) {
        super(properties);
    }
}

 

This is the error it gives me:

java.lang.IllegalStateException: Cannot register new entries to DeferredRegister after RegistryEvent.Register has been fired.
	at net.minecraftforge.registries.DeferredRegister.register(DeferredRegister.java:198) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2375%2381!/:?] {re:classloading}
	at net.battleroyale.modbtlr.item.AK47.use(AK47.java:16) ~[%2380!/:?] {re:classloading}
	at net.minecraft.world.item.ItemStack.use(ItemStack.java:252) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,xf:fml:forge:itemstack}
	at net.minecraft.server.level.ServerPlayerGameMode.useItem(ServerPlayerGameMode.java:293) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.server.network.ServerGamePacketListenerImpl.handleUseItem(ServerGamePacketListenerImpl.java:1017) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.network.protocol.game.ServerboundUseItemPacket.handle(ServerboundUseItemPacket.java:23) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.network.protocol.game.ServerboundUseItemPacket.handle(ServerboundUseItemPacket.java:7) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:22) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.server.TickTask.run(TickTask.java:17) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:143) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:22) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:799) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:164) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:116) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:782) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:776) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.thread.BlockableEventLoop.runAllTasks(BlockableEventLoop.java:104) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:761) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:689) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:261) ~[forge-1.18.2-40.1.0_mapped_official_1.18.2-recomp.jar%2376!/:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:833) [?:?] {}

 

Link to comment
Share on other sites

That makes zero sense and is not how registries, registry names or models work at all. Your DeferredRegister and RegistryObject fields should be final.

If you want separate items, then you need to register both items at startup and then change the Item held by the player on right click. Alternatively you can set a marker in the ItemStack's NBT data, however this will make changing the model a little more complicated - look at the bow for reference on how to do that.

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
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.

 Share



×
×
  • Create New...

Important Information

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