feat: refactor silence and unsilence logic into applyNametagAction method in SilenceNametag
This commit is contained in:
1 parent
f1250f2879
commit
5ae2ac933a
2 files changed
+27
-20
No files matched your search
+1
-14
@@ -2,7 +2,6 @@ package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag
|
|||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
|
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||||
import org.bukkit.inventory.EquipmentSlot;
|
import org.bukkit.inventory.EquipmentSlot;
|
||||||
@@ -27,18 +26,6 @@ public class NametagEntityListener extends ApplianceListener<SilenceNametag> {
|
|||||||
PersistentDataType.INTEGER
|
PersistentDataType.INTEGER
|
||||||
)) return;
|
)) return;
|
||||||
|
|
||||||
Entity mob = event.getRightClicked();
|
getAppliance().applyNametagAction(item, event.getRightClicked());
|
||||||
|
|
||||||
if (getAppliance().getNametagAction(item) == SilenceNametag.NametagActions.SILENCE) {
|
|
||||||
if(mob.isSilent()) return;
|
|
||||||
mob.setSilent(true);
|
|
||||||
} else if (getAppliance().getNametagAction(item) == SilenceNametag.NametagActions.UNSILENCE) {
|
|
||||||
if (!mob.isSilent()) return;
|
|
||||||
mob.setSilent(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.getItemMeta().customName() == null) {
|
|
||||||
item.setAmount(item.getAmount() - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+26
-6
@@ -3,6 +3,7 @@ package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.silenceNametag
|
|||||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.bukkit.NamespacedKey;
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
@@ -26,6 +27,9 @@ public class SilenceNametag extends Appliance {
|
|||||||
NamespacedKey nametagActionKey =
|
NamespacedKey nametagActionKey =
|
||||||
new NamespacedKey(pluginName, "nametag-action");
|
new NamespacedKey(pluginName, "nametag-action");
|
||||||
|
|
||||||
|
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) {
|
ItemStack setNametagAction(ItemStack item, NametagActions action) {
|
||||||
Objects.requireNonNull(item, "Item must not be null.");
|
Objects.requireNonNull(item, "Item must not be null.");
|
||||||
setPersistentDataOfNametag(item, action);
|
setPersistentDataOfNametag(item, action);
|
||||||
@@ -46,7 +50,9 @@ public class SilenceNametag extends Appliance {
|
|||||||
void removeLoreOfNametag(ItemStack item){
|
void removeLoreOfNametag(ItemStack item){
|
||||||
item.editMeta(meta -> {
|
item.editMeta(meta -> {
|
||||||
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
||||||
lore.removeIf(line -> line.toString().contains("Dieser Name Tag"));
|
lore.removeIf(line ->
|
||||||
|
line.toString().contains(silenceNametagLore.toString())
|
||||||
|
|| line.toString().contains(unsilenceNametagLore.toString()));
|
||||||
meta.lore(lore);
|
meta.lore(lore);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -55,18 +61,16 @@ public class SilenceNametag extends Appliance {
|
|||||||
item.editMeta(meta -> {
|
item.editMeta(meta -> {
|
||||||
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
List<Component> lore = meta.hasLore() ? new ArrayList<>(meta.lore()) : new ArrayList<>();
|
||||||
if(action == NametagActions.SILENCE) {
|
if(action == NametagActions.SILENCE) {
|
||||||
lore.add(Component.text("Dieser Name Tag verstummt Mobs."));
|
lore.add(silenceNametagLore);
|
||||||
} else if (action == NametagActions.UNSILENCE){
|
} else if (action == NametagActions.UNSILENCE){
|
||||||
lore.add(Component.text("Dieser Name Tag entstummt Mobs."));
|
lore.add(unsilenceNametagLore);
|
||||||
}
|
}
|
||||||
meta.lore(lore);
|
meta.lore(lore);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void restoreDefaultNameOfNametag(ItemStack item){
|
void restoreDefaultNameOfNametag(ItemStack item){
|
||||||
item.editMeta(meta -> {
|
item.editMeta(meta -> meta.customName(null));
|
||||||
meta.customName(null);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable NametagActions getNametagAction(ItemStack item) {
|
@Nullable NametagActions getNametagAction(ItemStack item) {
|
||||||
@@ -85,6 +89,22 @@ public class SilenceNametag extends Appliance {
|
|||||||
return ResultItem;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected @NotNull List<Listener> listeners() {
|
protected @NotNull List<Listener> listeners() {
|
||||||
return List.of(new NametagAnvilListener(), new NametagEntityListener());
|
return List.of(new NametagAnvilListener(), new NametagEntityListener());
|
||||||
|
|||||||
Reference in new issue
Block a user