Jump to content

Dynamic Item Registry


cossacksman

Recommended Posts

As of yet I've hated the messy standards of MinecraftForge development and I've taken to a more dynamic method of approaching item registry; my items do work and they're in the game with textures, localized names and all of the qualities I've given them but as soon as it comes to using them (for instance returning them when breaking a block) Forge fails to accept returning a new instance, for example..

 

        @Override
public Item getItemDropped(int metadata, Random random, int fortune)
{
	return new myClass();
}

 

I'll give you my registration in a spoiler but as I say, it registers fine but only seems to be able to be used if I use a static instance of it, if anyone has come across something similar it would be much appreciated if you could share what you found!

 

 

 

package com.cossacksman.qrpower.common;

import com.cossacksman.qrpower.common.blocks.WirelessAdapter;
import com.cossacksman.qrpower.common.items.WirelessReceiver;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;

public class RegistryHandler 
{
public static CreativeTabs creativeTab;

public static Block[] blocks = {
	new WirelessAdapter()
};

public static Item[] items = {
	new WirelessReceiver()
};

public static void init()
{		
	creativeTab = new CreativeTabs("QRPower") {
		@Override
		@SideOnly(Side.CLIENT)
		public Item getTabIconItem() { return Items.ender_pearl; }
	};
}

public static void registerBlocks()
{
	for (Block block : blocks) {						
		GameRegistry.registerBlock(block, block.getUnlocalizedName());
	}
}

public static void registerItems() 
{
	for (Item item : items) {
		GameRegistry.registerItem(item, item.getUnlocalizedName());
	}
}
}

 

 

 

And I'll throw in the item class I'm testing with

 

 

 

package com.cossacksman.qrpower.common.items;

import com.cossacksman.qrpower.QRPower;
import com.cossacksman.qrpower.common.RegistryHandler;

import net.minecraft.item.Item;

public class WirelessReceiver extends Item
{
public WirelessReceiver ()
{		
	maxStackSize = 32;
	setUnlocalizedName("WirelessReceiver");		
	setCreativeTab(RegistryHandler.creativeTab);
	setTextureName(QRPower.MODID + ":" + "wirelessReceiver");
}
}

 

 

 

Edit: I can get around this by using the exact same method in a HashMap but I'd prefer not to.

That is not dead which can eternal lie,

and with strange aeons even death may die.

Link to comment
Share on other sites

Items are singletons, if you want to drop an item you need to pass a reference to the same Item you registered.

 

        @Override
public Item getItemDropped(int metadata, Random random, int fortune)
{
	return MyMainModClass.myCoolItem;
}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I feared as such, as much as I hate to say it I may stick with using a HashMap (Technically an anonymous class derived from HashMap) for now then, likely if I encounter more issues I'll just have to stick to the old true and tested method. Thanks for the heads-up on the singleton pattern used here.

That is not dead which can eternal lie,

and with strange aeons even death may die.

Link to comment
Share on other sites

I don't understand your dismay. Items / Blocks are not actually Items / Blocks. They are types. Blocks.stone defines the type of block that is stone. Items.stick defines the type of item that is a stick. Every time you create a new Item / Block instance you define a new type.

I think your wording is a bit confusing. The word "Type" generally in programming means the type of object/variable (As in what class it is or if its an integer/string/etc).

 

There is only ever one single instance of each Block/Item Class which is what you register. Every Block/Item in the world only uses that one single instance (Which is why you can never store any information in the Block/Item class directly since its shared). With TileEntities on the other hand you have one instance per Block in the world which exists(Which is why you can store information in them directly since it isn't shared).

 

So in short, you only need 1 instance of a Block which you then give to other code which requires a block as an input. The Block is just a shared instance that contains no personal information about the block.

Link to comment
Share on other sites

Basically it is correct use of the English word "type" even if it isn't really correct for computer language definition of "type".

 

However, since we're discussing computer programming probably better to stick to the classic definitions ...

 

But it is difficult sometimes to explain to people the concept of why Items and Blocks are singletons without resorting to the English definition of the world type. Because the end result is you want to create a "type" of ItemStack and you do that by associating it with the relevant Item. So the Item associated defines the "type".

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Whether you are a fan of Hypixel Bedwars, SkyWars and PvP gamemodes like that, well you would enjoy this server! We have a very fun and unique style of PvP that a lot of our players really enjoy and we want to bring this server to more players like you! Yes you reading this post haha. Introducing, the Minezone Network, home of SUPER CRAFT BLOCKS. We've been working on this server for over 4 years now. Here is what we have to offer: SUPER CRAFT BLOCKS: This has 3 different gamemodes you can play, Classic, Duels and Frenzy. Each mode offers over 60 kits to choose from, along with a total of over 60 maps, allowing for various different playstyles on each map. There are also random powerups that spawn on the map which can include Health Pots, Bazookas, Nukes, Extra Lives and way way more! There is also double jump in this gamemode as well, which makes PvP a lot more fun & unique. You only need a minimum of 2 players to start any mode! Classic: Choose a kit, 5 lives for each player, fight it out and claim the #1 spot! Look out for lightning as they can spawn powerups to really give you an advantage in the game! Duels: Fight against another random player or one of your friends and see who is the best! Frenzy: Your kit is randomly selected for you, each life you will have a different kit. You can fight with up to 100 players in this mode and lets see who will be the best out of that 100! All the other stuff from Classic/Duels apply to this mode as well like powerups. We have 2 ranks on this server too, VIP and CAPTAIN which has a bunch of different perks for SCB and other things like Cosmetics and more.   SERVER IP: If this server has caught your interest in any way, please consider joining and you will NOT regret it! Bring some of your friends online for an even better experience and join in on the fun at: IP: minezone.club Hope to see you online!   SERVER TRAILER: https://www.youtube.com/watch?v=0phpMgu1mH0
    • The mod give new blocks  
    • I will a Mode for 1.21 in this Mod give new block, items and dimensions   
    • Remove Optifine For shader support and similar, use Embeddium + Oculus
  • Topics

×
×
  • Create New...

Important Information

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