Skip to content

Commit

Permalink
Fixed AutoRegister & improved SimpleEnchantment
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubix327 committed Jan 29, 2023
1 parent f0a2e48 commit db035a8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 67 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/mineacademy/fo/craft/CraftingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

import static org.mineacademy.fo.MinecraftVersion.atLeast;

/**
* The manager of custom crafting recipes.
*
* @author Rubix327
*/
public final class CraftingHandler {

/**
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/mineacademy/fo/model/SimpleEnchantment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package org.mineacademy.fo.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;

import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -22,17 +15,13 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.mineacademy.fo.ChatUtil;
import org.mineacademy.fo.Common;
import org.mineacademy.fo.MathUtil;
import org.mineacademy.fo.MinecraftVersion;
import org.mineacademy.fo.*;
import org.mineacademy.fo.MinecraftVersion.V;
import org.mineacademy.fo.Valid;
import org.mineacademy.fo.plugin.SimplePlugin;
import org.mineacademy.fo.remain.Remain;

import lombok.NonNull;
import net.md_5.bungee.api.ChatColor;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

/**
* Represents a simple way of getting your own enchantments into Minecraft
Expand Down Expand Up @@ -81,12 +70,11 @@ private static NamespacedKey toKey(@NonNull String name) {
if (!MinecraftVersion.atLeast(V.v1_13))
throw new RuntimeException("SimpleEnchantment requires Minecraft 1.13.2 or greater. Cannot make " + name);

name = new String(name);
name = name.toLowerCase().replace(" ", "_");
name = ChatUtil.replaceDiacritic(name);

Valid.checkBoolean(name != null && VALID_NAMESPACE.matcher(name).matches(), "Enchant name must only contain English alphabet names: " + name);
return new NamespacedKey(SimplePlugin.getInstance(), name);
return NamespacedKey.minecraft(name);
}

// ------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -178,7 +166,7 @@ public ItemStack applyTo(ItemStack item, int level) {
* @return
*/
public String getLore(int level) {
return this.name + " " + MathUtil.toRoman(level);
return "&r&7" + this.name + " " + MathUtil.toRoman(level);
}

/**
Expand Down Expand Up @@ -360,7 +348,7 @@ public static ItemStack addEnchantmentLores(ItemStack item) {
final String lore = ((SimpleEnchantment) e.getKey()).getLore(e.getValue());

if (lore != null && !lore.isEmpty())
customEnchants.add(Common.colorize("&r&7" + lore));
customEnchants.add(Common.colorize(lore));
}

} catch (final NullPointerException ex) {
Expand Down
93 changes: 47 additions & 46 deletions src/main/java/org/mineacademy/fo/plugin/AutoRegisterScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import org.mineacademy.fo.settings.YamlStaticConfig;

import java.io.IOException;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -89,13 +92,21 @@ public static void scanAndRegister() {
// Auto register classes
final AutoRegister autoRegister = clazz.getAnnotation(AutoRegister.class);

// Classes that should be auto registered without the annotation
boolean noAnnotationRequired = false;
List<Class<?>> registeredWithNoAnnotation = Arrays.asList(
Tool.class, SimpleEnchantment.class,
BungeeListener.class, SimpleExpansion.class,
PacketListener.class, DiscordListener.class
);
for (Class<?> cl : registeredWithNoAnnotation){
if (cl.isAssignableFrom(clazz)){
noAnnotationRequired = true;
}
}

// Require our annotation to be used, or support legacy classes from Foundation 5
if (autoRegister != null || Tool.class.isAssignableFrom(clazz)
|| SimpleEnchantment.class.isAssignableFrom(clazz)
|| BungeeListener.class.isAssignableFrom(clazz)
|| SimpleExpansion.class.isAssignableFrom(clazz)
|| PacketListener.class.isAssignableFrom(clazz)
|| DiscordListener.class.isAssignableFrom(clazz)) {
if (autoRegister != null || noAnnotationRequired) {

if (!Modifier.isFinal(clazz.getModifiers())){
Logger.error(new FoException("Non final class is attempted to be auto-registered!"),
Expand Down Expand Up @@ -278,22 +289,22 @@ private static void autoRegister(Class<?> clazz, boolean printWarnings) {
}

final SimplePlugin plugin = SimplePlugin.getInstance();
final Tuple<FindInstance, Object> tuple = findInstance(clazz);
final Tuple<InstanceType, Object> tuple = findInstance(clazz);

final FindInstance mode = tuple.getKey();
final InstanceType mode = tuple.getKey();
final Object instance = tuple.getValue();

boolean eventsRegistered = false;

if (SimpleListener.class.isAssignableFrom(clazz)) {
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

plugin.registerEvents((SimpleListener<?>) instance);
eventsRegistered = true;
}

else if (BungeeListener.class.isAssignableFrom(clazz)) {
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

if (!bungeeListenerRegistered) {
bungeeListenerRegistered = true;
Expand All @@ -317,15 +328,15 @@ else if (SimpleCommandGroup.class.isAssignableFrom(clazz)) {
}

else if (SimpleExpansion.class.isAssignableFrom(clazz)) {
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

Variables.addExpansion((SimpleExpansion) instance);
}

else if (YamlConfig.class.isAssignableFrom(clazz)) {

// Automatically called onLoadFinish when getting instance
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

if (SimplePlugin.isReloading()) {
((YamlConfig) instance).save();
Expand All @@ -336,19 +347,19 @@ else if (YamlConfig.class.isAssignableFrom(clazz)) {
else if (PacketListener.class.isAssignableFrom(clazz)) {

// Automatically registered by means of adding packet adapters
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

((PacketListener) instance).onRegister();
}

else if (DiscordListener.class.isAssignableFrom(clazz))
// Automatically registered in its constructor
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

else if (SimpleEnchantment.class.isAssignableFrom(clazz)) {

// Automatically registered in its constructor
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

if (!enchantListenersRegistered) {
enchantListenersRegistered = true;
Expand All @@ -360,41 +371,31 @@ else if (SimpleEnchantment.class.isAssignableFrom(clazz)) {
}

else if (SimpleCraft.class.isAssignableFrom(clazz)){
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
try {
Field field = clazz.getDeclaredField("instance");
field.setAccessible(true);
CraftingHandler.register(((SimpleCraft<? extends Recipe>) field.get(null)));
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new FoException("SimpleCraft class " + clazz.getName() + " must have 'private static final " +
clazz.getName() + " instance' field to be registered.");
}
enforceModeFor(clazz, mode, InstanceType.SINGLETON);
CraftingHandler.register((SimpleCraft<? extends Recipe>) instance);
}

else if (SimpleBossSkill.class.isAssignableFrom(clazz)){
enforceModeFor(clazz, mode, FindInstance.NEW_FROM_CONSTRUCTOR);
try{
SimpleBossSkill skill = (SimpleBossSkill) clazz.getConstructor().newInstance();
clazz.getMethod("register").invoke(skill);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
enforceModeFor(clazz, mode, InstanceType.NEW_FROM_CONSTRUCTOR);
((SimpleBossSkill) instance).register();
}

else if (Tool.class.isAssignableFrom(clazz))
// Automatically registered in its constructor
enforceModeFor(clazz, mode, FindInstance.SINGLETON);
enforceModeFor(clazz, mode, InstanceType.SINGLETON);

else if (instance instanceof Listener) {
// Pass-through to register events later
}

else
else {
throw new FoException("@AutoRegister cannot be used on " + clazz);
}

// Register events if needed
if (!eventsRegistered && instance instanceof Listener)
if (!eventsRegistered && instance instanceof Listener) {
plugin.registerEvents((Listener) instance);
}
}

/*
Expand Down Expand Up @@ -453,14 +454,14 @@ private static List<Class<?>> findValidClasses() {
}

/*
* Tries to return instance of the given class, either by returning its singleon
* or creating a new instance from constructor if valid
* Tries to return instance of the given class, either by returning its singleton
* or creating a new instance from a constructor
*/
private static Tuple<FindInstance, Object> findInstance(Class<?> clazz) {
private static Tuple<InstanceType, Object> findInstance(Class<?> clazz) {
final Constructor<?> constructor;

Object instance = null;
FindInstance mode = null;
InstanceType mode = null;

try{
constructor = clazz.getDeclaredConstructor();
Expand All @@ -469,7 +470,7 @@ private static Tuple<FindInstance, Object> findInstance(Class<?> clazz) {
// Case 1: Public constructor
if (Modifier.isPublic(modifiers)) {
instance = ReflectionUtil.instantiate(constructor);
mode = FindInstance.NEW_FROM_CONSTRUCTOR;
mode = InstanceType.NEW_FROM_CONSTRUCTOR;
}

// Case 2: Singleton
Expand All @@ -495,7 +496,7 @@ else if (Modifier.isPrivate(modifiers)) {
instanceField = field;
}
}
if (instanceField == null){
if (instanceField == null && suitable.size() != 0){
Logger.printErrors(
"PROBLEM:",
"Your " + clazz + " (using @AutoRegister) contains several",
Expand All @@ -510,7 +511,7 @@ else if (Modifier.isPrivate(modifiers)) {

if (instanceField != null) {
instance = ReflectionUtil.getFieldContent(instanceField, (Object) null);
mode = FindInstance.SINGLETON;
mode = InstanceType.SINGLETON;
}
}

Expand All @@ -528,15 +529,15 @@ else if (Modifier.isPrivate(modifiers)) {
/*
* Checks if the way the given class can be made a new instance of, correspond with the required way
*/
private static void enforceModeFor(Class<?> clazz, FindInstance actual, FindInstance required) {
Valid.checkBoolean(required == actual, clazz + " using @AutoRegister must have " + (required == FindInstance.NEW_FROM_CONSTRUCTOR ? "a single public no args constructor"
private static void enforceModeFor(Class<?> clazz, InstanceType actual, InstanceType required) {
Valid.checkBoolean(required == actual, clazz + " using @AutoRegister must have " + (required == InstanceType.NEW_FROM_CONSTRUCTOR ? "a single public no args constructor"
: "one private no args constructor and a 'private static final " + clazz.getSimpleName() + " instance' field to be a singleton"));
}

/*
* How a new instance can be made to autoregister
*/
enum FindInstance {
enum InstanceType {
NEW_FROM_CONSTRUCTOR,
SINGLETON
}
Expand Down

0 comments on commit db035a8

Please sign in to comment.