diff options
author | David <david@panic.tk> | 2018-11-07 23:50:06 +0100 |
---|---|---|
committer | David <david@panic.tk> | 2018-11-07 23:50:06 +0100 |
commit | 604cf01967ede98bf5024e4926bb0777fc4e8eee (patch) | |
tree | e2fa63d7e683769ee3bf3eddc75280648e92eb04 /src/main/java/com/redstoner/utils | |
parent | e86c52ef7c0e1e33c6af0e8674b038976bec11cc (diff) |
Converted Modules to gradle
Diffstat (limited to 'src/main/java/com/redstoner/utils')
4 files changed, 336 insertions, 0 deletions
diff --git a/src/main/java/com/redstoner/utils/CommandException.java b/src/main/java/com/redstoner/utils/CommandException.java new file mode 100644 index 0000000..0b12125 --- /dev/null +++ b/src/main/java/com/redstoner/utils/CommandException.java @@ -0,0 +1,29 @@ +package com.redstoner.utils; + +public class CommandException extends Exception +{ + private static final long serialVersionUID = -7176634557736106754L; + + public CommandException(String message, Throwable cause) + { + super(message, cause); + } + + public CommandException(Throwable cause) + { + super(cause); + } + + public CommandException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) + { + super(message, cause, enableSuppression, writableStackTrace); + } + + public CommandException() + {} + + public CommandException(String message) + { + super(message); + } +} diff --git a/src/main/java/com/redstoner/utils/CommandMap.java b/src/main/java/com/redstoner/utils/CommandMap.java new file mode 100644 index 0000000..4003b01 --- /dev/null +++ b/src/main/java/com/redstoner/utils/CommandMap.java @@ -0,0 +1,23 @@ +package com.redstoner.utils; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.plugin.SimplePluginManager; + +public class CommandMap +{ + @SuppressWarnings("unchecked") + public static Map<String, Command> getCommandMap() throws ReflectiveOperationException, ClassCastException + { + Field field = SimplePluginManager.class.getDeclaredField("commandMap"); + field.setAccessible(true); + Object map = field.get(Bukkit.getPluginManager()); + field = SimpleCommandMap.class.getDeclaredField("knownCommands"); + field.setAccessible(true); + return (Map<String, Command>) field.get(map); + } +} diff --git a/src/main/java/com/redstoner/utils/ItemProperties.java b/src/main/java/com/redstoner/utils/ItemProperties.java new file mode 100644 index 0000000..cddd0a4 --- /dev/null +++ b/src/main/java/com/redstoner/utils/ItemProperties.java @@ -0,0 +1,272 @@ +package com.redstoner.utils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.BiConsumer; + +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +/** Save and load {@link ItemStack} in json format + * Any additional NBT data not included by {@link ItemMeta} is discarded. */ +public class ItemProperties +{ + private int id = 0; + private byte data = 0; + private int amount = 1; + private Map<Enchantment, Integer> enchantments; + private List<String> lore; + private String displayName; + private boolean unbreakable = false; + + public ItemProperties() + {} + + @SuppressWarnings("deprecation") + public ItemProperties(ItemStack item) + { + if (item == null) + return; + id = item.getTypeId(); + data = item.getData().getData(); + amount = item.getAmount(); + enchantments = new HashMap<>(); + ItemMeta meta = item.getItemMeta(); + if (meta == null) + return; + if (meta.hasEnchants()) + { + enchantments.putAll(meta.getEnchants()); + } + if (meta.hasLore()) + { + lore = meta.getLore(); + } + if (meta.hasDisplayName()) + { + displayName = meta.getDisplayName(); + } + unbreakable = meta.isUnbreakable(); + } + + @SuppressWarnings("deprecation") + public ItemStack toItemStack() + { + ItemStack result = new ItemStack(id, amount, data); + ItemMeta meta = result.getItemMeta(); + if (meta == null) + return result; + if (enchantments != null) + { + enchantments.forEach(new BiConsumer<Enchantment, Integer>() + { + @Override + public void accept(Enchantment ench, Integer level) + { + meta.addEnchant(ench, level, true); + } + }); + } + if (lore != null) + { + meta.setLore(lore); + } + if (displayName != null) + { + meta.setDisplayName(displayName); + } + meta.setUnbreakable(unbreakable); + result.setItemMeta(meta); + return result; + } + + @SuppressWarnings("unchecked") + public JSONObject toJSONObject() + { + JSONObject object = new JSONObject(); + object.put("id", id + ""); + object.put("data", data + ""); + object.put("amount", amount + ""); + if (displayName != null) + { + object.put("displayName", displayName); + } + if (enchantments != null) + { + Map<Enchantment, Integer> enchantments = this.enchantments; + JSONObject stringKeys = new JSONObject(); + for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) + { + stringKeys.put(entry.getKey().getName(), entry.getValue()); + } + object.put("enchantments", stringKeys); + } + if (lore != null) + { + object.put("lore", JSONArray.toJSONString(lore)); + } + if (unbreakable) + { + object.put("unbreakable", true); + } + return object; + } + + @Override + public String toString() + { + return toJSONObject().toString(); + } + + @SuppressWarnings("unchecked") + public ItemProperties loadFrom(JSONObject object) + { + for (Object obj : object.entrySet()) + { + Entry<String, Object> entry = (Entry<String, Object>) obj; + final String key = entry.getKey(); + switch (key) + { + case "id": + id = Integer.parseInt((String) entry.getValue()); + break; + case "data": + data = Byte.parseByte((String) entry.getValue()); + break; + case "amount": + amount = Integer.parseInt((String) entry.getValue()); + break; + case "unbreakable": + unbreakable = (boolean) entry.getValue(); + break; + case "enchantments": + { + if (enchantments == null) + { + enchantments = new HashMap<>(); + } + else if (!enchantments.isEmpty()) + { + enchantments.clear(); + } + JSONObject read = (JSONObject) entry.getValue(); + if (read != null) + { + for (Object obj2 : read.entrySet()) + { + Entry<String, Integer> entry2 = (Entry<String, Integer>) obj2; + Enchantment ench = Enchantment.getByName(entry2.getKey()); + if (ench != null) + { + enchantments.put(ench, entry2.getValue()); + } + } + } + break; + } + case "lore": + JSONParser parser = new JSONParser(); + Object rawObject; + try + { + rawObject = parser.parse((String) entry.getValue()); + } + catch (ParseException e) + { + rawObject = new JSONArray(); + } + JSONArray jsonArray = (JSONArray) rawObject; + lore = jsonArray; + break; + case "displayName": + displayName = (String) entry.getValue(); + default: + } + } + return this; + } + + public int getId() + { + return id; + } + + public byte getData() + { + return data; + } + + public int getAmount() + { + return amount; + } + + public Map<Enchantment, Integer> getEnchantments() + { + return enchantments; + } + + public List<String> getLore() + { + return lore; + } + + public String getDisplayName() + { + return displayName; + } + + public boolean isUnbreakable() + { + return unbreakable; + } + + public ItemProperties setId(int id) + { + this.id = id; + return this; + } + + public ItemProperties setData(byte data) + { + this.data = data; + return this; + } + + public ItemProperties setAmount(int amount) + { + this.amount = amount; + return this; + } + + public ItemProperties setEnchantments(Map<Enchantment, Integer> enchantments) + { + this.enchantments = enchantments; + return this; + } + + public ItemProperties setLore(List<String> lore) + { + this.lore = lore; + return this; + } + + public ItemProperties setDisplayName(String displayName) + { + this.displayName = displayName; + return this; + } + + public ItemProperties setUnbreakable(boolean unbreakable) + { + this.unbreakable = unbreakable; + return this; + } +} diff --git a/src/main/java/com/redstoner/utils/ThrowingSupplier.java b/src/main/java/com/redstoner/utils/ThrowingSupplier.java new file mode 100644 index 0000000..986746b --- /dev/null +++ b/src/main/java/com/redstoner/utils/ThrowingSupplier.java @@ -0,0 +1,12 @@ +package com.redstoner.utils; + +/** + * A supplier with a throws declaration. + * Once again, I have more solid alternatives, but if you want it in your utils... be my guest :D + * + * @param <T> The type of object computed by this supplier. + */ +@FunctionalInterface +public interface ThrowingSupplier<T> { + T get() throws Throwable; +} |