Skip to content

Commit 02016ce

Browse files
committed
fix reloading
1 parent 920fad0 commit 02016ce

File tree

6 files changed

+62
-40
lines changed

6 files changed

+62
-40
lines changed

src/me/simplicitee/project/items/BendingItem.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import java.util.List;
66
import java.util.Map;
77
import java.util.Map.Entry;
8-
import java.util.concurrent.ThreadLocalRandom;
98

9+
import org.bukkit.configuration.file.FileConfiguration;
1010
import org.bukkit.entity.Player;
1111
import org.bukkit.inventory.ItemStack;
12-
import org.bukkit.inventory.meta.ItemMeta;
1312

1413
import com.projectkorra.projectkorra.Element;
1514
import com.projectkorra.projectkorra.ability.CoreAbility;
@@ -27,13 +26,15 @@ public enum Usage {
2726
private Usage usage;
2827
private Element element;
2928
private Map<String, List<BendingModifier>> mods;
29+
private final FileConfiguration config;
3030

31-
public BendingItem(String name, ItemStack item, Usage usage, Element element, Map<String, List<BendingModifier>> mods) {
31+
public BendingItem(String name, ItemStack item, Usage usage, Element element, Map<String, List<BendingModifier>> mods, FileConfiguration config) {
3232
this.name = name;
3333
this.item = item;
3434
this.usage = usage;
3535
this.element = element;
3636
this.mods = mods;
37+
this.config = config;
3738
}
3839

3940
public String getInternalName() {
@@ -52,14 +53,16 @@ public String getDisplayName() {
5253
return item.getItemMeta().getDisplayName();
5354
}
5455

56+
public ItemStack internal() {
57+
return item;
58+
}
59+
60+
public FileConfiguration config() {
61+
return config;
62+
}
63+
5564
public ItemStack newStack() {
56-
ItemStack stack = item.clone();
57-
ItemMeta meta = stack.getItemMeta();
58-
List<String> lore = meta.getLore();
59-
lore.add(1, randomString());
60-
meta.setLore(lore);
61-
stack.setItemMeta(meta);
62-
return stack;
65+
return item.clone();
6366
}
6467

6568
public List<String> listMods() {
@@ -108,9 +111,4 @@ public void give(Player player) {
108111
public DisplayItem toDisplay() {
109112
return new DisplayItem(item, (p, g) -> give(p));
110113
}
111-
112-
private static String randomString() {
113-
String random = String.valueOf(ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE));
114-
return "�" + String.join("�", random.split(""));
115-
}
116114
}

src/me/simplicitee/project/items/ItemManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import me.simplicitee.project.items.BendingItem.Usage;
2828
import me.simplicitee.project.items.item.ItemData;
29+
import me.simplicitee.project.items.item.RecipeParser;
2930
import net.md_5.bungee.api.ChatColor;
3031

3132
public final class ItemManager {
@@ -245,7 +246,7 @@ public static BendingItem register(File file) throws IllegalArgumentException {
245246
}
246247
}
247248

248-
BendingItem bItem = new BendingItem(name, item, usage, element, mods);
249+
BendingItem bItem = new BendingItem(name, item, usage, element, mods, config);
249250
NAME_CACHE.put(name, bItem);
250251
ID_CACHE.put(id, bItem);
251252
return bItem;
@@ -389,6 +390,10 @@ static void init(ItemsPlugin plugin) {
389390
for (File file : folder.listFiles()) {
390391
register(file);
391392
}
393+
394+
for (BendingItem item : ID_CACHE.values()) {
395+
RecipeParser.apply(item);
396+
}
392397
}
393398

394399
static void disable() {

src/me/simplicitee/project/items/ItemsPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import com.projectkorra.projectkorra.ability.CoreAbility;
1414
import com.projectkorra.projectkorra.event.AbilityStartEvent;
15+
import com.projectkorra.projectkorra.event.BendingReloadEvent;
1516

1617
import me.simplicitee.project.items.command.AttributesCommand;
1718
import me.simplicitee.project.items.command.ItemCommand;
@@ -68,4 +69,10 @@ public void onCraft(CraftItemEvent event) {
6869
event.getInventory().setResult(item.newStack());
6970
}
7071
}
72+
73+
@EventHandler(priority = EventPriority.MONITOR)
74+
public void onPKReload(BendingReloadEvent event) {
75+
new ItemCommand();
76+
new AttributesCommand();
77+
}
7178
}

src/me/simplicitee/project/items/item/ItemData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import me.simplicitee.project.items.item.parser.DurabilityParser;
1010
import me.simplicitee.project.items.item.parser.EnchantsParser;
1111
import me.simplicitee.project.items.item.parser.FlagsParser;
12+
import me.simplicitee.project.items.item.parser.ItemMetaParser;
1213
import me.simplicitee.project.items.item.parser.LoreParser;
1314
import me.simplicitee.project.items.item.parser.ModelParser;
14-
import me.simplicitee.project.items.item.parser.RecipeParser;
1515
import me.simplicitee.project.items.item.parser.UnbreakableParser;
1616
import me.simplicitee.project.items.item.parser.UsesParser;
1717

@@ -22,9 +22,9 @@ public enum ItemData {
2222
DURABILITY (new DurabilityParser()),
2323
ENCHANTS (new EnchantsParser()),
2424
FLAGS (new FlagsParser()),
25+
ITEM_META (new ItemMetaParser()),
2526
LORE (new LoreParser()),
2627
MODEL (new ModelParser()),
27-
RECIPE (new RecipeParser()),
2828
UNBREAKABLE (new UnbreakableParser()),
2929
USES (new UsesParser()),
3030
;

src/me/simplicitee/project/items/item/parser/RecipeParser.java renamed to src/me/simplicitee/project/items/item/RecipeParser.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.simplicitee.project.items.item.parser;
1+
package me.simplicitee.project.items.item;
22

33
import java.util.ArrayList;
44
import java.util.List;
@@ -8,31 +8,30 @@
88
import org.bukkit.Material;
99
import org.bukkit.NamespacedKey;
1010
import org.bukkit.configuration.file.FileConfiguration;
11-
import org.bukkit.inventory.ItemStack;
12-
import org.bukkit.inventory.Recipe;
11+
import org.bukkit.inventory.CraftingRecipe;
12+
import org.bukkit.inventory.RecipeChoice;
1313
import org.bukkit.inventory.ShapedRecipe;
1414
import org.bukkit.inventory.ShapelessRecipe;
15-
import org.bukkit.inventory.meta.ItemMeta;
1615
import org.bukkit.plugin.java.JavaPlugin;
1716

17+
import me.simplicitee.project.items.BendingItem;
18+
import me.simplicitee.project.items.ItemManager;
1819
import me.simplicitee.project.items.ItemsPlugin;
19-
import me.simplicitee.project.items.item.ItemDataParser;
2020

21-
public class RecipeParser implements ItemDataParser {
21+
public final class RecipeParser {
2222

2323
private static final String PATH = "Recipe";
2424
private static final char[] CHARS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
2525

26-
@Override
27-
public String getPath() {
28-
return PATH;
29-
}
30-
31-
@Override
32-
public void apply(String name, ItemStack item, ItemMeta meta, FileConfiguration config) {
26+
private RecipeParser() {}
27+
28+
public static void apply(BendingItem item) {
29+
FileConfiguration config = item.config();
3330
if (!config.contains(PATH)) return;
3431

3532
Logger logger = JavaPlugin.getPlugin(ItemsPlugin.class).getLogger();
33+
String name = item.getInternalName();
34+
3635

3736
if (!config.contains(PATH + ".Ingredients")) {
3837
logger.warning("Recipe for '" + name + "' requires a list of ingredients under the config path 'Recipe.Ingredients'");
@@ -55,38 +54,51 @@ public void apply(String name, ItemStack item, ItemMeta meta, FileConfiguration
5554
logger.warning("Ingredients list for '" + name + "' recipe cannot be longer than 9 items!");
5655
return;
5756
}
58-
59-
List<Material> mats = new ArrayList<>();
57+
List<RecipeChoice> mats = new ArrayList<>();
6058
for (String mat : ingredients) {
59+
if (mat.startsWith("EXACT:")) {
60+
BendingItem bItem = ItemManager.get(mat.substring(6));
61+
if (bItem == item) {
62+
logger.warning("Recipe for '" + name + "' contains self, ignoring recipe.");
63+
return;
64+
}
65+
66+
mats.add(new RecipeChoice.ExactChoice(bItem.internal()));
67+
continue;
68+
}
69+
6170
try {
62-
mats.add(Material.valueOf(mat.toUpperCase()));
71+
mats.add(new RecipeChoice.MaterialChoice(Material.valueOf(mat.toUpperCase())));
6372
} catch (Exception e) {
6473
logger.warning("Unable to parse material from '" + mat + "' in '" + name + "' recipe!");
6574
return;
6675
}
6776
}
6877

69-
Recipe recipe = null;
78+
CraftingRecipe recipe = null;
7079

7180
if (isShaped) {
7281
if (!config.contains(PATH + ".Shape")) {
7382
logger.warning("Recipe for '" + name + "' requires a shape under the config path 'Recipe.Shape'");
7483
return;
7584
}
7685

77-
ShapedRecipe shaped = new ShapedRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item);
78-
86+
ShapedRecipe shaped = new ShapedRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item.internal());
7987
shaped.shape(config.getStringList(PATH + ".Shape").toArray(new String[0]));
8088

8189
for (int i = 0; i < ingredients.size(); ++i) {
8290
shaped.setIngredient(CHARS[i], mats.get(i));
8391
}
92+
93+
recipe = shaped;
8494
} else {
85-
ShapelessRecipe shapeless = new ShapelessRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item);
95+
ShapelessRecipe shapeless = new ShapelessRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item.internal());
8696

87-
for (Material ingredient : mats) {
97+
for (RecipeChoice ingredient : mats) {
8898
shapeless.addIngredient(ingredient);
8999
}
100+
101+
recipe = shapeless;
90102
}
91103

92104
Bukkit.addRecipe(recipe);

src/me/simplicitee/project/items/item/parser/ItemMetaParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,6 @@ private void parseLeatherArmor(String name, LeatherArmorMeta meta, FileConfigura
305305
return;
306306
}
307307

308-
meta.setColor(Color.fromRGB(rgb));
308+
meta.setColor(Color.fromRGB(rgb));
309309
}
310310
}

0 commit comments

Comments
 (0)