Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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!

Edited by renren

  • renren changed the title to [1.16.5] How can I change the REACH_DISTANCE attribute?
  • Author

I'm incredibly sorry that I couldn't respond in the last couple of days :(

I think I know what you meant with your reply, I definitely know the difference between an Item and an ItemStack now, so thanks for that. I'm not quite sure how to store that information inside of an ItemStack though. Like, how should I store and change these values using ItemStacks?

A big problem I encountered too now, is that the REACH_DISTANCE modifier is a "RegistryObject<Attribute>" instead of an "Attribute", which made it impossible to return while overriding the "getAttributeModifiers" method. Is there any way I can convert a registry object to an attribute or do anything so that the method is able to accept that data type? Because I've already tried casting it and it didn't work.

Also, Merry Christmas if you celebrate!

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.