Implement Event System and Basic Events

This commit is contained in:
2022-06-30 23:19:24 +02:00
parent d745821dad
commit 90237abca6
17 changed files with 300 additions and 4 deletions

View File

@@ -0,0 +1,43 @@
package me.hypherionmc.craterlib.client.events;
import me.hypherionmc.craterlib.events.Event;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.color.item.ItemColors;
/**
* @author HypherionSA
* @date 17/06/2022
*/
public class ColorRegistrationEvent {
public static class BLOCKS extends Event {
private BlockColors colors;
public BLOCKS() {}
public BLOCKS(BlockColors colors) {
this.colors = colors;
}
public BlockColors getColors() {
return colors;
}
}
public static class ITEMS extends Event {
private ItemColors colors;
public ITEMS() {}
public ITEMS(ItemColors colors) {
this.colors = colors;
}
public ItemColors getColors() {
return colors;
}
}
}

View File

@@ -0,0 +1,48 @@
package me.hypherionmc.craterlib.client.gui.tabs;
import me.hypherionmc.craterlib.platform.Services;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import java.util.function.Supplier;
/**
* @author HypherionSA
* @date 16/06/2022
*/
public class CreativeTabBuilder {
public static Builder builder(String modid, String tabid) {
return new Builder(modid, tabid);
}
public static final class Builder {
private Supplier<ItemStack> tabIcon;
private final String modid;
private final String tabid;
private String backgroundPrefix;
public Builder(String modid, String tabid) {
this.modid = modid;
this.tabid = tabid;
}
public Builder setIcon(Supplier<ItemStack> stack) {
this.tabIcon = stack;
return this;
}
public Builder setBackgroundPrefix(String prefix) {
this.backgroundPrefix = prefix;
return this;
}
public CreativeModeTab build() {
return Services.CLIENT_HELPER.tabBuilder(this.modid, this.tabid, this.tabIcon, this.backgroundPrefix);
}
}
}

View File

