Woah, this is pretty old and it hasn't been closed, so I may as well shove my solution in here.
You instantiate a new Lazy, create a new
ImmutableMultimap.Builder<Attribute, AttributeModifier>
, shove your attributes alongside their modified values into that map, and return that map inside the Lazy.
Afterwards (this is what I personally did), override the 'onEntitySwing' method in your item class. Get the player's eye position and check where they're looking. Get the distance (squared) between the attacker and the target. If the distance is close enough to where you can reach the target with your weapon/item, hurt the target.
I feel like my explanation isn't that clear, so to make up for my lack of better wording, I made an example item class for anyone reading to follow:
public class ExampleReachItem extends SwordItem {
public static final UUID REACH_MOD = UUID.fromString("dccd59ec-6391-436d-9e00-47f2e6005e20"); //A randomly generated version 4 UUID
public static final UUID KNOCKBACK_MOD = UUID.fromString("15bf6a06-b73b-4d8e-946a-61f63e1ba01e"); //Another randomly generated version 4 UUID
public static double reach;
public static double knockBack;
public static int damage;
public static int attackSpd;
public static Lazy<? extends Multimap<Attribute, AttributeModifier>> ATTRIBUTE_LAZY_MAP = Lazy.of(() -> {
Multimap<Attribute, AttributeModifier> map;
ImmutableMultimap.Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
builder.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", damage, AttributeModifier.Operation.ADDITION));
builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", attackSpd, AttributeModifier.Operation.ADDITION));
//Checking if the Forge 'Reach' Attribute is present
if (ForgeMod.REACH_DISTANCE.isPresent()) {
builder.put(ForgeMod.REACH_DISTANCE.get(), new AttributeModifier(REACH_MOD, "Weapon modifier", reach, AttributeModifier.Operation.ADDITION));
}
builder.put(Attributes.ATTACK_KNOCKBACK, new AttributeModifier(KNOCKBACK_MOD, "Weapon modifier", knockBack, AttributeModifier.Operation.ADDITION));
map = builder.build();
return map;
});
//Now you can modify the reach value and other attribute values per entry
public ExampleReachItem(IItemTier tier, int dmg, float atkspd, double reach, double kb, Properties properties) {
super(tier, dmg, atkspd, properties);
this.damage = (int) ((float)dmg + tier.getAttackDamageBonus());
this.attackSpd = (int) atkspd;
this.reach = reach;
this.knockBack = kb;
}
@Override
public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlotType slot, ItemStack stack) {
return slot == EquipmentSlotType.MAINHAND ? ATTRIBUTE_LAZY_MAP.get() : super.getAttributeModifiers(slot, stack);
}
//This also works on blocks btw
@Override
public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
double reach = entity.getAttributeValue(ForgeMod.REACH_DISTANCE.get());
double reachSqr = reach * reach;
World world = entity.level;
Vector3d viewVec = entity.getViewVector(1.0F);
Vector3d eyeVec = entity.getEyePosition(1.0F);
Vector3d targetVec = eyeVec.add(viewVec.x * reach, viewVec.y * reach, viewVec.z * reach);
//Expanding the attacker's bounding box by the view vector's scale, and inflating it by 4.0D (x, y, z)
AxisAlignedBB viewBB = entity.getBoundingBox().expandTowards(viewVec.scale(reach)).inflate(4.0D, 4.0D, 4.0D);
EntityRayTraceResult result = ProjectileHelper.getEntityHitResult(world, entity, eyeVec, targetVec, viewBB, EntityPredicates.NO_CREATIVE_OR_SPECTATOR);
if (result == null || !(result.getEntity() instanceof LivingEntity)) return false;
LivingEntity target = (LivingEntity) result.getEntity();
double distanceToTargetSqr = entity.distanceToSqr(target);
boolean hitResult = (result != null ? target : null) != null;
if (hitResult) {
if (entity instanceof PlayerEntity) {
if (reachSqr >= distanceToTargetSqr) {
target.hurt(DamageSource.playerAttack((PlayerEntity) entity), attackDamage);
//Do stuff
}
}
}
return super.onEntitySwing(stack, entity);
}
}
I'm bad at explaining stuff, so I apologize if I'm at any point unclear.
I hope this helps anybody having the same issue, or a similar issue of some sort.
Edit: The variables above the constructor don't have to be static, but they can be static. Me making them static was just force of habit XD. It was also a fault on my end for calling them in a non-static context inside my constructor.