Recipe key cleanup. Entering recipe ingredients now support caps.

This commit is contained in:
Indyuce 2020-02-22 19:21:49 +01:00
parent 9a3a51d6f6
commit 96b7eaa955
6 changed files with 71 additions and 67 deletions

View File

@ -59,4 +59,18 @@ public class ConfigFile {
MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not generate " + name + ".yml");
}
}
public void registerItemEdition(Type type, String id) {
/*
* uncaches the item so it can be generated to apply newest changes in
* case the same inventory is opened again.
*/
MMOItems.plugin.getItems().uncache(type, id);
/*
* finally saves the changes
*/
save();
}
}

View File

@ -188,20 +188,6 @@ public class Type {
available = stats;
}
public void registerItemEdition(ConfigFile config, String id) {
/*
* uncaches the item so it can be generated to apply newest changes in
* case the same inventory is opened again.
*/
MMOItems.plugin.getItems().uncache(this, id);
/*
* finally saves the item.
*/
config.save();
}
public boolean canHave(ItemStat stat) {
if (isSubtype())
return getParent().canHave(stat);

View File

@ -430,13 +430,17 @@ public class MMOItemsCommand implements CommandExecutor {
if (args[1].equalsIgnoreCase("recipes")) {
Bukkit.getScheduler().runTaskAsynchronously(MMOItems.plugin, () -> {
MMOItems.plugin.getRecipes().reloadRecipes();
if(MMOItems.plugin.getConfig().getBoolean("auto-recipe-book"))
for(Player p : Bukkit.getOnlinePlayers())
p.discoverRecipes(MMOItems.plugin.getRecipes().getNamespacedKeys());
sender.sendMessage(MMOItems.plugin.getPrefix() + "Successfully reloaded recipes.");
sender.sendMessage(MMOItems.plugin.getPrefix() + "- " + ChatColor.RED + MMOItems.plugin.getRecipes().getLoadedRecipes().size() + ChatColor.GRAY + " Recipes");
/*
* discoverRecipes must be called on the main thread.
*/
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> {
if (MMOItems.plugin.getConfig().getBoolean("auto-recipe-book"))
Bukkit.getOnlinePlayers().forEach(online -> online.discoverRecipes(MMOItems.plugin.getRecipes().getNamespacedKeys()));
sender.sendMessage(MMOItems.plugin.getPrefix() + "Successfully reloaded recipes.");
sender.sendMessage(MMOItems.plugin.getPrefix() + "- " + ChatColor.RED + MMOItems.plugin.getRecipes().getLoadedRecipes().size() + ChatColor.GRAY + " Recipes");
});
});
}
return true;
@ -483,7 +487,7 @@ public class MMOItemsCommand implements CommandExecutor {
}
config.getConfig().set(id2, config.getConfig().getConfigurationSection(id1));
type.registerItemEdition(config, id2);
config.registerItemEdition(type, id2);
if (sender instanceof Player)
new ItemEdition((Player) sender, type, id2).open();
sender.sendMessage(MMOItems.plugin.getPrefix() + ChatColor.GREEN + "You successfully copied " + id1 + " to " + id2 + "!");
@ -664,7 +668,7 @@ public class MMOItemsCommand implements CommandExecutor {
}
config.getConfig().set(name + ".material", args[0].equalsIgnoreCase("load") ? item.getType().name() : type.getItem().getType().name());
type.registerItemEdition(config, name);
config.registerItemEdition(type, name);
if (sender instanceof Player)
new ItemEdition((Player) sender, type, name).open();
sender.sendMessage(MMOItems.plugin.getPrefix() + ChatColor.GREEN + "You successfully " + args[0].replace("d", "de") + "d " + name + "!");
@ -786,7 +790,7 @@ public class MMOItemsCommand implements CommandExecutor {
}
config.getConfig().set(id, null);
type.registerItemEdition(config, id);
config.registerItemEdition(type, id);
/*
* remove the item updater data and uuid data from the plugin to

View File

@ -60,6 +60,10 @@ public abstract class EditionInventory extends PluginInventory {
cached = null;
}
public void registerItemEdition(ConfigFile config) {
registerItemEdition(config, true);
}
public void registerItemEdition(ConfigFile config, boolean uuid) {
/*
@ -67,11 +71,7 @@ public abstract class EditionInventory extends PluginInventory {
* cannot display on the edition GUI
*/
flushItem();
getItemType().registerItemEdition(config, uuid ? id : null);
}
public void registerItemEdition(ConfigFile config) {
registerItemEdition(config, true);
config.registerItemEdition(getItemType(), uuid ? id : null);
}
public void addEditionInventoryItems(Inventory inv, boolean backBool) {

View File

@ -67,7 +67,7 @@ public abstract class RecipeManager {
}
public NamespacedKey getRecipeKey(Type type, String id, String recipeType, String number) {
return new NamespacedKey(MMOItems.plugin, "mmoitems_" + recipeType + "_" + type.getId() + "_" + id + "_" + number);
return new NamespacedKey(MMOItems.plugin, recipeType + "_" + type.getId() + "_" + id + "_" + number);
}
public void reloadRecipes() {
@ -76,7 +76,7 @@ public abstract class RecipeManager {
Iterator<Recipe> iterator = Bukkit.recipeIterator();
while (iterator.hasNext()) {
Recipe recipe = iterator.next();
if (recipe instanceof Keyed && ((Keyed) recipe).getKey().getKey().startsWith("mmoitems:"))
if (recipe instanceof Keyed && ((Keyed) recipe).getKey().getNamespace().equals("mmoitems"))
iterator.remove();
}

View File

@ -8,6 +8,7 @@ import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
@ -32,7 +33,17 @@ public class Crafting extends ItemStat {
@Override
public boolean whenClicked(EditionInventory inv, InventoryClickEvent event) {
new CraftingEdition(inv.getPlayer(), inv.getItemType(), inv.getItemId()).open(inv.getPage());
if (event.getAction() == InventoryAction.PICKUP_ALL)
new CraftingEdition(inv.getPlayer(), inv.getItemType(), inv.getItemId()).open(inv.getPage());
else if (event.getAction() == InventoryAction.PICKUP_HALF) {
ConfigFile config = inv.getItemType().getConfigFile();
ConfigurationSection section = config.getConfig().getConfigurationSection(inv.getItemId());
if (section.contains("crafting")) {
section.set("crafting", null);
inv.registerItemEdition(config);
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Crafting recipes successfully removed. Make sure you reload active recipes using " + ChatColor.RED + "/mi reload recipes" + ChatColor.GRAY + ".");
}
}
return true;
}
@ -65,14 +76,9 @@ public class Crafting extends ItemStat {
}
}
} else if (type.equals("item")) {
if (validate(inv.getPlayer(), message)) {
Bukkit.getScheduler().runTask(MMOItems.plugin, new Runnable() {
@Override
public void run() {
new StatEdition(inv, ItemStat.CRAFTING, "time", info[1], message).enable("Write in the chat the cooktime (in ticks) for your recipe.", "Format: '[INTEGER]'");
}
});
}
if (validate(inv.getPlayer(), message))
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> new StatEdition(inv, ItemStat.CRAFTING, "time", info[1], message).enable("Write in the chat the cooktime (in ticks) for your recipe.", "Format: '[INTEGER]'"));
} else if (type.equals("time")) {
int time;
try {
@ -81,13 +87,8 @@ public class Crafting extends ItemStat {
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + ChatColor.RED + message + " is not a valid number.");
return false;
}
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> new StatEdition(inv, ItemStat.CRAFTING, "exp", info[1], time, info[2]).enable("Write in the chat the experience given for your recipe.", "Format: '[FLOAT]'"));
Bukkit.getScheduler().runTask(MMOItems.plugin, new Runnable() {
@Override
public void run() {
new StatEdition(inv, ItemStat.CRAFTING, "exp", info[1], time, info[2]).enable("Write in the chat the experience given for your recipe.", "Format: '[FLOAT]'");
}
});
} else if (type.equals("exp")) {
double exp;
try {
@ -121,52 +122,51 @@ public class Crafting extends ItemStat {
public void whenLoaded(MMOItem mmoitem, NBTItem item) {
}
private boolean validate(Player player, String s) {
if (s.contains(".")) {
String[] typeid = s.split("\\.");
private boolean validate(Player player, String input) {
if (input.contains(".")) {
String[] typeid = input.split("\\.");
if (typeid.length != 2) {
player.sendMessage("Invalid format.");
player.sendMessage(MMOItems.plugin.getPrefix() + "Invalid format.");
return false;
}
if (!Type.isValid(typeid[0])) {
player.sendMessage("'" + typeid[0] + "' isn't a valid Type.");
if (!Type.isValid(typeid[0].toUpperCase().replace("-", "_").replace(" ", "_"))) {
player.sendMessage(MMOItems.plugin.getPrefix() + "'" + typeid[0].toUpperCase().replace("-", "_").replace(" ", "_") + "' isn't a valid item type.");
return false;
}
if (!Type.get(typeid[0]).getConfigFile().getConfig().contains(typeid[1])) {
player.sendMessage("'" + typeid[1] + "' isn't a valid MMOItem.");
Type type = Type.get(typeid[0].toUpperCase().replace("-", "_").replace(" ", "_"));
if (MMOItems.plugin.getItems().getItem(type, typeid[1]) == null) {
player.sendMessage(MMOItems.plugin.getPrefix() + "Could not find item with ID '" + typeid[1].toUpperCase().replace("-", "_").replace(" ", "_") + "'.");
return false;
}
return true;
}
if (s.contains(":")) {
String[] matmeta = s.split("\\:");
if (input.contains(":")) {
String[] matmeta = input.split("\\:");
if (matmeta.length != 2) {
player.sendMessage("Invalid format.");
player.sendMessage(MMOItems.plugin.getPrefix() + "Invalid format.");
return false;
}
try {
Material.valueOf(matmeta[0]);
} catch (Exception e) {
player.sendMessage("'" + matmeta[0] + "' isn't a valid Material.");
Material.valueOf(matmeta[0].toUpperCase().replace("-", "_"));
} catch (IllegalArgumentException exception) {
player.sendMessage(MMOItems.plugin.getPrefix() + "'" + matmeta[0].toUpperCase().replace("-", "_") + "' isn't a valid material.");
return false;
}
try {
Integer.parseInt(matmeta[1]);
} catch (NumberFormatException e) {
player.sendMessage("'" + matmeta[1] + "' isn't a valid number.");
return false;
} catch (Exception e) {
player.sendMessage("Invalid format.");
} catch (NumberFormatException exception) {
player.sendMessage(MMOItems.plugin.getPrefix() + "'" + matmeta[1] + "' isn't a valid number.");
return false;
}
return true;
}
try {
Material.valueOf(s);
Material.valueOf(input.toUpperCase().replace("-", "_"));
} catch (Exception e) {
player.sendMessage("'" + s + "' isn't a valid Material.");
player.sendMessage(MMOItems.plugin.getPrefix() + "'" + input.toUpperCase().replace("-", "_") + "' isn't a valid material.");
return false;
}