Update PlayerJoin to handle attribute keys and improve attribute modification logic

This commit is contained in:
akastijn 2025-11-01 00:56:19 +01:00
parent df347fde7f
commit cb98c3761b
2 changed files with 38 additions and 10 deletions

View File

@ -41,7 +41,7 @@ public final class PlayerUtils extends JavaPlugin {
pluginManager.registerEvents(new GoatHornEvent(), this); pluginManager.registerEvents(new GoatHornEvent(), this);
pluginManager.registerEvents(new LimitArmorStands(this), this); pluginManager.registerEvents(new LimitArmorStands(this), this);
pluginManager.registerEvents(new BlockBlockUseEvent(), this); pluginManager.registerEvents(new BlockBlockUseEvent(), this);
pluginManager.registerEvents(new PlayerJoin(), this); pluginManager.registerEvents(new PlayerJoin(this), this);
RotateBlockEvent rotateBlockEvent = new RotateBlockEvent(); RotateBlockEvent rotateBlockEvent = new RotateBlockEvent();
pluginManager.registerEvents(rotateBlockEvent, this); pluginManager.registerEvents(rotateBlockEvent, this);

View File

@ -2,6 +2,7 @@ package com.alttd.playerutils.event_listeners;
import com.alttd.playerutils.config.Config; import com.alttd.playerutils.config.Config;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier; import org.bukkit.attribute.AttributeModifier;
@ -9,18 +10,28 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
@Slf4j @Slf4j
public class PlayerJoin implements Listener { 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 @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
setModifiedAttribute(player, Attribute.WAYPOINT_RECEIVE_RANGE, Config.LOCATOR_BAR.WAYPOINT_RECEIVE_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); 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); AttributeInstance attributeInstance = player.getAttribute(attribute);
if (attributeInstance == null) { if (attributeInstance == null) {
@ -28,11 +39,28 @@ public class PlayerJoin implements Listener {
return; return;
} }
double waypointReceiveDelta = attributeInstance.getValue() - configValue; for (AttributeModifier modifier : attributeInstance.getModifiers()) {
AttributeModifier attributeModifier = new AttributeModifier(attribute.getKey(), attributeInstance.removeModifier(modifier);
waypointReceiveDelta, }
AttributeModifier.Operation.ADD_NUMBER);
attributeInstance.addModifier(attributeModifier); 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);
}
}
} }