Jump to content

[1.9.4] [SOLVED] Singular item class, change block placed upon registry


Recommended Posts

Posted

Hi guys. I'm currently trying to update my mod to the latest version and as a result, I've been trying to optimise how I do things all over the place.

 

What I want to achieve is a singular item "parent" class that holds all the necessary logic for the item, but have the block it places on item use be configurable so I can manually define that.

 

Anybody have any suggestions as to how I could go about this?

 

Thanks!

Posted

The static keyword is almost certainly not what you want.

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.

Posted

*Facepalm*

 

What if you do this in your preInit:

 

myStoneItem = new ItemClass(Blocks.stone);
myWoodItem = new ItemClass(Blocks.planks);

 

What do you expect to happen?

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.

Posted

I'm not a total idiot you know... you don't have to talk to me like one... just saying.

 

Yes you're saying that because I'm calling on a block in the preInit yet to be created in the init it will not associate and return null.

 

My point simply at this stage is to find a way to associate a particular block type with my item so I can manually define which block the item will place during the declaration instead of using 50 item classes to define the particular block in question.

Posted

Draco's point is that a static field will be shared between all instances of the class, so all instances would use the

Block

passed to the last instance created. This is basic Java that you should already know before creating a mod.

 

Use an instance field instead.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Ah got ya!

 

Thanks Choonster and Draco for the advice as always! My background is mainly VB sadly so I'm still getting to grips with Java so I appreciate the help.

Posted

Hopefully I've understood what my IDE told me and you guys...

 

    Block block;

    public MysticalCropSeed(String regName, Block block) {
        super();
        this.regName = regName;
        this.block = block;
        setCreativeTab(MysticalCrops.cropsTab);
    }

Posted

