From cb98c3761bddb22c4de9ae0f92c1ffd7630fbea5 Mon Sep 17 00:00:00 2001 From: akastijn Date: Sat, 1 Nov 2025 00:56:19 +0100 Subject: [PATCH] Update PlayerJoin to handle attribute keys and improve attribute modification logic --- .../com/alttd/playerutils/PlayerUtils.java | 2 +- .../event_listeners/PlayerJoin.java | 46 +++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/alttd/playerutils/PlayerUtils.java b/src/main/java/com/alttd/playerutils/PlayerUtils.java index 7f596f2..573fbd3 100644 --- a/src/main/java/com/alttd/playerutils/PlayerUtils.java +++ b/src/main/java/com/alttd/playerutils/PlayerUtils.java @@ -41,7 +41,7 @@ public final class PlayerUtils extends JavaPlugin { pluginManager.registerEvents(new GoatHornEvent(), this); pluginManager.registerEvents(new LimitArmorStands(this), this); pluginManager.registerEvents(new BlockBlockUseEvent(), this); - pluginManager.registerEvents(new PlayerJoin(), this); + pluginManager.registerEvents(new PlayerJoin(this), this); RotateBlockEvent rotateBlockEvent = new RotateBlockEvent(); pluginManager.registerEvents(rotateBlockEvent, this); diff --git a/src/main/java/com/alttd/playerutils/event_listeners/PlayerJoin.java b/src/main/java/com/alttd/playerutils/event_listeners/PlayerJoin.java index c88bbb7..a1bc11d 100644 --- a/src/main/java/com/alttd/playerutils/event_listeners/PlayerJoin.java +++ b/src/main/java/com/alttd/playerutils/event_listeners/PlayerJoin.java @@ -2,6 +2,7 @@ package com.alttd.playerutils.event_listeners; import com.alttd.playerutils.config.Config; import lombok.extern.slf4j.Slf4j; +import org.bukkit.NamespacedKey; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeModifier; @@ -9,18 +10,28 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.plugin.java.JavaPlugin; @Slf4j public class PlayerJoin implements Listener { + + private final NamespacedKey WAYPOINT_RECEIVE_KEY; + private final NamespacedKey WAYPOINT_TRANSMIT_KEY; + + public PlayerJoin(JavaPlugin plugin) { + this.WAYPOINT_RECEIVE_KEY = new NamespacedKey(plugin, "waypoint_receive_modifier"); + this.WAYPOINT_TRANSMIT_KEY = new NamespacedKey(plugin, "waypoint_transmit_modifier"); + } + @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - setModifiedAttribute(player, Attribute.WAYPOINT_RECEIVE_RANGE, Config.LOCATOR_BAR.WAYPOINT_RECEIVE_RANGE); - setModifiedAttribute(player, Attribute.WAYPOINT_TRANSMIT_RANGE, Config.LOCATOR_BAR.WAYPOINT_TRANSMIT_RANGE); + setModifiedAttribute(player, Attribute.WAYPOINT_RECEIVE_RANGE, Config.LOCATOR_BAR.WAYPOINT_RECEIVE_RANGE, WAYPOINT_RECEIVE_KEY); + setModifiedAttribute(player, Attribute.WAYPOINT_TRANSMIT_RANGE, Config.LOCATOR_BAR.WAYPOINT_TRANSMIT_RANGE, WAYPOINT_TRANSMIT_KEY); } - private void setModifiedAttribute(Player player, Attribute attribute, double configValue) { + private void setModifiedAttribute(Player player, Attribute attribute, double configValue, NamespacedKey key) { AttributeInstance attributeInstance = player.getAttribute(attribute); if (attributeInstance == null) { @@ -28,11 +39,28 @@ public class PlayerJoin implements Listener { return; } - double waypointReceiveDelta = attributeInstance.getValue() - configValue; - AttributeModifier attributeModifier = new AttributeModifier(attribute.getKey(), - waypointReceiveDelta, - AttributeModifier.Operation.ADD_NUMBER); - attributeInstance.addModifier(attributeModifier); - } + for (AttributeModifier modifier : attributeInstance.getModifiers()) { + attributeInstance.removeModifier(modifier); + } + + double attributeDelta = configValue - attributeInstance.getBaseValue(); + if (attributeDelta != 0) { + AttributeModifier attributeModifier = new AttributeModifier( + key, + attributeDelta, + AttributeModifier.Operation.ADD_NUMBER + ); + + attributeInstance.addModifier(attributeModifier); + } + + double actualValue = attributeInstance.getValue(); + if (Math.abs(actualValue - configValue) > 0.01) { + log.warn("Failed to set attribute {} to {} for {}, actual value is {}.", + attribute, configValue, player.getName(), actualValue); + } else { + log.info("Set attribute {} for {} to {}.", attribute, player.getName(), configValue); + } + } }