BLEEDING-EDGE v1.3_04

This commit is contained in:
Paldiu 2021-02-27 17:27:58 -06:00
parent 997c333cc1
commit 45cf69bf94
11 changed files with 277 additions and 33 deletions

View File

@ -2,18 +2,22 @@ package io.github.simplexdev.api;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Sign;
import java.util.List;
public interface IUsableSign { public interface IUsableSign {
Block getBlock(); Sign getSign();
Location getSignLocation(); Location getSignLocation();
World getWorld(); World getWorld();
String getSignText(); List<String> getLines();
void executeOnInteract(); boolean canInteract();
void execute();
String signTag(); String signTag();
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -2,7 +2,7 @@ package io.github.simplexdev.simplexcore.ban;
import io.github.simplexdev.api.IBan; import io.github.simplexdev.api.IBan;
import io.github.simplexdev.simplexcore.chat.Messages; 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.Constants;
import io.github.simplexdev.simplexcore.utils.Utilities; import io.github.simplexdev.simplexcore.utils.Utilities;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -20,7 +20,7 @@ public final class BanFactory {
private String banReason; private String banReason;
private long banDuration; 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.player = player;
this.sender = sender; this.sender = sender;
this.banDate = banDate; this.banDate = banDate;
@ -31,10 +31,6 @@ public final class BanFactory {
banId = createBanId(); 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. * 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 () -> { return () -> {
if (type.equals(BanType.PERMANENT)) { if (type.equals(BanType.PERMANENT)) {
banDuration = Constants.getPlugin().getInstances().getTimeValues().YEAR() * 99; banDuration = Constants.getPlugin().getInstances().getTimeValues().YEAR() * 99;

View File

@ -1,19 +1,19 @@
package io.github.simplexdev.simplexcore.chat; 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 net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
public final class ChatUtils { public final class ChatUtils {
protected final Bean<? extends CommandSender> target; protected final Wrapper<? extends CommandSender> target;
protected final TextComponentFactory factory = new TextComponentFactory(); protected final TextComponentFactory factory = new TextComponentFactory();
private ChatUtils(Bean<? extends CommandSender> target) { private ChatUtils(Wrapper<? extends CommandSender> target) {
this.target = target; this.target = target;
} }
public static <T extends CommandSender> ChatUtils target(T target) { public static <T extends CommandSender> ChatUtils target(T target) {
return new ChatUtils(new Bean<>(target)); return new ChatUtils(new Wrapper<>(target));
} }
public void msg(String message) { public void msg(String message) {

View File

@ -34,11 +34,11 @@ public final class DependencyListener extends SimplexListener {
Validate temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName()); Validate temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName());
if (temp.isValid()) { if (temp.isValid()) {
Constants.getDependencyManager().registerProtocolLib(); Constants.getPlugin().getInstances().getDependencyManager().registerProtocolLib();
} }
if (temp2.isValid()) { if (temp2.isValid()) {
Constants.getDependencyManager().registerPAPI(); Constants.getPlugin().getInstances().getDependencyManager().registerPAPI();
} }
} }
} }

View File

@ -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<String> 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<String> 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;
}
}

View File

@ -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<Material> 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<Sign, AbstractSign> 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<Sign, AbstractSign> getUnlabeledSigns() {
return signMap;
}
}
}

View File

@ -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<Class<?>> getAnnotatedClasses(Class<?> loadFrom, Class<? extends Annotation> 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;
}
}
}

View File

@ -1,6 +1,5 @@
package io.github.simplexdev.simplexcore.utils; package io.github.simplexdev.simplexcore.utils;
import io.github.simplexdev.api.func.Guard;
import io.github.simplexdev.simplexcore.ban.BanType; import io.github.simplexdev.simplexcore.ban.BanType;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -3,10 +3,10 @@ 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;
public final class Bean<T> { public final class Wrapper<T> {
protected T bean; protected T bean;
public Bean(T bean) { public Wrapper(T bean) {
this.bean = bean; this.bean = bean;
} }
@ -25,7 +25,7 @@ public final class Bean<T> {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (!(obj instanceof Bean<?>)) { if (!(obj instanceof Wrapper<?>)) {
return false; return false;
} }
@ -33,7 +33,7 @@ public final class Bean<T> {
return true; return true;
} }
return new EqualsBuilder().append(((Bean<?>) obj).get(), get()).isEquals(); return new EqualsBuilder().append(((Wrapper<?>) obj).get(), get()).isEquals();
} }
@Override @Override