[Tutorial] [1.12.2] How to make a shield.

Started by mega1134227 on

Topic category: User side tutorials

Last seen on 20:43, 14. Feb 2024
Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] [1.12.2] How to make a shield.
Thu, 04/09/2020 - 00:31 (edited)

Hi there, again... If you think your mod needs shields and you want to add them in but don't know how, then this is for you :D I'll be showing you how to add in shields via code in your mod in any version of MCreator :) (or so I believe)

First of all, you'll need to make an item. Lock the items code and then procede to edit it. You'll need to add in an import named import javax.annotation.Nullable; in the very top next to the other imports since the reformat code and imports function doesn't work with it. After that, you'll want to add in the shields property to the item in a section that looks like this :

public ItemCustom() {
            setMaxDamage(234);
            maxStackSize = 1;
            setUnlocalizedName("yourshield");
            setRegistryName("yourshield");
            setCreativeTab(MCreatorYourtab.tab);

}

The property looks like this :

this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {

                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });

Your final bit of code should look like this :

public ItemCustom() {
            setMaxDamage(234);
            maxStackSize = 1;
            setUnlocalizedName("yourshield");
            setRegistryName("yourshield");
            setCreativeTab(MCreatorYourtab.tab);
            this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {

                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });
        }

But your shield won't work with that simple and small bit of code only. You will also need to add in an argument under it that allows it to actually block stuff. It looks like this :

public EnumAction getItemUseAction(ItemStack stack) {
            return EnumAction.BLOCK;
        }

Then, you'll need to change the item duration to 72000, otherwise your shield won't be able to hold long enough to push back the arrow. Like so :

@Override
        public int getMaxItemUseDuration(ItemStack par1ItemStack) {
            return 72000;
        }

After that, you will also have to add in another bit of code that returns the success of your action. It looks like this :

public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            ItemStack itemstack = playerIn.getHeldItem(handIn);
            playerIn.setActiveHand(handIn);
            return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
        }

Finaly, you only have to add two little bits of code close to the beginning to make it so that the shield actually looses durability. I'd like to thank CodingSuperNoob_JDV for providing me with it.

First, you have to add in these 7 little imports right at the beginning, around line 5 :
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;

Then, you will have to add this FMLPreInitialization register around line 40.

@Override
    public void preInit(FMLPreInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
    }
 

Finaly, the last bit of code you'll have to add in looks like this. Put it right before the ItemCustom bit that should be around line 55 now. This is the code that makes it so that your shield takes damage.
 

@SubscribeEvent
    public void onEntityAttacked(LivingAttackEvent event){
        Entity entity = event != null ? event.getEntity() : null;
        float damage = event.getAmount();
        int damageInt = (int) damage;
        if (entity != null && entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer)entity;
            ItemStack activeItem = player.getActiveItemStack();

            if (player.isActiveItemStackBlocking() == true && ((activeItem).getItem() == new ItemStack(MCreatorYourShield.block, (int) (1)).getItem())) {
                
                activeItem.damageItem(damageInt, player);
            }
        }
    }

Do you see that "int damageint = (int) damage;" bit? The damageint part basically is the amount of damage your shield will take. In this case, it's basically equal to the amount of damage you've protected yourself from. If you say "int damageint = 1;", then your shield will only lose one durability everytime it gets hit and if you say "int damage int = (int) ((damage)*4);" then your shield will take 4 times the damage it protected you from. For a zombie, that would be 5 damage * 4, so 20 durability lost.

That's all! Remember to use the reformat code and imports function and use the right names instead of my yourshield and yourtab names.

Your final code should look somewhat like this :

package net.mcreator.yourmod;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraft.world.World;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.ActionResult;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.EnumAction;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.block.state.IBlockState;

import javax.annotation.Nullable;

public class MCreatoryourshield extends yourmod.ModElement {

    @GameRegistry.ObjectHolder("yourmod:yourshield")
    public static final Item block = null;

    public MCreatoryourshield(yourmod instance) {
        super(instance);
        instance.items.add(() -> new ItemCustom());
    }
@Override
    public void preInit(FMLPreInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
    }
 

