refactor: separate nametag item handling from silence actions and add rename aliases
This commit is contained in:
1 parent
5ae2ac933a
commit
fb78a450b8
5 files changed
+136
-108
No files matched your search
+52
@@ -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;
|
||||
}
|
||||
}
|
||||
+5
-7
@@ -9,19 +9,17 @@ import org.bukkit.inventory.view.AnvilView;
|
||||
|
||||
public class NametagAnvilListener extends ApplianceListener<SilenceNametag> {
|
||||
@EventHandler
|
||||
public void onAnvilRename(PrepareAnvilEvent event){
|
||||
public void onAnvilRename(PrepareAnvilEvent event) {
|
||||
ItemStack resultItem = event.getResult();
|
||||
if(resultItem == null) return;
|
||||
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;
|
||||
|
||||
String renameText = anvilView.getRenameText();
|
||||
NametagAction action = NametagAction.fromRenameText(anvilView.getRenameText());
|
||||
if(action == null) return;
|
||||
|
||||
if(!SilenceNametag.muteString.equals(renameText)
|
||||
&& !SilenceNametag.unmuteString.equals(renameText)) return;
|
||||
|
||||
event.setResult(this.getAppliance().createSilenceNametag(resultItem, renameText));
|
||||
event.setResult(SilenceNametagItem.create(resultItem, action));
|
||||
}
|
||||
}
|
||||
+18
-10
@@ -1,19 +1,19 @@
|
||||
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.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public class NametagEntityListener extends ApplianceListener<SilenceNametag> {
|
||||
@EventHandler
|
||||
public void onNametagUse(PlayerInteractEntityEvent event){
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onNametagUse(PlayerInteractEntityEvent event) {
|
||||
ItemStack item;
|
||||
|
||||
if (event.getHand() == EquipmentSlot.HAND) {
|
||||
if(event.getHand() == EquipmentSlot.HAND) {
|
||||
item = event.getPlayer().getInventory().getItemInMainHand();
|
||||
} else {
|
||||
item = event.getPlayer().getInventory().getItemInOffHand();
|
||||
@@ -21,11 +21,19 @@ public class NametagEntityListener extends ApplianceListener<SilenceNametag> {
|
||||
|
||||
if(item.getType() != Material.NAME_TAG) return;
|
||||
|
||||
if(!item.getItemMeta().getPersistentDataContainer().has(
|
||||
this.getAppliance().nametagActionKey,
|
||||
PersistentDataType.INTEGER
|
||||
)) return;
|
||||
NametagAction action = SilenceNametagItem.readAction(item);
|
||||
if(action == null) return;
|
||||
|
||||
getAppliance().applyNametagAction(item, event.getRightClicked());
|
||||
SilenceNametag.ActionResult result = this.getAppliance().applyNametagAction(event.getRightClicked(), action);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-91
@@ -1,108 +1,28 @@
|
||||
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SilenceNametag extends Appliance {
|
||||
public static final String pluginName = "silence-nametag";
|
||||
public static final String muteString = "@verstummen";
|
||||
public static final String unmuteString = "@entstummen";
|
||||
|
||||
public enum NametagActions {
|
||||
SILENCE,
|
||||
UNSILENCE
|
||||
public enum ActionResult {
|
||||
CHANGED,
|
||||
UNCHANGED,
|
||||
UNSUPPORTED
|
||||
}
|
||||
|
||||
NamespacedKey nametagActionKey =
|
||||
new NamespacedKey(pluginName, "nametag-action");
|
||||
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;
|
||||
|
||||
private final Component silenceNametagLore = Component.text("Dieses Namensschild verstummt Mobs.");
|
||||
private final Component unsilenceNametagLore = Component.text("Dieses Namensschild entstummt Mobs.");
|
||||
|
||||
ItemStack setNametagAction(ItemStack item, NametagActions action) {
|
||||
Objects.requireNonNull(item, "Item must not be null.");
|
||||
setPersistentDataOfNametag(item, action);
|
||||
removeLoreOfNametag(item);
|
||||
setLoreOfNametag(item, action);
|
||||
restoreDefaultNameOfNametag(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
void setPersistentDataOfNametag(ItemStack item, NametagActions action){
|
||||
item.editPersistentDataContainer(pdc -> pdc.set(
|
||||
nametagActionKey,
|
||||
PersistentDataType.INTEGER,
|
||||
action.ordinal()
|
||||
));
|
||||
}
|
||||
|
||||
void removeLoreOfNametag(ItemStack item){
|
||||
item.editMeta(meta -> {
|
||||
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
||||
lore.removeIf(line ->
|
||||
line.toString().contains(silenceNametagLore.toString())
|
||||
|| line.toString().contains(unsilenceNametagLore.toString()));
|
||||
meta.lore(lore);
|
||||
});
|
||||
}
|
||||
|
||||
void setLoreOfNametag(ItemStack item, NametagActions action){
|
||||
item.editMeta(meta -> {
|
||||
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
||||
if(action == NametagActions.SILENCE) {
|
||||
lore.add(silenceNametagLore);
|
||||
} else if (action == NametagActions.UNSILENCE){
|
||||
lore.add(unsilenceNametagLore);
|
||||
}
|
||||
meta.lore(lore);
|
||||
});
|
||||
}
|
||||
|
||||
void restoreDefaultNameOfNametag(ItemStack item){
|
||||
item.editMeta(meta -> meta.customName(null));
|
||||
}
|
||||
|
||||
@Nullable NametagActions getNametagAction(ItemStack item) {
|
||||
Objects.requireNonNull(item, "Item must not be null.");
|
||||
return item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER) == null
|
||||
? null
|
||||
: NametagActions.values()[item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER)];
|
||||
}
|
||||
|
||||
public ItemStack createSilenceNametag(ItemStack ResultItem, String renameText){
|
||||
if(renameText.equals(muteString)) {
|
||||
return this.setNametagAction(ResultItem, NametagActions.SILENCE);
|
||||
} else if(renameText.equals(unmuteString)) {
|
||||
return this.setNametagAction(ResultItem, NametagActions.UNSILENCE);
|
||||
}
|
||||
return ResultItem;
|
||||
}
|
||||
|
||||
public void applyNametagAction(ItemStack item, Entity mob) {
|
||||
if (getNametagAction(item) == SilenceNametag.NametagActions.SILENCE) {
|
||||
if(mob.isSilent()) return;
|
||||
mob.setSilent(true);
|
||||
} else if (getNametagAction(item) == SilenceNametag.NametagActions.UNSILENCE) {
|
||||
if (!mob.isSilent()) return;
|
||||
mob.setSilent(false);
|
||||
}
|
||||
|
||||
// Consume one unnamed nametag manually.
|
||||
// Custom-named tags use vanilla nametag behavior, including item consumption.
|
||||
if (item.getItemMeta().customName() == null) {
|
||||
item.setAmount(item.getAmount() - 1);
|
||||
}
|
||||
entity.setSilent(action.silent());
|
||||
return ActionResult.CHANGED;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+50
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user