Jump to content

[1.10.2, 1.11.2] Blocking only Melee Attacks with a custom Weapon?


ObliviousSpartan

Recommended Posts

Hi there,

This is my first time posting on these forums, so apologies in advance if I miss anything.

I am attempting to make a dagger weapon in a new mod I am making that will have the ability to block melee attacks, but not projectiles. In my attempts to do this, I've ran into a few issues. If I make use of the EnumAction.BLOCK in the method below, it will do the blocking animation I have specified, but it will block projectiles as well rather than using the custom behaviour I have put in a LivingAttackEvent method, which ignores projectiles.

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

As an alternative, I made a custom EnumAction known as MELEE_BLOCK using the method EnumHelper.addAction(), and by doing that, while it makes the blocking behaviour work properly, it doesn't make the weapon appear to visually block the attack, both in first and third person. In first person, the weapon disappears entirely, while in third person, the player just wields it as normal.

 

Here's the Item class, and the LivingAttackEvent method that is called for reference. The following code is based on my alternative custom EnumAction solution I have taken.

ItemParryingDagger:

Spoiler

public class ItemParryingDagger extends ItemWeaponBase implements IBlockingWeapon
{
	public static EnumAction MELEE_BLOCK = EnumHelper.addAction(Reference.ModID.toLowerCase() + ":melee_block");
	
	public ItemParryingDagger(String unlocName, ToolMaterial material, float weaponDamage) 
	{
		super(unlocName, material, weaponDamage, 2.5f, WeaponProperty.BLOCKING);
        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;
            }
        });
	}

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
	@Override
    public EnumAction getItemUseAction(ItemStack stack)
    {
        return MELEE_BLOCK;
    }

    /**
     * How long it takes to use or consume an item
     */
	@Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 72000;
    }

	@Override
    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
        playerIn.setActiveHand(hand);
        return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);
    }

	@Override
	public boolean canBlockProjectiles() 
	{
		return false;
	}

	@Override
	public boolean canBlockMelee() 
	{
		return true;
	}
}

 

Snippet of my mod's EventHandler class:

Spoiler

// Attack Event - Handles damage.
@SubscribeEvent
public void attackEvent(LivingAttackEvent ev)
{
	float damage = ev.getAmount();
		
	if(ev.getEntityLiving() instanceof EntityPlayer)
	{
		EntityPlayer player = (EntityPlayer)ev.getEntityLiving();
			
		if(player.getActiveItemStack() != Reference.ITEMSTACK_NULL)
		{
			ItemStack activeStack = player.getActiveItemStack();
				
			if(activeStack.getItem() instanceof IBlockingWeapon)
			{
				IBlockingWeapon weapon = (IBlockingWeapon)activeStack.getItem();
				DamageSource source = ev.getSource();
					
				if(weapon.canBlockMelee() && !source.isExplosion() && !source.isFireDamage() && !source.isMagicDamage() && !source.isProjectile() && !source.isUnblockable())
				{
					// Do knockback due to damage.
                    if (source.getSourceOfDamage() instanceof EntityLivingBase)
                    {
                        ((EntityLivingBase)source.getSourceOfDamage()).knockBack(player, 0.3F, player.posX - source.getSourceOfDamage().posX, player.posZ - source.getSourceOfDamage().posZ);
                    }
                    activeStack.damageItem(2, player);
                    player.worldObj.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_KNOCKBACK, SoundCategory.PLAYERS, 0.8f, 0.8f);
                    ev.setCanceled(true);
				}
				else if(weapon.canBlockProjectiles() && ev.getSource().isProjectile())
				{
					player.playSound(SoundEvents.ENTITY_PLAYER_ATTACK_KNOCKBACK, 0.8f, 0.8f);
					ev.setCanceled(true);
				}
			}
		}
	}
}

 

Is there any way to use the EnumAction.BLOCK action to make the weapon only block specific types of damage sources? Or if there is not, how would I go about adding the player animations for blocking for both first- and third-person to account for my custom EnumAction?

Any help would be greatly appreciated.

Link to comment
Share on other sites

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

    • If you are using AMD/ATI, get the latest drivers from their website - do not update via system
    • So I don't have any other mods and I try to install my first mod. At first I tried turning on Forge but it didn't work for me. I entered into these logs and this is what debug I found.   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0. 9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   -------------------------------------------------------------   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.428] [main/DEBUG] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Found launch services [fmlclientdev,forgeclient,minecraft,forgegametestserverdev,fmlserveruserdev,fmlclient,fmldatauserdev,forgeserverdev,forgeserveruserdev,forgeclientdev,forgeclientuserdev,forgeserver,forgedatadev,fmlserver,fmlclientuserdev,fmlserverdev,forgedatauserdev,testharness,forgegametestserveruserdev] [05maj2024 19:29:16.448] [main/DEBUG] [cpw.mods.modlauncher.NameMappingServiceHandler/MODLAUNCHER]: Found naming services : [srgtomcp] [05maj2024 19:29:16.468] [main/DEBUG] [cpw.mods.modlauncher.LaunchPluginHandler/MODLAUNCHER]: Found launch plugins: [mixin,eventbus,slf4jfixer,object_holder_definalize,runtime_enum_extender,capability_token_subclass,accesstransformer,runtimedistcleaner] [05maj2024 19:29:16.485] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Discovering transformation services [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path GAMEDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path MODSDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft\mods [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path CONFIGDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft\config [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path FMLCONFIG is C:\Users\Tadeusz\AppData\Roaming\.minecraft\config\fml.toml [05maj2024 19:29:16.550] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Found additional transformation services from discovery services:  [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   Can you help me with this?
    • So I don't have any other mods and I try to install my first mod. At first I tried turning on Forge but it didn't work for me. I entered into these logs and this is what debug I found.   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0. 9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   Can you help me with this?  
    • Thank you so, so much for your help! I have implemented the code from the example you gave me and it has been excepted, the only downside currently being that the game crashes on launch, saying:  Caused by: java.lang.IllegalStateException: Cannot register new entries to DeferredRegister after RegisterEvent has been fired.  (Three times) and  Caused by: java.lang.ExceptionInInitializerError (Two times) With further scanning I have found this error: 2024-05-05T18:41:24.287+0100 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] I am not exactly sure what to do with these errors but I am sure I can resolve them. Once again, thank you for your help! It is much appreciated. 😁
  • Topics

×
×
  • Create New...

Important Information

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