Add PlayerJoin event listener to modify player attributes on join and update project configuration

This commit is contained in:
2025-10-31 01:45:54 +01:00
parent ab162b5094
commit df347fde7f
5 changed files with 66 additions and 4 deletions
@@ -41,6 +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);
RotateBlockEvent rotateBlockEvent = new RotateBlockEvent();
pluginManager.registerEvents(rotateBlockEvent, this);
@@ -79,4 +79,17 @@ import java.util.Set;
}
}
}
public static class LOCATOR_BAR {
private static final String prefix = "locator_bar.";
public static double WAYPOINT_RECEIVE_RANGE = 200;
public static double WAYPOINT_TRANSMIT_RANGE = 200;
@SuppressWarnings("unused")
private static void load() {
WAYPOINT_RECEIVE_RANGE = config.getDouble(prefix, "waypoint_receive_range", WAYPOINT_RECEIVE_RANGE);
WAYPOINT_TRANSMIT_RANGE = config.getDouble(prefix, "waypoint_transmit_range", WAYPOINT_TRANSMIT_RANGE);
}
}
}
@@ -0,0 +1,38 @@
package com.alttd.playerutils.event_listeners;
import com.alttd.playerutils.config.Config;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
@Slf4j
public class PlayerJoin implements Listener {
@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);
}
private void setModifiedAttribute(Player player, Attribute attribute, double configValue) {
AttributeInstance attributeInstance = player.getAttribute(attribute);
if (attributeInstance == null) {
log.error("Unable to retrieve attribute instance for player {}.", player.getName());
return;
}
double waypointReceiveDelta = attributeInstance.getValue() - configValue;
AttributeModifier attributeModifier = new AttributeModifier(attribute.getKey(),
waypointReceiveDelta,
AttributeModifier.Operation.ADD_NUMBER);
attributeInstance.addModifier(attributeModifier);
}
}