This commit is contained in:
Indyuce 2020-09-26 23:30:21 +02:00
commit d613c781c2
6 changed files with 58 additions and 78 deletions

View File

@ -22,7 +22,6 @@ import net.Indyuce.mmoitems.api.SoulboundInfo;
import net.Indyuce.mmoitems.api.Type; import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem; import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.RecipeBookUtil;
import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot; import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot;
import net.Indyuce.mmoitems.command.UpdateItemCommand; import net.Indyuce.mmoitems.command.UpdateItemCommand;
import net.Indyuce.mmoitems.command.completion.UpdateItemCompletion; import net.Indyuce.mmoitems.command.completion.UpdateItemCompletion;
@ -294,12 +293,20 @@ public class MMOItems extends JavaPlugin {
// compatibility with /reload // compatibility with /reload
Bukkit.getScheduler().runTask(this, () -> Bukkit.getOnlinePlayers().forEach(player -> PlayerData.load(player))); Bukkit.getScheduler().runTask(this, () -> Bukkit.getOnlinePlayers().forEach(player -> PlayerData.load(player)));
if (getConfig().getBoolean("recipes.recipe-amounts")) { boolean book = getConfig().getBoolean("recipes.use-recipe-book");
RecipeBookUtil.enableAmounts(); boolean amounts = getConfig().getBoolean("recipes.recipe-amounts");
Bukkit.getPluginManager().registerEvents(new CraftingListener(), this);
if(book && amounts) {
getLogger().warning("Tried to enable recipe book while amounts are active!");
getLogger().warning("Please use only ONE of these options!");
getLogger().warning("Disabling both options for now...");
book = false;
amounts = false;
} }
if (getConfig().getBoolean("recipes.use-recipe-book"))
RecipeBookUtil.enableBook(); recipeManager.load(book, amounts);
if(amounts)
Bukkit.getPluginManager().registerEvents(new CraftingListener(), this);
// amount and bukkit recipes // amount and bukkit recipes
getLogger().log(Level.INFO, "Loading recipes, please wait..."); getLogger().log(Level.INFO, "Loading recipes, please wait...");

View File

@ -26,7 +26,6 @@ import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.AirIngredient; import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.AirIngredient;
import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.WorkbenchIngredient; import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.WorkbenchIngredient;
import net.Indyuce.mmoitems.api.util.RecipeBookUtil;
import net.Indyuce.mmoitems.stat.data.DoubleData; import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.type.ItemStat; import net.Indyuce.mmoitems.stat.type.ItemStat;
@ -47,7 +46,7 @@ public class CustomRecipe implements Comparable<CustomRecipe> {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
WorkbenchIngredient ingredient = MMOItems.plugin.getRecipes().getWorkbenchIngredient(recipe.get(i)); WorkbenchIngredient ingredient = MMOItems.plugin.getRecipes().getWorkbenchIngredient(recipe.get(i));
// Only add AirIngredients if the amount system is enabled // Only add AirIngredients if the amount system is enabled
if (RecipeBookUtil.isAmounts() || !(ingredient instanceof AirIngredient)) if (MMOItems.plugin.getRecipes().isAmounts() || !(ingredient instanceof AirIngredient))
ingredients.put(i, ingredient); ingredients.put(i, ingredient);
} }
return; return;

View File

@ -1,62 +0,0 @@
package net.Indyuce.mmoitems.api.util;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import net.Indyuce.mmoitems.MMOItems;
import net.mmogroup.mmolib.MMOLib;
public class RecipeBookUtil {
private static boolean amounts = false;
private static boolean enabled = false;
public static void enableAmounts() {
amounts = true;
}
public static void enableBook() {
if(amounts) {
MMOItems.plugin.getLogger().warning("Tried to enable recipe book while amounts are active!");
MMOItems.plugin.getLogger().warning("Please use only ONE of these options!");
return;
}
enabled = true;
}
public static void refresh(Player player) {
if(!enabled) return;
if (MMOLib.plugin.getVersion().isStrictlyHigher(1, 16)) {
for (NamespacedKey key : player.getDiscoveredRecipes())
if (key.getNamespace().equals("mmoitems")
&& !MMOItems.plugin.getRecipes().getNamespacedKeys().contains(key))
player.undiscoverRecipe(key);
for (NamespacedKey recipe : MMOItems.plugin.getRecipes().getNamespacedKeys())
if (!player.hasDiscoveredRecipe(recipe))
player.discoverRecipe(recipe);
return;
}
for (NamespacedKey recipe : MMOItems.plugin.getRecipes().getNamespacedKeys())
player.discoverRecipe(recipe);
}
public static void refreshOnline() {
if(!enabled) return;
for (Player player : Bukkit.getOnlinePlayers())
refresh(player);
}
public static void clear() {
for (NamespacedKey recipe : MMOItems.plugin.getRecipes().getNamespacedKeys())
Bukkit.removeRecipe(recipe);
}
public static boolean isAmounts() {
return amounts;
}
}

View File

