Added "Can Break" item option for 1.20.6+

This commit is contained in:
Jules 2024-10-21 20:09:15 +02:00
parent e3f4288fbd
commit 7d479773f9
7 changed files with 328 additions and 154 deletions

View File

@ -27,6 +27,7 @@ public class ItemStats {
LORE = new Lore(), LORE = new Lore(),
NBT_TAGS = new NBTTags(), NBT_TAGS = new NBTTags(),
MAX_STACK_SIZE = new MaxStackSize(), MAX_STACK_SIZE = new MaxStackSize(),
CAN_BREAK = new CanBreak(),
LORE_FORMAT = new LoreFormat(), LORE_FORMAT = new LoreFormat(),
TOOLTIP = new TooltipStat(), TOOLTIP = new TooltipStat(),

View File

@ -33,8 +33,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
// TODO getItemMeta, asNMSCopy = two clones. could be done with NO clone, simply initializing item with proper interfacing
public class ItemStackBuilder { public class ItemStackBuilder {
@NotNull @NotNull
private final MMOItem mmoitem; private final MMOItem mmoitem;
@ -44,6 +46,12 @@ public class ItemStackBuilder {
private final LoreBuilder lore; private final LoreBuilder lore;
private final List<ItemTag> tags = new ArrayList<>(); private final List<ItemTag> tags = new ArrayList<>();
/**
* @deprecated Temp fix before MI7
*/
@Deprecated
private List<Consumer<NBTItem>> futureActions;
private static final AttributeModifier FAKE_MODIFIER = VersionUtils.attrMod(new NamespacedKey(MMOItems.plugin, "decoy"), 0, Operation.ADD_NUMBER); private static final AttributeModifier FAKE_MODIFIER = VersionUtils.attrMod(new NamespacedKey(MMOItems.plugin, "decoy"), 0, Operation.ADD_NUMBER);
/** /**
@ -97,6 +105,15 @@ public class ItemStackBuilder {
return meta; return meta;
} }
/**
* @deprecated Temp fix before MI7
*/
@Deprecated
public void addFutureAction(Consumer<NBTItem> action) {
if (futureActions == null) futureActions = new ArrayList<>();
futureActions.add(action);
}
public void addItemTag(List<ItemTag> newTags) { public void addItemTag(List<ItemTag> newTags) {
tags.addAll(newTags); tags.addAll(newTags);
} }
@ -241,7 +258,10 @@ public class ItemStackBuilder {
item.setItemMeta(meta); item.setItemMeta(meta);
return NBTItem.get(item).addTag(tags); NBTItem nbt = NBTItem.get(item).addTag(tags);
if (futureActions != null) futureActions.forEach(a -> a.accept(nbt));
return nbt;
} }
/** /**

View File

@ -0,0 +1,166 @@
package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.gson.JsonArray;
import io.lumine.mythic.lib.gson.JsonParser;
import io.lumine.mythic.lib.gson.JsonSyntaxException;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.annotation.VersionDependant;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@VersionDependant(version = {1, 20, 6})
public class CanBreak extends ItemStat<StringListData, StringListData> {
public CanBreak() {
super("CAN_BREAK",
Material.IRON_PICKAXE,
"Can Break?",
new String[]{"The list of materials that this item can break.", "This restriction only applies to adventure mode."},
new String[]{"tool"});
}
@Override
@SuppressWarnings("unchecked")
public StringListData whenInitialized(Object object) {
Validate.isTrue(object instanceof List<?>, "Must specify a string list");
return new StringListData((List<String>) object);
}
@Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, this).enable("Write in the chat the name of the material you want to add.");
if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath()))
return;
List<String> list = inv.getEditedSection().getStringList(getPath());
if (list.isEmpty()) return;
String last = list.removeLast();
inv.getEditedSection().set(getPath(), list);
inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'.");
}
@Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
List<String> list = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>();
// Validate material
final Material material = Material.valueOf(UtilityMethods.enumName(message));
list.add(material.name());
inv.getEditedSection().set(getPath(), list);
inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material " + material.name() + " successfully added.");
}
@Override
public void whenDisplayed(List<String> lore, Optional<StringListData> statData) {
if (statData.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:");
statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str));
} else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Cannot mine any item (adventure mode).");
lore.add("");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a new material.");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last material.");
}
@NotNull
@Override
public StringListData getClearStatData() {
return new StringListData();
}
@Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) {
List<Material> mats = data.getList().stream().map(Material::valueOf).collect(Collectors.toList());
item.addFutureAction(nbt -> {
nbt.setCanMine(mats);
});
item.addItemTag(getAppliedNBT(data));
}
@NotNull
@Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull StringListData data) {
// Build Json Array
JsonArray array = new JsonArray();
// For each string in the ids of the data
for (String sts : data.getList()) {
array.add(sts);
}
// Make returning array
ArrayList<ItemTag> tags = new ArrayList<>();
// Add Json Array
tags.add(new ItemTag(getNBTPath(), array.toString()));
return tags;
}
@Override
public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
// FInd relevant tags
ArrayList<ItemTag> relevantTags = new ArrayList<>();
if (mmoitem.getNBT().hasTag(getNBTPath()))
relevantTags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING));
// Generate data
StatData data = getLoadedNBT(relevantTags);
if (data != null)
mmoitem.setData(this, data);
}
@Nullable
@Override
public StringListData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
// Find relevant tag
ItemTag rTag = ItemTag.getTagAtPath(getNBTPath(), storedTags);
// Found?
if (rTag == null)
// Nope
return null;
try {
// Parse onto Json Array
JsonArray array = new JsonParser().parse((String) rTag.getValue()).getAsJsonArray();
// Make and return list
return new StringListData(array);
} catch (JsonSyntaxException | IllegalStateException exception) {
/*
* OLD ITEM WHICH MUST BE UPDATED.
*/
}
return null;
}
}

