Implement Event System and Basic Events
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -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
|
||||
*/
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
@@ -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)
|
||||
|
@@ -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);
|
||||
|
||||
}
|
@@ -6,6 +6,8 @@
|
||||
"mixins": [
|
||||
],
|
||||
"client": [
|
||||
"colors.BlockColorsMixin",
|
||||
"colors.ItemColorsMixin"
|
||||
],
|
||||
"server": [
|
||||
],
|
||||
|
Reference in New Issue
Block a user