Skip to content

Commit afbcac0

Browse files
committed
crafting recipes, attributes command
1 parent 2c824f7 commit afbcac0

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>project</groupId>
44
<artifactId>items</artifactId>
5-
<version>1.4.0</version>
5+
<version>1.5.0</version>
66
<name>BendingItems</name>
77

88
<repositories>

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.logging.Logger;
910
import java.util.stream.Collectors;
1011

12+
import org.bukkit.Bukkit;
1113
import org.bukkit.Material;
1214
import org.bukkit.NamespacedKey;
1315
import org.bukkit.configuration.ConfigurationSection;
@@ -17,6 +19,9 @@
1719
import org.bukkit.entity.Player;
1820
import org.bukkit.inventory.ItemFlag;
1921
import org.bukkit.inventory.ItemStack;
22+
import org.bukkit.inventory.Recipe;
23+
import org.bukkit.inventory.ShapedRecipe;
24+
import org.bukkit.inventory.ShapelessRecipe;
2025
import org.bukkit.inventory.meta.Damageable;
2126
import org.bukkit.inventory.meta.ItemMeta;
2227
import org.bukkit.persistence.PersistentDataType;
@@ -37,6 +42,7 @@ private ItemManager() {}
3742
private static final Map<String, BendingItem> NAME_CACHE = new HashMap<>();
3843
private static final Map<Integer, BendingItem> ID_CACHE = new HashMap<>();
3944
private static final Map<Player, BendingItem> EQUIPPED = new HashMap<>();
45+
private static final char[] CHARS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
4046

4147
private static final String DISPLAY_PATH = "Display";
4248
private static final String LORE_PATH = "Lore";
@@ -48,6 +54,7 @@ private ItemManager() {}
4854
private static final String ENCHANTS_PATH = "Enchants";
4955
private static final String FLAGS_PATH = "Flags";
5056
private static final String USES_PATH = "Uses";
57+
private static final String RECIPE_PATH = "Recipe";
5158

5259
public static void equip(Player player, BendingItem item) {
5360
EQUIPPED.put(player, item);
@@ -282,12 +289,83 @@ public static BendingItem register(File file) throws IllegalArgumentException {
282289
}
283290
}
284291

292+
if (config.contains(RECIPE_PATH)) {
293+
Recipe recipe = null;
294+
try {
295+
recipe = loadRecipe(name, item, config);
296+
} catch (Exception e) {
297+
e.printStackTrace();
298+
recipe = null;
299+
}
300+
Bukkit.addRecipe(recipe);
301+
}
302+
285303
BendingItem bItem = new BendingItem(name, item, usage, element, mods);
286304
NAME_CACHE.put(name, bItem);
287305
ID_CACHE.put(id, bItem);
288306
return bItem;
289307
}
290308