    @SideOnly(Side.CLIENT)
    @Override
    public void registerModels(ModelRegistryEvent event) {
        ModelLoader.setCustomModelResourceLocation(block, 0, new ModelResourceLocation("yourmod:yourshield", "inventory"));
    }

@SubscribeEvent
    public void onEntityAttacked(LivingAttackEvent event){
        Entity entity = event != null ? event.getEntity() : null;
        float damage = event.getAmount();
        int damageInt = (int) damage;
        if (entity != null && entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer)entity;
            ItemStack activeItem = player.getActiveItemStack();

            if (player.isActiveItemStackBlocking() == true && ((activeItem).getItem() == new ItemStack(MCreatorYourShield.block, (int) (1)).getItem())) {
                
                activeItem.damageItem(damageInt, player);
            }
        }
    }

    public static class ItemCustom extends Item {

        public ItemCustom() {
            setMaxDamage(234);
            maxStackSize = 1;
            setUnlocalizedName("yourshield");
            setRegistryName("yourshield");
            setCreativeTab(MCreatorYourtab.tab);
            this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {

                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });
        }

        public EnumAction getItemUseAction(ItemStack stack) {
            return EnumAction.BLOCK;
        }

        @Override
        public int getItemEnchantability() {
            return 2;
        }

        @Override
        public int getMaxItemUseDuration(ItemStack par1ItemStack) {
            return 72000;
        }

        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            ItemStack itemstack = playerIn.getHeldItem(handIn);
            playerIn.setActiveHand(handIn);
            return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
        }

        @Override
        public float getDestroySpeed(ItemStack par1ItemStack, IBlockState par2Block) {
            return 1F;
        }
    }
}

Here's a workspace file for those of you it would help. I would like to thank NatePlays95 for providing us with this workspace.
https://mcreator.net/forum/58742/template-worspace-112-shield-bow-and-more

Edited by mega1134227 on Thu, 04/09/2020 - 00:31
Last seen on 20:38, 30. Dec 2022
Joined Jul 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Actually, the game uses two…
Mon, 03/02/2020 - 23:53

Actually, the game uses two models: one for the shield and another for when it's blocking

this is the default shield.json

{
    "parent": "builtin/entity",
    "textures": {
        "particle": "block/dark_oak_planks"
    },
    "display": {
        "thirdperson_righthand": {
            "rotation": [ 0, 90, 0 ],
            "translation": [ 10, 6, -4 ],
            "scale": [ 1, 1, 1 ]
        },
        "thirdperson_lefthand": {
            "rotation": [ 0, 90, 0 ],
            "translation": [ 10, 6, 12 ],
            "scale": [ 1, 1, 1 ]
        },
        "firstperson_righthand": {
            "rotation": [ 0, 180, 5 ],
            "translation": [ -10, 2, -10 ],
            "scale": [ 1.25, 1.25, 1.25 ]
        },
        "firstperson_lefthand": {
            "rotation": [ 0, 180, 5 ],
            "translation": [ 10, 0, -10 ],
            "scale": [ 1.25, 1.25, 1.25 ]
        },
        "gui": {
            "rotation": [ 15, -25, -5 ],
            "translation": [ 2, 3, 0 ],
            "scale": [ 0.65, 0.65, 0.65 ]
        },
        "fixed": {
            "rotation": [ 0, 180, 0 ],
            "translation": [ -2, 4, -5],
            "scale":[ 0.5, 0.5, 0.5]
        },
        "ground": {
            "rotation": [ 0, 0, 0 ],
            "translation": [ 4, 4, 2],
            "scale":[ 0.25, 0.25, 0.25]
        }
    },
    "overrides": [
        {
            "predicate": {
                "blocking": 1
            },
            "model": "item/shield_blocking"
        }
    ]
}

and here is the default shield_blocking.json

{
    "parent": "builtin/entity",
    "textures": {
        "particle": "block/dark_oak_planks"
    },
    "display": {
        "thirdperson_righthand": {
            "rotation": [ 45, 135, 0 ],
            "translation": [ 3.51, 11, -2 ],
            "scale": [ 1, 1, 1 ]
        },
        "thirdperson_lefthand": {
            "rotation": [ 45, 135, 0 ],
            "translation": [ 13.51, 3, 5 ],
            "scale": [ 1, 1, 1 ]
        },
        "firstperson_righthand": {
            "rotation": [ 0, 180, -5 ],
            "translation": [ -15, 5, -11 ],
            "scale": [ 1.25, 1.25, 1.25 ]
        },
        "firstperson_lefthand": {
            "rotation": [ 0, 180, -5 ],
            "translation": [ 5, 5, -11 ],
            "scale": [ 1.25, 1.25, 1.25 ]
        },
        "gui": {
            "rotation": [ 15, -25, -5 ],
            "translation": [ 2, 3, 0 ],
            "scale": [ 0.65, 0.65, 0.65 ]
        }
    }
}

 

