Jump to content

Recommended Posts

Posted

I am fairly new to this whole modding thing, so I have started to experiment by implementing random stuffs into the game.

 

I just started to work with food items, so I made a new ModFood class file.

 

Here is the source code:

package net.gwee.mod;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.util.Icon;

public class ModFood extends ItemFood {

        private String iconPath;
        private String unlocalizedName;
       
        @SideOnly(Side.CLIENT)
        private Icon icon;
       
        public ModFood( int par1, String par5String, int par2, float par3, boolean par4 ){
                super(par1, par2, par3, par4);
                this.iconPath = par5String;
        }
       
        @SideOnly(Side.CLIENT)
        public void registerIcons(IconRegister par1IconRegister){
                this.icon = par1IconRegister.registerIcon("mod/" + this.iconPath);
        }
       
        @SideOnly(Side.CLIENT)
        public Icon getIconFromDamage(int par1){
                return this.icon;
        }
       
        public ModFood setUnlocalizedName(String par1Str)
    {
        this.unlocalizedName = par1Str;
        return this;
    }
}

http://paste.minecraftforge.net/view/dbe7ac49

 

Just in case, here is the source code of ItemFood:

package net.minecraft.item;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.potion.PotionEffect;
import net.minecraft.world.World;

public class ItemFood extends Item
{

    public final int itemUseDuration;

    private final int healAmount;
    private final float saturationModifier;

    private final boolean isWolfsFavoriteMeat;

    private boolean alwaysEdible;

    private int potionId;

    private int potionDuration;

    private int potionAmplifier;

    private float potionEffectProbability;

    public ItemFood(int par1, int par2, float par3, boolean par4)
    {
        super(par1);
        this.itemUseDuration = 32;
        this.healAmount = par2;
        this.isWolfsFavoriteMeat = par4;
        this.saturationModifier = par3;
        this.setCreativeTab(CreativeTabs.tabFood);
    }

    public ItemFood(int par1, int par2, boolean par3)
    {
        this(par1, par2, 0.6F, par3);
    }

    public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        --par1ItemStack.stackSize;
        par3EntityPlayer.getFoodStats().addStats(this);
        par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F);
        this.onFoodEaten(par1ItemStack, par2World, par3EntityPlayer);
        return par1ItemStack;
    }

    protected void onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        if (!par2World.isRemote && this.potionId > 0 && par2World.rand.nextFloat() < this.potionEffectProbability)
        {
            par3EntityPlayer.addPotionEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier));
        }
    }

    public int getMaxItemUseDuration(ItemStack par1ItemStack)
    {
        return 32;
    }


    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
        return EnumAction.eat;
    }


    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        if (par3EntityPlayer.canEat(this.alwaysEdible))
        {
            par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
        }

        return par1ItemStack;
    }

    public int getHealAmount()
    {
        return this.healAmount;
    }

    public float getSaturationModifier()
    {
        return this.saturationModifier;
    }


    public boolean isWolfsFavoriteMeat()
    {
        return this.isWolfsFavoriteMeat;
    }


    public ItemFood setPotionEffect(int par1, int par2, int par3, float par4)
    {
        this.potionId = par1;
        this.potionDuration = par2;
        this.potionAmplifier = par3;
        this.potionEffectProbability = par4;
        return this;
    }

    public ItemFood setAlwaysEdible()
    {
        this.alwaysEdible = true;
        return this;
    }
}

http://paste.minecraftforge.net/view/0ccae1ba

 

Anyways, so I created new 2 new food items, toast and butter. I created everything needed to implement it into the game, but for some odd reason, both of them appear as "Butter". I suspect it must be UnlocalizedName in some way, but I haven't really found out how to fix it.

Here is the code of interest in the main class file:

   public static ModFood toast = new ModFood(8150, "toast", 6, 6.5F, false).setUnlocalizedName("toast");
        public static ModFood butter = new ModFood(8151, "butter", 1, 1F, false).setUnlocalizedName("butter");
       
        public static CreativeTabs tabBillMod = new CreativeTabBillMod(CreativeTabs.getNextID(), derpEssenceR.itemID, "tabBillMod", "Bill's Mod");

        @Override
        public void load() {
               
                //Register CreativeTabs
                toast.setCreativeTab(tabBillMod);
                butter.setCreativeTab(tabBillMod);
               
                //Register Item
                GameRegistry.registerItem(toast, "toast");
                GameRegistry.registerItem(butter, "butter");

                //Register Names
                LanguageRegistry.addName(toast, "Toast");
                LanguageRegistry.addName(butter, "Butter");
               
}

http://paste.minecraftforge.net/view/dee39682

 

Any help or feedback would be extremely appreciated! ;D

Posted

well that and ... you're using the same class for both item WITHOUT meta. you should be either using meta OR 2 different class

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Posted

