mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-14 11:46:06 +00:00
implement getType method in registries
This commit is contained in:
@@ -26,9 +26,11 @@ import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.structure.LootTable;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.StringUtil;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public class TerraScriptAddon implements AddonInitializer {
|
||||
public static TypeKey<FunctionBuilder<?>> FUNCTION_BUILDER_TYPE = new TypeKey<>() {};
|
||||
@Inject
|
||||
private Platform platform;
|
||||
|
||||
@@ -50,7 +52,7 @@ public class TerraScriptAddon implements AddonInitializer {
|
||||
StructureScript structureScript = new StructureScript(entry.getValue(), id, platform, structureRegistry,
|
||||
lootRegistry,
|
||||
event.getPack().getOrCreateRegistry(
|
||||
(Type) FunctionBuilder.class));
|
||||
FUNCTION_BUILDER_TYPE));
|
||||
structureRegistry.register(structureScript.getID(), structureScript);
|
||||
} catch(ParseException e) {
|
||||
throw new LoadException("Failed to load script \"" + entry.getKey() + "\"", e);
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.dfsek.terra.api.command;
|
||||
|
||||
import cloud.commandframework.ArgumentDescription;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
|
||||
import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
|
||||
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class RegistryArgument<T, R> extends CommandArgument<T, R> {
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private RegistryArgument(
|
||||
final boolean required,
|
||||
final @NonNull String name,
|
||||
final Registry<R> registry,
|
||||
final @NonNull String defaultValue,
|
||||
final @Nullable BiFunction<@NonNull CommandContext<T>, @NonNull String,
|
||||
@NonNull List<@NonNull String>> suggestionsProvider,
|
||||
final @NonNull ArgumentDescription description
|
||||
) {
|
||||
super(required,
|
||||
name,
|
||||
new RegistryArgumentParser<>(registry),
|
||||
defaultValue,
|
||||
(TypeToken<R>) TypeToken.get(registry.getType().getType()),
|
||||
suggestionsProvider,
|
||||
description);
|
||||
}
|
||||
|
||||
public static <T, R> Builder<T, R> builder(String name, Registry<R> registry) {
|
||||
return new Builder<>(name, registry);
|
||||
}
|
||||
|
||||
public static final class Builder<T, R> extends CommandArgument.Builder<T, R> {
|
||||
private final Registry<R> registry;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Builder(@NonNull String name, Registry<R> registry) {
|
||||
super((TypeToken<R>) registry.getType().getType(), name);
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull RegistryArgument<T, R> build() {
|
||||
return new RegistryArgument<>(
|
||||
isRequired(),
|
||||
getName(),
|
||||
registry,
|
||||
getDefaultValue(),
|
||||
getSuggestionsProvider(),
|
||||
getDefaultDescription()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final class RegistryArgumentParser<T, R> implements ArgumentParser<T, R> {
|
||||
private final Registry<R> registry;
|
||||
|
||||
private RegistryArgumentParser(Registry<R> registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ArgumentParseResult<@NonNull R> parse(@NonNull CommandContext<@NonNull T> commandContext,
|
||||
@NonNull Queue<@NonNull String> inputQueue) {
|
||||
String input = inputQueue.remove();
|
||||
return registry.get(input).map(ArgumentParseResult::success).orElse(ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + input)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull List<@NonNull String> suggestions(@NonNull CommandContext<T> commandContext, @NonNull String input) {
|
||||
return registry.keys().stream().sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,15 +35,11 @@ public interface ConfigPack extends LoaderRegistrar, ConfigLoadingDelegate, Regi
|
||||
|
||||
BiomeProvider getBiomeProvider();
|
||||
|
||||
<T> CheckedRegistry<T> getOrCreateRegistry(Type clazz);
|
||||
|
||||
default <T> CheckedRegistry<T> getOrCreateRegistry(Class<T> clazz) {
|
||||
return getOrCreateRegistry((Type) clazz);
|
||||
return getOrCreateRegistry(TypeKey.of(clazz));
|
||||
}
|
||||
|
||||
default <T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> type) {
|
||||
return getOrCreateRegistry(type.getType());
|
||||
}
|
||||
<T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> type);
|
||||
|
||||
List<GenerationStage> getStages();
|
||||
|
||||
@@ -53,10 +49,10 @@ public interface ConfigPack extends LoaderRegistrar, ConfigLoadingDelegate, Regi
|
||||
|
||||
Version getVersion();
|
||||
|
||||
<T> ConfigPack registerShortcut(Type clazz, String shortcut, ShortcutLoader<T> loader);
|
||||
<T> ConfigPack registerShortcut(TypeKey<T> clazz, String shortcut, ShortcutLoader<T> loader);
|
||||
|
||||
default <T> ConfigPack registerShortcut(Class<T> clazz, String shortcut, ShortcutLoader<T> loader) {
|
||||
return registerShortcut((Type) clazz, shortcut, loader);
|
||||
return registerShortcut(TypeKey.of(clazz), shortcut, loader);
|
||||
}
|
||||
|
||||
ChunkGeneratorProvider getGeneratorProvider();
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -70,4 +74,6 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
Set<String> keys();
|
||||
|
||||
TypeKey<T> getType();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.api.util.reflection;
|
||||
|
||||
import com.dfsek.tectonic.util.ClassAnnotatedTypeImpl;
|
||||
|
||||
import java.lang.reflect.AnnotatedParameterizedType;
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
@@ -33,6 +35,17 @@ public class TypeKey<T> {
|
||||
this.hashCode = type.hashCode();
|
||||
}
|
||||
|
||||
protected TypeKey(Class<T> clazz) {
|
||||
this.type = clazz;
|
||||
this.rawType = clazz;
|
||||
this.annotatedType = new ClassAnnotatedTypeImpl(clazz);
|
||||
this.hashCode = type.hashCode();
|
||||
}
|
||||
|
||||
public static <T> TypeKey<T> of(Class<T> clazz) {
|
||||
return new TypeKey<>(clazz);
|
||||
}
|
||||
|
||||
public static boolean equals(Type a, Type b) {
|
||||
if(a == b) {
|
||||
return true;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
package com.dfsek.terra;
|
||||
|
||||
import com.dfsek.tectonic.api.TypeRegistry;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -93,7 +96,7 @@ public abstract class AbstractPlatform implements Platform {
|
||||
|
||||
private final CommandManager manager = new TerraCommandManager(this);
|
||||
|
||||
private final CheckedRegistry<BaseAddon> addonRegistry = new CheckedRegistryImpl<>(new OpenRegistryImpl<>());
|
||||
private final CheckedRegistry<BaseAddon> addonRegistry = new CheckedRegistryImpl<>(new OpenRegistryImpl<>(TypeKey.of(BaseAddon.class)));
|
||||
|
||||
private final Registry<BaseAddon> lockedAddonRegistry = new LockedRegistryImpl<>(addonRegistry);
|
||||
|
||||
@@ -172,7 +175,7 @@ public abstract class AbstractPlatform implements Platform {
|
||||
|
||||
List<BaseAddon> addonList = new ArrayList<>();
|
||||
|
||||
InternalAddon internalAddon = new InternalAddon();
|
||||
InternalAddon internalAddon = new InternalAddon(this);
|
||||
|
||||
addonList.add(internalAddon);
|
||||
|
||||
|
||||
@@ -20,12 +20,48 @@ package com.dfsek.terra.addon;
|
||||
import ca.solostudios.strata.Versions;
|
||||
import ca.solostudios.strata.version.Version;
|
||||
|
||||
import cloud.commandframework.ArgumentDescription;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.Description;
|
||||
|
||||
import cloud.commandframework.arguments.standard.StringArgument;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
|
||||
|
||||
public class InternalAddon implements BaseAddon {
|
||||
private static final Version VERSION = Versions.getVersion(1, 0, 0);
|
||||
|
||||
public InternalAddon(Platform platform) {
|
||||
platform.getEventManager()
|
||||
.getHandler(FunctionalEventHandler.class)
|
||||
.register(this, CommandRegistrationEvent.class)
|
||||
.then(event -> {
|
||||
CommandManager<CommandSender> manager = event.getCommandManager();
|
||||
manager.command(
|
||||
manager.commandBuilder("addons", ArgumentDescription.of("Get information about installed Terra addons"))
|
||||
.handler(context -> {
|
||||
StringBuilder addons = new StringBuilder("Installed addons:\n");
|
||||
platform.getAddons()
|
||||
.forEach(addon -> addons
|
||||
.append("- ")
|
||||
.append(addon.getID())
|
||||
.append('@')
|
||||
.append(addon.getVersion())
|
||||
.append('\n'));
|
||||
context.getSender().sendMessage(addons.toString());
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return "terra";
|
||||
|
||||
@@ -48,6 +48,7 @@ import com.dfsek.terra.api.tectonic.ShortcutLoader;
|
||||
import com.dfsek.terra.api.util.generic.Construct;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
|
||||
@@ -82,6 +83,7 @@ import java.util.zip.ZipFile;
|
||||
* Represents a Terra configuration pack.
|
||||
*/
|
||||
public class ConfigPackImpl implements ConfigPack {
|
||||
public static final TypeKey<ConfigType<?, ?>> CONFIG_TYPE_TYPE_KEY = new TypeKey<>() {};
|
||||
private static final Logger logger = LoggerFactory.getLogger(ConfigPackImpl.class);
|
||||
|
||||
private final ConfigPackTemplate template = new ConfigPackTemplate();
|
||||
@@ -274,14 +276,14 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> CheckedRegistry<T> getOrCreateRegistry(Type type) {
|
||||
return (CheckedRegistry<T>) registryMap.computeIfAbsent(type, c -> {
|
||||
OpenRegistry<T> registry = new OpenRegistryImpl<>();
|
||||
public <T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> typeKey) {
|
||||
return (CheckedRegistry<T>) registryMap.computeIfAbsent(typeKey.getType(), c -> {
|
||||
OpenRegistry<T> registry = new OpenRegistryImpl<>(typeKey);
|
||||
selfLoader.registerLoader(c, registry);
|
||||
abstractConfigLoader.registerLoader(c, registry);
|
||||
logger.debug("Registered loader for registry of class {}", ReflectionUtil.typeToString(c));
|
||||
|
||||
if(type instanceof ParameterizedType param) {
|
||||
if(typeKey.getType() instanceof ParameterizedType param) {
|
||||
Type base = param.getRawType();
|
||||
if(base instanceof Class // should always be true but we'll check anyways
|
||||
&& Supplier.class.isAssignableFrom((Class<?>) base)) { // If it's a supplier
|
||||
@@ -327,12 +329,12 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
@SuppressWarnings("unchecked,rawtypes")
|
||||
@Override
|
||||
public <T> ConfigPack registerShortcut(Type clazz, String shortcut, ShortcutLoader<T> loader) {
|
||||
public <T> ConfigPack registerShortcut(TypeKey<T> clazz, String shortcut, ShortcutLoader<T> loader) {
|
||||
ShortcutHolder<?> holder = shortcuts
|
||||
.computeIfAbsent(clazz, c -> new ShortcutHolder<>(getOrCreateRegistry(clazz)))
|
||||
.computeIfAbsent(clazz.getType(), c -> new ShortcutHolder<>(getOrCreateRegistry(clazz)))
|
||||
.register(shortcut, (ShortcutLoader) loader);
|
||||
selfLoader.registerLoader(clazz, holder);
|
||||
abstractConfigLoader.registerLoader(clazz, holder);
|
||||
selfLoader.registerLoader(clazz.getType(), holder);
|
||||
abstractConfigLoader.registerLoader(clazz.getType(), holder);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -342,13 +344,13 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
}
|
||||
|
||||
private OpenRegistry<ConfigType<?, ?>> createConfigRegistry() {
|
||||
return new OpenRegistryImpl<>(new LinkedHashMap<>()) {
|
||||
return new OpenRegistryImpl<>(new LinkedHashMap<>(), CONFIG_TYPE_TYPE_KEY) {
|
||||
@Override
|
||||
public boolean register(@NotNull String identifier, @NotNull ConfigType<?, ?> value) {
|
||||
if(!registryMap
|
||||
.containsKey(value.getTypeKey()
|
||||
.getType())) {
|
||||
OpenRegistry<?> openRegistry = new OpenRegistryImpl<>();
|
||||
OpenRegistry<?> openRegistry = new OpenRegistryImpl<>(value.getTypeKey());
|
||||
selfLoader.registerLoader(value.getTypeKey().getType(), openRegistry);
|
||||
abstractConfigLoader.registerLoader(value.getTypeKey().getType(), openRegistry);
|
||||
registryMap.put(value.getTypeKey().getType(), new CheckedRegistryImpl<>(openRegistry));
|
||||
|
||||
@@ -19,6 +19,9 @@ package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -86,6 +89,11 @@ public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
|
||||
return registry.keys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return registry.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader) throws LoadException {
|
||||
return registry.load(t, c, loader);
|
||||
|
||||
@@ -19,6 +19,9 @@ package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
@@ -74,7 +77,12 @@ public class LockedRegistryImpl<T> implements Registry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
|
||||
public TypeKey<T> getType() {
|
||||
return registry.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader) throws LoadException {
|
||||
return registry.load(t, c, loader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
@@ -46,17 +49,19 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
private static final Entry<?> NULL = new Entry<>(null);
|
||||
private static final Pattern ID_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]*$");
|
||||
private final Map<String, Entry<T>> objects;
|
||||
private final TypeKey<T> typeKey;
|
||||
|
||||
public OpenRegistryImpl() {
|
||||
objects = new HashMap<>();
|
||||
public OpenRegistryImpl(TypeKey<T> typeKey) {
|
||||
this(new HashMap<>(), typeKey);
|
||||
}
|
||||
|
||||
protected OpenRegistryImpl(Map<String, Entry<T>> init) {
|
||||
protected OpenRegistryImpl(Map<String, Entry<T>> init, TypeKey<T> typeKey) {
|
||||
this.objects = init;
|
||||
this.typeKey = typeKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
public T load(@NotNull AnnotatedType type, @NotNull Object o, @NotNull ConfigLoader configLoader) throws LoadException {
|
||||
return get((String) o).orElseThrow(() -> {
|
||||
String list = objects.keySet().stream().sorted().reduce("", (a, b) -> a + "\n - " + b);
|
||||
if(objects.isEmpty()) list = "[ ]";
|
||||
@@ -123,6 +128,11 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
return objects.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return typeKey;
|
||||
}
|
||||
|
||||
public Map<String, T> getDeadEntries() {
|
||||
Map<String, T> dead = new HashMap<>();
|
||||
objects.forEach((id, entry) -> {
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
package com.dfsek.terra.registry.master;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.ConfigException;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -36,7 +39,11 @@ import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
*/
|
||||
public class ConfigRegistry extends OpenRegistryImpl<ConfigPack> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ConfigRegistry.class);
|
||||
|
||||
|
||||
public ConfigRegistry() {
|
||||
super(TypeKey.of(ConfigPack.class));
|
||||
}
|
||||
|
||||
public void load(File folder, Platform platform) throws ConfigException {
|
||||
ConfigPack pack = new ConfigPackImpl(folder, platform);
|
||||
register(pack.getID(), pack);
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
package registry;
|
||||
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
@@ -31,7 +33,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
public class RegistryTest {
|
||||
@Test
|
||||
public void openRegistry() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>();
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.register("test", "bazinga");
|
||||
|
||||
@@ -40,7 +42,7 @@ public class RegistryTest {
|
||||
|
||||
@Test
|
||||
public void openRegistryChecked() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>();
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.registerChecked("test", "bazinga");
|
||||
|
||||
@@ -54,7 +56,7 @@ public class RegistryTest {
|
||||
|
||||
@Test
|
||||
public void checkedRegistry() {
|
||||
CheckedRegistry<String> test = new CheckedRegistryImpl<>(new OpenRegistryImpl<>());
|
||||
CheckedRegistry<String> test = new CheckedRegistryImpl<>(new OpenRegistryImpl<>(TypeKey.of(String.class)));
|
||||
|
||||
test.register("test", "bazinga");
|
||||
|
||||
|
||||
@@ -17,6 +17,13 @@
|
||||
|
||||
package com.dfsek.terra.bukkit;
|
||||
|
||||
import cloud.commandframework.brigadier.CloudBrigadierManager;
|
||||
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.paper.PaperCommandManager;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
@@ -30,19 +37,16 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.dfsek.terra.api.command.CommandManager;
|
||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
|
||||
import com.dfsek.terra.bukkit.command.BukkitCommandAdapter;
|
||||
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.bukkit.listeners.CommonListener;
|
||||
import com.dfsek.terra.bukkit.listeners.PaperListener;
|
||||
import com.dfsek.terra.bukkit.listeners.SpigotListener;
|
||||
import com.dfsek.terra.bukkit.util.PaperUtil;
|
||||
import com.dfsek.terra.bukkit.util.VersionUtil;
|
||||
import com.dfsek.terra.commands.CommandUtil;
|
||||
import com.dfsek.terra.commands.TerraCommandManager;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
|
||||
|
||||
public class TerraBukkitPlugin extends JavaPlugin {
|
||||
@@ -61,12 +65,26 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
||||
|
||||
PluginCommand cmd = Objects.requireNonNull(getCommand("terra"));
|
||||
|
||||
CommandManager manager = new TerraCommandManager(platform);
|
||||
|
||||
|
||||
try {
|
||||
CommandUtil.registerAll(manager);
|
||||
} catch(MalformedCommandException e) { // This should never happen.
|
||||
PaperCommandManager<CommandSender> commandManager = new PaperCommandManager<>(this,
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
BukkitAdapter::adapt,
|
||||
BukkitAdapter::adapt);
|
||||
if (commandManager.queryCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
|
||||
commandManager.registerBrigadier();
|
||||
final CloudBrigadierManager<?, ?> brigManager = commandManager.brigadierManager();
|
||||
if (brigManager != null) {
|
||||
brigManager.setNativeNumberSuggestions(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (commandManager.queryCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
|
||||
commandManager.registerAsynchronousCompletions();
|
||||
}
|
||||
|
||||
platform.getEventManager().callEvent(new CommandRegistrationEvent(commandManager));
|
||||
|
||||
} catch(Exception e) { // This should never happen.
|
||||
logger.error("""
|
||||
TERRA HAS BEEN DISABLED
|
||||
|
||||
@@ -77,12 +95,6 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
BukkitCommandAdapter command = new BukkitCommandAdapter(manager);
|
||||
|
||||
cmd.setExecutor(command);
|
||||
cmd.setTabCompleter(command);
|
||||
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new CommonListener(), this); // Register master event listener
|
||||
PaperUtil.checkPaper(this);
|
||||
|
||||
|
||||
@@ -17,7 +17,15 @@
|
||||
|
||||
package com.dfsek.terra.fabric;
|
||||
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import cloud.commandframework.fabric.FabricServerCommandManager;
|
||||
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import org.slf4j.Logger;
|
||||
@@ -43,5 +51,15 @@ public class FabricEntryPoint implements ModInitializer {
|
||||
// register the things
|
||||
Registry.register(Registry.CHUNK_GENERATOR, new Identifier("terra:terra"), FabricChunkGeneratorWrapper.CODEC);
|
||||
Registry.register(Registry.BIOME_SOURCE, new Identifier("terra:terra"), TerraBiomeSource.CODEC);
|
||||
|
||||
FabricServerCommandManager<CommandSender> manager = new FabricServerCommandManager<>(
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
serverCommandSource -> (CommandSender) serverCommandSource,
|
||||
commandSender -> (ServerCommandSource) commandSender
|
||||
);
|
||||
|
||||
manager.brigadierManager().setNativeNumberSuggestions(false);
|
||||
|
||||
TERRA_PLUGIN.getEventManager().callEvent(new CommandRegistrationEvent(manager));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user