Last seen on 04:26, 22. Oct 2021
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@NatePlays95 Exactly. With…
Tue, 03/03/2020 - 00:03

@NatePlays95

Exactly. With the code that mega1134227 provided, its actually quite easy to set up a shield blocking model. His code creates a property override called "blocking" with two returns, 1.0 or 0.0, that can be accessed the same way the vanilla shield json does it. The shield's json will look for property overrides, and the shield's code will return 1.0 if its blocking. So you can set the shield's json to use another json model if the the property override "blocking" returns "1.0".

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
can you give us a example?
Tue, 03/03/2020 - 00:36

can you give us a example?

Last seen on 04:26, 22. Oct 2021
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sure! If you use mega1134227…
Tue, 03/03/2020 - 01:30

Sure! If you use mega1134227's code in your shield item, you'll have a piece of code that looks like this:

this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {
                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });

This creates a property override called "blocking", which returns 1 if the item is blocking.

The way I actually use this is a bit 'hacky' I guess, you'll need to find your "MCreatorWorkspaces" folder on your PC, and directly modify the generated json file for your shield item, and add a new one. You should be able to access your that folder from your user folder inside the "Users" folder on your main disk. (Just searching your computer for the folder "McreatorWorkspaces" should bring it up). Inside there, open the folder for your mod workspace, and then go into src\main\resources\assets\immersive_shields\models\item. Inside there, you'll find the json files for all your mod items and blocks. Open the json file of your shield, and you'll need to add this 'code':

,
  "overrides": [
    { "predicate": { "blocking": 1 }, "model": "yourMod:item/yourShield" }
  ]

So that the json file looks like this:

{
  "parent": "item/generated",
  "textures": {
    "layer0": "yourMod:items/yourShield"
  },
  "overrides": [
    { "predicate": { "blocking": 1 }, "model": "yourMod:item/newJsonFileName" }
  ]
}

Then you'll need to create a new json file in this same folder, and give it newJsonFileName (if you get my meaning).

Now, when you run your workspace in MCreator to test it, and you use (right-click) your shield item, the main json file will detect that your shield item is blocking, and will proceed to use the second (blocking) json file that you told it to while the shield is blocking.

You'll need to put the necessary positioning data in the blocking json, like the vanilla shield blocking json that NatePlays95 showed in order to get the shield to actually change position nicely, or get the blocking json to use a custom model. (I use custom models that I make with BlockBench. Its a very handy tool)

Disclaimer: I am by no means a pro at this, just trying to get things to work how I'd like 'em to :D

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
so you actually don't have…
Tue, 03/03/2020 - 01:32

so you actually don't have to edit the code, only the .json files

thx so much 

Last seen on 04:26, 22. Oct 2021
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Exactly. Sorry for the long…
Tue, 03/03/2020 - 01:36

Exactly. Sorry for the long explanation :D

The code already has the property override, you just need to get the shield's json file to use it!

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks! It was not actually…
Tue, 03/03/2020 - 01:39

Thanks! It was not actually that long.

(Maybe because I understand 5% of java syntax)

I'll try it!

 

Last seen on 04:26, 22. Oct 2021
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
No worries. Let me know if…
Tue, 03/03/2020 - 01:57

No worries. Let me know if it works out!

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
the code does not give build…
Tue, 03/03/2020 - 03:30

the code does not give build errors but the test item I made didd not show up in game.

I tried to /give myself the item but it also does not work...


package net.mcreator.alloy_the_fusion;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraft.world.World;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.ActionResult;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.EnumAction;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.block.state.IBlockState;

import javax.annotation.Nullable;

public class MCreatorTest extends Elementsalloy_the_fusion.ModElement {

    @GameRegistry.ObjectHolder("alloy_the_fusion:test")
    public static final Item block = null;

    public MCreatorTest(Elementsalloy_the_fusion instance) {
        super(instance, 55);
        instance.items.add(() -> new ItemCustom());
    }
@Override
    public void preInit(FMLPreInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
    } 

