From a79c9bbcd0335fb657f63eefdad6454e5c346e89 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sun, 16 Aug 2020 15:29:50 +0200 Subject: [PATCH] !Fixed crafting recipes (still don't work properly tho) --- .../java/net/Indyuce/mmoitems/MMOItems.java | 18 ++--- .../api/recipe/workbench/CustomRecipe.java | 30 ++++---- .../mmoitems/manager/AbilityManager.java | 6 +- .../mmoitems/manager/RecipeManager.java | 71 +++++++------------ 4 files changed, 48 insertions(+), 77 deletions(-) diff --git a/src/main/java/net/Indyuce/mmoitems/MMOItems.java b/src/main/java/net/Indyuce/mmoitems/MMOItems.java index 6ca70a55..3d6cca86 100644 --- a/src/main/java/net/Indyuce/mmoitems/MMOItems.java +++ b/src/main/java/net/Indyuce/mmoitems/MMOItems.java @@ -22,9 +22,8 @@ import net.Indyuce.mmoitems.api.SoulboundInfo; import net.Indyuce.mmoitems.api.Type; import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem; import net.Indyuce.mmoitems.api.player.PlayerData; -import net.Indyuce.mmoitems.command.MMOItemsCommand; +import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot; import net.Indyuce.mmoitems.command.UpdateItemCommand; -import net.Indyuce.mmoitems.command.completion.MMOItemsCompletion; import net.Indyuce.mmoitems.command.completion.UpdateItemCompletion; import net.Indyuce.mmoitems.comp.AdvancedEnchantmentsHook; import net.Indyuce.mmoitems.comp.MMOItemsMetrics; @@ -96,13 +95,13 @@ public class MMOItems extends JavaPlugin { private final TypeManager typeManager = new TypeManager(); private final TemplateManager templateManager = new TemplateManager(); private final ItemManager itemManager = new ItemManager(); + private final RecipeManager recipeManager = new RecipeManager(); private DropTableManager dropTableManager; private WorldGenManager worldGenManager; private UpgradeManager upgradeManager; private UpdaterManager dynamicUpdater; private ConfigManager configManager; - private RecipeManager recipeManager; private BlockManager blockManager; private TierManager tierManager; private StatManager statManager; @@ -275,14 +274,15 @@ public class MMOItems extends JavaPlugin { // advanced recipes getLogger().log(Level.INFO, "Loading recipes, please wait..."); - recipeManager = new RecipeManager(); + recipeManager.loadRecipes(); - // commands - getCommand("mmoitems").setExecutor(new MMOItemsCommand()); + // main command + MMOItemsCommandTreeRoot mmoitemsCommand = new MMOItemsCommandTreeRoot(); + getCommand("mmoitems").setExecutor(mmoitemsCommand); + getCommand("mmoitems").setTabCompleter(mmoitemsCommand); + + // update item command getCommand("updateitem").setExecutor(new UpdateItemCommand()); - - // tab completion - getCommand("mmoitems").setTabCompleter(new MMOItemsCompletion()); getCommand("updateitem").setTabCompleter(new UpdateItemCompletion()); } diff --git a/src/main/java/net/Indyuce/mmoitems/api/recipe/workbench/CustomRecipe.java b/src/main/java/net/Indyuce/mmoitems/api/recipe/workbench/CustomRecipe.java index 6fccec79..5906438a 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/recipe/workbench/CustomRecipe.java +++ b/src/main/java/net/Indyuce/mmoitems/api/recipe/workbench/CustomRecipe.java @@ -16,8 +16,8 @@ import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.WorkbenchIngredient import net.mmogroup.mmolib.api.item.NBTItem; public class CustomRecipe implements Comparable { - private final boolean shapeless; private final ItemStack output; + private final boolean shapeless; private final Map ingredients = new HashMap<>(9); public CustomRecipe(NBTItem output, List recipe, boolean isShapeless) { @@ -28,20 +28,19 @@ public class CustomRecipe implements Comparable { if (shapeless) { if (recipe.size() != 9) { - MMOItems.plugin.getLogger().warning("Invalid shapeless recipe for '" + output.getType().getId() + "." - + output.getString("MMOITEMS_ITEM_ID") + "'"); + MMOItems.plugin.getLogger() + .warning("Invalid shapeless recipe for '" + output.getType().getId() + "." + output.getString("MMOITEMS_ITEM_ID") + "'"); recipe = Arrays.asList("AIR", "AIR", "AIR", "AIR", "AIR", "AIR", "AIR", "AIR", "AIR"); } for (int i = 0; i < 9; i++) { ItemStack stack = MMOItems.plugin.getRecipes().parseStack(recipe.get(i)); - if (stack == null || stack.getType() == Material.AIR) - continue; - ingredients.put(i, WorkbenchIngredient.getAutomatically(stack)); + if (stack != null && stack.getType() != Material.AIR) + ingredients.put(i, WorkbenchIngredient.getAutomatically(stack)); } } else { if (recipe.size() != 3) { - MMOItems.plugin.getLogger().warning("Invalid shaped recipe for '" + output.getType().getId() + "." - + output.getString("MMOITEMS_ITEM_ID") + "'"); + MMOItems.plugin.getLogger() + .warning("Invalid shaped recipe for '" + output.getType().getId() + "." + output.getString("MMOITEMS_ITEM_ID") + "'"); recipe = Arrays.asList("AIR AIR AIR", "AIR AIR AIR", "AIR AIR AIR"); } for (int i = 0; i < 9; i++) { @@ -50,10 +49,8 @@ public class CustomRecipe implements Comparable { line.add("AIR"); ItemStack stack = MMOItems.plugin.getRecipes().parseStack(line.get(i % 3)); - if (stack == null || stack.getType() == Material.AIR) - ingredients.put(i, new AirIngredient()); - else - ingredients.put(i, WorkbenchIngredient.getAutomatically(stack)); + ingredients.put(i, + stack == null || stack.getType() == Material.AIR ? new AirIngredient() : WorkbenchIngredient.getAutomatically(stack)); } } } @@ -63,13 +60,10 @@ public class CustomRecipe implements Comparable { } public boolean fitsPlayerCrafting() { - boolean check = true; for (int value : ingredients.keySet()) - if (value > 3) { - check = false; - break; - } - return check; + if (value > 3) + return false; + return true; } public boolean isEmpty() { diff --git a/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java b/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java index 69a2986e..1624df33 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java @@ -6,7 +6,6 @@ import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Level; @@ -21,6 +20,7 @@ import net.Indyuce.mmoitems.comp.mythicmobs.MythicMobsAbility; public class AbilityManager { private final Map abilities = new HashMap<>(); + private boolean registration = true; public Ability getAbility(String id) { @@ -35,10 +35,6 @@ public class AbilityManager { return abilities.values(); } - public Set getAbilityKeys() { - return abilities.keySet(); - } - public void registerAbility(Ability ability) { if (!registration) { MMOItems.plugin.getLogger().log(Level.INFO, diff --git a/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java b/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java index 8d11ffd7..687eb140 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java @@ -31,22 +31,27 @@ import net.Indyuce.mmoitems.api.recipe.MMORecipeChoice; import net.Indyuce.mmoitems.api.recipe.workbench.CustomRecipe; public class RecipeManager { + + /** + * Custom recipes which are handled by MMOItems + */ private final Set craftingRecipes = new HashSet<>(); - private final Set loadedRecipes = new HashSet<>(); - public RecipeManager() { - reload(); - } + /** + * Recipes which are handled by the vanilla spigot API. All recipes + * registered here are Keyed + */ + private final Set loadedRecipes = new HashSet<>(); - public void reload() { - clearCustomRecipes(); + public void loadRecipes() { + craftingRecipes.clear(); for (Type type : MMOItems.plugin.getTypes().getAll()) { FileConfiguration config = type.getConfigFile().getConfig(); for (MMOItemTemplate template : MMOItems.plugin.getTemplates().getTemplates(type)) - if (config.contains(template.getId() + ".crafting")) + if (config.contains(template.getId() + ".base.crafting")) try { - ConfigurationSection section = config.getConfigurationSection(template.getId() + ".crafting"); + ConfigurationSection section = config.getConfigurationSection(template.getId() + ".base.crafting"); if (section.contains("shaped")) section.getConfigurationSection("shaped").getKeys(false) @@ -77,14 +82,14 @@ public class RecipeManager { } sortRecipes(); - Bukkit.getScheduler().runTask(MMOItems.plugin, () -> getLoadedRecipes().forEach(recipe -> Bukkit.addRecipe(recipe.getRecipe()))); + Bukkit.getScheduler().runTask(MMOItems.plugin, () -> getLoadedRecipes().forEach(recipe -> Bukkit.addRecipe(recipe))); } public void registerBurningRecipe(BurningRecipeType recipeType, Type type, String id, BurningRecipeInformation info, String recipeId) { NamespacedKey key = getRecipeKey(type, id, recipeType.getPath(), recipeId); Recipe recipe = recipeType.provideRecipe(key, MMOItems.plugin.getItem(type, id), toBukkit(info.getChoice()), info.getExp(), info.getBurnTime()); - registerRecipe(key, recipe); + loadedRecipes.add(recipe); } public void registerShapedRecipe(Type type, String id, List list) { @@ -106,16 +111,12 @@ public class RecipeManager { return choice.isVanilla() ? new RecipeChoice.MaterialChoice(choice.getItem().getType()) : new RecipeChoice.ExactChoice(choice.getItem()); } - public void registerRecipe(NamespacedKey key, Recipe recipe) { - loadedRecipes.add(new LoadedRecipe(key, recipe)); - } - public void registerRecipe(CustomRecipe recipe) { if (!recipe.isEmpty()) craftingRecipes.add(recipe); } - public Set getLoadedRecipes() { + public Set getLoadedRecipes() { return loadedRecipes; } @@ -124,7 +125,7 @@ public class RecipeManager { } public Set getNamespacedKeys() { - return loadedRecipes.stream().map(recipe -> recipe.getKey()).collect(Collectors.toSet()); + return loadedRecipes.stream().map(recipe -> ((Keyed) recipe).getKey()).collect(Collectors.toSet()); } public void sortRecipes() { @@ -134,14 +135,13 @@ public class RecipeManager { craftingRecipes.addAll(temporary.stream().sorted().collect(Collectors.toList())); } - public void clearCustomRecipes() { - craftingRecipes.clear(); - } - public NamespacedKey getRecipeKey(Type type, String id, String recipeType, String number) { return new NamespacedKey(MMOItems.plugin, recipeType + "_" + type.getId() + "_" + id + "_" + number); } + /** + * Unregisters bukkit recipes and loads everything again + */ public void reloadRecipes() { Bukkit.getScheduler().runTask(MMOItems.plugin, () -> { @@ -153,7 +153,7 @@ public class RecipeManager { } loadedRecipes.clear(); - reload(); + loadRecipes(); }); } @@ -228,30 +228,11 @@ public class RecipeManager { Recipe provide(NamespacedKey key, ItemStack result, RecipeChoice source, float experience, int cookTime); } - /* - * used because spigot API does not let us access namespaced key of a Recipe - * instance. - */ - public class LoadedRecipe { - private final Recipe recipe; - private final NamespacedKey key; - - public LoadedRecipe(NamespacedKey key, Recipe recipe) { - this.recipe = recipe; - this.key = key; - } - - public NamespacedKey getKey() { - return key; - } - - public Recipe getRecipe() { - return recipe; - } - } - - /* - * blast furnace, smoker, campfire and furnace recipes have extra parameters + /** + * Used to handle furnace/smoker/campifire/furnace extra crafting recipe + * parameters + * + * @author ASangarin */ public class BurningRecipeInformation { private final MMORecipeChoice choice;