[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:43, 14. Feb 2024
Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
As a warning, there's a…
Mon, 07/15/2019 - 13:43

As a warning, there's a single problem with the code and that is that it doesn't change the position of the shield when it blocks.

I don't know either how to do that but I have a feeling it's somewhere in the code and in the shields model...

Last seen on 13:38, 4. Oct 2020
Joined Apr 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how to make shield take dmg…
Tue, 09/03/2019 - 12:25

how to make shield take dmg when blocking?

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It should automatically... I…
Sat, 09/07/2019 - 02:27

It should automatically... I will look into this to see how it could be fixed.

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey Mega, That code for the…
Fri, 02/07/2020 - 23:28

Hey Mega,

That code for the shield was a life-saver! It really worked. Thanks! Just a couple questions?

First of all, I'd like to add the ability for the shield to take durability damage when it blocks damage... Any suggestions for how to do that? Like remove durability equal to damage blocked when it detects that it blocked damage... Would that work?

And one more question. Items don't seem to have the direct ability to have durability. Is there a way to apply your code for blocking, to a tool?

Any help or advice would be appreciated!

Thanks!

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@CodingSuperNoob_JDV I've…
Sun, 02/23/2020 - 00:10

@CodingSuperNoob_JDV
I've been working on that part, but it seems the problem is located deeper in the files than I thought. It's been stated in the code that if something is blocking AND that it's a shield specificaly, then it takes damage. I think there's a way to fix this, but I haven't had the opportunity to do it yet.

And yeah, I'm pretty sure you can apply it to a tool, unless there's some weird incompatibility thing between tools and the shields code. I'll have to test on it.

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks for looking into this…
Wed, 02/26/2020 - 15:43

Thanks for looking into this! I did eventually figure out a way to get my shields to take durability damage when they are hit, so if you're curious at all I can post my code and stuff? I'm still figuring out how java and MCreator work, so I'm sure my solution is a bit dinky, but it works well for me

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
That would be great :D
Thu, 02/27/2020 - 22:53

That would be great :D

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Okay! These are the…
Sat, 02/29/2020 - 00:31

Okay! These are the additional imports that you'll need:

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 this code needs to be added into the main class, close to the beginning:

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

And then this code needs to be added into the main class as well, right above the ItemCustom class. It must be in the main class, and not in the ItemCustom class, otherwise it wont work.

@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);
            }
        }
    }

This extra code added on to yours, should make the shields take durability damage when hit! Hopefully I didn't forget anything. Let me know if it works please!

The code is set to make the shield take damage equal to the amount of damage given out by the attacker. You can change it make the shield take a set amount of damage, if you wanted.

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thx! Never thought of doing…
Sun, 03/01/2020 - 14:57

Thx! Never thought of doing it this way.

I'll add it in the main tutorial comment as well if you don't mind so that other people won't have the same problem.

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how about the "blocking"…
Mon, 03/02/2020 - 02:02

how about the "blocking" animation

and does it work with tools also

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I have to do some extra…
Mon, 03/02/2020 - 05:18

I have to do some extra research for that, but I think it has something to do with the model since the shield model kinda has two different forms? It basically has this vanilla form and this rotated by .56 something degree other form.

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how about code for blockable…
Mon, 03/02/2020 - 06:04

how about code for blockable tools? Is that possible?
about the models, it has 2 models, shield and shield blocking I think

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank you all, this was…
Mon, 03/02/2020 - 12:07

Thank you all, this was really helpful!

For the "changing model" part, maybe it is found somewhere in the "use" part, but you can also take a look on the old 1.8 sword party code, the same used by that one Zelda Mod to move the shield on the front of the player

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@crispy_chips1234 As far as…
Mon, 03/02/2020 - 14:11

@crispy_chips1234
As far as I am concerned, I don't see why it wouldn't work with tools since this is just a property you add on top that doesn't change what the custom item is.
@NatePlays95
I might look at the mod, but if the method is unoptimized, I'll just try to find the vanilla method for changing the shield angle.