Jump to content

Recommended Posts

Posted

I want to make a custom attack reach attribute, however it doesn't register when I launch my game.

 

Yes, any events using it were register to the mod event bus. I'm using DeferredRegister. 

 

Do I need to work with capabilities or something?

Posted (edited)

Registry Class:

 

	public static final DeferredRegister<Attribute> ATTRIBUTES = DeferredRegister.create(ForgeRegistries.ATTRIBUTES, ReachMod.MODID);
	
	//ATTRIBUTES
	public static final RegistryObject<Attribute> ATTACK_REACH = ATTRIBUTES.register("attack_reach", () -> (Attribute) new RangedAttribute("attribute.mod.attack_reach", 5.0D, 0.0D, 1024.0D).setSyncable(true));
	
	//MODIFIERS
	public static final UUID ATTACH_REACH_MODIFIER = UUID.fromString("7cc9a781-fdde-4e1b-85fe-acb912fc0430");

 

Main Class:

 

//Register stuff
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();

ModAttributes.ATTRIBUTES.register(bus);

 

Extra stuff (events):

 

    @EventBusSubscriber(bus = Bus.MOD)
    public static class ClientHelper{ 
        @SuppressWarnings("unused")
    	private static RayTraceResult getEntityMouseOverExtended(float reach) {
            BlockRayTraceResult blockRayTraceResult = null;
            RayTraceResult result = null;
            Minecraft mc = Minecraft.getInstance();
            Entity viewEntity = mc.crosshairPickEntity;
            PlayerController p = mc.gameMode;
            if (viewEntity != null) {
              double d0 = reach;
              RayTraceResult rayTrace = viewEntity.pick(d0, 0.0F, false);
              Vector3d eyePos = viewEntity.getEyePosition(0.0F);
              boolean flag = false;
              double d1 = d0;
              if (p.hasFarPickRange() && d1 < 6.0D) {
                d1 = 6.0D;
                d0 = d1;
              } else if (d0 > reach) {
                flag = true;
              } 
              d1 *= d1;
              if (rayTrace != null)
                d1 = rayTrace.getLocation().distanceToSqr(eyePos); 
              Vector3d lookVec = viewEntity.getEyePosition(1.0F);
              Vector3d attackVec = eyePos.add(lookVec.x * d0, lookVec.y * d0, lookVec.z * d0);
              AxisAlignedBB expBounds = viewEntity.getBoundingBox().expandTowards(lookVec.scale(d0)).inflate(1.0D, 1.0D, 1.0D);
              EntityRayTraceResult entityRayTrace = ProjectileHelper.getEntityHitResult(viewEntity, eyePos, attackVec, expBounds, entity -> 
                  
                  (!entity.isSpectator() && entity.canBeCollidedWith()), d1);
              if (entityRayTrace != null) {
                Vector3d hitVec = entityRayTrace.getLocation();
                double d2 = eyePos.distanceToSqr(hitVec);
                if (flag && d2 > (reach * reach)) {
                  blockRayTraceResult = BlockRayTraceResult.miss(hitVec, Direction.getNearest(lookVec.x, lookVec.y, lookVec.z), new BlockPos(hitVec));
                } else if (d2 < d1 || blockRayTraceResult == null) {
                  EntityRayTraceResult entityRayTraceResult = entityRayTrace;
                } 
              } else {
                blockRayTraceResult = BlockRayTraceResult.miss(attackVec, Direction.getNearest(lookVec.x, lookVec.y, lookVec.z), new BlockPos(attackVec));
              } 
            } 
            return (RayTraceResult)blockRayTraceResult;
          }
        
        private static void checkForReachAttack() {
            Minecraft mc = Minecraft.getInstance();
            ClientPlayerEntity clientPlayerEntity = mc.player;
            if (clientPlayerEntity == null || clientPlayerEntity.isSpectator())
              return; 
            double reach = (float)clientPlayerEntity.getAttributeValue(ModAttributes.ATTACK_REACH.get());
            if (reach == ModAttributes.ATTACK_REACH.get().getDefaultValue())
              return; 
            RayTraceResult rayTrace = getEntityMouseOverExtended((float)reach);
            if (!(rayTrace instanceof EntityRayTraceResult))
              return; 
            EntityRayTraceResult entityRayTrace = (EntityRayTraceResult)rayTrace;
            Entity entityHit = entityRayTrace.getEntity();
            if (entityHit != null && entityHit.invulnerableTime == 0 && entityHit != clientPlayerEntity && entityHit != clientPlayerEntity.getControllingPassenger()) {
              float velocity = 0.0F;
              if (clientPlayerEntity.getControllingPassenger() != null) {
                Entity riding = clientPlayerEntity.getControllingPassenger();
                Vector3d vec = riding.getDeltaMovement();
                velocity = (float)vec.length();
              } 
              PacketHandler.sendPacketToServer(new PacketReach(velocity, entityHit.getId()));
            } 
          }

        @SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
        @SuppressWarnings("resource")
    	public static void mouseEvent(InputEvent.MouseInputEvent event) {
            if (Minecraft.getInstance().level == null || Minecraft.getInstance().screen == null || Minecraft.getInstance().isPaused())
                return; 
              KeyBinding keyAttack = Minecraft.getInstance().options.keyAttack;
              if (event.getButton() == keyAttack.getKey().getValue() && event.getAction() == 1 && ModAttributes.ATTACK_REACH.isPresent())
                checkForReachAttack(); 
        }
}

 

