!Invalid inputted materials are now handled differently

This commit is contained in:
ASangarin 2020-10-03 03:17:17 +02:00
parent 77ea222e1e
commit b94404cc1b

View File

@ -1,14 +1,15 @@
package net.Indyuce.mmoitems.stat;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import com.google.common.base.Enums;
import com.google.common.base.Optional;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.edition.StatEdition;
@ -19,13 +20,17 @@ import net.Indyuce.mmoitems.stat.data.MaterialData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ClickEvent.Action;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.mmogroup.mmolib.api.util.AltChar;
import net.mmogroup.mmolib.version.VersionMaterial;
public class MaterialStat extends ItemStat {
public MaterialStat() {
super("MATERIAL", new ItemStack(VersionMaterial.GRASS_BLOCK.toMaterial()), "Material", new String[] { "Your item material." },
new String[] { "all" });
super("MATERIAL", new ItemStack(VersionMaterial.GRASS_BLOCK.toMaterial()), "Material",
new String[] { "Your item material." }, new String[] { "all" });
}
@Override
@ -41,21 +46,22 @@ public class MaterialStat extends ItemStat {
@Override
public void whenInput(EditionInventory inv, String message, Object... info) {
try {
Material material = Material.valueOf(message.toUpperCase().replace("-", "_").replace(" ", "_"));
inv.getEditedSection().set("material", material.name());
Optional<Material> material = Enums.getIfPresent(Material.class,
message.toUpperCase().replace("-", "_").replace(" ", "_"));
if (material.isPresent()) {
inv.getEditedSection().set("material", material.get().name());
inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material successfully changed to " + material.name() + ".");
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException(
exception.getMessage() + " (all materials can be found here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html)");
}
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material successfully changed to " + material.get().name() + ".");
} else
inv.getPlayer().spigot().sendMessage(
new ComponentBuilder("Invalid material! (Click for a list of valid materials)").color(ChatColor.RED).event(
new ClickEvent(Action.OPEN_URL, "https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html")).create());
}
@Override
public void whenApplied(ItemStackBuilder item, StatData data) {
/*
* material is set handled directly in the MMOBuilder constructor
* material is handled directly in the MMOBuilder constructor,
* therefore nothing needs to be done here
*/
}
@ -66,11 +72,11 @@ public class MaterialStat extends ItemStat {
}
@Override
public void whenDisplayed(List<String> lore, Optional<RandomStatData> optional) {
public void whenDisplayed(List<String> lore, java.util.Optional<RandomStatData> optional) {
lore.add(ChatColor.GRAY + "Current Value: "
+ (optional.isPresent()
? ChatColor.GREEN + MMOUtils.caseOnWords(((MaterialData) optional.get()).getMaterial().name().toLowerCase().replace("_", " "))
? ChatColor.GREEN + MMOUtils.caseOnWords(
((MaterialData) optional.get()).getMaterial().name().toLowerCase().replace("_", " "))
: ChatColor.RED + "None"));
lore.add("");