That will work, but you should make the field private (so it can't be accessed outside of the class) and final (so it can't be modified after it's set in the constructor).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I actually found an easier way to achieve what I was trying to do, so I thought I'd post it here in case people were interested:

 

My item registry class:

public class CropItems {
    public static Item[] MCSeeds;
    public static Item[] MCDrops;
    public static HashMap<MysticalCropBlock, Item> seedsMap = new HashMap<MysticalCropBlock, Item>();
    public static HashMap<MysticalCropBlock, Item> harvestedItemMap = new HashMap<MysticalCropBlock, Item>();

    public static Item redstoneSeedItem = new MysticalCropSeed(CropBlocks.redstoneCrop, Blocks.FARMLAND, "redstoneCropSeed");

    public static Item regItem(Item item, String regName) {
        item.setRegistryName(regName);
        item.setUnlocalizedName(regName);

        return GameRegistry.register(item);
    }

    public static void loadItemRegistry() {
        regItem(redstoneSeedItem, "redstoneCropSeed");

        MCSeeds = new Item[] {redstoneSeedItem};
        MCDrops = new Item[] {Items.REDSTONE};
    }
}

 

My crop block class that binds the seeds and eventually any drops to the block in question:

    protected Item getSeeds() {
        final Item seeds = CropItems.seedsMap.get(this);

        if(seeds == null) {
            FMLLog.bigWarning("No seeds detected!");
            return new Item();
        }
        return seeds;
    }

    @Override
    public ItemStack getItem(World world, BlockPos pos, IBlockState state) {
        return new ItemStack(getSeeds());
    }

    @Override
    public boolean canGrow(World worl, BlockPos pos, IBlockState state, boolean isClient) {
        return !isHarvestReady(state);
    }

    protected Item getHarvestedItem() {
        final Item harvestedItem = CropItems.harvestedItemMap.get(this);
        if(harvestedItem == null) {
            FMLLog.bigWarning("No drop registered!");
            return new Item();
        }
        return harvestedItem;
    }

 

There's more to it of course, but I wanted to share this in case people were struggling like I was.

 

Thanks everyone!

Posted

...What are you doing there?  No really, I'm confused.  Why are you doing that?

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.

Posted

So I confused even the great Draco18s! I'll add that as an Achievement Unlocked! lol!!!

 

OK so I admit the code may seem a bit broken up and in all honesty, I had missed a few steps but I've since gone back and tweaked a few things.

 

My item class now simply looks like this:

public class MysticalCropSeed extends ItemSeeds{

    public MysticalCropSeed(Block crops, Block soil, String regName) {
        super(crops, soil);
        setUnlocalizedName(regName);
        setCreativeTab(MysticalCrops.cropsTab);
    }
}

So everything seed related is handled by the underlying seed logic. A hint I got from reading your example mod on that winter wheat or something...

 

Block side, it now associates with the seed by checking the seedsMap as defined in CropItems:

public class CropItems {
    public static HashMap<MysticalCropBlock, Item> seedsMap = new HashMap<MysticalCropBlock, Item>();
    public static HashMap<MysticalCropBlock, Item> harvestedItemMap = new HashMap<MysticalCropBlock, Item>();

    public static Item redstoneCropSeed;

    public static Item regItem(Item item, String regName) {
        item.setRegistryName(regName);
        item.setUnlocalizedName(regName);

        return GameRegistry.register(item);
    }

    public static Item regSeeds(String regName, Block crop) {
        Item item = new MysticalCropSeed(crop, Blocks.FARMLAND, regName);

        seedsMap.put((MysticalCropBlock) crop, item);

        return regItem(item, regName);
    }

    public static Item regDrops(Item item, Block crop) {
        return harvestedItemMap.put((MysticalCropBlock) crop, item);
    }

    public static void loadItemRegistry() {
        regSeeds("redstoneCropSeed", CropBlocks.redstoneCrop);
        regDrops(Items.REDSTONE, CropBlocks.redstoneCrop);

        MCSeeds = new Item[] {redstoneCropSeed};
        MCDrops = new Item[] {Items.REDSTONE};
    }

The methods regSeeds and regDrops add the relevant drop item to the relevant HashMap. By doing this, the seed required to plant the block and the required drops upon breaking can be defined in the block class instead as a general constructor.

 

I implement it like this:

public class MysticalCropBlock extends BlockCrops implements IGrowable, IPlantable {

    protected Item getSeeds() {
        final Item seeds = CropItems.seedsMap.get(this);

        if(seeds == null) {
            FMLLog.bigWarning("No seeds detected!");
            return new Item();
        }
        return seeds;
    }

    @Override
    public ItemStack getItem(World world, BlockPos pos, IBlockState state) {
        return new ItemStack(getSeeds());
    }

    protected Item getHarvestedItem() {
        final Item harvestedItem = CropItems.harvestedItemMap.get(this);
        if(harvestedItem == null) {
            FMLLog.bigWarning("No drop registered!");
            return new Item();
        }
        return harvestedItem;
    }

    @Override
    public Item getItemDropped(IBlockState state, Random rnd, int fortune) {
        if(!isHarvestReady(state)) {
            return getSeeds();
        }else{
            return getHarvestedItem();
        }
    }

    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
                                    ItemStack stack, EnumFacing side, float hitX, float hitY, float hitZ) {
        if(isHarvestReady(state)) {
            if(world.isRemote) {
                return true;
            }

            final ItemStack savedStack = new ItemStack(getHarvestedItem());

            world.setBlockState(pos, state.withProperty(AGE, 0), 7);
            final EntityItem entItem = new EntityItem(world, player.posX, player.posY - 1D, player.posZ, savedStack);
            world.spawnEntityInWorld(entItem);
            entItem.onCollideWithPlayer(player);
            return true;
        }
        return false;
    }

So in effect, the item doesn't define the block, the block defines the item. By linking it through the getSeeds and getHarvestedItem methods, they can be bound in the item registry.

 

Hope that makes a bit more sense.

 

And yes; I purposely only included the methods relevant. That's why the block class may seem empty but trust me, it's all there and works great.

Posted

No, the question I'm asking is:

 

Why do you have a HashMap at all?

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.

Posted

It works and I have no complaints about it.

 

How else did you expect me to map the seeds and drops to the relevant block using only one class for each?

 

Anyway, this is all fixed now and works just as I had intended. With the added benefit that I achieved my goal of halving the file size of the mod since 1.8.9 and now.

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

    • Also check the worldsave / serverconfig folder If there is no such file, make a test without this mod  
    • Hi, I've been having trouble trying to use forge as it shows a black screen when I open the game, but I can still interact with it and hear the music.  I've done all of the step by steps and most common fixes like updating drivers, keeping up to date with Java, deleting and reinstalling minecraft, restarting my computer MANY times, even smaller things like splash.properties (I didn't have that file so I added it and set it to false thinking it would do something, definitely not) and making sure to prioritize my rtx 3070 in the settings but with no luck. Minecraft works as intended when I uninstall forge and I also don't have any mods currently, it just gives me this issue when I install forge. I also increased the ram usage, made sure my hardware isn't full or anything, and even changed the resolution in hopes it would fix things. I checked my antivirus and firewall but that isn't the issue either. Trust me, I've done everything I can think of. For some reason the black screen does flicker a little into the main menu, but obviously unplayable. I couldn't even make my way to the settings with how little it flickered. I'm not sure if it flickered randomly or if it was because I was messing around moving and clicking a bunch, I didn't really test it that much.  
    • I've had a really weird issue recently,  I wanted to add the Depper and Darker mod on my dedicated server (MC 1.21 with Fabric 0.16.9, hosted on nitroserv.com) but whenever I do add the mod the sever stops doing anything after listing the mods, and I get no crash or error or anything, just a stuck server. Here's a normal log of the server booting up: https://pastebin.com/JipFF2Eh and here's the log of the server doing the weird thing: https://pastebin.com/W4JBh3eX I just don't understand it. I've tried removing other mods (somewhat randomly) but deeper and darker still breaks my server whenever I add it. NitroServ support staff is about as confused as I am and I've had no response from the Deeper and Darker support staff... Now I know this is the Forge support not the Fabric support but I'm just trying to know if anyone has any kind of idea to fix this (aside from not using the mod obviously) Also I still have a bunch of errors and warnings whenever the server does start properly, are there any of them I should be worried about?
  • Topics

×
×
  • Create New...

Important Information

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