View File

@ -6,7 +6,6 @@ import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonArray;
import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonParser;
import io.lumine.mythic.lib.gson.JsonSyntaxException; import io.lumine.mythic.lib.gson.JsonSyntaxException;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
@ -43,36 +42,35 @@ public class CompatibleIds extends ItemStat<StringListData, StringListData> {
@Override @Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL) if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, ItemStats.COMPATIBLE_IDS).enable("Write in the chat the item id you want to add."); new StatEdition(inv, this).enable("Write in the chat the item id you want to add.");
if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains("compatible-ids")) if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath()))
return;
List<String> lore = inv.getEditedSection().getStringList("compatible-ids");
if (lore.size() < 1)
return; return;
String last = lore.get(lore.size() - 1); List<String> lore = inv.getEditedSection().getStringList(getPath());
lore.remove(last); if (lore.isEmpty()) return;
inv.getEditedSection().set("compatible-ids", lore);
String last = lore.removeLast();
inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'.");
} }
@Override @Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
List<String> lore = inv.getEditedSection().contains("compatible-ids") ? inv.getEditedSection().getStringList("compatible-ids") List<String> lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath())
: new ArrayList<>(); : new ArrayList<>();
lore.add(message.toUpperCase()); lore.add(message.toUpperCase());
inv.getEditedSection().set("compatible-ids", lore); inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Compatible IDs successfully added."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "ID '" + message.toUpperCase() + "' successfully added.");
} }
@Override @Override
public void whenDisplayed(List<String> lore, Optional<StringListData> statData) { public void whenDisplayed(List<String> lore, Optional<StringListData> statData) {
if (statData.isPresent()) { if (statData.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:"); lore.add(ChatColor.GRAY + "Current Value:");
statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str));
} else } else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item."); lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item.");
@ -91,7 +89,7 @@ public class CompatibleIds extends ItemStat<StringListData, StringListData> {
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) {
// Copy Array, for lore // Copy Array, for lore
List<String> compatibleIds = new ArrayList<>(data.getList()); List<String> compatibleIds = new ArrayList<>(data.getList());
item.getLore().insert("compatible-ids", compatibleIds); item.getLore().insert(getPath(), compatibleIds);
// Add data // Add data
item.addItemTag(getAppliedNBT(data)); item.addItemTag(getAppliedNBT(data));

View File

@ -1,12 +1,12 @@
package net.Indyuce.mmoitems.stat; package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.item.ItemTag; import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues; import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import io.lumine.mythic.lib.api.util.AltChar; import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonArray;
import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonParser;
import io.lumine.mythic.lib.gson.JsonSyntaxException; import io.lumine.mythic.lib.gson.JsonSyntaxException;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
@ -18,14 +18,12 @@ import net.Indyuce.mmoitems.stat.type.ItemStat;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -46,43 +44,37 @@ public class CompatibleMaterials extends ItemStat<StringListData, StringListData
@Override @Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL) if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, ItemStats.COMPATIBLE_MATERIALS).enable("Write in the chat the name of the material you want to add."); new StatEdition(inv, this).enable("Write in the chat the name of the material you want to add.");
if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains("compatible-materials")) if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath()))
return;
List<String> lore = inv.getEditedSection().getStringList("compatible-materials");
if (lore.size() < 1)
return; return;
String last = lore.get(lore.size() - 1); final List<String> lore = inv.getEditedSection().getStringList(getPath());
lore.remove(last); if (lore.isEmpty()) return;
inv.getEditedSection().set("compatible-materials", lore);
final String last = lore.removeLast();
inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'.");
} }
@Override @Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
final Player player = inv.getPlayer();
// Check if material exists
if (Arrays.stream(Material.values()).noneMatch(versionMaterial -> versionMaterial.name().equalsIgnoreCase(message))) {
player.sendMessage(MMOItems.plugin.getPrefix() + "Invalid material name.");
return;
}
List<String> lore = inv.getEditedSection().contains("compatible-materials") ? inv.getEditedSection().getStringList("compatible-materials") Material mat = Material.valueOf(UtilityMethods.enumName(message));
: new ArrayList<>();
lore.add(message.toUpperCase()); List<String> lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>();
inv.getEditedSection().set("compatible-materials", lore); lore.add(mat.name());
inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
player.sendMessage(MMOItems.plugin.getPrefix() + "Compatible Materials successfully added."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material " + mat.name() + " successfully added.");
} }
@Override @Override
public void whenDisplayed(List<String> lore, Optional<StringListData> statData) { public void whenDisplayed(List<String> lore, Optional<StringListData> statData) {
if (statData.isPresent()) { if (statData.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:"); lore.add(ChatColor.GRAY + "Current Value:");
statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str));
} else } else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any material."); lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any material.");
@ -101,7 +93,7 @@ public class CompatibleMaterials extends ItemStat<StringListData, StringListData
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) {
// Copy Array, for lore // Copy Array, for lore
List<String> compatibleMaterials = new ArrayList<>(data.getList()); List<String> compatibleMaterials = new ArrayList<>(data.getList());
item.getLore().insert("compatible-materials", compatibleMaterials); item.getLore().insert(getPath(), compatibleMaterials);
// Add data // Add data
item.addItemTag(getAppliedNBT(data)); item.addItemTag(getAppliedNBT(data));

View File

@ -6,7 +6,6 @@ import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonArray;
import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonParser;
import io.lumine.mythic.lib.gson.JsonSyntaxException; import io.lumine.mythic.lib.gson.JsonSyntaxException;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
@ -43,36 +42,35 @@ public class CompatibleTypes extends ItemStat<StringListData, StringListData> {
@Override @Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL) if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, ItemStats.COMPATIBLE_TYPES).enable("Write in the chat the name of the type you want to add."); new StatEdition(inv, this).enable("Write in the chat the name of the type you want to add.");
if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains("compatible-types")) if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath()))
return;
List<String> lore = inv.getEditedSection().getStringList("compatible-types");
if (lore.size() < 1)
return; return;
String last = lore.get(lore.size() - 1); List<String> lore = inv.getEditedSection().getStringList(getPath());
lore.remove(last); if (lore.isEmpty()) return;
inv.getEditedSection().set("compatible-types", lore);
String last = lore.removeLast();
inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'.");
} }
@Override @Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
List<String> lore = inv.getEditedSection().contains("compatible-types") ? inv.getEditedSection().getStringList("compatible-types") List<String> lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath())
: new ArrayList<>(); : new ArrayList<>();
lore.add(message.toUpperCase()); lore.add(message.toUpperCase());
inv.getEditedSection().set("compatible-types", lore); inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Compatible Types successfully added."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Type " + message.toUpperCase() + " successfully added.");
} }
@Override @Override
public void whenDisplayed(List<String> lore, Optional<StringListData> statData) { public void whenDisplayed(List<String> lore, Optional<StringListData> statData) {
if (statData.isPresent()) { if (statData.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:"); lore.add(ChatColor.GRAY + "Current Value:");
statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str));
} else } else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item."); lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item.");
@ -91,7 +89,7 @@ public class CompatibleTypes extends ItemStat<StringListData, StringListData> {
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) {
// Copy Array, for lore // Copy Array, for lore
List<String> compatibleTypes = new ArrayList<>(data.getList()); List<String> compatibleTypes = new ArrayList<>(data.getList());
item.getLore().insert("compatible-types", compatibleTypes); item.getLore().insert(getPath(), compatibleTypes);
// Add data // Add data
item.addItemTag(getAppliedNBT(data)); item.addItemTag(getAppliedNBT(data));

View File

@ -31,131 +31,130 @@ import java.util.Optional;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class RequiredClass extends StringListStat implements ItemRestriction, GemStoneStat { public class RequiredClass extends StringListStat implements ItemRestriction, GemStoneStat {
public RequiredClass() { public RequiredClass() {
super("REQUIRED_CLASS", Material.WRITABLE_BOOK, "Required Class", super("REQUIRED_CLASS", Material.WRITABLE_BOOK, "Required Class",
new String[] { "The class you need to", "profess to use your item." }, new String[] { "!block", "all" }); new String[]{"The class you need to", "profess to use your item."}, new String[]{"!block", "all"});
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public StringListData whenInitialized(Object object) { public StringListData whenInitialized(Object object) {
Validate.isTrue(object instanceof List<?>, "Must specify a string list"); Validate.isTrue(object instanceof List<?>, "Must specify a string list");
return new StringListData((List<String>) object); return new StringListData((List<String>) object);
} }
@Override @Override
public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL) if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, this).enable("Write in the chat the class you want your item to support."); new StatEdition(inv, this).enable("Write in the chat the class you want your item to support.");
if (event.getAction() == InventoryAction.PICKUP_HALF) { if (event.getAction() == InventoryAction.PICKUP_HALF && inv.getEditedSection().getKeys(false).contains("required-class")) {
if (inv.getEditedSection().getKeys(false).contains("required-class")) { List<String> supportedClasses = inv.getEditedSection().getStringList("required-class");
List<String> supportedClasses = inv.getEditedSection().getStringList("required-class"); if (supportedClasses.size() < 1)
if (supportedClasses.size() < 1) return;
return;
String last = supportedClasses.get(supportedClasses.size() - 1); String last = supportedClasses.remove(supportedClasses.size() - 1);
supportedClasses.remove(last); inv.getEditedSection().set(getPath(), supportedClasses.size() == 0 ? null : supportedClasses);
inv.getEditedSection().set(getPath(), supportedClasses.size() == 0 ? null : supportedClasses); inv.registerTemplateEdition();
inv.registerTemplateEdition(); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed " + last + ".");
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed " + last + "."); }
} }
}
}
@Override @Override
public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) {
List<String> lore = (inv.getEditedSection().getKeys(false).contains("required-class") ? inv.getEditedSection().getStringList("required-class") List<String> lore = (inv.getEditedSection().getKeys(false).contains("required-class") ? inv.getEditedSection().getStringList("required-class")
: new ArrayList<>()); : new ArrayList<>());
lore.add(message); lore.add(message);
inv.getEditedSection().set(getPath(), lore); inv.getEditedSection().set(getPath(), lore);
inv.registerTemplateEdition(); inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Required Class successfully added."); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Required Class successfully added.");
} }
@Override @Override
public void whenLoaded(@NotNull ReadMMOItem mmoitem) { public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
// Find tag // Find tag
ArrayList<ItemTag> rtags = new ArrayList<>(); ArrayList<ItemTag> rtags = new ArrayList<>();
if (mmoitem.getNBT().hasTag(getNBTPath())) if (mmoitem.getNBT().hasTag(getNBTPath()))
rtags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING)); rtags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING));
// Build // Build
StatData data = getLoadedNBT(rtags); StatData data = getLoadedNBT(rtags);
// Success? // Success?
if (data != null) { mmoitem.setData(this, data);} if (data != null) {
} mmoitem.setData(this, data);
}
}
@Nullable @Nullable
@Override @Override
public StringListData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) { public StringListData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
// Get it // Get it
ItemTag listTag = ItemTag.getTagAtPath(getNBTPath(), storedTags); ItemTag listTag = ItemTag.getTagAtPath(getNBTPath(), storedTags);
// Found? // Found?
if (listTag != null) { if (listTag != null) {
// Create String List Data // Create String List Data
return new StringListData(((String) listTag.getValue()).split(Pattern.quote(", "))); return new StringListData(((String) listTag.getValue()).split(Pattern.quote(", ")));
} }
// No correct tags // No correct tags
return null; return null;
} }
@Override @Override
public void whenDisplayed(List<String> lore, Optional<StringListData> statData) { public void whenDisplayed(List<String> lore, Optional<StringListData> statData) {
if (statData.isPresent()) { if (statData.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:"); lore.add(ChatColor.GRAY + "Current Value:");
StringListData data = (StringListData) statData.get(); StringListData data = (StringListData) statData.get();
data.getList().forEach(el -> lore.add(ChatColor.GRAY + "* " + ChatColor.GREEN + el)); data.getList().forEach(el -> lore.add(ChatColor.GRAY + "* " + ChatColor.GREEN + el));
} else } else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None"); lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None");
lore.add(""); lore.add("");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a class."); lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a class.");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last class."); lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last class.");
} }
@NotNull @NotNull
@Override @Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull StringListData data) { public ArrayList<ItemTag> getAppliedNBT(@NotNull StringListData data) {
// Make the result list // Make the result list
ArrayList<ItemTag> ret = new ArrayList<>(); ArrayList<ItemTag> ret = new ArrayList<>();
// Add the Json Array // Add the Json Array
ret.add(new ItemTag(getNBTPath(), String.join(", ", ((StringListData) data).getList()))); ret.add(new ItemTag(getNBTPath(), String.join(", ", ((StringListData) data).getList())));
// Ready. // Ready.
return ret; return ret;
} }
@Override @Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) { public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
String requiredClass = item.getString(ItemStats.REQUIRED_CLASS.getNBTPath()); String requiredClass = item.getString(ItemStats.REQUIRED_CLASS.getNBTPath());
if (!requiredClass.equals("") && !hasRightClass(player, requiredClass) && !player.getPlayer().hasPermission("mmoitems.bypass.class")) { if (!requiredClass.equals("") && !hasRightClass(player, requiredClass) && !player.getPlayer().hasPermission("mmoitems.bypass.class")) {
if (message) { if (message) {
Message.WRONG_CLASS.format(ChatColor.RED).send(player.getPlayer()); Message.WRONG_CLASS.format(ChatColor.RED).send(player.getPlayer());
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
} }
return false; return false;
} }
return true; return true;
} }
private boolean hasRightClass(RPGPlayer player, String requiredClass) { private boolean hasRightClass(RPGPlayer player, String requiredClass) {
String name = ChatColor.stripColor(player.getClassName()); String name = ChatColor.stripColor(player.getClassName());
for (String found : requiredClass.split(Pattern.quote(", "))) for (String found : requiredClass.split(Pattern.quote(", ")))
if (found.equalsIgnoreCase(name)) if (found.equalsIgnoreCase(name))
return true; return true;
return false; return false;
} }
} }