26 Commits
Author SHA1 Message Date
MineTec b920d989b2 Merge pull request 'Feature/silence named mobs' (#18) from feature/silence-named-mobs into master
Reviewed-on: #18
Reviewed-by: Elias Müller <elias@elias-mueller.com>
2026-11-10 22:23:53 +00:00
MineTec b601eba016 Merge branch 'master' into feature/silence-named-mobs 2026-11-09 18:44:39 +00:00
besiece 6505bda43e feat: implement feedback system for silence nametag actions with action bar updates 2026-11-08 13:04:30 +01:00
besiece fb78a450b8 refactor: separate nametag item handling from silence actions and add rename aliases 2026-11-08 12:38:27 +01:00
MineTec d18ddc7b07 Remove outdated outlawed PvP system and related configurations 2026-10-29 19:51:36 +01:00
MineTec b06ba40d16 Merge pull request 'added combined info bar for TPS, MSPT and online player count' (#17) from feature/combinedInfoBar into master
Reviewed-on: #17
Reviewed-by: Elias Müller <elias@elias-mueller.com>
2026-10-28 11:57:53 +00:00
jannis 3eca590c6f added combined info bar for TPS, MSPT and online player count 2026-10-18 23:31:10 +02:00
besiece 5ae2ac933a feat: refactor silence and unsilence logic into applyNametagAction method in SilenceNametag 2026-09-25 17:56:08 +02:00
besiece f1250f2879 feat: streamline silence and unsilence logic in NametagEntityListener 2026-09-25 17:37:25 +02:00
besiece 961a22beac feat: improve silence and unsilence feedback for mobs in NametagEntityListener 2026-09-25 17:24:50 +02:00
besiece 98dc59ca15 feat: enhance silence and unsilence functionality and manage nametag item amount 2026-09-25 17:19:54 +02:00
besiece 091bc8f10c feat: implement silence and unsilence actions in NametagEntityListener 2026-09-25 16:53:22 +02:00
besiece 879dfb9bdd feat: add NametagEntityListener to handle special nametag interactions 2026-09-25 16:47:48 +02:00
besiece 3513477265 refactor: enhance nametag action handling and improve lore management 2026-09-25 16:22:32 +02:00
besiece 167ed811b6 fix: update lore text for silence nametag and improve nullability handling 2026-09-19 21:12:36 +02:00
besiece 0f415b9aba refactor: streamline nametag action handling and update lore management 2026-08-18 17:12:02 +02:00
besiece aebd7f7bc8 fix: ensure lore is correctly initialized when editing item meta 2026-08-18 16:45:14 +02:00
besiece 410658259d setting the result item in the anvil 2026-08-18 16:36:59 +02:00
besiece 1dbc9b29a3 adding logging to debug 2026-08-18 16:32:39 +02:00
besiece b1c92310cb setting the lore of the result item 2026-08-18 16:25:18 +02:00
besiece 15ee6ee79e catch anvil event and return if not a nametag 2026-08-18 15:40:12 +02:00
besiece dd2d71d754 Created necessary classes and implement console logs for PrepareAnvilEvent 2026-08-18 15:28:11 +02:00
besiece 3221450cde Clean up test scripts and to prepare for development of the feature 2026-08-18 14:53:13 +02:00
besiece f615dc867a Rerunning my first test, to verify if my install worked properly 2026-08-13 13:09:14 +02:00
besiece 673bae4d50 Fixing my first test, to verify if my install worked properly 2026-08-13 10:33:38 +02:00
besiece 6ad9407cb4 Creating a first test, to verify if my install worked properly 2026-08-13 10:14:38 +02:00
17 changed files with 480 additions and 222 deletions

No files matched your search

@@ -19,7 +19,8 @@ public class InfoBarSetting extends MultiBoolSetting<InfoBarSetting.InfoBarConfi
public record InfoBarConfiguration( public record InfoBarConfiguration(
@DisplayName("Millisekunden pro Tick") boolean mspt, @DisplayName("Millisekunden pro Tick") boolean mspt,
@DisplayName("Spieler online") boolean playerCounter, @DisplayName("Spieler online") boolean playerCounter,
@DisplayName("Ticks pro Sekunde") boolean tps @DisplayName("Ticks pro Sekunde") boolean tps,
@DisplayName("TPS, MSPT & Spieler") boolean tpsMsptPlayerCounter
) {} ) {}
@Override @Override
@@ -39,7 +40,7 @@ public class InfoBarSetting extends MultiBoolSetting<InfoBarSetting.InfoBarConfi
@Override @Override
protected InfoBarConfiguration defaultValue() { protected InfoBarConfiguration defaultValue() {
return new InfoBarConfiguration(false, false, false); return new InfoBarConfiguration(false, false, false, false);
} }
@Override @Override
@@ -1,5 +1,6 @@
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars; package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars;
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars.bars.TpsMsptPlayerCounterBar;
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings; import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance; import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand; import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
@@ -16,7 +17,8 @@ public class InfoBars extends Appliance {
private final List<Bar> infoBars = List.of( private final List<Bar> infoBars = List.of(
new TpsBar(), new TpsBar(),
new MsptBar(), new MsptBar(),
new PlayerCounterBar() new PlayerCounterBar(),
new TpsMsptPlayerCounterBar()
); );
public void showAllEnabled(Player player) { public void showAllEnabled(Player player) {
@@ -24,6 +26,7 @@ public class InfoBars extends Appliance {
this.setVisible(player, MsptBar.name, config.mspt()); this.setVisible(player, MsptBar.name, config.mspt());
this.setVisible(player, PlayerCounterBar.name, config.playerCounter()); this.setVisible(player, PlayerCounterBar.name, config.playerCounter());
this.setVisible(player, TpsBar.name, config.tps()); this.setVisible(player, TpsBar.name, config.tps());
this.setVisible(player, TpsMsptPlayerCounterBar.name, config.tpsMsptPlayerCounter());
} }
public void setVisible(Player player, String bar, boolean visible) { public void setVisible(Player player, String bar, boolean visible) {
@@ -0,0 +1,103 @@
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars.bars;
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars.Bar;
import eu.mhsl.craftattack.spawn.common.appliances.tooling.playerlimit.PlayerLimit;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.util.statistics.ServerMonitor;
import eu.mhsl.craftattack.spawn.core.util.text.ColorUtil;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import java.time.Duration;
public class TpsMsptPlayerCounterBar extends Bar {
public static String name = "serverStats";
@Override
protected Duration refresh() {
return Duration.ofSeconds(3);
}
@Override
protected String name() {
return name;
}
@Override
protected Component title() {
float tps = this.currentTps();
float mspt = this.currentMspt();
int players = this.currentPlayerCount();
Component separator = Component.text(" | ", NamedTextColor.DARK_GRAY);
TextColor color = ColorUtil.mapGreenToRed(players, 0, this.getMaxPlayerCount(), true);
return Component.text()
.append(Component.text("TPS: ", NamedTextColor.GRAY))
.append(Component.text(String.format("%.2f", tps), ColorUtil.tpsColor(tps)))
.append(separator)
.append(Component.text("MSPT: ", NamedTextColor.GRAY))
.append(Component.text(String.format("%.2f", mspt), ColorUtil.msptColor(mspt)))
.append(separator)
.append(Component.text("Spieler: ", NamedTextColor.GRAY))
.append(Component.text(players, color))
.build();
}
@Override
protected float progress() {
return Math.clamp(this.currentTps() / 20f, 0f, 1f);
}
@Override
protected BossBar.Color color() {
int severity = Math.max(
this.tpsSeverity(),
Math.max(this.msptSeverity(), this.playerSeverity())
);
return switch (severity) {
case 2 -> BossBar.Color.RED;
case 1 -> BossBar.Color.YELLOW;
default -> BossBar.Color.GREEN;
};
}
@Override
protected BossBar.Overlay overlay() {
return BossBar.Overlay.PROGRESS;
}
private int tpsSeverity() {
float tps = this.currentTps();
return tps >= 18 ? 0 : tps >= 15 ? 1 : 2;
}
private int msptSeverity() {
return this.currentMspt() <= 50 ? 0 : 2;
}
private int playerSeverity() {
int freeSlots = this.getMaxPlayerCount() - this.currentPlayerCount();
return freeSlots <= 0 ? 2 : freeSlots < 5 ? 1 : 0;
}
private float currentTps() {
return (float) Bukkit.getTPS()[0];
}
private float currentMspt() {
return ServerMonitor.getServerMSPT();
}
private int currentPlayerCount() {
return Bukkit.getOnlinePlayers().size();
}
private int getMaxPlayerCount() {
return Main.instance().getAppliance(PlayerLimit.class).getLimit();
}
}
-3
View File
@@ -110,9 +110,6 @@ tablist:
projectTitle: "Title" projectTitle: "Title"
interface: eth0 interface: eth0
outlawed:
voluntarily: []
packselect: packselect:
packs: packs:
- somepack: - somepack:
@@ -1,7 +0,0 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.outlawed;
class OutlawChangeNotPermitted extends Exception {
public OutlawChangeNotPermitted(String message) {
super(message);
}
}
@@ -1,165 +0,0 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.outlawed;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.displayName.DisplayName;
import eu.mhsl.craftattack.spawn.craftattack.appliances.tooling.whitelist.Whitelist;
import eu.mhsl.craftattack.spawn.core.config.Configuration;
import eu.mhsl.craftattack.spawn.core.util.text.DisconnectInfo;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.util.*;
public class Outlawed extends Appliance implements DisplayName.Prefixed {
public final int timeoutInMs = 1000 * 60 * 60 * 6;
private final Map<UUID, Long> timeouts = new HashMap<>();
public enum Status {
DISABLED,
VOLUNTARILY,
FORCED
}
private final Map<Player, Status> playerStatusMap = new WeakHashMap<>();
private final String voluntarilyEntry = "voluntarily";
public Outlawed() {
super("outlawed");
Bukkit.getScheduler().runTaskTimerAsynchronously(
Main.instance(),
() -> this.playerStatusMap.forEach((player, status) -> {
if(!player.isOnline()) return;
if(status != Status.FORCED) return;
try {
this.queryAppliance(Whitelist.class).fullIntegrityCheck(player);
} catch(DisconnectInfo.Throwable e) {
e.getDisconnectScreen().applyKick(player);
}
}),
20 * 60,
20 * 60 * 5
);
}
void askForConfirmation(Player player) {
Component confirmationMessage = switch(this.getLawStatus(player)) {
case DISABLED -> Component.text("Wenn du den PVP-Modus aktivierst, darfst du von allen anderen PVP-Spielern grundlos angegriffen werden." +
" Du kannst den PVP-Modus erst wieder nach " + this.timeoutInMs / 1000 / 60 / 60 + " Stunden deaktivieren!");
case VOLUNTARILY -> Component.text("Wenn du den PVP-Modus deaktivierst, darfst du nicht mehr grundlos von anderen Spielern angegriffen werden.");
case FORCED -> Component.text("Du darfst zurzeit deinen PVP-Modus nicht ändern, da die˝ser als Strafe auferlegt wurde!");
};
String command = String.format("/%s confirm", OutlawedCommand.commandName);
Component changeText = Component.text(
String.format(
"Zum ändern deines PVP-Status klicke auf diese Nachricht oder tippe '%s'",
command
),
NamedTextColor.GOLD
).clickEvent(ClickEvent.suggestCommand(command));
player.sendMessage(
Component.text()
.append(confirmationMessage.color(NamedTextColor.RED))
.appendNewline()
.append(changeText)
);
}
void switchLawStatus(Player player) throws OutlawChangeNotPermitted {
if(this.getLawStatus(player).equals(Status.FORCED)) {
throw new OutlawChangeNotPermitted("Dein PVP-Status wurde als Strafe auferlegt und kann daher nicht verändert werden.");
}
if(this.isTimeout(player)) {
throw new OutlawChangeNotPermitted("Du kannst deinen PVP-Status nicht so schnell wechseln. Bitte warte einige Stunden bevor du umschaltest!");
}
this.setLawStatus(player, this.isOutlawed(player) ? Status.DISABLED : Status.VOLUNTARILY);
this.setTimeout(player);
}
public void updateForcedStatus(Player player, boolean forced) {
this.setLawStatus(player, forced ? Status.FORCED : this.getLawStatus(player) == Status.FORCED ? Status.DISABLED : this.getLawStatus(player));
}
public Status getLawStatus(Player player) {
return this.playerStatusMap.computeIfAbsent(player, p -> {
if(this.localConfig().getStringList(this.voluntarilyEntry).contains(p.getUniqueId().toString()))
return Status.VOLUNTARILY;
return Status.DISABLED;
});
}
private void setLawStatus(Player player, Status status) {
this.playerStatusMap.put(player, status);
this.queryAppliance(DisplayName.class).update(player);
List<String> newList = this.localConfig().getStringList(this.voluntarilyEntry);
if(status.equals(Status.VOLUNTARILY)) {
newList.add(player.getUniqueId().toString());
} else {
newList.remove(player.getUniqueId().toString());
}
this.localConfig().set(this.voluntarilyEntry, newList.stream().distinct().toList());
Configuration.saveChanges();
}
public boolean isOutlawed(Player player) {
return this.getLawStatus(player) != Status.DISABLED;
}
private boolean isTimeout(Player player) {
return this.timeouts.getOrDefault(player.getUniqueId(), 0L) > System.currentTimeMillis() - this.timeoutInMs;
}
private void setTimeout(Player player) {
this.timeouts.put(player.getUniqueId(), System.currentTimeMillis());
}
public Component getStatusDescription(Status status) {
return switch(status) {
case DISABLED -> Component.text("PVP-Modus inaktiv: ", NamedTextColor.GREEN)
.append(Component.text("Es gelten die normalen Regeln!", NamedTextColor.GOLD));
case VOLUNTARILY, FORCED -> Component.text("PVP-Modus aktiv: ", NamedTextColor.RED)
.append(Component.text(
"Du darfst von allen anderen PVP-Spielern angegriffen und getötet werden!\n" +
"Wenn du getötet wirst, müssen andere Spieler deine Items *nicht* zurückerstatten!",
NamedTextColor.GOLD
));
};
}
@Override
public Component getNamePrefix(Player player) {
if(this.isOutlawed(player)) {
return Component.text("[☠]", NamedTextColor.RED)
.hoverEvent(HoverEvent.showText(Component.text(
"PVP-Modus Spieler dürfen von anderen vogelfreien Spielern ohne Grund angegriffen werden!"
)));
}
return null;
}
@Override
@NotNull
protected List<ApplianceCommand<?>> commands() {
return List.of(new OutlawedCommand());
}
@Override
@NotNull
protected List<Listener> listeners() {
return List.of(new OutlawedReminderListener());
}
}
@@ -1,30 +0,0 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.outlawed;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
class OutlawedCommand extends ApplianceCommand.PlayerChecked<Outlawed> {
public static final String commandName = "pvp";
public OutlawedCommand() {
super(commandName);
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
if(args.length == 1 && args[0].equals("confirm")) {
try {
this.getAppliance().switchLawStatus(this.getPlayer());
sender.sendMessage(this.getAppliance().getStatusDescription(this.getAppliance().getLawStatus(this.getPlayer())));
} catch(OutlawChangeNotPermitted e) {
sender.sendMessage(Component.text(e.getMessage(), NamedTextColor.RED));
}
} else {
this.getAppliance().askForConfirmation(this.getPlayer());
}
}
}
@@ -1,14 +0,0 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.outlawed;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
class OutlawedReminderListener extends ApplianceListener<Outlawed> {
@EventHandler
public void onJoin(PlayerJoinEvent e) {
if(this.getAppliance().isOutlawed(e.getPlayer())) {
e.getPlayer().sendMessage(this.getAppliance().getStatusDescription(this.getAppliance().getLawStatus(e.getPlayer())));
}
}
}
@@ -0,0 +1,46 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
final class ActionBarNametagFeedback implements NametagFeedback {
@Override
public void send(Player player, NametagFeedbackUpdate update) {
player.sendActionBar(this.render(update));
}
Component render(NametagFeedbackUpdate update) {
if(update.summary()) {
var counts = update.counts();
List<Component> parts = new ArrayList<>();
addCount(parts, counts.silenced(), "verstummt", NamedTextColor.GREEN);
addCount(parts, counts.unsilenced(), "entstummt", NamedTextColor.GREEN);
addCount(parts, counts.alreadySilent(), "bereits stumm", NamedTextColor.YELLOW);
addCount(parts, counts.alreadyAudible(), "bereits hörbar", NamedTextColor.YELLOW);
addCount(parts, counts.unsupported(), "nicht unterstützt", NamedTextColor.RED);
return Component.join(JoinConfiguration.separator(Component.text(" · ", NamedTextColor.GRAY)), parts);
}
boolean silence = update.action() == NametagAction.SILENCE;
String suffix = switch(update.result()) {
case CHANGED -> silence ? " ist jetzt stumm." : " ist jetzt wieder hörbar.";
case UNCHANGED -> silence ? " ist bereits stumm." : " ist bereits hörbar.";
case UNSUPPORTED -> " unterstützt dieses Namensschild nicht.";
};
NamedTextColor color = switch(update.result()) {
case CHANGED -> NamedTextColor.GREEN;
case UNCHANGED -> NamedTextColor.YELLOW;
case UNSUPPORTED -> NamedTextColor.RED;
};
return Component.empty().color(color).append(update.targetName()).append(Component.text(suffix));
}
private static void addCount(List<Component> parts, int count, String label, NamedTextColor color) {
if(count > 0) parts.add(Component.text(count + " " + label, color));
}
}
@@ -0,0 +1,52 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
import java.util.Set;
public enum NametagAction {
SILENCE(0, Set.of("@verstummen", "@stumm", "@mute"), "Dieses Namensschild muted Mobs.", true),
UNSILENCE(1, Set.of("@entstummen", "@laut", "@unmute"), "Dieses Namensschild unmuted Mobs.", false);
private final int persistentId;
private final Set<String> renameTexts;
private final Component lore;
private final boolean silent;
NametagAction(int persistentId, Set<String> renameTexts, String lore, boolean silent) {
this.persistentId = persistentId;
this.renameTexts = Set.copyOf(renameTexts);
this.lore = Component.text(lore);
this.silent = silent;
}
public int persistentId() {
return this.persistentId;
}
public Component lore() {
return this.lore;
}
public boolean silent() {
return this.silent;
}
public static @Nullable NametagAction fromPersistentId(int id) {
for(NametagAction action : values()) {
if(action.persistentId == id) return action;
}
return null;
}
public static @Nullable NametagAction fromRenameText(@Nullable String text) {
if(text == null) return null;
String normalizedText = text.strip().toLowerCase(Locale.ROOT);
for(NametagAction action : values()) {
if(action.renameTexts.contains(normalizedText)) return action;
}
return null;
}
}
@@ -0,0 +1,25 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.view.AnvilView;
public class NametagAnvilListener extends ApplianceListener<SilenceNametag> {
@EventHandler
public void onAnvilRename(PrepareAnvilEvent event) {
ItemStack resultItem = event.getResult();
if(resultItem == null || resultItem.getType() != Material.NAME_TAG) return;
AnvilView anvilView = event.getView();
ItemStack inputItem = anvilView.getItem(0);
if(inputItem == null || inputItem.getType() != Material.NAME_TAG) return;
NametagAction action = NametagAction.fromRenameText(anvilView.getRenameText());
if(action == null) return;
event.setResult(SilenceNametagItem.create(resultItem, action));
}
}
@@ -0,0 +1,46 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
public class NametagEntityListener extends ApplianceListener<SilenceNametag> {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onNametagUse(PlayerInteractEntityEvent event) {
ItemStack item;
if(event.getHand() == EquipmentSlot.HAND) {
item = event.getPlayer().getInventory().getItemInMainHand();
} else {
item = event.getPlayer().getInventory().getItemInOffHand();
}
if(item.getType() != Material.NAME_TAG) return;
NametagAction action = SilenceNametagItem.readAction(item);
if(action == null) return;
SilenceNametag.ActionResult result = this.getAppliance().applyNametagAction(event.getRightClicked(), action);
this.getAppliance().feedback().send(event.getPlayer(), event.getRightClicked(), action, result);
if(result == SilenceNametag.ActionResult.UNSUPPORTED) return;
// Named tags retain vanilla naming and consumption, even if the mob is already in the desired state.
if(item.getItemMeta().customName() != null) return;
// Unnamed special tags are handled entirely here, including no-op interactions.
event.setCancelled(true);
if(result == SilenceNametag.ActionResult.CHANGED && event.getPlayer().getGameMode() != GameMode.CREATIVE) {
item.setAmount(item.getAmount() - 1);
}
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
this.getAppliance().feedback().forget(event.getPlayer().getUniqueId());
}
}
@@ -0,0 +1,7 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import org.bukkit.entity.Player;
interface NametagFeedback {
void send(Player player, NametagFeedbackUpdate update);
}
@@ -0,0 +1,82 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.LongSupplier;
final class NametagFeedbackService {
private static final long SERIES_TIMEOUT = TimeUnit.SECONDS.toNanos(1);
private final LongSupplier clock;
private final List<NametagFeedback> outputs;
private final Map<UUID, Series> seriesByPlayer = new HashMap<>();
NametagFeedbackService(LongSupplier clock, List<NametagFeedback> outputs) {
this.clock = clock;
this.outputs = List.copyOf(outputs);
}
void send(Player player, Entity target, NametagAction action, SilenceNametag.ActionResult result) {
Component name = target.customName();
if(name == null) name = Component.translatable(target.getType().translationKey());
NametagFeedbackUpdate update = this.recordInteraction(player.getUniqueId(), target.getUniqueId(), name, action, result);
this.outputs.forEach(output -> output.send(player, update));
}
// Called synchronously by the entity listener. Series retain no Bukkit objects or display names.
NametagFeedbackUpdate recordInteraction(UUID playerId, UUID targetId, Component name,
NametagAction action, SilenceNametag.ActionResult result) {
long now = this.clock.getAsLong();
this.seriesByPlayer.values().removeIf(series -> now - series.lastInteraction >= SERIES_TIMEOUT);
boolean newSeries = !this.seriesByPlayer.containsKey(playerId);
Series series = this.seriesByPlayer.computeIfAbsent(playerId, ignored -> new Series());
series.lastInteraction = now;
if(result == SilenceNametag.ActionResult.UNSUPPORTED) {
series.unsupported.add(targetId);
} else {
series.results.merge(new TargetAction(targetId, action), result, (previous, current) ->
previous == SilenceNametag.ActionResult.CHANGED ? previous : current);
}
return new NametagFeedbackUpdate(targetId, name, action, result, newSeries, !newSeries, series.counts());
}
void forget(UUID playerId) {
this.seriesByPlayer.remove(playerId);
}
void clear() {
this.seriesByPlayer.clear();
}
private record TargetAction(UUID targetId, NametagAction action) {
}
private static final class Series {
private long lastInteraction;
private final Map<TargetAction, SilenceNametag.ActionResult> results = new HashMap<>();
private final Set<UUID> unsupported = new HashSet<>();
private NametagFeedbackUpdate.Counts counts() {
int silenced = 0, unsilenced = 0, alreadySilent = 0, alreadyAudible = 0;
for(var entry : this.results.entrySet()) {
boolean changed = entry.getValue() == SilenceNametag.ActionResult.CHANGED;
if(entry.getKey().action() == NametagAction.SILENCE) {
if(changed) silenced++;
else alreadySilent++;
} else {
if(changed) unsilenced++;
else alreadyAudible++;
}
}
return new NametagFeedbackUpdate.Counts(silenced, unsilenced, alreadySilent, alreadyAudible, this.unsupported.size());
}
}
}
@@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import net.kyori.adventure.text.Component;
import java.util.UUID;
record NametagFeedbackUpdate(
UUID targetId,
Component targetName,
NametagAction action,
SilenceNametag.ActionResult result,
boolean newSeries,
boolean summary,
Counts counts
) {
record Counts(int silenced, int unsilenced, int alreadySilent, int alreadyAudible, int unsupported) {
}
}
@@ -0,0 +1,44 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Objects;
public class SilenceNametag extends Appliance {
private final NametagFeedbackService feedback = new NametagFeedbackService(
System::nanoTime, List.of(new ActionBarNametagFeedback()));
NametagFeedbackService feedback() {
return this.feedback;
}
@Override
public void onDisable() {
this.feedback.clear();
}
public enum ActionResult {
CHANGED,
UNCHANGED,
UNSUPPORTED
}
public ActionResult applyNametagAction(Entity entity, NametagAction action) {
Objects.requireNonNull(action, "Action must not be null.");
if(!(entity instanceof Mob)) return ActionResult.UNSUPPORTED;
if(entity.isSilent() == action.silent()) return ActionResult.UNCHANGED;
entity.setSilent(action.silent());
return ActionResult.CHANGED;
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(new NametagAnvilListener(), new NametagEntityListener());
}
}
@@ -0,0 +1,50 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
final class SilenceNametagItem {
// Keep the key and IDs compatible with existing nametags.
private static final NamespacedKey ACTION_KEY = new NamespacedKey("silence-nametag", "nametag-action");
private SilenceNametagItem() {
}
static @Nullable NametagAction readAction(ItemStack item) {
if(item.getType() != Material.NAME_TAG) return null;
var data = item.getPersistentDataContainer();
if(!data.has(ACTION_KEY, PersistentDataType.INTEGER)) return null;
Integer id = data.get(ACTION_KEY, PersistentDataType.INTEGER);
return id == null ? null : NametagAction.fromPersistentId(id);
}
static ItemStack create(ItemStack source, NametagAction action) {
Objects.requireNonNull(action, "Action must not be null.");
if(source.getType() != Material.NAME_TAG) {
throw new IllegalArgumentException("Item must be a nametag.");
}
ItemStack result = source.clone();
result.editMeta(meta -> {
meta.getPersistentDataContainer().set(ACTION_KEY, PersistentDataType.INTEGER, action.persistentId());
List<Component> existingLore = meta.lore();
List<Component> lore = existingLore == null ? new ArrayList<>() : new ArrayList<>(existingLore);
for(NametagAction knownAction : NametagAction.values()) {
lore.removeIf(knownAction.lore()::equals);
}
lore.add(action.lore());
meta.lore(lore);
meta.customName(null);
});
return result;
}
}