Jump to content

[1.19.2] what happends to the onEntitySwing() method ?


perromercenary00

Recommended Posts

good nights 
i made a custome lance item and need to make it has a longer reach soo it can hurt zombies at 6 or 7 meters  the SwordItem only has 4 maybe 5 meters reach 

 

Spoiler
package merctool.item;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Vanishable;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;

public class LanceItem extends SwordItem implements Vanishable {
   private final float attackDamage;
   private final Multimap<Attribute, AttributeModifier> defaultModifiers;

   public LanceItem(Tier p_43269_, int p_43270_, float p_43271_, Item.Properties p_43272_) {
      super(p_43269_, p_43270_, p_43271_, p_43272_);
      this.attackDamage = (float)p_43270_ + p_43269_.getAttackDamageBonus();
      ImmutableMultimap.Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
      builder.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", (double)this.attackDamage, AttributeModifier.Operation.ADDITION));
      builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", (double)p_43271_, AttributeModifier.Operation.ADDITION));
      this.defaultModifiers = builder.build();
   }

   public float getDamage() {
      return this.attackDamage;
   }

   public boolean canAttackBlock(BlockState p_43291_, Level p_43292_, BlockPos p_43293_, Player p_43294_) {
	   System.out.println("canAttackBlock");
	   
	   
      return !p_43294_.isCreative();
   }

   public float getDestroySpeed(ItemStack p_43288_, BlockState p_43289_) {
      if (p_43289_.is(Blocks.COBWEB)) {
         return 15.0F;
      } else {
         Material material = p_43289_.getMaterial();
         return material != Material.PLANT && material != Material.REPLACEABLE_PLANT && !p_43289_.is(BlockTags.LEAVES) && material != Material.VEGETABLE ? 1.0F : 1.5F;
      }
   }

   public boolean hurtEnemy(ItemStack p_43278_, LivingEntity p_43279_, LivingEntity p_43280_) {
	   System.out.println("hurtEnemy");
	   
      p_43278_.hurtAndBreak(1, p_43280_, (p_43296_) -> {
         p_43296_.broadcastBreakEvent(EquipmentSlot.MAINHAND);
      });
      return true;
   }

   public boolean mineBlock(ItemStack p_43282_, Level p_43283_, BlockState p_43284_, BlockPos p_43285_, LivingEntity p_43286_) {
	   System.out.println("mineBlock");
	   
      if (p_43284_.getDestroySpeed(p_43283_, p_43285_) != 0.0F) {
         p_43282_.hurtAndBreak(2, p_43286_, (p_43276_) -> {
            p_43276_.broadcastBreakEvent(EquipmentSlot.MAINHAND);
         });
      }

      return true;
   }

   public boolean isCorrectToolForDrops(BlockState p_43298_) {
      return p_43298_.is(Blocks.COBWEB);
   }

   public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlot p_43274_) {
      return p_43274_ == EquipmentSlot.MAINHAND ? this.defaultModifiers : super.getDefaultAttributeModifiers(p_43274_);
   }

   @Override
   public boolean canPerformAction(ItemStack stack, net.minecraftforge.common.ToolAction toolAction) {
	   System.out.println("canPerformAction");
      return net.minecraftforge.common.ToolActions.DEFAULT_SWORD_ACTIONS.contains(toolAction);
   }
   
   @Override
   public InteractionResult useOn(UseOnContext p_41427_) {
	   System.out.println("useOn");
	      return InteractionResult.PASS;
	   }
   
   @Override
   public InteractionResultHolder<ItemStack> use(Level p_41432_, Player p_41433_, InteractionHand p_41434_) {
	   System.out.println("use");
	      ItemStack itemstack = p_41433_.getItemInHand(p_41434_);
	      if (itemstack.isEdible()) {
	         if (p_41433_.canEat(itemstack.getFoodProperties(p_41433_).canAlwaysEat())) {
	            p_41433_.startUsingItem(p_41434_);
	            return InteractionResultHolder.consume(itemstack);
	         } else {
	            return InteractionResultHolder.fail(itemstack);
	         }
	      } else {
	         return InteractionResultHolder.pass(p_41433_.getItemInHand(p_41434_));
	      }
	   }
   
   
   @Override
   public net.minecraft.world.InteractionResult interactLivingEntity(ItemStack stack, net.minecraft.world.entity.player.Player playerIn, LivingEntity entity, net.minecraft.world.InteractionHand hand) {
	   System.out.println("interactLivingEntity");

	   return net.minecraft.world.InteractionResult.SUCCESS;
   }
	   
}

 

 

lokking into mi old code the function name was 

    public boolean onEntitySwing(EntityLivingBase shootingEntity, ItemStack pistola) 

 

soo what function is trigered on left click ?

 


 

 

 

 

Link to comment
Share on other sites

Maybe this?

https://github.com/MinecraftForge/MinecraftForge/blob/d176bab28e265263908b0bb509a23734d0d53516/src/main/java/net/minecraftforge/common/ForgeMod.java#L158

i.e. use LivingEquipmentChangeEvent to add/remove AttributeModifiers to the player when it is your lance.

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

thanks for asking 
i was looking to the forge class code and see the attributes  and seems complicated to get them change them and apply to player also the original value needs to be stored somehow soo it can be restored when player deselect the lance 

 

event its the easy part 

    @SubscribeEvent
    public static void on_using_lance(LivingEquipmentChangeEvent event) {

        ItemStack itemstack = event.getTo();
        Entity entity = event.getEntity();
        Level warudo = entity.level;
        if (entity instanceof Player) {

            // if (itemstack.getItem() == ItemInit.LANCE_STEEL.get() &&
            // !warudo.isClientSide) {
            if (itemstack.getItem() instanceof LanceItem && !warudo.isClientSide) {
                System.out.println("\nEs una lanza\n");
                 //IForgePlayer.getReachDistance();
            }

            else {

                     //set back the original values 

            }

        }

    }

 

 

i need help whi the forge class part 

but also really really theres no replace to detect when players leftclick whit an item ?

 

Link to comment
Share on other sites

You can see how to apply attribute modifiers in for example Horse.setArmourEquipment() it's not complicated at all.

It's also the correct way to do it.

 

The swing method still exists:

https://github.com/MinecraftForge/MinecraftForge/blob/d176bab28e265263908b0bb509a23734d0d53516/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java#L394

That's a policy hook to control animations, not for ad hoc processing.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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



×
×
  • Create New...

Important Information

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