@@ -50,7 +50,7 @@ public class ModuleConfig {
* @param config - The config class to use
*/
public void registerAndSetup(ModuleConfig config) {
if (!configPath.exists() || configPath.length() < 10) {
if (!configPath.exists() || configPath.length() < 2) {
saveConfig(config);
} else {
migrateConfig(config);
@@ -67,7 +67,7 @@ public class ModuleConfig {
ObjectConverter converter = new ObjectConverter();
CommentedFileConfig config = CommentedFileConfig.builder(configPath).build();
/* Save the config and fire the reload event */
/* Save the config and fire the reload events */
converter.toConfig(conf, config);
config.save();
}
@@ -90,7 +90,7 @@ public class ModuleConfig {
}
/**
* INTERNAL METHOD - Upgrades the config files in the event the config structure changes
* INTERNAL METHOD - Upgrades the config files in the events the config structure changes
*
* @param conf - The config class to load
*/

View File

@@ -0,0 +1,32 @@
package me.hypherionmc.craterlib.events;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author HypherionSA
* @date 21/06/2022
*/
public class CraterEventBus {
private static final ConcurrentHashMap<Class<? extends Event>, List<IEventExecutor<?>>> map = new ConcurrentHashMap();
public static <T extends Event> void register(Class<T> clazz, IEventExecutor<T> handler) {
if (!map.containsKey(clazz)) map.put(clazz, new ArrayList<>());
map.get(clazz).add(handler);
}
public static boolean post(Event event) {
Class<? extends Event> clazz = event.getClass();
if (map.containsKey(clazz)) {
List<IEventExecutor<?>> handlers = map.get(clazz);
for (IEventExecutor handler : handlers) {
handler.execute(event);
if (event.isCancelled()) {
return false;
}
}
}
return true;
}
}

View File

@@ -0,0 +1,25 @@
package me.hypherionmc.craterlib.events;
/**
* @author HypherionSA
* @date 21/06/2022
*/
public class Event {
private boolean cancelled;
public boolean isCancellable() {
return false;
}
public void setCancelled(boolean canceled) {
if (!this.isCancellable()) {
throw new RuntimeException("Cannot cancel event " + this);
}
this.cancelled = canceled;
}
public boolean isCancelled() {
return cancelled;
}
}

View File

@@ -0,0 +1,10 @@
package me.hypherionmc.craterlib.events;
/**
* @author HypherionSA
* @date 21/06/2022
*/
@FunctionalInterface
public interface IEventExecutor<T extends Event> {
void execute(T event);
}

View File

@@ -0,0 +1,23 @@
package me.hypherionmc.craterlib.mixin.colors;
import me.hypherionmc.craterlib.client.events.ColorRegistrationEvent;
import me.hypherionmc.craterlib.events.CraterEventBus;
import net.minecraft.client.color.block.BlockColors;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
* @author HypherionSA
* @date 17/06/2022
*/
@Mixin(BlockColors.class)
public class BlockColorsMixin {
@Inject(method = "createDefault", at = @At("RETURN"))
private static void injectBlockColors(CallbackInfoReturnable<BlockColors> cir) {
CraterEventBus.post(new ColorRegistrationEvent.BLOCKS(cir.getReturnValue()));
}
}

View File

@@ -0,0 +1,24 @@
package me.hypherionmc.craterlib.mixin.colors;
import me.hypherionmc.craterlib.client.events.ColorRegistrationEvent;
import me.hypherionmc.craterlib.events.CraterEventBus;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.color.item.ItemColors;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
* @author HypherionSA
* @date 17/06/2022
*/
@Mixin(ItemColors.class)
public class ItemColorsMixin {
@Inject(method = "createDefault", at = @At("RETURN"))
private static void injectItemColors(BlockColors $$0, CallbackInfoReturnable<ItemColors> cir) {
CraterEventBus.post(new ColorRegistrationEvent.ITEMS(cir.getReturnValue()));
}
}

View File

@@ -2,6 +2,7 @@ package me.hypherionmc.craterlib.platform;
import me.hypherionmc.craterlib.Constants;
import me.hypherionmc.craterlib.platform.services.IPlatformHelper;
import me.hypherionmc.craterlib.platform.services.LibClientHelper;
import java.util.ServiceLoader;
@@ -9,6 +10,8 @@ public class Services {
public static final IPlatformHelper PLATFORM = load(IPlatformHelper.class);
public static final LibClientHelper CLIENT_HELPER = load(LibClientHelper.class);
public static <T> T load(Class<T> clazz) {
final T loadedService = ServiceLoader.load(clazz)

View File

@@ -0,0 +1,16 @@
package me.hypherionmc.craterlib.platform.services;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import java.util.function.Supplier;
/**
* @author HypherionSA
* @date 16/06/2022
*/
public interface LibClientHelper {
public CreativeModeTab tabBuilder(String modid, String tabid, Supplier<ItemStack> icon, String backgroundSuf);
}

View File

@@ -6,6 +6,8 @@
"mixins": [
],
"client": [
"colors.BlockColorsMixin",
"colors.ItemColorsMixin"
],
"server": [
],

View File

@@ -0,0 +1,33 @@
package me.hypherionmc.craterlib.client;
import me.hypherionmc.craterlib.platform.services.LibClientHelper;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import java.util.function.Supplier;
/**
* @author HypherionSA
* @date 16/06/2022
*/
class FabricClientHelper implements LibClientHelper {
@Override
public CreativeModeTab tabBuilder(String modid, String tabid, Supplier<ItemStack> icon, String backgroundSuf) {
FabricItemGroupBuilder tab = FabricItemGroupBuilder.create(new ResourceLocation(modid, tabid));
if (icon != null) {
tab.icon(icon);
}
CreativeModeTab tab1 = tab.build();
if (backgroundSuf != null && !backgroundSuf.isEmpty()) {
tab1.setBackgroundSuffix(backgroundSuf);
}
return tab1;
}
}

View File

@@ -0,0 +1 @@
me.hypherionmc.craterlib.client.FabricClientHelper

View File

@@ -0,0 +1,34 @@
package me.hypherionmc.craterlib.client;
import me.hypherionmc.craterlib.platform.services.LibClientHelper;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import java.util.function.Supplier;
/**
* @author HypherionSA
* @date 16/06/2022
*/
class ForgeClientHelper implements LibClientHelper {
@Override
public CreativeModeTab tabBuilder(String modid, String tabid, Supplier<ItemStack> icon, String backgroundSuf) {
CreativeModeTab tab = new CreativeModeTab(modid + "." + tabid) {
@Override
public ItemStack makeIcon() {
if (icon != null) {
return icon.get();
} else {
return ItemStack.EMPTY;
}
}
};
if (backgroundSuf != null && !backgroundSuf.isEmpty()) {
tab.setBackgroundSuffix(backgroundSuf);
}
return tab;
}
}

View File

@@ -0,0 +1 @@
me.hypherionmc.craterlib.client.ForgeClientHelper

View File

@@ -12,6 +12,7 @@ A library mod used by HypherionSA's mods. Mostly used by Hyper Lighting 2.
* Built in FluidTank and Energy systems for Forge/Fabric (Forge versions are just wrappers).
* Built in Optifine-Compat utilities
* Various utilities for Blockstates, LANG, Math and Rendering
* Cross Mod-Loader Events
* TODO: Built in Cross Mod-Loader Network system
* TODO: Various GUI widgets and Utilities
* TODO: Cross Mod-Loader Dynamic Lighting

View File

@@ -1,7 +1,7 @@
# Project
version_major=1
version_minor=0
version_patch=0
version_patch=6d
group=me.hypherionmc.craterlib
# Common