Bug fixed :)))

This commit is contained in:
Roch Blonndiaux 2022-11-07 12:47:36 +01:00
parent 8da95a78ff
commit 1bef5fa21e

View File

@ -18,115 +18,108 @@ import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap; import java.util.stream.Collectors;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
public class CraftingRecipeDisplay extends ConfigItem { public class CraftingRecipeDisplay extends ConfigItem {
public CraftingRecipeDisplay() { public CraftingRecipeDisplay() {
super("CRAFTING_RECIPE_DISPLAY", Material.BARRIER, "&a&lCraft&f #name#", "{conditions}", "{conditions}&8Conditions:", "{crafting_time}", super("CRAFTING_RECIPE_DISPLAY", Material.BARRIER, "&a&lCraft&f #name#", "{conditions}", "{conditions}&8Conditions:", "{crafting_time}",
"{crafting_time}&7Crafting Time: &c#crafting-time#&7s", "", "&8Ingredients:", "#ingredients#", "", "&eLeft-Click to craft!", "{crafting_time}&7Crafting Time: &c#crafting-time#&7s", "", "&8Ingredients:", "#ingredients#", "", "&eLeft-Click to craft!",
"&eRight-Click to preview!"); "&eRight-Click to preview!");
} }
public ItemBuilder newBuilder(CheckedRecipe recipe) { public ItemBuilder newBuilder(CheckedRecipe recipe) {
return new ItemBuilder(recipe); return new ItemBuilder(recipe);
} }
/* /*
* allows to build an unidentified item based on the given NBTItem. * allows to build an unidentified item based on the given NBTItem.
*/ */
public class ItemBuilder { public class ItemBuilder {
private final CheckedRecipe recipe; private final CheckedRecipe recipe;
private final CraftingRecipe craftingRecipe; private final CraftingRecipe craftingRecipe;
private final String name = getName(); private final String name = getName();
private final List<String> lore = new ArrayList<>(getLore()); private final List<String> lore = new ArrayList<>(getLore());
public ItemBuilder(CheckedRecipe recipe) { public ItemBuilder(CheckedRecipe recipe) {
this.recipe = recipe; this.recipe = recipe;
craftingRecipe = (CraftingRecipe) recipe.getRecipe(); craftingRecipe = (CraftingRecipe) recipe.getRecipe();
} }
public ItemStack build() { public ItemStack build() {
Map<String, String> replace = new HashMap<>(); Map<String, String> replace = new HashMap<>();
/* /*
* used to calculate the last index for conditions. if there are no * used to calculate the last index for conditions. if there are no
* conditions, just clean up all {conditions}, otherwise replace all * conditions, just clean up all {conditions}, otherwise replace all
* {conditions} and display conditions at the last index * {conditions} and display conditions at the last index
*/ */
int conditionsIndex = -1; int conditionsIndex = -1;
for (ListIterator<String> iterator = lore.listIterator(); iterator.hasNext();) { for (ListIterator<String> iterator = lore.listIterator(); iterator.hasNext(); ) {
int index = iterator.nextIndex(); int index = iterator.nextIndex();
String str = iterator.next(); String str = iterator.next();
/* /*
* crafting time * crafting time
*/ */
if (str.startsWith("{crafting_time}")) { if (str.startsWith("{crafting_time}")) {
if (craftingRecipe.getCraftingTime() <= 0) { if (craftingRecipe.getCraftingTime() <= 0) {
iterator.remove(); iterator.remove();
continue; continue;
} }
replace.put(str, str.replace("{crafting_time}", "").replace("#crafting-time#", replace.put(str, str.replace("{crafting_time}", "").replace("#crafting-time#",
MythicLib.plugin.getMMOConfig().decimal.format(craftingRecipe.getCraftingTime()))); MythicLib.plugin.getMMOConfig().decimal.format(craftingRecipe.getCraftingTime())));
} }
if (str.startsWith("{conditions}")) { if (str.startsWith("{conditions}")) {
conditionsIndex = index + 1; conditionsIndex = index + 1;
if (recipe.getConditions().size() == 0) if (recipe.getConditions().size() == 0)
iterator.remove(); iterator.remove();
else else
replace.put(str, str.replace("{conditions}", "")); replace.put(str, str.replace("{conditions}", ""));
} }
} }
for (String key : replace.keySet()) for (String key : replace.keySet())
lore.set(lore.indexOf(key), replace.get(key)); lore.set(lore.indexOf(key), replace.get(key));
/* /*
* load ingredients. will register an error if the lore does not * load ingredients. will register an error if the lore does not
* contain that line. users MUST display the ingredients somewhere. * contain that line. users MUST display the ingredients somewhere.
*/ */
int index = lore.indexOf("#ingredients#"); int index = lore.indexOf("#ingredients#");
lore.remove(index); lore.remove(index);
recipe.getIngredients().forEach(info -> lore.add(index, info.format())); recipe.getIngredients().forEach(info -> lore.add(index, info.format()));
if (conditionsIndex >= 0) if (conditionsIndex >= 0)
for (CheckedCondition condition : recipe.getConditions()) { for (CheckedCondition condition : recipe.getConditions()) {
ConditionalDisplay display = condition.getCondition().getDisplay(); ConditionalDisplay display = condition.getCondition().getDisplay();
if (display != null) if (display != null)
// ++ allows to sort displays in the same order as in // ++ allows to sort displays in the same order as in
// the config // the config
lore.add(conditionsIndex++, condition.format()); lore.add(conditionsIndex++, condition.format());
} }
ItemStack item = craftingRecipe.getPreviewItemStack(); ItemStack item = craftingRecipe.getPreviewItemStack();
int amount = craftingRecipe.getOutputAmount(); int amount = craftingRecipe.getOutputAmount();
if (amount > 64) if (amount > 64)
lore.add(0, Message.STATION_BIG_STACK.format(ChatColor.GOLD, "#size#", String.valueOf(amount)).toString()); lore.add(0, Message.STATION_BIG_STACK.format(ChatColor.GOLD, "#size#", String.valueOf(amount)).toString());
else else
item.setAmount(amount); item.setAmount(amount);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.values()); meta.addItemFlags(ItemFlag.values());
item.setItemMeta(meta); meta.setDisplayName(MythicLib.plugin.parseColors(name.replace("#name#", (amount > 1 ? (ChatColor.WHITE + "" + amount + " x ") : "") + MMOUtils.getDisplayName(item))));
meta.setLore(lore.stream()
.map(s -> MythicLib.plugin.parseColors(s))
.collect(Collectors.toList()));
item.setItemMeta(meta);
NBTItem nbtItem = NBTItem.get(item); return NBTItem.get(item).addTag(new ItemTag("recipeId", craftingRecipe.getId())).toItem();
}
nbtItem.setDisplayNameComponent(LegacyComponent.parse(name.replace("#name#", (amount > 1 ? (ChatColor.WHITE + "" + amount + " x ") : "") + MMOUtils.getDisplayName(item)))); }
List<Component> componentLore = new ArrayList<>();
lore.forEach(line -> componentLore.add(LegacyComponent.parse(line)));
nbtItem.setLoreComponents(componentLore);
return nbtItem.addTag(new ItemTag("recipeId", craftingRecipe.getId())).toItem();
}
}
} }