All I need is to register the events and my registries to the mod event bus, right?

Edited by Ninjaguy169
Posted
7 minutes ago, Ninjaguy169 said:

All I need is to register the events and my registries to the mod event bus, right?

InputEvent.MouseInputEvent is a Forge Bus Event, only the registry Events are Mod Bus Events (all Events which implements IModBusEvent)

Posted (edited)
Description: Unexpected error

java.lang.IllegalArgumentException: Can't find attribute reachmod:attack_reach
	at net.minecraft.entity.ai.attributes.AttributeModifierMap.getAttributeInstance(AttributeModifierMap.java:21) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.ai.attributes.AttributeModifierMap.getValue(AttributeModifierMap.java:28) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.ai.attributes.AttributeModifierManager.getValue(AttributeModifierManager.java:62) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.LivingEntity.getAttributeValue(LivingEntity.java:1731) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}
	at com.creator.reachmod.client.ClientEvent$ReachClientHelper.checkForReachAttack(ClientEvent.java:105) ~[main/:?] {re:classloading}
	at com.creator.reachmod.client.ClientEventEvent$ReachClientHelper.mouseEvent(ClientEvent.java:131) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_60_ReachClientHelper_mouseEvent_MouseInputEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.client.ForgeHooksClient.fireMouseInput(ForgeHooksClient.java:811) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.client.MouseHelper.onPress(MouseHelper.java:118) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.MouseHelper.lambda$null$4(MouseHelper.java:181) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.execute(ThreadTaskExecutor.java:94) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}
	at net.minecraft.client.MouseHelper.lambda$setup$5(MouseHelper.java:180) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at org.lwjgl.glfw.GLFWMouseButtonCallbackI.callback(GLFWMouseButtonCallbackI.java:36) ~[lwjgl-glfw-3.2.2.jar:build 10] {}
	at org.lwjgl.system.JNI.invokeV(Native Method) ~[lwjgl-3.2.2.jar:build 10] {}
	at org.lwjgl.glfw.GLFW.glfwPollEvents(GLFW.java:3101) ~[lwjgl-glfw-3.2.2.jar:build 10] {}
	at com.mojang.blaze3d.systems.RenderSystem.flipFrame(RenderSystem.java:102) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.MainWindow.updateDisplay(MainWindow.java:306) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:997) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:607) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
	at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:108) [forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {}


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Render thread
Stacktrace:
	at net.minecraft.entity.ai.attributes.AttributeModifierMap.getAttributeInstance(AttributeModifierMap.java:21) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.ai.attributes.AttributeModifierMap.getValue(AttributeModifierMap.java:28) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.ai.attributes.AttributeModifierManager.getValue(AttributeModifierManager.java:62) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.entity.LivingEntity.getAttributeValue(LivingEntity.java:1731) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}
	at com.creator.reachmod.client.ClientEvent$ReachClientHelper.checkForReachAttack(ClientEvent.java:105) ~[main/:?] {re:classloading}
	at com.creator.reachmod.client.ClientEvent$ReachClientHelper.mouseEvent(ClientEvent.java:131) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_60_ReachClientHelper_mouseEvent_MouseInputEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.client.ForgeHooksClient.fireMouseInput(ForgeHooksClient.java:811) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading}
	at net.minecraft.client.MouseHelper.onPress(MouseHelper.java:118) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.MouseHelper.lambda$null$4(MouseHelper.java:181) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.execute(ThreadTaskExecutor.java:94) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}
	at net.minecraft.client.MouseHelper.lambda$setup$5(MouseHelper.java:180) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at org.lwjgl.glfw.GLFWMouseButtonCallbackI.callback(GLFWMouseButtonCallbackI.java:36) ~[lwjgl-glfw-3.2.2.jar:build 10] {}
	at org.lwjgl.system.JNI.invokeV(Native Method) ~[lwjgl-3.2.2.jar:build 10] {}
	at org.lwjgl.glfw.GLFW.glfwPollEvents(GLFW.java:3101) ~[lwjgl-glfw-3.2.2.jar:build 10] {}
	at com.mojang.blaze3d.systems.RenderSystem.flipFrame(RenderSystem.java:102) ~[forge:?] {re:classloading,pl:runtimedistcleaner:A}
