Ive had this problem for a while now, I wan to change the players reach distance whenever he right clicks on a custom item. I know now that I need to create an attribute modifier and somehow register it
Builder<RegistryObject<Attribute>, AttributeModifier> builder = ImmutableMultimap.builder();
builder.put(ForgeMod.REACH_DISTANCE, new AttributeModifier(REACH_MODIFIER, "Reach modifier",reach_distance_value, Operation.ADDITION));
this.attributeModifiers = builder.build();
the REACH_MODIFIER also has a unique UUID so its not the problem here
Heres the entire class of the custom item:
package com.ren.grapplehook.common.item;
import com.google.common.collect.ImmutableMultimap;
import com.ren.grapplehook.core.itemgroup.GrapplingHookItemGroup;
import net.minecraft.entity.ai.attributes.Attribute;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Rarity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeMod;
import com.google.common.collect.ImmutableMultimap.Builder;
import net.minecraft.entity.ai.attributes.AttributeModifier.Operation;
import net.minecraftforge.fml.RegistryObject;
import java.util.UUID;
public class GrapplingHook extends Item{
protected static final UUID REACH_MODIFIER = UUID.fromString("f33361d2-b398-4404-b4f1-67560fa578f2");
private ImmutableMultimap<RegistryObject<Attribute>, AttributeModifier> attributeModifiers;
private int reach_distance_value = 20;
public GrapplingHook(Properties group) {
super(new Item.Properties().maxStackSize(1).rarity(Rarity.RARE).group(GrapplingHookItemGroup.GRAPPLING_HOOK_ITEM_GROUP));
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
ItemStack stack = playerIn.getHeldItemMainhand();
RayTraceResult raytraceresult = rayTrace(worldIn, playerIn, RayTraceContext.FluidMode.ANY);
Builder<RegistryObject<Attribute>, AttributeModifier> builder = ImmutableMultimap.builder();
builder.put(ForgeMod.REACH_DISTANCE, new AttributeModifier(REACH_MODIFIER, "Reach modifier",reach_distance_value, Operation.ADDITION));
this.attributeModifiers = builder.build();
if(raytraceresult.getType() == RayTraceResult.Type.MISS) {
return ActionResult.resultPass(stack);
}
else if (raytraceresult.getType() != RayTraceResult.Type.BLOCK) {
return ActionResult.resultPass(stack);
}
else {
BlockRayTraceResult blockraytraceresult = (BlockRayTraceResult) raytraceresult;
BlockPos blockpos = blockraytraceresult.getPos();
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}
}
I guess its not registered yet and i somehow have to register it, but i dont know anymore, it seems the more i dwell into that topic, the less i know : (
Also just for clarification, what I want to do, is to increase the players reach distance, as he right clicks. Then raytracing scans the block hes looking at and after he gets the position, the players reach distance resets back to normal. If theres any other way to achieve this without modifying the reach attribute it would be great too. I really want to finish this project, if someone knows how to fix this pls tell me i would be so grateful!