Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ae2ac933a | ||
|
|
f1250f2879 | ||
|
|
961a22beac | ||
|
|
98dc59ca15 | ||
|
|
091bc8f10c | ||
|
|
879dfb9bdd | ||
|
|
3513477265 | ||
|
|
167ed811b6 | ||
|
|
0f415b9aba | ||
|
|
aebd7f7bc8 | ||
|
|
410658259d | ||
|
|
1dbc9b29a3 | ||
|
|
b1c92310cb | ||
|
|
15ee6ee79e | ||
|
|
dd2d71d754 | ||
|
|
3221450cde | ||
|
|
f615dc867a | ||
|
|
673bae4d50 | ||
|
|
6ad9407cb4 |
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
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) return;
|
||||
|
||||
AnvilView anvilView = event.getView();
|
||||
ItemStack inputItem = anvilView.getItem(0);
|
||||
if(inputItem == null || inputItem.getType() != Material.NAME_TAG) return;
|
||||
|
||||
String renameText = anvilView.getRenameText();
|
||||
|
||||
if(!SilenceNametag.muteString.equals(renameText)
|
||||
&& !SilenceNametag.unmuteString.equals(renameText)) return;
|
||||
|
||||
event.setResult(this.getAppliance().createSilenceNametag(resultItem, renameText));
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
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.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){
|
||||
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;
|
||||
|
||||
if(!item.getItemMeta().getPersistentDataContainer().has(
|
||||
this.getAppliance().nametagActionKey,
|
||||
PersistentDataType.INTEGER
|
||||
)) return;
|
||||
|
||||
getAppliance().applyNametagAction(item, event.getRightClicked());
|
||||
}
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
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.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
|
||||
}
|
||||
|
||||
NamespacedKey nametagActionKey =
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<Listener> listeners() {
|
||||
return List.of(new NametagAnvilListener(), new NametagEntityListener());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user