    @SideOnly(Side.CLIENT)
    @Override
    public void registerModels(ModelRegistryEvent event) {
        ModelLoader.setCustomModelResourceLocation(block, 0, new ModelResourceLocation("alloy_the_fusion:test", "inventory"));
    }

@SubscribeEvent
    public void onEntityAttacked(LivingAttackEvent event){
        Entity entity = event != null ? event.getEntity() : null;
        float damage = event.getAmount();
        int damageInt = (int) damage;
        if (entity != null && entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer)entity;
            ItemStack activeItem = player.getActiveItemStack();

            if (player.isActiveItemStackBlocking() == true && ((activeItem).getItem() == new ItemStack(MCreatorTest.block, (int) (1)).getItem())) {
                
                activeItem.damageItem(damageInt, player);
            }
        }
    }

    public static class ItemCustom extends Item {

        public ItemCustom() {
            setMaxDamage(234);
            maxStackSize = 1;
            setUnlocalizedName("test");
            setRegistryName("test");
            setCreativeTab(MCreatorAlloyTab.tab);
            this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {

                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });
        }

        public EnumAction getItemUseAction(ItemStack stack) {
            return EnumAction.BLOCK;
        }

        @Override
        public int getItemEnchantability() {
            return 2;
        }

        @Override
        public int getMaxItemUseDuration(ItemStack par1ItemStack) {
            return 72000;
        }

        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            ItemStack itemstack = playerIn.getHeldItem(handIn);
            playerIn.setActiveHand(handIn);
            return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
        }

        @Override
        public float getDestroySpeed(ItemStack par1ItemStack, IBlockState par2Block) {
            return 1F;
        }
    }
}

 

Last seen on 04:26, 22. Oct 2021
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Did the item work/show up…
Tue, 03/03/2020 - 23:17

Did the item work/show up before you added the custom code? If there's no errors in code, it could be something wrong with your json files if you were working with them a well...

Make a test item, and make sure it works, and then start adding the the custom code, blocking code first, test it, then the durability damage code, then test it. Just to eliminate possible error sites.

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I did not do the .json code…
Wed, 03/04/2020 - 00:56

I did not do the .json code. I compiled but did not show up.

I'll try again later today

Last seen on 20:38, 30. Dec 2022
Joined Jul 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
so, at least for me, using…
Wed, 03/04/2020 - 23:43

so, at least for me, using the 2020.1, the item format is a little different. It looks like this:

package net.mcreator.monsterhunter;

import net.minecraftforge.registries.ObjectHolder;

import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.block.BlockState;

@Elementsmonsterhunter.ModElement.Tag
public class MCreatorShieldIron extends Elementsmonsterhunter.ModElement {
	@ObjectHolder("monsterhunter:shieldiron")
	public static final Item block = null;

	public MCreatorShieldIron(Elementsmonsterhunter instance) {
		super(instance, 34);
	}

	@Override
	public void initElements() {
		elements.items.add(() -> new ItemCustom());
	}

	public static class ItemCustom extends Item {
		public ItemCustom() {
			super(new Item.Properties().group(MCreatorTabSS.tab).maxDamage(512));
			setRegistryName("shieldiron");
		}

		@Override
		public int getItemEnchantability() {
			return 0;
		}

		@Override
		public int getUseDuration(ItemStack itemstack) {
			return 0;
		}

		@Override
		public float getDestroySpeed(ItemStack par1ItemStack, BlockState par2Block) {
			return 1F;
		}
	}
}

 

there is not many items below public ItemCustom(), instead is a "super" something, i dunno java. I also can't find where it sets the Unlocalized name here.

So, I don't know where to place the elements now...

Also, spoilers from my project

Last seen on 20:38, 30. Dec 2022
Joined Jul 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Things are not looking good…
Thu, 03/05/2020 - 00:14

Things are not looking good for me...

here's my attempt at the code (on MCreator 2020.1, minecraft 1.14.4)

package net.mcreator.monsterhunter;

import net.minecraftforge.registries.ObjectHolder;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.block.BlockState;

@Elementsmonsterhunter.ModElement.Tag
public class MCreatorShieldIron extends Elementsmonsterhunter.ModElement {
	@ObjectHolder("monsterhunter:shieldiron")
	public static final Item block = null;

	public MCreatorShieldIron(Elementsmonsterhunter instance) {
		super(instance, 34);
	}

