diff --git a/src/main/java/io/github/simplexdev/api/IGUI.java b/src/main/java/io/github/simplexdev/api/IGUI.java index 7aa9f1e..f294979 100644 --- a/src/main/java/io/github/simplexdev/api/IGUI.java +++ b/src/main/java/io/github/simplexdev/api/IGUI.java @@ -1,6 +1,6 @@ package io.github.simplexdev.api; -import io.github.simplexdev.api.func.Action; +import io.github.simplexdev.api.func.ClickAction; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -11,24 +11,79 @@ import org.jetbrains.annotations.Nullable; import java.util.Map; import java.util.UUID; +/** + * An interface which supplies customizable inventory GUI instances. + */ public interface IGUI { + /** + * @return UUID of the inventory provided by the interface instance. + */ UUID getInvUUId(); + /** + * The actual inventory provided by the interface instance. + * @return the inventory which is provided by the interface instance. + */ Inventory getInventory(); - void setItem(int slot, @NotNull ItemStack stack, @Nullable Action action); + /** + * Sets the item for the defined slot to the specified item stack. + * You may define an action to run when the player clicks on the inventory slot, + * however it is not required; you may also pass null to the clickAction parameter. + * @param slot The inventory slot to set; remember that arrays start at 0, so + * an inventory size of 9 will range from 0 to 8. + * @param stack The item to display in the slot. This can be Material.AIR. + * @param clickAction The action to perform when a player clicks the inventory slot. + * This may be null. + */ + void setItem(int slot, @NotNull ItemStack stack, @Nullable ClickAction clickAction); + /** + * Sets the item for the defined slot to the specified item stack. + * This method should automatically define the slot action as null. + * @param slot The inventory slot to set; remember that arrays start at 0, so + * an inventory size of 9 will range from 0 to 8. + * @param stack The item to display in the slot. This can be Material.AIR. + */ void setItem(int slot, @NotNull ItemStack stack); + /** + * Force a player to open the inventory provided by the interface instance. + * @param player The player to open the inventory on. + */ void open(@NotNull Player player); + /** + * Force a player to close the inventory provided by the interface instance. + * @param player The player to close the inventory on. + */ void close(@NotNull Player player); + /** + * Delete the inventory provided by the interface instance. + */ void delete(); - Map getActions(); + /** + * @return A map containing each inventory slot and their respective actions, + * but only if the slot actually has an action. + */ + Map getActions(); + /** + * Creates a new ItemStack instance to place in the inventory provided by the interface instance. + * @param material The item material + * @param name The name of the item + * @param lore Optional item descriptions + * @return The newly created item + */ ItemStack newItem(@NotNull Material material, @NotNull String name, String... lore); + /** + * Creates a new ItemStack instance to place in the inventory provided by the interface instance. + * @param material The item material + * @param name The name of the item + * @return The newly created item with no lore. + */ ItemStack newItem(@NotNull Material material, @NotNull String name); } diff --git a/src/main/java/io/github/simplexdev/api/func/Action.java b/src/main/java/io/github/simplexdev/api/func/Action.java deleted file mode 100644 index 44bfe1f..0000000 --- a/src/main/java/io/github/simplexdev/api/func/Action.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.github.simplexdev.api.func; - -import org.bukkit.entity.Player; - -@FunctionalInterface -public interface Action { - void onClick(Player player); -} diff --git a/src/main/java/io/github/simplexdev/api/func/ClickAction.java b/src/main/java/io/github/simplexdev/api/func/ClickAction.java new file mode 100644 index 0000000..17f3a54 --- /dev/null +++ b/src/main/java/io/github/simplexdev/api/func/ClickAction.java @@ -0,0 +1,17 @@ +package io.github.simplexdev.api.func; + +import org.bukkit.entity.Player; + +/** + * Functional interface which provides an action to execute + * when a player clicks on an {@link io.github.simplexdev.api.IGUI} provided inventory slot. + */ +@FunctionalInterface +public interface ClickAction { + /** + * Returns a void when a player clicks on an + * inventory slot provided by the GUI class for the GUIListener. + * @param player The player who interacted with the inventory slot. + */ + void onClick(Player player); +} diff --git a/src/main/java/io/github/simplexdev/api/func/Validate.java b/src/main/java/io/github/simplexdev/api/func/Validate.java deleted file mode 100644 index 309cf2e..0000000 --- a/src/main/java/io/github/simplexdev/api/func/Validate.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.github.simplexdev.api.func; - -@FunctionalInterface -public interface Validate { - boolean isValid(); -} diff --git a/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java b/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java index 871cefc..ca38693 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java +++ b/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java @@ -1,38 +1,37 @@ package io.github.simplexdev.simplexcore.chat; -import io.github.simplexdev.simplexcore.utils.Wrapper; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.command.CommandSender; public final class ChatUtils { - protected final Wrapper target; + protected final CommandSender target; protected final TextComponentFactory factory = new TextComponentFactory(); - private ChatUtils(Wrapper target) { + private ChatUtils(T target) { this.target = target; } public static ChatUtils target(T target) { - return new ChatUtils(Wrapper.of(target)); + return new ChatUtils(target); } public void msg(String message) { - target.get().sendMessage(message); + target.sendMessage(message); } public void msg(TextComponent component) { - target.get().sendMessage(component); + target.sendMessage(component); } public void err(Messages message) { - target.get().sendMessage(message.getMessage()); + target.sendMessage(message.getMessage()); } public void color(String message) { - target.get().sendMessage(factory.colorize(message)); + target.sendMessage(factory.colorize(message)); } public void color(TextComponent component) { - target.get().sendMessage(factory.colorize(component.getText())); + target.sendMessage(factory.colorize(component.getText())); } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java b/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java index 7072820..fac7c8b 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java +++ b/src/main/java/io/github/simplexdev/simplexcore/command/CommandLoader.java @@ -3,6 +3,7 @@ package io.github.simplexdev.simplexcore.command; import io.github.simplexdev.api.annotations.CommandInfo; import io.github.simplexdev.simplexcore.command.defaults.DefaultCommand; import io.github.simplexdev.simplexcore.utils.Constants; +import io.github.simplexdev.simplexcore.utils.ReflectionTools; import org.bukkit.Bukkit; import org.bukkit.command.*; import org.bukkit.plugin.Plugin; @@ -13,7 +14,6 @@ import org.reflections.Reflections; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.Map; import java.util.MissingResourceException; @@ -52,7 +52,7 @@ public final class CommandLoader { PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name().toLowerCase()); objectToRegister.setAliases(Arrays.asList(info.aliases().split(","))); objectToRegister.setDescription(info.description()); - objectToRegister.setExecutor(getFromSetName(info.name())); + objectToRegister.setExecutor(getExecutorFromName(info.name())); objectToRegister.setLabel(info.name().toLowerCase()); objectToRegister.setPermission(info.permission()); objectToRegister.setPermissionMessage(info.permissionMessage()); @@ -62,7 +62,7 @@ public final class CommandLoader { }); } - public synchronized CommandExecutor getFromSetName(String name) { + public synchronized CommandExecutor getExecutorFromName(String name) { for (Class obj : reflections.getSubTypesOf(CommandExecutor.class)) { if (!obj.isAnnotationPresent(CommandInfo.class)) { throw new RuntimeException("Missing annotation CommandInfo!"); @@ -73,8 +73,9 @@ public final class CommandLoader { if (name.equalsIgnoreCase(info.name())) { try { Constructor constr = obj.getDeclaredConstructor(); + constr.setAccessible(true); return constr.newInstance(); - } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { + } catch (ReflectiveOperationException ignored) { return new DefaultCommand(); } } @@ -93,8 +94,9 @@ public final class CommandLoader { if (name.equalsIgnoreCase(info.name())) { try { Constructor constr = obj.getDeclaredConstructor(); + constr.setAccessible(true); return constr.newInstance(); - } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { + } catch (ReflectiveOperationException ignored) { return new DefaultCommand(); } } @@ -108,40 +110,13 @@ public final class CommandLoader { private static final Field knownCmdsField; static { - Constructor temp; - try { - temp = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); - temp.setAccessible(true); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - constructor = temp; - - Field cmf; - try { - cmf = SimplePluginManager.class.getDeclaredField("commandMap"); - cmf.setAccessible(true); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } - cmdMapField = cmf; - - Field kcf; - try { - kcf = SimpleCommandMap.class.getDeclaredField("knownCommands"); - kcf.setAccessible(true); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } - knownCmdsField = kcf; + constructor = ReflectionTools.getDeclaredConstructor(PluginCommand.class, String.class, Plugin.class); + cmdMapField = ReflectionTools.getDeclaredField(SimplePluginManager.class, "commandMap"); + knownCmdsField = ReflectionTools.getDeclaredField(SimpleCommandMap.class, "knownCommands"); } public static PluginCommand create(@NotNull Plugin plugin, @NotNull String name) { - try { - return constructor.newInstance(name, plugin); - } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { - throw new RuntimeException(e); - } + return ReflectionTools.initConstructor(constructor, name, plugin); } public static void registerCommand(PluginCommand command) { diff --git a/src/main/java/io/github/simplexdev/simplexcore/gui/AbstractGUI.java b/src/main/java/io/github/simplexdev/simplexcore/gui/AbstractGUI.java index be42c38..a0fcd0c 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/gui/AbstractGUI.java +++ b/src/main/java/io/github/simplexdev/simplexcore/gui/AbstractGUI.java @@ -1,6 +1,6 @@ package io.github.simplexdev.simplexcore.gui; -import io.github.simplexdev.api.func.Action; +import io.github.simplexdev.api.func.ClickAction; import io.github.simplexdev.api.IGUI; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -16,7 +16,7 @@ import java.util.*; public abstract class AbstractGUI implements InventoryHolder, IGUI { private final Inventory INV; - private final Map actions; + private final Map actions; private final UUID uuid; private final List validSize = new ArrayList<>(){{ @@ -53,10 +53,10 @@ public abstract class AbstractGUI implements InventoryHolder, IGUI { } @Override - public void setItem(int slot, @NotNull ItemStack stack, @Nullable Action action) { + public void setItem(int slot, @NotNull ItemStack stack, @Nullable ClickAction clickAction) { INV.setItem(slot, stack); - if (action != null) { - actions.put(slot, action); + if (clickAction != null) { + actions.put(slot, clickAction); } } @@ -97,7 +97,7 @@ public abstract class AbstractGUI implements InventoryHolder, IGUI { } @Override - public Map getActions() { + public Map getActions() { return actions; } diff --git a/src/main/java/io/github/simplexdev/simplexcore/gui/GUIHandler.java b/src/main/java/io/github/simplexdev/simplexcore/gui/GUIHandler.java index 0667c4b..e58189e 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/gui/GUIHandler.java +++ b/src/main/java/io/github/simplexdev/simplexcore/gui/GUIHandler.java @@ -1,7 +1,9 @@ package io.github.simplexdev.simplexcore.gui; -import io.github.simplexdev.api.func.Action; +import io.github.simplexdev.api.func.ClickAction; import io.github.simplexdev.api.IGUI; +import io.github.simplexdev.simplexcore.listener.SimplexListener; +import io.github.simplexdev.simplexcore.plugin.SimplexAddon; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -11,7 +13,11 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import java.util.UUID; -public final class GUIHandler implements Listener { +public final class GUIHandler extends SimplexListener { + public GUIHandler(SimplexAddon plugin) { + register(this, plugin); + } + @EventHandler(priority = EventPriority.NORMAL) public void invClick(InventoryClickEvent event) { if (!(event.getWhoClicked() instanceof Player)) { @@ -25,9 +31,9 @@ public final class GUIHandler implements Listener { if (invUUID != null) { event.setCancelled(true); IGUI gui = AbstractGUI.getInvByUUId().get(invUUID); - Action action = gui.getActions().get(event.getSlot()); - if (action != null) { - action.onClick(player); + ClickAction clickAction = gui.getActions().get(event.getSlot()); + if (clickAction != null) { + clickAction.onClick(player); } } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java b/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java index 03f38d5..b504a76 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java +++ b/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java @@ -1,6 +1,5 @@ package io.github.simplexdev.simplexcore.listener; -import io.github.simplexdev.api.func.Validate; import io.github.simplexdev.simplexcore.utils.Constants; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -8,6 +7,7 @@ import org.bukkit.event.server.PluginEnableEvent; import java.util.ArrayList; import java.util.List; +import java.util.function.BooleanSupplier; public final class DependencyListener extends SimplexListener { public List PAPI_NAMES = new ArrayList<>() {{ @@ -30,14 +30,14 @@ public final class DependencyListener extends SimplexListener { @EventHandler(priority = EventPriority.HIGHEST) public void pluginRegister(PluginEnableEvent event) { - Validate temp = () -> PLIB_NAMES.contains(event.getPlugin().getName()); - Validate temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName()); + BooleanSupplier temp = () -> PLIB_NAMES.contains(event.getPlugin().getName()); + BooleanSupplier temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName()); - if (temp.isValid()) { + if (temp.getAsBoolean()) { Constants.getPlugin().getInstances().getDependencyManager().registerProtocolLib(); } - if (temp2.isValid()) { + if (temp2.getAsBoolean()) { Constants.getPlugin().getInstances().getDependencyManager().registerPAPI(); } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java b/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java index 7f499ba..123f9b0 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java @@ -70,6 +70,10 @@ public class SignFactory { @Nullable public IUsableSign addTagsToSign(Sign sign, Object... tags) { if (getSignData() != null) { + if (!SignData.getUnlabeledSigns().containsKey(sign)) { + createNoTag(sign); + } + AbstractSign abs = SignData.getUnlabeledSigns().get(sign); for (Object tag : tags) { if (tag instanceof VoidSupplier) { @@ -126,10 +130,10 @@ public class SignFactory { && SIGN_TYPES.contains(event.getClickedBlock().getType())) { Sign sign = (Sign) event.getClickedBlock(); if (signMap.containsKey(sign)) { - IUsableSign isign = signMap.get(sign); - String tag = isign.signTag(); - if (isign.getLines().get(0).equals(tag)) { - isign.execute(); + IUsableSign iSign = signMap.get(sign); + String tag = iSign.signTag(); + if (iSign.getLines().get(0).equals(tag)) { + iSign.execute(); } } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java b/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java index 146520f..628fe79 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java @@ -1,68 +1,95 @@ package io.github.simplexdev.simplexcore.utils; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.reflections.Reflections; import java.lang.annotation.Annotation; +import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Objects; import java.util.Set; public final class ReflectionTools { - public ReflectionTools() { - } - @NotNull - public Reflections reflect(Class loadFrom) { + public static Reflections reflect(Class loadFrom) { return new Reflections(loadFrom.getName()); } @NotNull - public Set> getAnnotatedClasses(Class loadFrom, Class annotation) { + public static Set> getAnnotatedClasses(Class loadFrom, Class annotation) { return new Reflections(loadFrom.getName()).getTypesAnnotatedWith(annotation); } @Nullable - public Field getField(Class cls, String name) { + public static Field getField(Class cls, String name) { try { - Field field = cls.getField(name); - field.setAccessible(true); - return field; + return asAccessible(cls.getField(name)); } catch (NoSuchFieldException ignored) { return null; } } @Nullable - public Constructor getConstructor(Class cls, String name, Class... initializers) { + public static Field getDeclaredField(Class cls, String name) { try { - return cls.getDeclaredConstructor(initializers); + return asAccessible(cls.getDeclaredField(name)); + } catch (ReflectiveOperationException ignored) { + return null; + } + } + + @Nullable + public static Constructor getConstructor(Class cls, Class... initializers) { + try { + return asAccessible(cls.getConstructor(initializers)); } catch (NoSuchMethodException ignored) { return null; } } @Nullable - public Wrapper initConstructor(Constructor constructor, Object... initializers) { + public static Constructor getDeclaredConstructor(Class cls, Class... initializers) { try { - return Wrapper.of(constructor.newInstance(initializers)); - } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { + return asAccessible(cls.getDeclaredConstructor(initializers)); + } catch (NoSuchMethodException ignored) { return null; } } @Nullable - public Method getMethod(Class clazz, String name, Class... params) { + public static T initConstructor(Constructor constructor, Object... initializers) { try { - return Wrapper.of(clazz.getMethod(name, params)) - .perform(obj -> obj.setAccessible(true)) - .get(); + return constructor.newInstance(initializers); + } catch (ReflectiveOperationException e) { + return null; + } + } + + @Nullable + public static Method getMethod(Class clazz, String name, Class... params) { + try { + return asAccessible(clazz.getMethod(name, params)); } catch (NoSuchMethodException e) { return null; } } + + @Nullable + public static Method getDeclaredMethod(Class clazz, String name, Class... params) { + try { + return asAccessible(clazz.getDeclaredMethod(name, params)); + } catch (ReflectiveOperationException ignored) { + return null; + } + } + + @NotNull + @Contract(pure = true) + public static T asAccessible(T object) { + object.setAccessible(true); + return object; + } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java b/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java deleted file mode 100644 index 7ab9ff7..0000000 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java +++ /dev/null @@ -1,78 +0,0 @@ -package io.github.simplexdev.simplexcore.utils; - -import io.github.simplexdev.api.func.VoidSupplier; -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; - -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; - -public final class Wrapper { - protected T bean; - - private Wrapper(T bean) { - this.bean = bean; - } - - public void set(T bean) { - this.bean = bean; - } - - public T get() { - return bean; - } - - @Override - public int hashCode() { - return new HashCodeBuilder().append(get()).toHashCode(); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Wrapper)) { - return false; - } - - if (obj == this) { - return true; - } - - return new EqualsBuilder().append(((Wrapper) obj).get(), get()).isEquals(); - } - - @Override - public String toString() { - return get().toString(); - } - - public static Wrapper of(T object) { - return new Wrapper<>(object); - } - - public final

P as(Function, P> function) { - return function.apply(this); - } - - public final Wrapper flatMap(Function> function) { - return new Wrapper<>(function.apply(get()).get()); - } - - public final Wrapper map(Function function) { - return new Wrapper<>(function.apply(get())); - } - - public final Wrapper perform(Consumer consumer) { - consumer.accept(get()); - return this; - } - - public final Wrapper then(VoidSupplier supplier) { - supplier.get(); - return this; - } - - public final Wrapper filter(Predicate predicate) { - return predicate.test(get()) ? this : null; - } -}