Skip to content

Commit

Permalink
chore: work around
Browse files Browse the repository at this point in the history
- Some changes in base
- Change a lot in config
  • Loading branch information
RawDiamondMC committed Dec 1, 2024
1 parent 3528aec commit 0daf098
Show file tree
Hide file tree
Showing 23 changed files with 561 additions and 507 deletions.
32 changes: 19 additions & 13 deletions base/common/src/main/java/band/kessoku/lib/api/KessokuLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package band.kessoku.lib.api;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

import org.apache.logging.log4j.core.util.ReflectionUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,21 +33,23 @@ public final class KessokuLib {
private KessokuLib() {
}

public static void loadModule(Class<?> moduleCommonClass) {
public static void loadModule(@NotNull Class<?> moduleCommonClass) {
if (moduleCommonClass.isArray())
throw new UnsupportedOperationException("What the hell are you doing?? KessokuLib.loadModule receives an array class! " + moduleCommonClass.getName());
if (isModuleLoaded(moduleCommonClass)) {
throw new UnsupportedOperationException("Module `" + moduleCommonClass.getName() + "` has already been loaded!");
}
// Try to get module name
String moduleName;
try {
Field field = moduleCommonClass.getField("NAME");
if (!Modifier.isStatic(field.getModifiers()))
throw new IllegalArgumentException("NAME in " + moduleCommonClass.getPackageName() + " is not static!");
field.setAccessible(true);
moduleName = (String) field.get(null);
final Field field = moduleCommonClass.getField("NAME");
moduleName = (String) ReflectionUtil.getStaticFieldValue(field);
} catch (NoSuchFieldException e) {
getLogger().warn("no NAME found for {}! Using package name", moduleCommonClass.getPackageName());
moduleName = moduleCommonClass.getPackageName();
} catch (IllegalAccessException e) {
// Already set accessible, shouldn't be called
moduleName = moduleCommonClass.getPackageName();
moduleName = moduleCommonClass.getName();
getLogger().warn("Field `NAME` is not found in {}! Using fully qualified class name.", moduleName);
} catch (NullPointerException e) {
moduleName = moduleCommonClass.getName();
getLogger().warn("NAME in {} is not static! Using package name.", moduleName);
}
initializedModules.add(moduleCommonClass);
getLogger().info("{} loaded!", moduleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,26 @@
*/
package band.kessoku.lib.api.base;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jetbrains.annotations.NotNull;

public final class KessokuUtils {
private KessokuUtils() {
}

public static <T> boolean isType(final List<?> list, final Class<T> type) {
for (final Object element : list) {
public static <T> boolean isType(final Collection<?> collection, final Class<T> type) {
for (final Object element : collection) {
if (!(type.isInstance(element))) {
return false;
}
}
return true;
}

public static <V> V constructUnsafely(final Class<V> cls) {
try {
final Constructor<V> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

public static <K, V> @NotNull Set<K> getKeysByValue(final Map<K, V> map, final V value) {
final Set<K> result = new HashSet<>();
map.forEach((k, v) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public final class ReflectUtil {
private ReflectUtil() {
}

public static boolean isAssignableFrom(Field field, Class<?>... clazzs) {
var flag = Arrays.stream(clazzs).anyMatch(clazz -> !field.getType().isAssignableFrom(clazz));
public static boolean isAssignableFrom(Field field, Class<?>... classes) {
var flag = Arrays.stream(classes).anyMatch(clazz -> !field.getType().isAssignableFrom(clazz));
return !flag;
}

public static boolean isAssignableFrom(Object o, Class<?>... classes) {
var flag = Arrays.stream(classes).anyMatch(clazz -> !o.getClass().isAssignableFrom(clazz));
return !flag;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package band.kessoku.lib.api.config;

import band.kessoku.lib.impl.config.AbstractConfig;

public class Config extends AbstractConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package band.kessoku.lib.api.config;

import band.kessoku.lib.impl.config.AbstractConfig;

import java.util.Map;

public interface ConfigSerializer {
String serialize(Map<String, AbstractConfig.ValueWithComment> value);
String serialize(Map<String, AbstractConfig.WrappedValue> valueMap);

Map<String, Object> deserialize(String value);

Expand Down
Loading

0 comments on commit 0daf098

Please sign in to comment.