Sorry, forgot to mention. Both of them have their own respective properties, just that the names are swapped. Is there anyway of doing it without 2 different classes?

Posted

Well, as I stated before, I am quite new to modding, so I'll have to learn how to "meta", but is there an alternate solution where I can declare a new food similar to how I declare regular items?

ModItem Code:

package net.gwee.mod;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.item.Item;
import net.minecraft.util.Icon;

public class ModItem extends Item {

private String iconPath;

@SideOnly(Side.CLIENT)
private Icon icon;

public ModItem(int par1, String par2String){
	super(par1);
	this.iconPath = par2String;
}

@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister){
	this.icon = par1IconRegister.registerIcon("mod/" + this.iconPath);
}

@SideOnly(Side.CLIENT)
public Icon getIconFromDamage(int par1){
	return this.icon;
}
}

 

If I want to make a new item with this method:

public static Item example = new ModItem(1337, "texture").setUnlocalizedName("example");

Posted

public String getItemDisplayName(ItemStack par1)

{

    return "YourItemName";

}

 

this function is vannila and make GameRegistry/Modloader addName totally useless!

 

Just put the Function i wrote up ther inside of your Item and you will see how good that works^^

Posted

LanguageRegistry.addName() asks for your item return of method getUnlocalizedName().

In your ModFood, it is null because you are overriding setUnlocalizedName to change your own internal String instead of the one in the Item class.

Either use super.setUnlocalizedName(string) in it, or don't override it (i highly recommend this, since you don't use your unlocalizedname string).

Posted

LanguageRegistry.addName() asks for your item return of method getUnlocalizedName().

In your ModFood, it is null because you are overriding setUnlocalizedName to change your own internal String instead of the one in the Item class.

Either use super.setUnlocalizedName(string) in it, or don't override it (i highly recommend this, since you don't use your unlocalizedname string).

 

