From 45cf69bf944fefccd9b39833eed5a18a8ef3c6c5 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sat, 27 Feb 2021 17:27:58 -0600 Subject: [PATCH] BLEEDING-EDGE v1.3_04 --- .../io/github/simplexdev/api/IUsableSign.java | 12 +- .../io/github/simplexdev/api/func/Guard.java | 11 -- .../simplexdev/api/func/VoidSupplier.java | 11 ++ .../simplexcore/ban/BanFactory.java | 10 +- .../simplexcore/chat/ChatUtils.java | 8 +- .../listener/DependencyListener.java | 4 +- .../simplexcore/sign/AbstractSign.java | 60 ++++++++ .../simplexcore/sign/SignFactory.java | 130 ++++++++++++++++++ .../simplexcore/utils/ReflectionTools.java | 55 ++++++++ .../simplexcore/utils/Utilities.java | 1 - .../utils/{Bean.java => Wrapper.java} | 8 +- 11 files changed, 277 insertions(+), 33 deletions(-) delete mode 100644 src/main/java/io/github/simplexdev/api/func/Guard.java create mode 100644 src/main/java/io/github/simplexdev/api/func/VoidSupplier.java create mode 100644 src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java create mode 100644 src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java create mode 100644 src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java rename src/main/java/io/github/simplexdev/simplexcore/utils/{Bean.java => Wrapper.java} (78%) diff --git a/src/main/java/io/github/simplexdev/api/IUsableSign.java b/src/main/java/io/github/simplexdev/api/IUsableSign.java index dc85814..997af45 100644 --- a/src/main/java/io/github/simplexdev/api/IUsableSign.java +++ b/src/main/java/io/github/simplexdev/api/IUsableSign.java @@ -2,18 +2,22 @@ package io.github.simplexdev.api; import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.block.Block; +import org.bukkit.block.Sign; + +import java.util.List; public interface IUsableSign { - Block getBlock(); + Sign getSign(); Location getSignLocation(); World getWorld(); - String getSignText(); + List getLines(); - void executeOnInteract(); + boolean canInteract(); + + void execute(); String signTag(); } diff --git a/src/main/java/io/github/simplexdev/api/func/Guard.java b/src/main/java/io/github/simplexdev/api/func/Guard.java deleted file mode 100644 index 03eacd7..0000000 --- a/src/main/java/io/github/simplexdev/api/func/Guard.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.simplexdev.api.func; - -@FunctionalInterface -public interface Guard { - void verify(); - - default Guard after(Guard clause) { - clause.verify(); - return this; - } -} diff --git a/src/main/java/io/github/simplexdev/api/func/VoidSupplier.java b/src/main/java/io/github/simplexdev/api/func/VoidSupplier.java new file mode 100644 index 0000000..9911313 --- /dev/null +++ b/src/main/java/io/github/simplexdev/api/func/VoidSupplier.java @@ -0,0 +1,11 @@ +package io.github.simplexdev.api.func; + +@FunctionalInterface +public interface VoidSupplier { + void get(); + + default VoidSupplier after(VoidSupplier supplier) { + supplier.get(); + return this; + } +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java index 26269fa..0963457 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java @@ -2,7 +2,7 @@ package io.github.simplexdev.simplexcore.ban; import io.github.simplexdev.api.IBan; import io.github.simplexdev.simplexcore.chat.Messages; -import io.github.simplexdev.api.func.Guard; +import io.github.simplexdev.api.func.VoidSupplier; import io.github.simplexdev.simplexcore.utils.Constants; import io.github.simplexdev.simplexcore.utils.Utilities; import org.bukkit.command.CommandSender; @@ -20,7 +20,7 @@ public final class BanFactory { private String banReason; private long banDuration; - private BanFactory(Player player, CommandSender sender, Date banDate, BanType type) { + public BanFactory(Player player, CommandSender sender, Date banDate, BanType type) { this.player = player; this.sender = sender; this.banDate = banDate; @@ -31,10 +31,6 @@ public final class BanFactory { banId = createBanId(); } - public static BanFactory define(Player player, CommandSender sender, Date banDate, BanType type) { - return new BanFactory(player, sender, banDate, type); - } - /** * The values here are optional to define. They are defined by default, and this does not need to be used. * @@ -96,7 +92,7 @@ public final class BanFactory { } - private Guard assignBanDuration() { + private VoidSupplier assignBanDuration() { return () -> { if (type.equals(BanType.PERMANENT)) { banDuration = Constants.getPlugin().getInstances().getTimeValues().YEAR() * 99; 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 2ec8ab4..87dee4a 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java +++ b/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java @@ -1,19 +1,19 @@ package io.github.simplexdev.simplexcore.chat; -import io.github.simplexdev.simplexcore.utils.Bean; +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 Bean target; + protected final Wrapper target; protected final TextComponentFactory factory = new TextComponentFactory(); - private ChatUtils(Bean target) { + private ChatUtils(Wrapper target) { this.target = target; } public static ChatUtils target(T target) { - return new ChatUtils(new Bean<>(target)); + return new ChatUtils(new Wrapper<>(target)); } public void msg(String message) { 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 108062f..03f38d5 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java +++ b/src/main/java/io/github/simplexdev/simplexcore/listener/DependencyListener.java @@ -34,11 +34,11 @@ public final class DependencyListener extends SimplexListener { Validate temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName()); if (temp.isValid()) { - Constants.getDependencyManager().registerProtocolLib(); + Constants.getPlugin().getInstances().getDependencyManager().registerProtocolLib(); } if (temp2.isValid()) { - Constants.getDependencyManager().registerPAPI(); + Constants.getPlugin().getInstances().getDependencyManager().registerPAPI(); } } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java b/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java new file mode 100644 index 0000000..00e4f32 --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java @@ -0,0 +1,60 @@ +package io.github.simplexdev.simplexcore.sign; + +import io.github.simplexdev.api.IUsableSign; +import io.github.simplexdev.api.func.VoidSupplier; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Sign; + +import java.util.Arrays; +import java.util.List; + +public abstract class AbstractSign implements IUsableSign { + protected final Sign sign; + protected final Location location; + protected final World world; + protected final List lines; + + protected String signTag = null; + protected VoidSupplier executeScript = null; + protected boolean canInteract = false; + + protected AbstractSign(Sign sign) { + this.sign = sign; + this.location = sign.getLocation(); + this.world = sign.getWorld(); + this.lines = Arrays.asList(sign.getLines()); + } + + @Override + public Sign getSign() { + return sign; + } + + @Override + public Location getSignLocation() { + return location; + } + + @Override + public World getWorld() { + return world; + } + + @Override + public List getLines() { + return lines; + } + + public void setSignTag(String signTag) { + this.signTag = signTag(); + } + + public void setExecuteScript(VoidSupplier executeScript) { + this.executeScript = executeScript; + } + + public void setCanInteract(boolean canInteract) { + this.canInteract = canInteract; + } +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java b/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java new file mode 100644 index 0000000..7cd5cd1 --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java @@ -0,0 +1,130 @@ +package io.github.simplexdev.simplexcore.sign; + +import io.github.simplexdev.api.IUsableSign; +import io.github.simplexdev.api.func.VoidSupplier; +import io.github.simplexdev.simplexcore.listener.SimplexListener; +import io.github.simplexdev.simplexcore.plugin.SimplexAddon; +import io.github.simplexdev.simplexcore.utils.Constants; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockPlaceEvent; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.Nullable; + +import java.util.*; +import java.util.function.Supplier; + +public class SignFactory { + private static final List SIGN_TYPES = new ArrayList<>() {{ + add(Material.OAK_SIGN); + add(Material.OAK_WALL_SIGN); + add(Material.DARK_OAK_SIGN); + add(Material.DARK_OAK_WALL_SIGN); + add(Material.BIRCH_SIGN); + add(Material.BIRCH_WALL_SIGN); + add(Material.ACACIA_SIGN); + add(Material.ACACIA_WALL_SIGN); + add(Material.CRIMSON_SIGN); + add(Material.CRIMSON_WALL_SIGN); + add(Material.JUNGLE_SIGN); + add(Material.JUNGLE_WALL_SIGN); + add(Material.SPRUCE_SIGN); + add(Material.SPRUCE_WALL_SIGN); + add(Material.WARPED_SIGN); + add(Material.WARPED_WALL_SIGN); + }}; + + private SignData signData = null; + + public void activateBasicSignDataListener() { + signData = new SignData(Constants.getPlugin()); + } + + public SignData getSignData() { + return signData; + } + + public IUsableSign create(Sign sign, boolean canInteract, String tag, VoidSupplier execute) { + return new AbstractSign(sign) { + @Override + public boolean canInteract() { + return canInteract; + } + + @Override + public void execute() { + execute.get(); + } + + @Override + public String signTag() { + return tag; + } + }; + } + + @Nullable + public IUsableSign addTagsToSign(Sign sign, Object... tags) { + if (getSignData() != null) { + AbstractSign abs = SignData.getUnlabeledSigns().get(sign); + for (Object tag : tags) { + if (tag instanceof VoidSupplier) { + abs.setExecuteScript((VoidSupplier)tag); + } + if (tag instanceof String) { + abs.setSignTag((String)tag); + } + if (tag instanceof Boolean) { + abs.setCanInteract((Boolean)tag); + } + } + return abs; + } else { + return null; + } + } + + private AbstractSign createNoTag(Sign sign) { + return new AbstractSign(sign) { + @Override + public boolean canInteract() { + return canInteract; + } + + @Override + public void execute() { + executeScript.get(); + } + + @Override + public String signTag() { + return signTag; + } + }; + } + + private static class SignData extends SimplexListener { + public SignData(SimplexAddon plugin) { + register(this, plugin); + } + + private static final Map signMap = new HashMap<>(); + + @EventHandler + public void blockPlaceEvent(BlockPlaceEvent event) { + if (SIGN_TYPES.contains(event.getBlockPlaced().getType()) + && (event.getBlockPlaced() instanceof Sign)) { + Sign sign = (Sign) event.getBlockPlaced(); + AbstractSign abs = new SignFactory().createNoTag(sign); + signMap.put(sign, abs); + } + } + + public static Map getUnlabeledSigns() { + return signMap; + } + } +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java b/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java new file mode 100644 index 0000000..b49a3ca --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java @@ -0,0 +1,55 @@ +package io.github.simplexdev.simplexcore.utils; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.reflections.Reflections; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.Set; + +public final class ReflectionTools { + public ReflectionTools() { + } + + @NotNull + public Reflections reflect(Class loadFrom) { + return new Reflections(loadFrom.getName()); + } + + @NotNull + public Set> getAnnotatedClasses(Class loadFrom, Class annotation) { + return new Reflections(loadFrom.getName()).getTypesAnnotatedWith(annotation); + } + + @Nullable + public Field getField(Class cls, String name) { + try { + Field field = cls.getField(name); + field.setAccessible(true); + return field; + } catch (NoSuchFieldException ignored) { + return null; + } + } + + @Nullable + public Constructor getConstructor(Class cls, String name, Class... initializers) { + try { + return cls.getDeclaredConstructor(initializers); + } catch (NoSuchMethodException ignored) { + return null; + } + } + + @Nullable + public Wrapper initConstructor(Constructor constructor, Object... initializers) { + try { + return new Wrapper<>(constructor.newInstance(initializers)); + } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { + return null; + } + } +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/Utilities.java b/src/main/java/io/github/simplexdev/simplexcore/utils/Utilities.java index d867a18..5f140c8 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/Utilities.java +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/Utilities.java @@ -1,6 +1,5 @@ package io.github.simplexdev.simplexcore.utils; -import io.github.simplexdev.api.func.Guard; import io.github.simplexdev.simplexcore.ban.BanType; import java.util.ArrayList; diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/Bean.java b/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java similarity index 78% rename from src/main/java/io/github/simplexdev/simplexcore/utils/Bean.java rename to src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java index ac2b8d3..51e2880 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/Bean.java +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java @@ -3,10 +3,10 @@ package io.github.simplexdev.simplexcore.utils; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; -public final class Bean { +public final class Wrapper { protected T bean; - public Bean(T bean) { + public Wrapper(T bean) { this.bean = bean; } @@ -25,7 +25,7 @@ public final class Bean { @Override public boolean equals(Object obj) { - if (!(obj instanceof Bean)) { + if (!(obj instanceof Wrapper)) { return false; } @@ -33,7 +33,7 @@ public final class Bean { return true; } - return new EqualsBuilder().append(((Bean) obj).get(), get()).isEquals(); + return new EqualsBuilder().append(((Wrapper) obj).get(), get()).isEquals(); } @Override