This commit is contained in:
parent
2a65e9dbcb
commit
d8d2022525
2 changed files with 84 additions and 0 deletions
|
@ -14,6 +14,7 @@ import eu.m724.tweaks.module.auth.AuthModule;
|
||||||
import eu.m724.tweaks.module.chat.ChatModule;
|
import eu.m724.tweaks.module.chat.ChatModule;
|
||||||
import eu.m724.tweaks.module.door.DoorKnockModule;
|
import eu.m724.tweaks.module.door.DoorKnockModule;
|
||||||
import eu.m724.tweaks.module.door.DoorOpenModule;
|
import eu.m724.tweaks.module.door.DoorOpenModule;
|
||||||
|
import eu.m724.tweaks.module.durability.DurabilityModule;
|
||||||
import eu.m724.tweaks.module.full.FullModule;
|
import eu.m724.tweaks.module.full.FullModule;
|
||||||
import eu.m724.tweaks.module.hardcore.HardcoreModule;
|
import eu.m724.tweaks.module.hardcore.HardcoreModule;
|
||||||
import eu.m724.tweaks.module.killswitch.KillswitchModule;
|
import eu.m724.tweaks.module.killswitch.KillswitchModule;
|
||||||
|
@ -154,6 +155,8 @@ public class TweaksPlugin extends MStatsPlugin {
|
||||||
TweaksModule.init(SwingModule.class);
|
TweaksModule.init(SwingModule.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TweaksModule.init(DurabilityModule.class);
|
||||||
|
|
||||||
/* end modules */
|
/* end modules */
|
||||||
|
|
||||||
if (config.metrics()) {
|
if (config.metrics()) {
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* 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.DebugLogger;
|
||||||
|
import eu.m724.tweaks.module.TweaksModule;
|
||||||
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerItemDamageEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.Damageable;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DurabilityModule extends TweaksModule implements Listener {
|
||||||
|
@Override
|
||||||
|
protected void onInit() {
|
||||||
|
registerEvents(this);
|
||||||
|
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Bukkit.getServer().getOnlinePlayers().forEach(p -> refreshBar(p));
|
||||||
|
}
|
||||||
|
}.runTaskTimerAsynchronously(getPlugin(), 0, 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerItemDamage(PlayerItemDamageEvent event) {
|
||||||
|
refreshBar(event.getPlayer(), event.getItem(), event.getDamage());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshBar(Player player) {
|
||||||
|
refreshBar(player, null, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshBar(Player player, ItemStack justDamaged, int damage) {
|
||||||
|
var map = new HashMap<Character, ItemStack>();
|
||||||
|
map.put('H', player.getInventory().getHelmet());
|
||||||
|
map.put('C', player.getInventory().getChestplate());
|
||||||
|
map.put('L', player.getInventory().getLeggings());
|
||||||
|
map.put('B', player.getInventory().getBoots());
|
||||||
|
map.put('<', player.getInventory().getItemInOffHand());
|
||||||
|
map.put('>', player.getInventory().getItemInMainHand());
|
||||||
|
|
||||||
|
String s = "";
|
||||||
|
|
||||||
|
for (Map.Entry<Character, ItemStack> e : map.entrySet()) {
|
||||||
|
var itemStack = e.getValue();
|
||||||
|
if (itemStack == null || !itemStack.hasItemMeta()) continue;
|
||||||
|
|
||||||
|
if (itemStack.getItemMeta() instanceof Damageable meta) {
|
||||||
|
var maxDurability = itemStack.getType().getMaxDurability();
|
||||||
|
var durability = maxDurability - meta.getDamage();
|
||||||
|
if (itemStack.equals(justDamaged))
|
||||||
|
durability -= damage;
|
||||||
|
var percentage = (double) durability / maxDurability * 100;
|
||||||
|
|
||||||
|
DebugLogger.finer("%s's %s: %d / %d (%.2f%%)", player.getName(), itemStack.getType().name(), durability, maxDurability, percentage);
|
||||||
|
|
||||||
|
var notify = durability < 30 && (durability < 10 || percentage < 0.1);
|
||||||
|
|
||||||
|
if (notify) {
|
||||||
|
s += e.getKey() + " " + (int) percentage + "% ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(s));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue