refactor: enhance nametag action handling and improve lore management
This commit is contained in:
1 parent
167ed811b6
commit
3513477265
2 files changed
+25
-11
No files matched your search
-1
@@ -13,7 +13,6 @@ public class NametagAnvilListener extends ApplianceListener<SilenceNametag> {
|
|||||||
ItemStack resultItem = event.getResult();
|
ItemStack resultItem = event.getResult();
|
||||||
if(resultItem == null) return;
|
if(resultItem == null) return;
|
||||||
|
|
||||||
|
|
||||||
AnvilView anvilView = event.getView();
|
AnvilView anvilView = event.getView();
|
||||||
ItemStack inputItem = anvilView.getItem(0);
|
ItemStack inputItem = anvilView.getItem(0);
|
||||||
if(inputItem == null || inputItem.getType() != Material.NAME_TAG) return;
|
if(inputItem == null || inputItem.getType() != Material.NAME_TAG) return;
|
||||||
|
|||||||
+25
-10
@@ -27,14 +27,30 @@ public class SilenceNametag extends Appliance {
|
|||||||
new NamespacedKey(pluginName, "nametag-action");
|
new NamespacedKey(pluginName, "nametag-action");
|
||||||
|
|
||||||
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.");
|
||||||
item.editPersistentDataContainer(pdc -> {
|
setPersistentDataOfNametag(item, action);
|
||||||
pdc.set(
|
removeLoreOfNametag(item);
|
||||||
|
setLoreOfNametag(item, action);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPersistentDataOfNametag(ItemStack item, NametagActions action){
|
||||||
|
item.editPersistentDataContainer(pdc -> pdc.set(
|
||||||
nametagActionKey,
|
nametagActionKey,
|
||||||
PersistentDataType.INTEGER,
|
PersistentDataType.INTEGER,
|
||||||
action.ordinal()
|
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("Dieser Name Tag"));
|
||||||
|
meta.lore(lore);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void setLoreOfNametag(ItemStack item, NametagActions action){
|
||||||
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) {
|
||||||
@@ -45,23 +61,22 @@ public class SilenceNametag extends Appliance {
|
|||||||
meta.customName(null);
|
meta.customName(null);
|
||||||
meta.lore(lore);
|
meta.lore(lore);
|
||||||
});
|
});
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable NametagActions getNametagAction(ItemStack item) {
|
@Nullable NametagActions getNametagAction(ItemStack item) {
|
||||||
Objects.requireNonNull(item, "item must not be null");
|
Objects.requireNonNull(item, "Item must not be null.");
|
||||||
return item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER) == null
|
return item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER) == null
|
||||||
? null
|
? null
|
||||||
: NametagActions.values()[item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER)];
|
: NametagActions.values()[item.getPersistentDataContainer().get(nametagActionKey, PersistentDataType.INTEGER)];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack createSilenceNametag(ItemStack oldResultItem, String renameText){
|
public ItemStack createSilenceNametag(ItemStack ResultItem, String renameText){
|
||||||
if(renameText.equals(muteString)) {
|
if(renameText.equals(muteString)) {
|
||||||
return this.setNametagAction(oldResultItem, NametagActions.SILENCE);
|
return this.setNametagAction(ResultItem, NametagActions.SILENCE);
|
||||||
} else if(renameText.equals(unmuteString)) {
|
} else if(renameText.equals(unmuteString)) {
|
||||||
return this.setNametagAction(oldResultItem, NametagActions.UNSILENCE);
|
return this.setNametagAction(ResultItem, NametagActions.UNSILENCE);
|
||||||
}
|
}
|
||||||
return oldResultItem;
|
return ResultItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user