	@Override
	public void initElements() {
		elements.items.add(() -> new ItemCustom());
	}
	@Override
    public void preInit(FMLPreInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
    }
    @SubscribeEvent
    public void onEntityAttacked(LivingAttackEvent event){
        Entity entity = event != null ? event.getEntity() : null;
        float damage = event.getAmount();
        int damageInt = (int) damage;
        if (entity != null && entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer)entity;
            ItemStack activeItem = player.getActiveItemStack();

            if (player.isActiveItemStackBlocking() == true && ((activeItem).getItem() == new ItemStack(MCreatorShieldIron.block, (int) (1)).getItem())) {
                
                activeItem.damageItem(damageInt, player);
            }
        }
    } 

	public static class ItemCustom extends Item {
		public ItemCustom() {
			super(new Item.Properties().group(MCreatorTabSS.tab).maxDamage(512));
			setRegistryName("shieldiron");
			this.addPropertyOverride(new ResourceLocation("blocking"), new IItemPropertyGetter() {

                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });
		}
		public EnumAction getItemUseAction(ItemStack stack) {
            return EnumAction.BLOCK;
        }
        @Override
        public int getMaxItemUseDuration(ItemStack par1ItemStack) {
            return 72000;
        }
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            ItemStack itemstack = playerIn.getHeldItem(handIn);
            playerIn.setActiveHand(handIn);
            return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
        }

		@Override
		public int getItemEnchantability() {
			return 0;
		}

		@Override
		public int getUseDuration(ItemStack itemstack) {
			return 0;
		}

		@Override
		public float getDestroySpeed(ItemStack par1ItemStack, BlockState par2Block) {
			return 1F;
		}
	}
}

and here's my gradle log

   
Executing gradle task: build
Build info: MCreator 2020.1.05419, 1.14.4, 64-bit, 8075 MB, Windows 10, JVM 1.8.0_232, JAVA_HOME: C:\Users\natan\Desktop\MCreator20201\jdk
> Configure project :
New Dep: net.minecraftforge:forge:1.14.4-28.1.117_mapped_snapshot_20190719-1.14.3
> Task :compileJava
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:5: error: package net.minecraftforge.fml.relauncher does not exist 
      
import net.minecraftforge.fml.relauncher.SideOnly;
                                        ^
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:6: error: package net.minecraftforge.fml.relauncher does not exist 
      
import net.minecraftforge.fml.relauncher.Side;
                                        ^
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:8: error: package net.minecraftforge.fml.common.event does not exist 
      
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
                                          ^
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:10: error: package net.minecraftforge.fml.common.eventhandler does not exist 
      
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
                                                 ^
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:14: error: cannot find symbol 
      
import net.minecraft.entity.EntityLivingBase;
                           ^
  symbol:   class EntityLivingBase
  location: package net.minecraft.entity
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:15: error: cannot find symbol 
      
import net.minecraft.entity.player.EntityPlayer;
                                  ^
  symbol:   class EntityPlayer
  location: package net.minecraft.entity.player
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:34: error: cannot find symbol 
      
    public void preInit(FMLPreInitializationEvent event) {
                        ^
  symbol:   class FMLPreInitializationEvent
  location: class MCreatorShieldIron
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:65: error: cannot find symbol 
      
      public EnumAction getItemUseAction(ItemStack stack) {
             ^
  symbol:   class EnumAction
  location: class ItemCustom
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:72: error: cannot find symbol 
      
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
                                                        ^
  symbol:   class World
  location: class ItemCustom
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:72: error: cannot find symbol 
      
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
                                                                       ^
  symbol:   class EntityPlayer
  location: class ItemCustom
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:72: error: cannot find symbol 
      
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
                                                                                              ^
  symbol:   class EnumHand
  location: class ItemCustom
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:72: error: cannot find symbol 
      
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
               ^
  symbol:   class ActionResult
  location: class ItemCustom
C:\Users\natan\MCreatorWorkspaces\monsterhunter-1.14\src\main\java\net\mcreator\monsterhunter\MCreatorShieldIron.java:37: error: cannot find symbol 
      
    @SubscribeEvent
     ^
  symbol:   class SubscribeEvent
  location: class MCreatorShieldIron
13 errors
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.9/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 8s
1 actionable task: 1 executed
BUILD FAILED
Task completed in 20613 milliseconds

Looks like MCreator isn't even recognising the imports...

Something's wrong, I can feel it

Last seen on 03:10, 19. Feb 2022
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
this code is for 1.12 I…
Thu, 03/05/2020 - 01:09

this code is for 1.12 I think, but I can't get it to work either