[Tutorial] How to make a shield.

Started by mega1134227 on Sun, 07/14/2019 - 20:49

Topic category: User side tutorials

Last seen on 02:45, 16. Aug 2019
Joined Feb 2016
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
[Tutorial] How to make a shield.
Sun, 07/14/2019 - 20:49

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

Finally, 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);
        }

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.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.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());
    }

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

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

 

Last seen on 02:45, 16. Aug 2019
Joined Feb 2016
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
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...