I have very limited knowledge, but with a stroke of luck, a little bit of common sense, your method worked for me! I didn't have to change the way I made all my items, or anything. So I thank you for that. As for everyone else who helped, I really appreciate it, for this experience made me learn A LOT. Thanks~ ;D

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

    • Make a test with another Launcher like MultiMC or AT Launcher
    • Add crash-reports with sites like https://mclo.gs/ Looks like immersiverailroading and Optifine are conflicting - make a test without Optifine
    • And without create_more_automation?
    • Hi. I cant join my minecraft world on singleplayer neither multiplayer. After reaching %100, it crashes. Heres the crash report, would appreciate the help.  Description: Exception in server tick loop java.lang.NullPointerException: Cannot invoke "net.minecraft.server.level.ServerLevel.m_213780_()" because the return value of "net.minecraft.server.MinecraftServer.m_129783_()" is null     at party.lemons.biomemakeover.level.BMWorldEvents.lambda$init$0(BMWorldEvents.java:32) ~[biomemakeover-FORGE-1.20.1-1.11.4.jar%23568!/:?] {re:mixin,re:classloading}     at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732) ~[?:?] {re:mixin}     at dev.architectury.event.EventFactory.invokeMethod(EventFactory.java:53) ~[architectury-9.2.14-forge.jar%23547!/:?] {re:classloading}     at dev.architectury.event.EventFactory$1.handleInvocation(EventFactory.java:62) ~[architectury-9.2.14-forge.jar%23547!/:?] {re:classloading}     at com.google.common.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:87) ~[guava-31.1-jre.jar%23109!/:?] {}     at jdk.proxy4.$Proxy229.tick(Unknown Source) ~[?:?] {}     at dev.architectury.event.forge.EventHandlerImplCommon.event(EventHandlerImplCommon.java:75) ~[architectury-9.2.14-forge.jar%23547!/:?] {re:classloading,re:mixin}     at dev.architectury.event.forge.__EventHandlerImplCommon_event_ServerTickEvent.invoke(.dynamic) ~[architectury-9.2.14-forge.jar%23547!/:?] {re:classloading,pl:eventbus:B}     at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:73) ~[eventbus-6.0.5.jar%2387!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:315) ~[eventbus-6.0.5.jar%2387!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.5.jar%2387!/:?] {}     at net.minecraftforge.event.ForgeEventFactory.onPostServerTick(ForgeEventFactory.java:950) ~[forge-1.20.1-47.3.11-universal.jar%23887!/:?] {re:mixin,re:classloading,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:835) ~[client-1.20.1-20230612.114412-srg.jar%23882!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,pl:mixin:APP:modernfix-common.mixins.json:core.MinecraftServerMixin,pl:mixin:APP:modernfix-common.mixins.json:perf.dedicated_reload_executor.MinecraftServerMixin,pl:mixin:APP:mixins.essential.json:feature.sps.Mixin_IntegratedServerResourcePack,pl:mixin:APP:mixins.essential.json:server.MinecraftServerMixin_PvPGameRule,pl:mixin:APP:mixins.essential.json:server.Mixin_ServerCoroutineScope,pl:mixin:APP:mixins.essential.json:server.Mixin_PublishServerStatusResponse,pl:mixin:APP:deltaboxlib.mixins.json:event.MinecraftServerMixin,pl:mixin:APP:ichunutil.mixins.json:MinecraftServerAccessorMixin,pl:mixin:APP:saturn.mixins.json:allocations.server_directory.MinecraftServerMixin,pl:mixin:APP:balm.mixins.json:MinecraftServerMixin,pl:mixin:APP:majruszlibrary-common.mixins.json:MixinMinecraftServer,pl:mixin:APP:neruina.mixins.json:MinecraftServerMixin,pl:mixin:APP:notenoughcrashes.mixins.json:client.MixinMinecraftServerClientOnly,pl:mixin:APP:citadel.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:inject_resources.MinecraftServerMixin,pl:mixin:A}     at net.minecraft.client.server.IntegratedServer.m_5705_(IntegratedServer.java:89) ~[client-1.20.1-20230612.114412-srg.jar%23882!/:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:mixins.essential.json:server.Mixin_ServerCoroutineScope_IntegratedServer,pl:mixin:APP:mixins.essential.json:server.integrated.Mixin_FixDefaultOpPermissionLevel,pl:mixin:APP:mixins.essential.json:server.integrated.Mixin_IntegratedServerManager,pl:mixin:APP:mixins.essential.json:server.integrated.MixinIntegratedServer,pl:mixin:APP:smoothboot.mixins.json:client.IntegratedServerMixin,pl:mixin:APP:lithostitched.mixins.json:client.IntegratedServerMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:661) ~[client-1.20.1-20230612.114412-srg.jar%23882!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,pl:mixin:APP:modernfix-common.mixins.json:core.MinecraftServerMixin,pl:mixin:APP:modernfix-common.mixins.json:perf.dedicated_reload_executor.MinecraftServerMixin,pl:mixin:APP:mixins.essential.json:feature.sps.Mixin_IntegratedServerResourcePack,pl:mixin:APP:mixins.essential.json:server.MinecraftServerMixin_PvPGameRule,pl:mixin:APP:mixins.essential.json:server.Mixin_ServerCoroutineScope,pl:mixin:APP:mixins.essential.json:server.Mixin_PublishServerStatusResponse,pl:mixin:APP:deltaboxlib.mixins.json:event.MinecraftServerMixin,pl:mixin:APP:ichunutil.mixins.json:MinecraftServerAccessorMixin,pl:mixin:APP:saturn.mixins.json:allocations.server_directory.MinecraftServerMixin,pl:mixin:APP:balm.mixins.json:MinecraftServerMixin,pl:mixin:APP:majruszlibrary-common.mixins.json:MixinMinecraftServer,pl:mixin:APP:neruina.mixins.json:MinecraftServerMixin,pl:mixin:APP:notenoughcrashes.mixins.json:client.MixinMinecraftServerClientOnly,pl:mixin:APP:citadel.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:inject_resources.MinecraftServerMixin,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[client-1.20.1-20230612.114412-srg.jar%23882!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,xf:fml:xaeroworldmap:xaero_wm_minecraftserver,pl:mixin:APP:modernfix-common.mixins.json:core.MinecraftServerMixin,pl:mixin:APP:modernfix-common.mixins.json:perf.dedicated_reload_executor.MinecraftServerMixin,pl:mixin:APP:mixins.essential.json:feature.sps.Mixin_IntegratedServerResourcePack,pl:mixin:APP:mixins.essential.json:server.MinecraftServerMixin_PvPGameRule,pl:mixin:APP:mixins.essential.json:server.Mixin_ServerCoroutineScope,pl:mixin:APP:mixins.essential.json:server.Mixin_PublishServerStatusResponse,pl:mixin:APP:deltaboxlib.mixins.json:event.MinecraftServerMixin,pl:mixin:APP:ichunutil.mixins.json:MinecraftServerAccessorMixin,pl:mixin:APP:saturn.mixins.json:allocations.server_directory.MinecraftServerMixin,pl:mixin:APP:balm.mixins.json:MinecraftServerMixin,pl:mixin:APP:majruszlibrary-common.mixins.json:MixinMinecraftServer,pl:mixin:APP:neruina.mixins.json:MinecraftServerMixin,pl:mixin:APP:notenoughcrashes.mixins.json:client.MixinMinecraftServerClientOnly,pl:mixin:APP:citadel.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:MinecraftServerMixin,pl:mixin:APP:kubejs-common.mixins.json:inject_resources.MinecraftServerMixin,pl:mixin:A}     at java.lang.Thread.run(Thread.java:833) ~[?:?] {re:mixin}    
  • Topics

×
×
  • Create New...

Important Information

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