@ -47,6 +47,7 @@ public class GetMMOItemObjective extends Objective {
@EventHandler @EventHandler
public void a(CitizenInteractEvent event) { public void a(CitizenInteractEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if(!getQuestProgress().getPlayer().isOnline()) return;
if (player.equals(getQuestProgress().getPlayer().getPlayer()) && event.getNPC().getId() == npcId && player.getInventory().getItemInMainHand() != null) { if (player.equals(getQuestProgress().getPlayer().getPlayer()) && event.getNPC().getId() == npcId && player.getInventory().getItemInMainHand() != null) {
NBTItem item = NBTItem.get(player.getInventory().getItemInMainHand()); NBTItem item = NBTItem.get(player.getInventory().getItemInMainHand());
int amount; int amount;

View File

@ -28,7 +28,6 @@ import net.Indyuce.mmoitems.api.ability.Ability.CastingMode;
import net.Indyuce.mmoitems.api.interaction.util.InteractItem; import net.Indyuce.mmoitems.api.interaction.util.InteractItem;
import net.Indyuce.mmoitems.api.interaction.weapon.Weapon; import net.Indyuce.mmoitems.api.interaction.weapon.Weapon;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.RecipeBookUtil;
import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.DamageType; import net.mmogroup.mmolib.api.DamageType;
import net.mmogroup.mmolib.api.item.NBTItem; import net.mmogroup.mmolib.api.item.NBTItem;
@ -37,7 +36,7 @@ public class PlayerListener implements Listener {
@EventHandler @EventHandler
public void loadPlayerData(PlayerJoinEvent event) { public void loadPlayerData(PlayerJoinEvent event) {
RecipeBookUtil.refresh(event.getPlayer()); MMOItems.plugin.getRecipes().refreshRecipeBook(event.getPlayer());
PlayerData.load(event.getPlayer()); PlayerData.load(event.getPlayer());
} }

View File

@ -13,6 +13,7 @@ import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.BlastingRecipe; import org.bukkit.inventory.BlastingRecipe;
import org.bukkit.inventory.CampfireRecipe; import org.bukkit.inventory.CampfireRecipe;
import org.bukkit.inventory.FurnaceRecipe; import org.bukkit.inventory.FurnaceRecipe;
@ -29,7 +30,7 @@ import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.AirIngredient;
import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.MMOItemIngredient; import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.MMOItemIngredient;
import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.VanillaIngredient; import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.VanillaIngredient;
import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.WorkbenchIngredient; import net.Indyuce.mmoitems.api.recipe.workbench.ingredients.WorkbenchIngredient;
import net.Indyuce.mmoitems.api.util.RecipeBookUtil; import net.mmogroup.mmolib.MMOLib;
public class RecipeManager { public class RecipeManager {
/** /**
@ -43,6 +44,18 @@ public class RecipeManager {
*/ */
private final Set<Recipe> loadedRecipes = new HashSet<>(); private final Set<Recipe> loadedRecipes = new HashSet<>();
private boolean book = false;
private boolean amounts = false;
public void load(boolean book, boolean amounts) {
this.book = book;
this.amounts = amounts;
}
public boolean isAmounts() {
return amounts;
}
public void loadRecipes() { public void loadRecipes() {
craftingRecipes.clear(); craftingRecipes.clear();
@ -98,7 +111,7 @@ public class RecipeManager {
public void registerShapedRecipe(Type type, String id, List<String> list, String number) { public void registerShapedRecipe(Type type, String id, List<String> list, String number) {
CustomRecipe recipe = new CustomRecipe(type, id, list, false); CustomRecipe recipe = new CustomRecipe(type, id, list, false);
if (RecipeBookUtil.isAmounts()) if (amounts)
registerRecipe(recipe); registerRecipe(recipe);
else else
registerBukkitRecipe(recipe, number); registerBukkitRecipe(recipe, number);
@ -106,7 +119,7 @@ public class RecipeManager {
public void registerShapelessRecipe(Type type, String id, List<String> list, String number) { public void registerShapelessRecipe(Type type, String id, List<String> list, String number) {
CustomRecipe recipe = new CustomRecipe(type, id, list, true); CustomRecipe recipe = new CustomRecipe(type, id, list, true);
if (RecipeBookUtil.isAmounts()) if (amounts)
registerRecipe(recipe); registerRecipe(recipe);
else else
registerBukkitRecipe(recipe, number); registerBukkitRecipe(recipe, number);
@ -152,13 +165,36 @@ public class RecipeManager {
*/ */
public void reloadRecipes() { public void reloadRecipes() {
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> { Bukkit.getScheduler().runTask(MMOItems.plugin, () -> {
RecipeBookUtil.clear(); for (NamespacedKey recipe : getNamespacedKeys())
Bukkit.removeRecipe(recipe);
loadedRecipes.clear(); loadedRecipes.clear();
loadRecipes(); loadRecipes();
RecipeBookUtil.refreshOnline(); if(book)
for (Player player : Bukkit.getOnlinePlayers())
refreshRecipeBook(player);
}); });
} }
public void refreshRecipeBook(Player player) {
if(!book) return;
if (MMOLib.plugin.getVersion().isStrictlyHigher(1, 16)) {
for (NamespacedKey key : player.getDiscoveredRecipes())
if (key.getNamespace().equals("mmoitems")
&& !getNamespacedKeys().contains(key))
player.undiscoverRecipe(key);
for (NamespacedKey recipe : getNamespacedKeys())
if (!player.hasDiscoveredRecipe(recipe))
player.discoverRecipe(recipe);
return;
}
for (NamespacedKey recipe : getNamespacedKeys())
player.discoverRecipe(recipe);
}
public WorkbenchIngredient getWorkbenchIngredient(String input) { public WorkbenchIngredient getWorkbenchIngredient(String input) {
String[] split = input.split("\\:"); String[] split = input.split("\\:");
int amount = split.length > 1 ? Integer.parseInt(split[1]) : 1; int amount = split.length > 1 ? Integer.parseInt(split[1]) : 1;