-- Affected level --
Details:
	All players: 1 total; [ClientPlayerEntity['Dev'/407, l='ClientLevel', x=107.50, y=84.00, z=-191.50]]
	Chunk stats: Client Chunk Cache: 841, 529
	Level dimension: minecraft:overworld
	Level spawn location: World: (112,78,-192), Chunk: (at 0,4,0 in 7,-12; contains blocks 112,0,-192 to 127,255,-177), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)
	Level time: 179 game time, 179 day time
	Server brand: forge
	Server type: Integrated singleplayer server
Stacktrace:
	at net.minecraft.client.world.ClientWorld.fillReportDetails(ClientWorld.java:447) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A,re:mixin,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.fillReport(Minecraft.java:2031) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:628) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
	at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:108) [forge-1.16.5-36.2.33_mapped_official_1.16.5.jar:?] {}

 

Edited by Ninjaguy169
Posted (edited)

EntityAttributeModificationEvent:

 

    @SubscribeEvent
    public static void onEntityAttributeModificationEvent(final EntityAttributeModificationEvent event) {
		event.add(EntityType.PLAYER, ForgeMod.REACH_DISTANCE.get());
	}

 

Test Weapon Item:

 

    protected static final UUID ATTACK_KNOCKBACK_MODIFIER = UUID.fromString("C59EC38E-DC43-11EB-BA80-0242AC130004");
    public static final UUID REACH_MODIFIER = UUID.fromString("5C05J864-4748-24CE-90D6-0242AC170006");
    private final Multimap<Attribute, AttributeModifier> attributeModifiers;
    public AnimationFactory factory = new AnimationFactory(this);

    public TestItem(IItemTier t, int dmg, float speed, double reachDistance, double knockBack, Properties prop) {
        super(t, dmg, speed, prop);
        float attackDamage = (float) dmg + t.getAttackDamageBonus();
        ImmutableMultimap.Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
        builder.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", dmg, AttributeModifier.Operation.ADDITION));
        builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", speed, AttributeModifier.Operation.ADDITION));

        ForgeMod.getInstance();
        if (ForgeMod.REACH_DISTANCE.isPresent()) {
        	builder.put(ForgeMod.REACH_DISTANCE.get(), new AttributeModifier(REACH_MODIFIER, "Weapon modifier", reachDistance, AttributeModifier.Operation.ADDITION));
        }
		
        builder.put(Attributes.ATTACK_KNOCKBACK, new AttributeModifier(ATTACK_KNOCKBACK_MODIFIER, "Weapon modifier", knockBack, AttributeModifier.Operation.ADDITION));
        this.attributeModifiers = builder.build();
    }
      
    @Override
    public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlotType equipmentSlot) {
        return equipmentSlot == EquipmentSlotType.MAINHAND ? this.attributeModifiers : super.getDefaultAttributeModifiers(equipmentSlot);
    }

 

