Update PlayerJoin to handle attribute keys and improve attribute modification logic
This commit is contained in:
parent
df347fde7f
commit
cb98c3761b
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user