Merge pull request #76 from PolyhedralDev/functionregistry

Function Registry and Event System
This commit is contained in:
dfsek 2021-02-16 10:12:48 -07:00 committed by GitHub
commit b3a4c3af19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 388 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import com.dfsek.terra.getGitHash
val versionObj = Version("4", "1", "0", true)
val versionObj = Version("4", "2", "0", true)
allprojects {
version = versionObj

View File

@ -1,6 +1,7 @@
package com.dfsek.terra.api.core;
import com.dfsek.terra.api.LoaderRegistrar;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.platform.handle.ItemHandle;
import com.dfsek.terra.api.platform.handle.WorldHandle;
import com.dfsek.terra.api.platform.world.World;
@ -50,4 +51,6 @@ public interface TerraPlugin extends LoaderRegistrar {
}
DebugLogger getDebugLogger();
EventManager getEventManager();
}

View File

@ -0,0 +1,60 @@
package com.dfsek.terra.api.core.event;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.util.ReflectionUtil;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TerraEventManager implements EventManager {
private final Map<Class<? extends Event>, Map<EventListener, List<Method>>> listeners = new HashMap<>();
private final TerraPlugin main;
public TerraEventManager(TerraPlugin main) {
this.main = main;
}
@Override
public void callEvent(Event event) {
if(!listeners.containsKey(event.getClass())) return;
listeners.get(event.getClass()).forEach((eventListener, methods) -> methods.forEach(method -> {
try {
method.invoke(eventListener, event);
} catch(InvocationTargetException e) {
StringWriter writer = new StringWriter();
e.getTargetException().printStackTrace(new PrintWriter(writer));
main.getLogger().warning("Exception occurred during event handling:");
main.getLogger().warning(writer.toString());
main.getLogger().warning("Report this to the maintainers of " + eventListener.getClass().getCanonicalName());
} catch(Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
main.getLogger().warning("Exception occurred during event handling:");
main.getLogger().warning(writer.toString());
main.getLogger().warning("Report this to the maintainers of " + eventListener.getClass().getCanonicalName());
}
})
);
}
@SuppressWarnings("unchecked")
@Override
public void registerListener(EventListener listener) {
Class<? extends EventListener> listenerClass = listener.getClass();
Method[] methods = ReflectionUtil.getMethods(listenerClass);
for(Method method : methods) {
if(method.getParameterCount() != 1) continue; // Check that parameter count is only 1.
Class<?> eventParam = method.getParameterTypes()[0];
if(!Event.class.isAssignableFrom(eventParam)) continue; // Check that parameter is an Event.
method.setAccessible(true);
listeners.computeIfAbsent((Class<? extends Event>) eventParam, e -> new HashMap<>()).computeIfAbsent(listener, l -> new ArrayList<>()).add(method);
}
}
}

View File

@ -0,0 +1,16 @@
package com.dfsek.terra.api.core.event.events.config;
import com.dfsek.terra.api.core.event.Event;
import com.dfsek.terra.config.pack.ConfigPack;
public abstract class ConfigPackLoadEvent implements Event {
private final ConfigPack pack;
public ConfigPackLoadEvent(ConfigPack pack) {
this.pack = pack;
}
public ConfigPack getPack() {
return pack;
}
}

View File

@ -0,0 +1,12 @@
package com.dfsek.terra.api.core.event.events.config;
import com.dfsek.terra.config.pack.ConfigPack;
/**
* Called when a config pack has finished loading.
*/
public class ConfigPackPostLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPostLoadEvent(ConfigPack pack) {
super(pack);
}
}

View File

@ -0,0 +1,12 @@
package com.dfsek.terra.api.core.event.events.config;
import com.dfsek.terra.config.pack.ConfigPack;
/**
* Called before a config pack's registries are filled. At this point, the pack manifest has been loaded, and all registries are empty.
*/
public class ConfigPackPreLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPreLoadEvent(ConfigPack pack) {
super(pack);
}
}

View File

@ -0,0 +1,19 @@
package com.dfsek.terra.api.core.event.events.world;
import com.dfsek.terra.api.core.event.Event;
import com.dfsek.terra.world.TerraWorld;
/**
* Called upon initialization of a TerraWorld.
*/
public class TerraWorldLoadEvent implements Event {
private final TerraWorld world;
public TerraWorldLoadEvent(TerraWorld world) {
this.world = world;
}
public TerraWorld getWorld() {
return world;
}
}