Registry Item:

 

    public static final RegistryObject<TestItem> TEST_ITEM = ITEMS.register("test_item", () -> new TestItem(ModItemTiers.TEST, 100, -3.2F, 15F, 0, new Item.Properties().rarity(Rarity.EPIC).tab(ModItemGroups.EQUIPMENT)));

 

Pretty sure I need to do something else, although I just can't seem to lay my finger on it.

Edited by Ninjaguy169
Posted

Yeah, I figured. So I got it to work (finally), but is there a specific event I can use to see if the player is holding a certain item and change the attribute's value based on that?

Posted (edited)

I haven't really worked with lazily initialized stuff before, but so far I've created:

 

 public static final Lazy<Multimap<Attribute, AttributeModifier>> LAZY = Lazy.of(() -> modifierMap);

 

Forgive me for acting dumb, but may I get a bit more clarity on what to do?

 

Edit: So for the method "getAttributeModifiers(EquipmentSlotType slot, ItemStack stack)", I'd just return 

return slot == EquipmentSlotType.MAINHAND ? LAZY.get() : super.getAttributeModifiers(slot, stack);

 

is that right?

Edited by Ninjaguy169
Posted (edited)

Ah alr, so a lambda?

    public final Lazy<Multimap<Attribute, AttributeModifier>> LAZY = Lazy.of(() ->  {    
      //Create a new MultiMap using ArrayListMultiMap.create(), probably not make it null
    	Multimap<Attribute, AttributeModifier> attributeModifiers = null;
    	return attributeModifiers;
    });

 

Then I would go about, modifying it inside the "getAttributeValue" method, or is there something else I need to do?

 

Edited by Ninjaguy169
Posted (edited)
20 hours ago, diesieben07 said:

create your map inside the supplier

you need to add all values to the Multimap inside the supplier

Edited by Luis_ST
Posted (edited)

So,

    public final Lazy<Multimap<Attribute, AttributeModifier>> LAZY = Lazy.of(() ->  {    
    	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", speed, AttributeModifier.Operation.ADDITION));
         
         if (ForgeMod.REACH_DISTANCE.isPresent()) {
             builder.put(ForgeMod.REACH_DISTANCE.get(), new AttributeModifier(ATTACK_REACH_MODIFIER, "Weapon modifier", reach, AttributeModifier.Operation.ADDITION));
         }
         builder.put(Attributes.ATTACK_KNOCKBACK, new AttributeModifier(ATTACK_KNOCKBACK_MODIFIER, "Weapon modifier", kb, AttributeModifier.Operation.ADDITION));
    	Multimap<Attribute, AttributeModifier> attributeModifiers = ArrayListMultimap.create();
    	attributeModifiers = builder.build();
    	return attributeModifiers;
    });

 

Then I set the new value in the EntityAttributeModificationEvent to whatever static int I have in the item's class?

 

EDIT: I finally got it to work after adding all the values inside the map as you told me to. Thanks a bunch.

Edited by Ninjaguy169
Posted (edited)
50 minutes ago, Ninjaguy169 said:
    	Multimap<Attribute, AttributeModifier> attributeModifiers = ArrayListMultimap.create();
    	attributeModifiers = builder.build();

this is completely unnecessary, why did you not return ImmutableMultimap.Builder#build

Edited by Luis_ST
  • 1 month later...
Posted
12 hours ago, Sokoly_ said:

Hello, I also want to increase the reach attack of some tools.

