Initial commit

This commit is contained in:
2024-10-18 17:49:49 +02:00
commit 5d843211c0
30 changed files with 1444 additions and 0 deletions
@@ -0,0 +1,148 @@
package com.alttd.custommobs.config;
import com.alttd.custommobs.Main;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@SuppressWarnings({"unused", "SameParameterValue"})
abstract class AbstractConfig {
File file;
YamlConfiguration yaml;
AbstractConfig(Main main, String filename) {
init(new File(main.getDataFolder(), filename), filename);
}
AbstractConfig(File file, String filename) {
init(new File(file.getPath() + File.separator + filename), filename);
}
private void init(File file, String filename) {
this.file = file;
this.yaml = new YamlConfiguration();
try {
yaml.load(file);
} catch (IOException ignore) {
} catch (InvalidConfigurationException ex) {
log.error("Could not load {}, please correct your syntax errors", filename, ex);
throw new RuntimeException(ex);
}
yaml.options().copyDefaults(true);
}
void readConfig(Class<?> clazz, Object instance) {
for (Class<?> declaredClass : clazz.getDeclaredClasses()) {
for (Method method : declaredClass.getDeclaredMethods()) {
if (!Modifier.isPrivate(method.getModifiers())) {
continue;
}
if (method.getParameterTypes().length != 0 || method.getReturnType() != Void.TYPE) {
continue;
}
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getCause());
} catch (Exception ex) {
log.error("Error invoking {}.", method, ex);
}
}
}
save();
}
private void save() {
try {
yaml.save(file);
} catch (IOException ex) {
log.error("Could not save {}.", file.toString(), ex);
}
}
void set(String prefix, String path, Object val) {
path = prefix + path;
yaml.addDefault(path, val);
yaml.set(path, val);
save();
}
String getString(String prefix, String path, String def) {
path = prefix + path;
yaml.addDefault(path, def);
return yaml.getString(path, yaml.getString(path));
}
boolean getBoolean(String prefix, String path, boolean def) {
path = prefix + path;
yaml.addDefault(path, def);
return yaml.getBoolean(path, yaml.getBoolean(path));
}
int getInt(String prefix, String path, int def) {
path = prefix + path;
yaml.addDefault(path, def);
return yaml.getInt(path, yaml.getInt(path));
}
double getDouble(String prefix, String path, double def) {
path = prefix + path;
yaml.addDefault(path, def);
return yaml.getDouble(path, yaml.getDouble(path));
}
<T> List<String> getList(String prefix, String path, T def) {
path = prefix + path;
yaml.addDefault(path, def);
List<?> list = yaml.getList(path, yaml.getList(path));
return list == null ? null : list.stream().map(Object::toString).collect(Collectors.toList());
}
List<String> getStringList(String prefix, String path, List<String> def) {
path = prefix + path;
yaml.addDefault(path, def);
return yaml.getStringList(path);
}
@NonNull
<T> Map<String, T> getMap(String prefix, @NonNull String path, final @Nullable Map<String, T> def) {
path = prefix + path;
final ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
if (def != null && yaml.getConfigurationSection(path) == null) {
yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
return def;
}
final ConfigurationSection section = yaml.getConfigurationSection(path);
if (section != null) {
for (String key : section.getKeys(false)) {
@SuppressWarnings("unchecked")
final T val = (T) section.get(key);
if (val != null) {
builder.put(key, val);
}
}
}
return builder.build();
}
ConfigurationSection getConfigurationSection(String path) {
return yaml.getConfigurationSection(path);
}
}
@@ -0,0 +1,40 @@
package com.alttd.custommobs.config;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
@Slf4j
public class Config extends AbstractConfig{
static Config config;
Config() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "custommobs"),
"config.yml");
}
public static void reload() {
log.info("Reloading config");
config = new Config();
config.readConfig(Config.class, null);
}
public static class SETTINGS {
private static final String prefix = "settings.";
public static boolean DEBUG = false;
public static boolean WARNINGS = true;
@SuppressWarnings("unused")
private static void load() {
log.info("Warnings logging is {}\nDebug logging is {}", WARNINGS, DEBUG);
DEBUG = config.getBoolean(prefix, "debug", DEBUG);
WARNINGS = config.getBoolean(prefix, "warnings", WARNINGS);
}
}
}
@@ -0,0 +1,55 @@
package com.alttd.custommobs.config;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
@Slf4j
public class Messages extends AbstractConfig {
static Messages config;
Messages() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "custommobs"),
"messages.yml");
}
public static void reload() {
config = new Messages();
config.readConfig(Messages.class, null);
log.info("Reloading messages");
}
public static class HELP {
private static final String prefix = "help.";
public static String HELP_MESSAGE_WRAPPER = "<gold>Main help:\n<commands></gold>";
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/cm help</gold></green>";
public static String SPAWN = "<green>Spawn a mob of a specific type: <gold>/cm spawn <mob_type> [<world> <x> <y> <z>]</gold></green>";
@SuppressWarnings("unused")
private static void load() {
HELP_MESSAGE_WRAPPER = config.getString(prefix, "help-wrapper", HELP_MESSAGE_WRAPPER);
HELP_MESSAGE = config.getString(prefix, "help", HELP_MESSAGE);
SPAWN = config.getString(prefix, "spawn", SPAWN);
}
}
public static class GENERIC {
private static final String prefix = "generic.";
public static String NO_PERMISSION = "<red><hover:show_text:'<red><permission></red>'>You don't have permission for this command</hover></red>";
public static String PLAYER_ONLY = "<red>This command can only be executed as a player</red>";
public static String PLAYER_NOT_FOUND = "<red>Unable to find online player <player></red>";
@SuppressWarnings("unused")
private static void load() {
NO_PERMISSION = config.getString(prefix, "no-permission", NO_PERMISSION);
PLAYER_ONLY = config.getString(prefix, "player-only", PLAYER_ONLY);
PLAYER_NOT_FOUND = config.getString(prefix, "player-only", PLAYER_NOT_FOUND);
}
}
}
@@ -0,0 +1,98 @@
package com.alttd.custommobs.config;
import com.alttd.custommobs.abilities.Ability;
import com.alttd.custommobs.abilities.modifiers.AttributeModifier;
import com.alttd.custommobs.abilities.EntityModifier;
import com.alttd.custommobs.abilities.MobType;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.attribute.Attribute;
import org.reflections.Reflections;
import java.io.File;
import java.util.*;
@Slf4j
public class MobTypes extends AbstractConfig {
static MobTypes config;
MobTypes() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "custommobs"),
"mob-types.yml");
}
public static void reload() {
log.info("Reloading mob types");
config = new MobTypes();
config.readConfig(MobTypes.class, null);
}
public static class MOB_TYPES {
private static final String prefix = "mob-types.";
protected static Object2ObjectOpenHashMap<String, MobType> MOB_TYPES = new Object2ObjectOpenHashMap<>();
public static Optional<MobType> get(String name) {
if (!MOB_TYPES.containsKey(name)) {
return Optional.empty();
}
return Optional.of(MOB_TYPES.get(name));
}
@SuppressWarnings("unused")
private static void load() {
MOB_TYPES.clear();
Set<String> keys = config.getConfigurationSection(prefix.substring(0, prefix.length() - 1)).getKeys(false);
keys.forEach(key -> {
List<Ability> abilities = config.getStringList(prefix + key, ".abilities", null).stream()
.map(MobTypes.MOB_TYPES::getAbility)
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
List<EntityModifier> entityModifiers = new ArrayList<>();
config.getConfigurationSection(prefix + key + ".modifiers").getKeys(false).forEach(modifier -> {
String attribute = config.getString(prefix + key + ".modifiers." + modifier, "attribute", null);
if (attribute == null) {
return;
}
EntityModifier value = getAttribute(attribute, config.getDouble(prefix + key + ".modifiers." + modifier, "value", 1));
if (value == null)
return;
entityModifiers.add(value);
});
MOB_TYPES.put(key, new MobType(abilities, entityModifiers));
});
}
private static EntityModifier getAttribute(String attributeName, double value) {
try {
Attribute attribute = Attribute.valueOf(attributeName);
return new AttributeModifier(attribute, value);
} catch (IllegalArgumentException e) {
log.error("Invalid attribute {}", attributeName, e);
return null;
}
}
private static Optional<Ability> getAbility(String name) {
Optional<Ability> first = new Reflections("com.alttd.custommobs.abilities").getSubTypesOf(Ability.class).stream().map(entry -> {
try {
return entry.getDeclaredConstructor().newInstance();
} catch (Exception e) {
return null;
}
})
.filter(Objects::nonNull)
.map(entry -> (Ability) entry)
.filter(entry -> entry.getName().equals(name))
.findFirst();
if (first.isEmpty()) {
log.warn("Invalid ability {}", name);
}
return first;
}
}
}