View File

@ -28,6 +28,7 @@ import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.DirectBuffer;
import com.dfsek.terra.api.structures.structure.buffer.StructureBuffer;
import com.dfsek.terra.registry.FunctionRegistry;
import com.dfsek.terra.registry.config.LootRegistry;
import com.dfsek.terra.registry.config.ScriptRegistry;
import com.dfsek.terra.world.generation.math.SamplerCache;
@ -48,7 +49,7 @@ public class StructureScript {
private final TerraPlugin main;
String tempID;
public StructureScript(InputStream inputStream, TerraPlugin main, ScriptRegistry registry, LootRegistry lootRegistry, SamplerCache cache) throws ParseException {
public StructureScript(InputStream inputStream, TerraPlugin main, ScriptRegistry registry, LootRegistry lootRegistry, SamplerCache cache, FunctionRegistry functionRegistry) throws ParseException {
Parser parser;
try {
parser = new Parser(IOUtils.toString(inputStream));
@ -85,6 +86,8 @@ public class StructureScript {
.registerFunction("max", new BinaryNumberFunctionBuilder((number, number2) -> FastMath.max(number.doubleValue(), number2.doubleValue())))
.registerFunction("min", new BinaryNumberFunctionBuilder((number, number2) -> FastMath.min(number.doubleValue(), number2.doubleValue())));
functionRegistry.forEach(parser::registerFunction); // Register registry functions.
block = parser.parse();
this.id = parser.getID();
tempID = id;

View File

@ -0,0 +1,28 @@
package com.dfsek.terra.api.util;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;
public class ReflectionUtil {
public static Field[] getFields(@NotNull Class<?> type) {
Field[] result = type.getDeclaredFields();
Class<?> parentClass = type.getSuperclass();
if(parentClass != null) {
result = Stream.concat(Arrays.stream(result), Arrays.stream(getFields(parentClass))).toArray(Field[]::new);
}
return result;
}
public static Method[] getMethods(@NotNull Class<?> type) {
Method[] result = type.getDeclaredMethods();
Class<?> parentClass = type.getSuperclass();
if(parentClass != null) {
result = Stream.concat(Arrays.stream(result), Arrays.stream(getMethods(parentClass))).toArray(Method[]::new);
}
return result;
}
}

View File

@ -8,6 +8,8 @@ import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.LoaderRegistrar;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.events.config.ConfigPackPostLoadEvent;
import com.dfsek.terra.api.core.event.events.config.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.structures.loot.LootTable;
import com.dfsek.terra.api.structures.script.StructureScript;
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
@ -44,6 +46,7 @@ import com.dfsek.terra.config.templates.OreTemplate;
import com.dfsek.terra.config.templates.PaletteTemplate;
import com.dfsek.terra.config.templates.StructureTemplate;
import com.dfsek.terra.config.templates.TreeTemplate;
import com.dfsek.terra.registry.FunctionRegistry;
import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.registry.config.BiomeRegistry;
import com.dfsek.terra.registry.config.CarverRegistry;
@ -95,6 +98,7 @@ public class ConfigPack implements LoaderRegistrar {
private final CarverRegistry carverRegistry = new CarverRegistry();
private final NormalizerRegistry normalizerRegistry = new NormalizerRegistry();
private final FunctionRegistry functionRegistry = new FunctionRegistry();
private final AbstractConfigLoader abstractConfigLoader = new AbstractConfigLoader();
private final ConfigLoader selfLoader = new ConfigLoader();
@ -193,6 +197,7 @@ public class ConfigPack implements LoaderRegistrar {
}
private void load(long start, TerraPlugin main) throws ConfigException {
main.getEventManager().callEvent(new ConfigPackPreLoadEvent(this));
main.packPreLoadCallback(this);
for(Map.Entry<String, Double> var : template.getVariables().entrySet()) {
varScope.create(var.getKey(), var.getValue());
@ -201,7 +206,7 @@ public class ConfigPack implements LoaderRegistrar {
loader.open("structures/data", ".tesf").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) {
try {
StructureScript structureScript = new StructureScript(entry.getValue(), main, scriptRegistry, lootRegistry, samplerCache);
StructureScript structureScript = new StructureScript(entry.getValue(), main, scriptRegistry, lootRegistry, samplerCache, functionRegistry);
scriptRegistry.add(structureScript.getId(), structureScript);
} catch(com.dfsek.terra.api.structures.parser.exceptions.ParseException e) {
throw new LoadException("Unable to load script \"" + entry.getKey() + "\"", e);
@ -226,6 +231,8 @@ public class ConfigPack implements LoaderRegistrar {
.open("flora", ".yml").then(streams -> buildAll(new FloraFactory(), floraRegistry, abstractConfigLoader.load(streams, FloraTemplate::new), main)).close()
.open("biomes", ".yml").then(streams -> buildAll(new BiomeFactory(this), biomeRegistry, abstractConfigLoader.load(streams, () -> new BiomeTemplate(this, main)), main)).close();
main.packPostLoadCallback(this);
main.getEventManager().callEvent(new ConfigPackPostLoadEvent(this));
LangUtil.log("config-pack.loaded", Level.INFO, template.getID(), String.valueOf((System.nanoTime() - start) / 1000000D), template.getAuthor(), template.getVersion());
}
@ -300,4 +307,36 @@ public class ConfigPack implements LoaderRegistrar {
public BiomeProvider.BiomeProviderBuilder getBiomeProviderBuilder() {
return biomeProviderBuilder;
}
public FunctionRegistry getFunctionRegistry() {
return functionRegistry;
}
public NormalizerRegistry getNormalizerRegistry() {
return normalizerRegistry;
}
public CarverRegistry getCarverRegistry() {
return carverRegistry;
}
public FloraRegistry getFloraRegistry() {
return floraRegistry;
}
public LootRegistry getLootRegistry() {
return lootRegistry;
}
public OreRegistry getOreRegistry() {
return oreRegistry;
}
public PaletteRegistry getPaletteRegistry() {
return paletteRegistry;
}
public StructureRegistry getStructureRegistry() {
return structureRegistry;
}
}

View File

@ -0,0 +1,6 @@
package com.dfsek.terra.registry;
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
public class FunctionRegistry extends TerraRegistry<FunctionBuilder<?>> {
}

View File

@ -9,6 +9,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public abstract class TerraRegistry<T> implements TypeLoader<T> {
@ -59,6 +60,10 @@ public abstract class TerraRegistry<T> implements TypeLoader<T> {
objects.forEach((id, obj) -> consumer.accept(obj));
}
public void forEach(BiConsumer<String, T> consumer) {
objects.forEach(consumer);
}
public Set<T> entries() {
return new HashSet<>(objects.values());
}

View File

@ -1,6 +1,7 @@
package com.dfsek.terra.world;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.events.world.TerraWorldLoadEvent;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.block.BlockData;
@ -30,6 +31,7 @@ public class TerraWorld {
this.provider = config.getBiomeProviderBuilder().build(w.getSeed());
this.world = w;
air = main.getWorldHandle().createBlockData("minecraft:air");
main.getEventManager().callEvent(new TerraWorldLoadEvent(this));
safe = true;
}

View File

@ -10,6 +10,7 @@ import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.math.ProbabilityCollection;
import com.dfsek.terra.api.platform.handle.ItemHandle;
import com.dfsek.terra.api.platform.handle.WorldHandle;
@ -126,6 +127,11 @@ public class DistributionTest {
return new DebugLogger(Logger.getLogger("Terra"));
}
@Override
public EventManager getEventManager() {
return null;
}
@Override
public void register(TypeRegistry registry) {

View File

@ -0,0 +1,148 @@
package event;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.Event;
import com.dfsek.terra.api.core.event.EventListener;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.core.event.TerraEventManager;
import com.dfsek.terra.api.platform.handle.ItemHandle;
import com.dfsek.terra.api.platform.handle.WorldHandle;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.config.PluginConfig;
import com.dfsek.terra.config.lang.Language;
import com.dfsek.terra.debug.DebugLogger;
import com.dfsek.terra.registry.ConfigRegistry;
import com.dfsek.terra.world.TerraWorld;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.logging.Logger;
public class EventTest {
public TerraPlugin main = new TerraPlugin() {
private final Logger logger = Logger.getLogger("Terra");
private final EventManager eventManager = new TerraEventManager(this);
@Override
public WorldHandle getWorldHandle() {
return null;
}
@Override
public boolean isEnabled() {
return false;
}
@Override
public TerraWorld getWorld(World world) {
return null;
}
@Override
public Logger getLogger() {
return logger;
}
@Override
public PluginConfig getTerraConfig() {
return null;
}
@Override
public File getDataFolder() {
return null;
}
@Override
public boolean isDebug() {
return false;
}
@Override
public Language getLanguage() {
return null;
}
@Override
public ConfigRegistry getRegistry() {
return null;
}
@Override
public void reload() {
}
@Override
public ItemHandle getItemHandle() {
return null;
}
@Override
public void saveDefaultConfig() {
}
@Override
public String platformName() {
return null;
}
@Override
public DebugLogger getDebugLogger() {
return null;
}
@Override
public void register(TypeRegistry registry) {
}
@Override
public EventManager getEventManager() {
return eventManager;
}
};
@Test
public void eventTest() {
EventManager eventManager = main.getEventManager();
eventManager.registerListener(new TestListener());
eventManager.registerListener(new TestListener2());
TestEvent event = new TestEvent(4);
eventManager.callEvent(event);
eventManager.registerListener(new TestListenerException());
TestEvent event2 = new TestEvent(5);
eventManager.callEvent(event2);
}
static class TestListener implements EventListener {
public void doThing(TestEvent event) {
System.out.println("Event value: " + event.value);
}
}
static class TestListener2 implements EventListener {
public void doThing(TestEvent event) {
System.out.println("Event value 2: " + event.value);
}
}
static class TestListenerException implements EventListener {
public void doThing(TestEvent event) {
throw new RuntimeException("bazinga: " + event.value);
}
}
static class TestEvent implements Event {
private final int value;
TestEvent(int value) {
this.value = value;
}
}
}

View File

@ -2,6 +2,8 @@ package com.dfsek.terra.bukkit;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.core.event.TerraEventManager;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.MaterialData;
import com.dfsek.terra.api.platform.handle.ItemHandle;
@ -56,6 +58,8 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
private final GenericLoaders genericLoaders = new GenericLoaders(this);
private DebugLogger debugLogger;
private final EventManager eventManager = new TerraEventManager(this);
public static final Version BUKKIT_VERSION;
static {
@ -111,6 +115,11 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
return debugLogger;
}
@Override
public EventManager getEventManager() {
return eventManager;
}
@Override
public void onDisable() {
BukkitChunkGeneratorWrapper.saveAll();

View File

@ -2,6 +2,8 @@ package com.dfsek.terra.fabric;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.core.event.TerraEventManager;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.MaterialData;
import com.dfsek.terra.api.platform.handle.ItemHandle;
@ -68,6 +70,8 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
private final Map<Long, TerraWorld> worldMap = new HashMap<>();
private static TerraFabricPlugin instance;
private final EventManager eventManager = new TerraEventManager(this);
public static TerraFabricPlugin getInstance() {
return instance;
}
@ -285,4 +289,9 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
}
}
@Override
public EventManager getEventManager() {
return eventManager;
}
}

View File

@ -2,6 +2,8 @@ package com.dfsek.terra;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventManager;
import com.dfsek.terra.api.core.event.TerraEventManager;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.MaterialData;
import com.dfsek.terra.api.platform.handle.ItemHandle;
@ -26,6 +28,7 @@ public class StandalonePlugin implements TerraPlugin {
private final ConfigRegistry registry = new ConfigRegistry();
private final PluginConfig config = new PluginConfig();
private final RawWorldHandle worldHandle = new RawWorldHandle();
private final EventManager eventManager = new TerraEventManager(this);
@Override
public WorldHandle getWorldHandle() {
@ -115,4 +118,9 @@ public class StandalonePlugin implements TerraPlugin {
registry.loadAll(this);
config.load(this);
}
@Override
public EventManager getEventManager() {
return eventManager;
}
}