then read the posts before there is a solution

 

12 hours ago, Sokoly_ said:

I would like you to help me especially with the PacketHandler and put it in the tools

what did you mean by "PacketHandler and put it in the tools"?

  • 2 months later...
Posted (edited)

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.

Edited by Ninjaguy169
  • Thanks 1

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

    • My apologies, it's a Postscript. I was able to play for an extended period of time right after generating the world without any problems. If I close it and reopen it, I get that message at random. Once it appears, I have to modify the Level.dat to open it.
    • Share logs/errors, and someone may know how to help. Make sure to read the FAQ on sharing logs.
    • Hello.  regarding Invalid player data.  First of all, my English is not good, so there might be mistakes. I am using Minecraft 1.20.1 forge 47.3.22 CurseForge and have over 250 mods in my modpack. Single-player. I can play the game after generating the world (about 5 hours) and restarting it, but there is no set timing and one day it suddenly shows ''Invalid player data''. Restarted the game several times after that, but the same message appears. Fix the level.dat file and play for a few hours, but the next day when I try to open the world I get the ''Invalid player data'' message again. Can open other worlds, but after some progress in the other worlds, the same message appears and I can't start them. Is there something wrong with the mod configuration? I would be very grateful if you could tell me how to solve this problem. ◉ErrorCode https://mclo.gs/4gcfPbY   ◉ModList AdvancementPlaques-forge-1.6.9.jar AI-Improvements-1.20-0.5.2.jar alexsmobs-1.22.9.jar alternate_current-mc1.20-1.7.0.jar AmbientSounds_FORGE_v6.1.6_mc1.20.1.jar amendments-1.20-1.2.18.jar Apotheosis-1.20.1-7.4.6.jar ApothicAttributes-1.20.1-1.3.7.jar appleskin-forge-mc1.20.1-2.5.1.jar aquamirae-6.API15.jar architectury-9.2.14-forge.jar ars_elemental-1.20.1-0.6.7.7.jar ars_extended_glyphs-1.20.1-1.9.jar ars_nouveau-1.20.1-4.12.6-all.jar AttributeFix-Forge-1.20.1-21.0.4.jar azurelib-neo-1.20.1-2.0.41.jar BadOptimizations-2.2.1-1.20.1.jar balm-forge-1.20.1-7.3.16-all.jar barbequesdelight-1.0.5.jar BattleArts-20.9.7.1.jar BattleArtsAPI-20.9.5.3.jar BEB-Forge-1.20.1-2.0.0.jar bendy-lib-forge-4.0.0.jar betterendcities-1.0.0-1.20.1.jar betterfpsdist-1.20.1-6.0.jar BetterThirdPerson-Forge-1.20-1.9.0.jar bettervillage-forge-1.20.1-3.2.0.jar biggerendcities-1.20.1-1.0.0.jar blockui-1.20.1-1.0.156-RELEASE.jar blueprint-1.20.1-7.1.1.jar blur-forge-3.1.1.jar BOMD-Forge-1.20.1-1.1.1.jar Bookshelf-Forge-1.20.1-20.2.13.jar BrewinAndChewin-1.20.1-3.1.2.jar BridgingMod-2.5.1+1.20.1.forge-release.jar caelus-forge-3.2.0+1.20.1.jar CarbonConfig-1.20-1.2.6.jar Cardiac-FORGE-0.5.3.2+1.20.1.jar carryon-forge-1.20.1-2.1.2.7.jar casualness_delight-1.20.1-0.4n.jar CerbonsApi-Forge-1.20.1-1.0.0.jar chat_heads-0.13.13-forge-1.20.jar cherishedworlds-forge-6.1.7+1.20.1.jar ChoiceTheorem's Overhauled Village-3.4.11.jar Chunk-Pregenerator-1.20-4.4.4.jar citadel-2.6.1-1.20.1.jar clean_tooltips-1.0-forge-1.20.1.jar cloth-config-11.1.136-forge.jar Clumps-forge-1.20.1-12.0.0.4.jar cobweb-forge-1.20.1-1.0.1.jar CocoaInput-1.20.5-fabric-4.4.1-EXPERIMENTAL.jar collective-1.20.1-7.91.jar cosmeticarmorreworked-1.20.1-v1a.jar create-1.20.1-0.5.1.j.jar create_easy_structures-0.1.2-forge-1.20.1.jar CreativeCore_FORGE_v2.12.31_mc1.20.1.jar creeperoverhaul-3.0.2-forge.jar cristellib-1.1.6-forge.jar cuisinedelight-1.1.16.jar cupboard-1.20.1-2.7.jar curios-forge-5.11.1+1.20.1.jar CutAllSMP_v2.5.2.jar default_skill_trees-1.1.jar DisenchantmentEditTable-1.20-1.1.2.jar DistantHorizons-2.2.1-a-1.20.1-forge-fabric.jar domesticationinnovation-1.7.1-1.20.1.jar domum_ornamentum-1.20.1-1.0.186-RELEASE-universal.jar dragonitegear-0.3.2.jar Dungeon Crawl-1.20.1-2.3.15.jar dungeons-and-taverns-ancient-city-overhaul-1 [Forge].jar DungeonsArise-1.20.x-2.1.58-release.jar dungeons_enhanced-1.20.1-5.3.0.jar dungeons_plus-1.20.1-1.5.0.jar ec_isasb_plugin-1.20.1-1.0.0-all.jar efiscompat-2.2.4.jar EFMCompat 20.2.0.1.jar embeddium-0.3.31+mc1.20.1.jar EnchantmentDescriptions-Forge-1.20.1-17.1.19.jar endermanoverhaul-forge-1.20.1-1.0.4.jar EnderWyrmlings-1.0.0-forge-1.20.1.jar endrem_forge-5.3.3-R-1.20.1.jar enhanced_boss_bars-1.20.1-1.0.0.jar entityculling-forge-1.7.2-mc1.20.1.jar entity_model_features_forge_1.20.1-2.4.1.jar entity_texture_features_forge_1.20.1-6.2.9.jar Epic-Knights-9.21.jar Epic-Knights-Addon-1.22.jar Epic-Knights-Slavic-Armory-1.5.jar epicfight-forge-20.9.7-1.20.1.jar essential_1-3-5-7_forge_1-20-1.jar ExCap-20.9.7.3.jar exoticbirds-1.20.1-1.0.0.jar expanded_combat-1.20.1-3.2.4-all.jar Explorify v1.6.2 f10-48.jar extrasounds-1.20.1-forge-1.3.jar falchionmoveset-20.8.2.jar Fallingleaves-1.20.1-2.1.0.jar FarmersDelight-1.20.1-1.2.7.jar farsight-1.20.1-3.7.jar FastFurnace-1.20.1-8.0.2.jar FastSuite-1.20.1-5.0.1.jar FastWorkbench-1.20.1-8.0.4.jar ferritecore-6.0.1-forge.jar forge-medievalend-1.0.1.jar framework-forge-1.20.1-0.7.12.jar frozen_zombie_castle-1.4.0-forge-1.20.1.jar fzzy_config-0.6.4+1.20.1+forge.jar geckolib-forge-1.20.1-4.7.jar globalxp-forge-1.20.1-1.12.jar goblintraders-forge-1.20.1-1.9.3.jar gravestone-forge-1.20.1-1.0.24.jar guardvillagers-1.20.1-1.6.10.jar harvest-with-ease-forge-1.20.1-9.4.0.jar Highlighter-1.20.1-forge-1.1.9.jar hole_filler_mod-1.2.8_mc-1.20.1_forge.jar Iceberg-1.20.1-forge-1.1.25.jar ImmediatelyFast-Forge-1.3.4+1.20.4.jar ImmersiveUI-FORGE-0.3.0.jar imst-2.1.0.jar infernalmobs-1.20.1.6.jar integrated_api-1.5.1+1.20.1-forge.jar integrated_villages-1.1.5+1.20.1-forge.jar inventoryhud.forge.1.20.1-3.4.26.jar InventoryProfilesNext-forge-1.20-1.10.14.jar inventorysorter-1.20.1-23.0.8.jar InventorySpam-1.20.1-1.5.6.jar ironchest-1.20.1-14.4.4.jar irons_spellbooks-1.20.1-3.4.0.7.jar iron_repair_kits-2.4.3-forge-1.20.1.jar ItemBorders-1.20.1-forge-1.2.2.jar ItemProductionLib-1.20.1-1.0.2a-all.jar Jade-1.20.1-Forge-11.12.3.jar jei-1.20.1-forge-15.20.0.106.jar journeymap-1.20.1-5.10.3-forge.jar justhammers-forge-2.0.3+mc1.20.1.jar Kobolds-2.12.0.jar kotlinforforge-4.11.0-all.jar LegendaryTooltips-1.20.1-forge-1.4.5.jar libIPN-forge-1.20-4.0.2.jar libraryferret-forge-1.20.1-4.0.0.jar lionfishapi-2.4-Fix.jar lithostitched-forge-1.20.1-1.4.4.jar lmft-1.0.4+1.20.1-forge.jar lootbeams-1.20.1-1.2.6.jar lootintegrations-1.20.1-4.0.jar lukis-grand-capitals-1.1.1.jar L_Enders_Cataclysm-2.54- 1.20.1.jar mes-1.3.4-1.20-forge.jar mexicans_delight-1.1.1-forge-1.20.1.jar MineAllSMP_v2.6.6.jar minecolonies-1.20.1-1.1.814-snapshot.jar mna-forge-1.20.1-3.1.0.4-all.jar modernfix-forge-5.20.2+mc1.20.1.jar ModernUI-Forge-1.20.1-3.11.1.6-universal.jar modlist.txt moonlight-1.20-2.13.65-forge.jar mowziesmobs-1.7.0.jar multipiston-1.20-1.2.43-RELEASE.jar MutantMonsters-v8.0.7-1.20.1-Forge.jar mutil-1.20.1-6.1.1.jar mvs-4.1.4-1.20-forge.jar NaturesCompass-1.20.1-1.11.2-forge.jar Neat-1.20.1-41-FORGE.jar netherportalfix-forge-1.20-13.0.1.jar notenoughanimations-forge-1.9.2-mc1.20.1.jar Obscure-Tooltips-2.2.jar obscure_api-15.jar OctoLib-FORGE-0.4.2+1.20.1.jar oculus-mc1.20.1-1.8.0.jar packetfixer-forge-2.0.0-1.19-to-1.20.1.jar PackingTape-1.20.1-0.14.3.jar PassiveSkillTree-1.20.1-BETA-0.6.14a-all.jar Patchouli-1.20.1-84.1-FORGE.jar phantasm-1.0.1.jar Placebo-1.20.1-8.6.2.jar player-animation-lib-forge-1.0.2-rc1+1.20.jar polymorph-forge-0.49.8+1.20.1.jar Prism-1.20.1-forge-1.0.5.jar projectvibrantjourneys-1.20.1-6.0.5.jar puffish_attributes-0.7.2-1.20-forge.jar puffish_skills-0.14.7-1.20-forge.jar PuzzlesLib-v8.1.25-1.20.1-Forge.jar QualityCrops-1.20.1-1.3.3.jar QualitysDelight-1.20.1-1.5.3.jar Quark-4.0-460.jar QUILT-2.0.0.jar repair_amulet-2.0-forge-1.20.1.jar repurposed_structures-7.1.15+1.20.1-forge.jar resourcefulconfig-forge-1.20.1-2.1.2.jar resourcefullib-forge-1.20.1-2.1.29.jar RPG-HUD-3.10.jar rpg_companions_tiny_dragons-0.0.4-forge-1.20.1.jar run.bat samurai_dynasty-0.0.48-1.20.1-neo.jar simplyswords-forge-1.56.0-1.20.1.jar SkyVillages-1.0.4-1.19.2-1.20.1-forge-release.jar smoothboot(reloaded)-mc1.20.1-0.0.4.jar sophisticatedbackpacks-1.20.1-3.23.5.1200.jar sophisticatedcore-1.20.1-1.2.12.872.jar sound-physics-remastered-forge-1.20.1-1.4.8.jar Stackable Potions-forge-1.20.1-1.0.0.jar StorageBox_v3.2.5.jar StorageDrawers-1.20.1-12.9.13.jar Structory_1.20.x_v1.3.5.jar Structory_Towers_1.20.x_v1.0.7.jar structure_gel-1.20.1-2.16.2.jar structurize-1.20.1-1.0.764-snapshot.jar SubtleEffects-forge-1.20.1-1.8.0.jar supermartijn642configlib-1.1.8-forge-mc1.20.jar supermartijn642corelib-1.1.18-forge-mc1.20.1.jar supplementaries-1.20-3.1.13.jar TaxCastlePillager+M.1.20.1+ForM.1.0.1.jar TaxTreeGiant+M.1.20.1+ForM.1.1.0.jar TerraBlender-forge-1.20.1-3.0.1.7.jar Terralith_1.20.x_v2.5.4.jar tetra-1.20.1-6.8.0.jar TheOuterEnd-1.0.10.jar tidal-towns-1.3.4.jar tlc_forge-1.0.3-R-1.20.X.jar toms_storage-1.20-1.7.0.jar toomanyglyphs-1.20.1-2.3.2.12345.jar totw_additions-1.3.1-1.20.x-forge.jar totw_modded-forge-1.20.1-1.0.5.jar Towns-and-Towers-1.12-Fabric+Forge.jar towntalk-1.20.1-1.1.0.jar trashcans-1.0.18b-forge-mc1.20.jar trashslot-forge-1.20-15.1.1.jar travelersbackpack-forge-1.20.1-9.1.16.jar TravelersTitles-1.20-Forge-4.0.2.jar tru.e-ending-v1.1.0c.jar uncrafter-forge-1.20.1-1.2.0.jar valarian_conquest-3.0-forge-1.20.1.jar valhelsia_core-forge-1.20.1-1.1.2.jar valhelsia_structures-forge-1.20.1-1.1.2.jar villagernames-1.20.1-8.2.jar visuality-forge-2.0.2.jar waystones-forge-1.20.1-14.1.9.jar WeaponsOfMiracles-20.1.8.5.6.jar XP From Harvest Reworked-1.20.x-1.2.4.jar YetAnotherConfigLib-3.6.2+1.20.1-forge.jar YungsApi-1.20-Forge-4.0.6.jar YungsBetterDesertTemples-1.20-Forge-3.0.3.jar YungsBetterDungeons-1.20-Forge-4.0.4.jar YungsBetterEndIsland-1.20-Forge-2.0.6.jar YungsBetterJungleTemples-1.20-Forge-2.0.5.jar YungsBetterMineshafts-1.20-Forge-4.0.4.jar YungsBetterNetherFortresses-1.20-Forge-2.0.6.jar YungsBetterOceanMonuments-1.20-Forge-3.0.4.jar YungsBetterWitchHuts-1.20-Forge-3.0.3.jar YungsBridges-1.20-Forge-4.0.3.jar YungsCaveBiomes-1.20.1-Forge-2.0.1.jar YungsExtras-1.20-Forge-4.0.3.jar Zeta-1.0-24.jar  
    • I did exactly like in the instruction , i even copied the build.gradle from alex mobs like he told in the instruction for citadel 1.7.0 and above, and i got 100 different error no matter what i changed in the build.gradle, i once managed to make a build succesfull but then the run client wasnt working I did exactly like in the instruction , i even copied the build.gradle from alex mobs like he told in the instruction for citadel 1.7.0 and above, and i got 100 different error no matter what i changed in the build.gradle, i once managed to make a build succesfull but then the run client wasnt working
    • Please share a link to your crash report on https://paste.ee, as explained in the FAQ
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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