BLEEDING EDGE v1.3_05

- Wrapper changes
- ReflectionTools updated
This commit is contained in:
Paldiu 2021-03-01 18:56:04 -06:00
parent 45cf69bf94
commit 924c607ebc
5 changed files with 70 additions and 16 deletions

View File

@ -13,7 +13,7 @@ public final class ChatUtils {
} }
public static <T extends CommandSender> ChatUtils target(T target) { public static <T extends CommandSender> ChatUtils target(T target) {
return new ChatUtils(new Wrapper<>(target)); return new ChatUtils(Wrapper.of(target));
} }
public void msg(String message) { public void msg(String message) {

View File

@ -46,6 +46,11 @@ public abstract class AbstractSign implements IUsableSign {
return lines; return lines;
} }
@Override
public String signTag() {
return "[Tag]";
}
public void setSignTag(String signTag) { public void setSignTag(String signTag) {
this.signTag = signTag(); this.signTag = signTag();
} }

View File

@ -5,17 +5,18 @@ import io.github.simplexdev.api.func.VoidSupplier;
import io.github.simplexdev.simplexcore.listener.SimplexListener; import io.github.simplexdev.simplexcore.listener.SimplexListener;
import io.github.simplexdev.simplexcore.plugin.SimplexAddon; import io.github.simplexdev.simplexcore.plugin.SimplexAddon;
import io.github.simplexdev.simplexcore.utils.Constants; import io.github.simplexdev.simplexcore.utils.Constants;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.jetbrains.annotations.Contract; import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.ArrayList;
import java.util.function.Supplier; import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SignFactory { public class SignFactory {
private static final List<Material> SIGN_TYPES = new ArrayList<>() {{ private static final List<Material> SIGN_TYPES = new ArrayList<>() {{
@ -98,11 +99,6 @@ public class SignFactory {
public void execute() { public void execute() {
executeScript.get(); 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<Sign, AbstractSign> getUnlabeledSigns() { public static Map<Sign, AbstractSign> getUnlabeledSigns() {
return signMap; return signMap;
} }

View File

@ -8,6 +8,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set; import java.util.Set;
public final class ReflectionTools { public final class ReflectionTools {
@ -47,9 +48,20 @@ public final class ReflectionTools {
@Nullable @Nullable
public Wrapper<?> initConstructor(Constructor<?> constructor, Object... initializers) { public Wrapper<?> initConstructor(Constructor<?> constructor, Object... initializers) {
try { try {
return new Wrapper<>(constructor.newInstance(initializers)); return Wrapper.of(constructor.newInstance(initializers));
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
return null; 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;
}
}
} }

View File

@ -3,10 +3,14 @@ package io.github.simplexdev.simplexcore.utils;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder; 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<T> { public final class Wrapper<T> {
protected T bean; protected T bean;
public Wrapper(T bean) { private Wrapper(T bean) {
this.bean = bean; this.bean = bean;
} }
@ -40,4 +44,25 @@ public final class Wrapper<T> {
public String toString() { public String toString() {
return get().toString(); return get().toString();
} }
public static <T> Wrapper<T> of(T object) {
return new Wrapper<>(object);
}
public final <P> P as(Function<? super Wrapper<T>, P> function) {
return function.apply(this);
}
public final <R> Wrapper<R> flatMap(Function<? super T, ? extends Wrapper<? extends R>> function) {
return new Wrapper<>(function.apply(get()).get());
}
public final <R> Wrapper<R> map(Function<? super T, ? extends R> function) {
return new Wrapper<>(function.apply(get()));
}
public final Wrapper<T> filter(Consumer<? super T> predicate) {
predicate.accept(get());
return this;
}
} }