From 5177c6e3d371017b59ffc679d4d0d7b32ef277ec Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Fri, 14 Apr 2023 22:17:16 -0700 Subject: [PATCH] commit --- .../ogredupealias/commands/CmdExHandler.java | 14 +++ .../commands/TabComplBuilder.java | 60 +++++++++++++ .../ogredupealias/data/ItemBuilder.java | 85 +++++++++++++++++++ .../ogredupealias/utils/ArrayUtils.java | 22 +++++ .../itzispyder/ogredupealias/utils/Text.java | 83 ++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/commands/CmdExHandler.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/commands/TabComplBuilder.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/data/ItemBuilder.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/ArrayUtils.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java diff --git a/src/main/java/io/github/itzispyder/ogredupealias/commands/CmdExHandler.java b/src/main/java/io/github/itzispyder/ogredupealias/commands/CmdExHandler.java new file mode 100644 index 0000000..5a05bce --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/commands/CmdExHandler.java @@ -0,0 +1,14 @@ +package io.github.itzispyder.ogredupealias.commands; + +import org.bukkit.command.Command; + +public record CmdExHandler(Exception ex, Command cmd) { + + public String getHelp() { + String help = "§cError: §7"; + if (ex instanceof IndexOutOfBoundsException) help += "Unknown or incomplete command."; + else if (ex instanceof NullPointerException) help += "Command contains a null value."; + else help += ex.getMessage(); + return help + "\n§cCaused by: §7" + ex.getClass().getSimpleName() + "\n" + "§cUsage: §7" + cmd.getUsage(); + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/commands/TabComplBuilder.java b/src/main/java/io/github/itzispyder/ogredupealias/commands/TabComplBuilder.java new file mode 100644 index 0000000..85b8b46 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/commands/TabComplBuilder.java @@ -0,0 +1,60 @@ +package io.github.itzispyder.ogredupealias.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.util.*; + +public class TabComplBuilder { + + private Map> entries = new HashMap<>(); + private final CommandSender sender; + private final Command command; + private final String alias; + private final String[] args; + + public TabComplBuilder(CommandSender sender, Command command, String alias, String[] args) { + this.sender = sender; + this.command = command; + this.alias = alias; + this.args = args; + } + + /** + * Adds to the tab completion + * @param atIndex should be a number above or equal to 1 + * @param entry string array + * @param condition condition + */ + public TabComplBuilder add(int atIndex, String[] entry, boolean condition) { + if (condition) add(atIndex,entry); + return this; + } + + /** + * Adds to the tab completion + * @param atIndex should be a number above or equal to 1 + * @param entry string array + */ + public TabComplBuilder add(int atIndex, String[] entry) { + atIndex = Math.max(1,atIndex); + entries.put(atIndex,Arrays.stream(entry).toList()); + return this; + } + + public TabComplBuilder add(int atIndex, List entry, boolean condition) { + if (condition) add(atIndex,entry); + return this; + } + + public TabComplBuilder add(int atIndex, List entry) { + entries.put(atIndex,entry); + return this; + } + + public List build() { + List list = new ArrayList<>(entries.get(args.length) != null ? entries.get(args.length) : new ArrayList<>()); + list.removeIf(s -> !s.toLowerCase().contains(args[args.length - 1].toLowerCase())); + return list; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/ItemBuilder.java b/src/main/java/io/github/itzispyder/ogredupealias/data/ItemBuilder.java new file mode 100644 index 0000000..e0e7576 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/ItemBuilder.java @@ -0,0 +1,85 @@ +package io.github.itzispyder.ogredupealias.data; + +import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class ItemBuilder { + + private final ItemStack stack; + private final ItemMeta meta; + + public ItemBuilder() { + this(new ItemStack(Material.STONE)); + } + + public ItemBuilder(ItemStack stack) { + this.stack = stack; + this.meta = this.stack.getItemMeta(); + } + + public ItemBuilder material(Material m) { + stack.setType(m); + return this; + } + + public ItemBuilder name(String s) { + meta.setDisplayName(s); + return this; + } + + public ItemBuilder lore(String s) { + List lore = meta.hasLore() ? meta.getLore() : new ArrayList<>(); + lore.add(s); + meta.setLore(lore); + return this; + } + + public ItemBuilder lore(Iterable s) { + s.forEach(this::lore); + return this; + } + + public ItemBuilder count(int c) { + stack.setAmount(c); + return this; + } + + public ItemBuilder enchant(Enchantment e, int level) { + meta.addEnchant(e,level,true); + return this; + } + + public ItemBuilder flag(ItemFlag... f) { + meta.addItemFlags(f); + return this; + } + + public ItemBuilder attribute(Attribute a, AttributeModifier am) { + meta.addAttributeModifier(a,am); + return this; + } + + public ItemBuilder unbreakable(boolean b) { + meta.setUnbreakable(b); + return this; + } + + public ItemBuilder customModelData(int d) { + meta.setCustomModelData(d); + return this; + } + + public ItemStack build() { + stack.setItemMeta(meta); + return stack; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/ArrayUtils.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/ArrayUtils.java new file mode 100644 index 0000000..daa70dc --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/ArrayUtils.java @@ -0,0 +1,22 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public abstract class ArrayUtils { + + /** + * Transforms an array to another one + * @param e iterable list + * @param a action + * @return new transformed list + * @param input + * @param output + */ + public static List toNewList(Iterable e, Function a) { + List list = new ArrayList<>(); + e.forEach(i -> list.add(a.apply(i))); + return list; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java new file mode 100644 index 0000000..881cefd --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java @@ -0,0 +1,83 @@ +package io.github.itzispyder.ogredupealias.utils; + +import static io.github.itzispyder.enderpv.EnderPV.prefix; + +public class Text { + + public static String of(String s) { + return s; + } + + /** + * Replaces all & with § to color the text + * @param s string + * @return result + */ + public static String color(String s) { + return s.replaceAll("&","§"); + } + + public static String asPath(String s) { + return s.toLowerCase() + .replaceAll(" ","_") + .replaceAll("[^.a-b0-9_-]","") + .trim(); + } + + public static String asDirectory(String s) { + return s.toLowerCase() + .replaceAll(" ","_") + .replaceAll("[^./a-b0-9_-]","") + .trim(); + } + + public static String prefixed(String s) { + return prefix + s; + } + + public static TextBuilder builder(String s) { + return new TextBuilder(s); + } + + public static TextBuilder builder() { + return builder(""); + } + + public static class TextBuilder { + + private String s; + + public TextBuilder(String s) { + this.s = s; + } + + public TextBuilder message(String s) { + this.s = s; + return this; + } + + public TextBuilder color() { + s = Text.color(s); + return this; + } + + public TextBuilder prefix() { + s = Text.prefixed(s); + return this; + } + + public TextBuilder asPath() { + s = Text.asPath(s); + return this; + } + + public TextBuilder asDirectory() { + s = Text.asDirectory(s); + return this; + } + + public String build() { + return s; + } + } +}