Durability toggle
All checks were successful
/ build (push) Successful in 44s

This commit is contained in:
Minecon724 2025-01-28 10:18:15 +01:00
parent 9345efe1d4
commit 232e0bfa9a
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
4 changed files with 111 additions and 4 deletions

View file

@ -0,0 +1,59 @@
/*
* 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);
}
}

View file

@ -0,0 +1,41 @@
/*
* 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.Language;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class DurabilityCommands implements CommandExecutor {
private final DPlayerProperties properties;
public DurabilityCommands(DPlayerProperties properties) {
this.properties = properties;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use this command");
return true;
}
if (properties.isPlayerEnabled(player)) {
properties.disableForPlayer(player);
sender.spigot().sendMessage(Language.getComponent("durabilityDisabled", ChatColor.GRAY));
} else {
properties.enableForPlayer(player);
sender.spigot().sendMessage(Language.getComponent("durabilityEnabled", ChatColor.GRAY));
}
return true;
}
}

View file

@ -11,7 +11,6 @@ import eu.m724.tweaks.module.TweaksModule;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -26,15 +25,18 @@ import java.awt.Color;
public class DurabilityModule extends TweaksModule implements Listener { public class DurabilityModule extends TweaksModule implements Listener {
private final DurabilityCaches cache = new DurabilityCaches(); private final DurabilityCaches cache = new DurabilityCaches();
private final DPlayerProperties properties = new DPlayerProperties();
@Override @Override
protected void onInit() { protected void onInit() {
registerEvents(this); registerEvents(this);
registerCommand("durabilityalert", new DurabilityCommands(properties));
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
Bukkit.getServer().getOnlinePlayers().forEach(p -> refreshBar(p)); properties.getPlayers().forEach(p -> refreshBar(p));
} }
}.runTaskTimerAsynchronously(getPlugin(), 0, 40); }.runTaskTimerAsynchronously(getPlugin(), 0, 40);
} }
@ -49,6 +51,8 @@ public class DurabilityModule extends TweaksModule implements Listener {
} }
private void refreshBar(Player player, ItemStack justDamaged, int damage) { private void refreshBar(Player player, ItemStack justDamaged, int damage) {
if (!properties.isPlayerEnabled(player)) return;
var items = new ItemStack[] { var items = new ItemStack[] {
player.getInventory().getHelmet(), player.getInventory().getHelmet(),
player.getInventory().getChestplate(), player.getInventory().getChestplate(),

View file

@ -1,5 +1,5 @@
# #
# Copyright (C) 2024 Minecon724 # Copyright (C) 2025 Minecon724
# Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file # Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
# in the project root for the full license text. # in the project root for the full license text.
# #
@ -33,3 +33,6 @@ authKickUnregistered = You are not whitelisted on this server!
redstoneGatewayItem = Redstone gateway redstoneGatewayItem = Redstone gateway
clickToCopy = Click to copy to clipboard clickToCopy = Click to copy to clipboard
durabilityEnabled = Enabled durability alert
durabilityDisabled = Disabled durability alert