309+
private static Recipe loadRecipe(String name, ItemStack item, FileConfiguration config) {
310+
Logger logger = JavaPlugin.getPlugin(ItemsPlugin.class).getLogger();
311+
if (!config.contains(RECIPE_PATH + ".Ingredients")) {
312+
logger.warning("Recipe for '" + name + "' requires a list of ingredients under the config path 'Recipe.Ingredients'");
313+
return null;
314+
} else if (!config.contains(RECIPE_PATH + ".Shaped")) {
315+
logger.warning("Recipe for '" + name + "' requires a boolean (true / false) under the config path 'Recipe.Shaped'");
316+
return null;
317+
}
318+
319+
boolean shaped = config.getBoolean(RECIPE_PATH + ".Shaped");
320+
List<String> ingredients = config.getStringList(RECIPE_PATH + ".Ingredients");
321+
322+
if (ingredients == null) {
323+
logger.warning("Ingredients list for '" + name + "' recipe not found!");
324+
return null;
325+
} else if (ingredients.isEmpty()) {
326+
logger.warning("Ingredients list for '" + name + "' recipe cannot be empty!");
327+
return null;
328+
} else if (ingredients.size() > 9) {
329+
logger.warning("Ingredients list for '" + name + "' recipe cannot be longer than 9 items!");
330+
return null;
331+
}
332+
333+
List<Material> mats = new ArrayList<>();
334+
for (String mat : ingredients) {
335+
try {
336+
mats.add(Material.valueOf(mat.toUpperCase()));
337+
} catch (Exception e) {
338+
logger.warning("Unable to parse material from '" + mat + "' in '" + name + "' recipe!");
339+
return null;
340+
}
341+
}
342+
343+
if (shaped) {
344+
if (!config.contains(RECIPE_PATH + ".Shape")) {
345+
logger.warning("Recipe for '" + name + "' requires a shape under the config path 'Recipe.Shape'");
346+
return null;
347+
}
348+
349+
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item);
350+
351+
recipe.shape(config.getStringList(RECIPE_PATH + ".Shape").toArray(new String[0]));
352+
353+
for (int i = 0; i < ingredients.size(); ++i) {
354+
recipe.setIngredient(CHARS[i], mats.get(i));
355+
}
356+
357+
return recipe;
358+
} else {
359+
ShapelessRecipe recipe = new ShapelessRecipe(new NamespacedKey(JavaPlugin.getPlugin(ItemsPlugin.class), name), item);
360+
361+
for (Material ingredient : mats) {
362+
recipe.addIngredient(ingredient);
363+
}
364+
365+
return recipe;
366+
}
367+
}
368+
291369
private static List<BendingModifier> loadMods(ConfigurationSection section) {
292370
List<BendingModifier> mods = new ArrayList<>();
293371

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.event.EventHandler;
55
import org.bukkit.event.EventPriority;
66
import org.bukkit.event.Listener;
7+
import org.bukkit.event.inventory.CraftItemEvent;
78
import org.bukkit.event.player.PlayerItemHeldEvent;
89
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
910
import org.bukkit.inventory.ItemStack;
@@ -12,6 +13,9 @@
1213
import com.projectkorra.projectkorra.ability.CoreAbility;
1314
import com.projectkorra.projectkorra.event.AbilityStartEvent;
1415

16+
import me.simplicitee.project.items.command.AttributesCommand;
17+
import me.simplicitee.project.items.command.ItemCommand;
18+
1519
public class ItemsPlugin extends JavaPlugin implements Listener {
1620

1721
@Override
@@ -24,6 +28,7 @@ public void onEnable() {
2428

2529
// register command
2630
new ItemCommand();
31+
new AttributesCommand();
2732
}
2833

2934
@Override
@@ -56,4 +61,13 @@ public void onSlotChange(PlayerItemHeldEvent event) {
5661
public void onOffhand(PlayerSwapHandItemsEvent event) {
5762
event.setCancelled(ItemManager.equipped(event.getPlayer()));
5863
}
64+
65+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
66+
public void onCraft(CraftItemEvent event) {
67+
BendingItem item = ItemManager.get(event.getInventory().getResult());
68+
69+
if (item != null) {
70+
event.getInventory().setResult(item.newStack());
71+
}
72+
}
5973
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.simplicitee.project.items.command;
2+
3+
import java.lang.reflect.Field;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.bukkit.command.CommandSender;
8+
9+
import com.projectkorra.projectkorra.ability.CoreAbility;
10+
import com.projectkorra.projectkorra.attribute.Attribute;
11+
import com.projectkorra.projectkorra.command.PKCommand;
12+
13+
import net.md_5.bungee.api.ChatColor;
14+
15+
public class AttributesCommand extends PKCommand {
16+
17+
public AttributesCommand() {
18+
super("attributes", "/bending attributes <ability>", "Lists out an ability's attributes, if any are present", new String[] {"attributes", "attr", "attribute", "attrs"});
19+
}
20+
21+
@Override
22+
public void execute(CommandSender sender, List<String> args) {
23+
if (!correctLength(sender, args.size(), 1, 1)) {
24+
return;
25+
}
26+
27+
CoreAbility ability = CoreAbility.getAbility(args.get(0));
28+
if (ability == null) {
29+
sender.sendMessage(ChatColor.RED + "Unknown ability!");
30+
return;
31+
}
32+
33+
List<String> attrs = new ArrayList<>();
34+
for (Field field : ability.getClass().getDeclaredFields()) {
35+
if (field.isAnnotationPresent(Attribute.class)) {
36+
attrs.add(ChatColor.GOLD + field.getAnnotation(Attribute.class).value());
37+
}
38+
}
39+
40+
if (attrs.isEmpty()) {
41+
sender.sendMessage(ability.getName() + ChatColor.RESET + " has no attributes!");
42+
} else {
43+
sender.sendMessage(String.join(ChatColor.RESET + ", ", attrs));
44+
}
45+
}
46+
47+
}

src/me/simplicitee/project/items/ItemCommand.java renamed to src/me/simplicitee/project/items/command/ItemCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.simplicitee.project.items;
1+
package me.simplicitee.project.items.command;
22

33
import java.util.ArrayList;
44
import java.util.List;
@@ -10,6 +10,8 @@
1010

1111
import com.projectkorra.projectkorra.command.PKCommand;
1212

13+
import me.simplicitee.project.items.BendingItem;
14+
import me.simplicitee.project.items.ItemManager;
1315
import me.simplicitee.project.items.BendingItem.Usage;
1416
import me.simplicitee.project.items.gui.ItemGui;
1517
import net.md_5.bungee.api.ChatColor;

src/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: BendingItems
22
author: Simplicitee
33
api-version: 1.16
4-
version: 1.4.0
4+
version: 1.5.0
55
main: me.simplicitee.project.items.ItemsPlugin
66
depend: [ProjectKorra]
77
permissions:

0 commit comments

Comments
 (0)