From 924c607ebc870801144b7a01d9dce9bb010e54f8 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Mon, 1 Mar 2021 18:56:04 -0600 Subject: [PATCH] BLEEDING EDGE v1.3_05 - Wrapper changes - ReflectionTools updated --- .../simplexcore/chat/ChatUtils.java | 2 +- .../simplexcore/sign/AbstractSign.java | 5 +++ .../simplexcore/sign/SignFactory.java | 38 ++++++++++++------- .../simplexcore/utils/ReflectionTools.java | 14 ++++++- .../simplexdev/simplexcore/utils/Wrapper.java | 27 ++++++++++++- 5 files changed, 70 insertions(+), 16 deletions(-) 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 87dee4a..871cefc 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java +++ b/src/main/java/io/github/simplexdev/simplexcore/chat/ChatUtils.java @@ -13,7 +13,7 @@ public final class ChatUtils { } public static ChatUtils target(T target) { - return new ChatUtils(new Wrapper<>(target)); + return new ChatUtils(Wrapper.of(target)); } public void msg(String message) { diff --git a/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java b/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java index 00e4f32..42d3219 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java +++ b/src/main/java/io/github/simplexdev/simplexcore/sign/AbstractSign.java @@ -46,6 +46,11 @@ public abstract class AbstractSign implements IUsableSign { return lines; } + @Override + public String signTag() { + return "[Tag]"; + } + public void setSignTag(String signTag) { this.signTag = signTag(); } 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 7cd5cd1..7f499ba 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/sign/SignFactory.java @@ -5,17 +5,18 @@ 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.Action; import org.bukkit.event.block.BlockPlaceEvent; -import org.jetbrains.annotations.Contract; +import org.bukkit.event.player.PlayerInteractEvent; import org.jetbrains.annotations.Nullable; -import java.util.*; -import java.util.function.Supplier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class SignFactory { private static final List SIGN_TYPES = new ArrayList<>() {{ @@ -72,13 +73,13 @@ public class SignFactory { AbstractSign abs = SignData.getUnlabeledSigns().get(sign); for (Object tag : tags) { if (tag instanceof VoidSupplier) { - abs.setExecuteScript((VoidSupplier)tag); + abs.setExecuteScript((VoidSupplier) tag); } if (tag instanceof String) { - abs.setSignTag((String)tag); + abs.setSignTag((String) tag); } if (tag instanceof Boolean) { - abs.setCanInteract((Boolean)tag); + abs.setCanInteract((Boolean) tag); } } return abs; @@ -98,11 +99,6 @@ public class SignFactory { public void execute() { executeScript.get(); } - - @Override - public String signTag() { - return signTag; - } }; } @@ -123,6 +119,22 @@ public class SignFactory { } } + @EventHandler + public void blockInteractEvent(PlayerInteractEvent event) { + if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) + && event.getClickedBlock() != null + && 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(); + } + } + } + } + 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 index b49a3ca..795f1eb 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/ReflectionTools.java @@ -8,6 +8,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Set; public final class ReflectionTools { @@ -47,9 +48,20 @@ public final class ReflectionTools { @Nullable public Wrapper initConstructor(Constructor constructor, Object... initializers) { try { - return new Wrapper<>(constructor.newInstance(initializers)); + return Wrapper.of(constructor.newInstance(initializers)); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { return null; } } + + @Nullable + public Method getMethod(Class clazz, String name, Class... params) { + try { + return Wrapper.of(clazz.getMethod(name, params)) + .filter(obj -> obj.setAccessible(true)) + .get(); + } catch (NoSuchMethodException e) { + return null; + } + } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java b/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java index 51e2880..25cd046 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java +++ b/src/main/java/io/github/simplexdev/simplexcore/utils/Wrapper.java @@ -3,10 +3,14 @@ package io.github.simplexdev.simplexcore.utils; 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; - public Wrapper(T bean) { + private Wrapper(T bean) { this.bean = bean; } @@ -40,4 +44,25 @@ public final class Wrapper { 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 filter(Consumer predicate) { + predicate.accept(get()); + return this; + } }