tweaks724/src/main/java/eu/m724/tweaks/module/durability/DPlayerProperties.java
Minecon724 232e0bfa9a
All checks were successful
/ build (push) Successful in 44s
Durability toggle
2025-01-28 10:18:15 +01:00

59 lines
1.8 KiB
Java

/*
* Copyright (C) 2025 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.module.durability;
import eu.m724.tweaks.TweaksPlugin;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.persistence.PersistentDataType;
import java.util.HashSet;
import java.util.Set;
public class DPlayerProperties implements Listener {
private final NamespacedKey namespacedKey = new NamespacedKey(TweaksPlugin.getInstance(), "durability_enabled");
private final Set<Player> players = new HashSet<>();
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
var player = event.getPlayer();
if (player.hasPermission("tweaks724.durabilityalert")) {
var enabled = player.getPersistentDataContainer().get(namespacedKey, PersistentDataType.BOOLEAN);
if (enabled != null && enabled) {
players.add(player);
}
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
players.remove(event.getPlayer());
}
Set<Player> getPlayers() {
return Set.copyOf(players);
}
boolean isPlayerEnabled(Player player) {
return players.contains(player);
}
void disableForPlayer(Player player) {
players.remove(player);
player.getPersistentDataContainer().set(namespacedKey, PersistentDataType.BOOLEAN, false);
}
void enableForPlayer(Player player) {
players.add(player);
player.getPersistentDataContainer().set(namespacedKey, PersistentDataType.BOOLEAN, true);
}
}