Skip to content

Commit

Permalink
add keybind, add logger, clean
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Aug 8, 2024
1 parent 3adf4d4 commit c3cfccb
Show file tree
Hide file tree
Showing 46 changed files with 320 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package band.kessoku.lib.base;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class KessokuBase {
public static final String MOD_ID = "kessoku_base";
public static final Marker MARKER = MarkerFactory.getMarker("[KessokuBase]");
}
7 changes: 7 additions & 0 deletions base-common/src/main/java/band/kessoku/lib/base/ModUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package band.kessoku.lib.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ServiceLoader;

public class ModUtils {
public static <T> T loadService(Class<T> clazz) {
return ServiceLoader.load(clazz).findFirst().orElseThrow(() -> new AssertionError("No impl found for " + clazz.getName()));
}

public static Logger getLogger() {
return LoggerFactory.getLogger("[KessokuLib]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
public class KessokuBaseEntrypoint implements ModInitializer {
@Override
public void onInitialize() {
ModUtils.getLogger().info(KessokuBase.MARKER, "KessokuLib-Base is loaded!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
@Mod(KessokuBase.MOD_ID)
public class KessokuBaseEntrypoint {
public KessokuBaseEntrypoint() {

ModUtils.getLogger().info(KessokuBase.MARKER, "KessokuLib-Base is loaded!");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package band.kessoku.lib.command;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class KessokuCommand {
public static final String MOD_ID = "kessoku_command";
public static final Marker MARKER = MarkerFactory.getMarker("[KessokuCommand]");
}
1 change: 1 addition & 0 deletions command-fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ group = "band.kessoku.lib.command"
base.archivesName = rootProject.name + "-command"

dependencies {
moduleImplementation(project(":base-common"))
moduleImplementation(project(":event-base-common"))

common(project(path: ':command-common', configuration: 'namedElements')) { transitive false }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package band.kessoku.lib.command;

import band.kessoku.lib.base.ModUtils;
import band.kessoku.lib.command.impl.KessokuCommandImpl;

import net.fabricmc.api.ModInitializer;

public class KessokuCommandEntrypoint implements ModInitializer {
@Override
public void onInitialize() {
ModUtils.getLogger().info(KessokuCommand.MARKER, "KessokuLib-Command is Loaded!");
KessokuCommandImpl.registerCommonEvents();
}
}
1 change: 1 addition & 0 deletions command-neo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ group = "band.kessoku.lib.command"
base.archivesName = rootProject.name + "-command"

dependencies {
moduleImplementation(project(":base-common"))
moduleImplementation(project(":event-base-common"))
moduleImplementation(project(":event-base-neo"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package band.kessoku.lib.command;

import band.kessoku.lib.base.ModUtils;
import band.kessoku.lib.command.impl.KessokuCommandImpl;

import net.neoforged.bus.api.IEventBus;
Expand All @@ -10,7 +11,7 @@
public class KessokuCommandEntrypoint {
public KessokuCommandEntrypoint(IEventBus modEventBus) {
var forgeEventBus = NeoForge.EVENT_BUS;

ModUtils.getLogger().info(KessokuCommand.MARKER, "KessokuLib-Command is Loaded!");
KessokuCommandImpl.registerCommonEvents(forgeEventBus);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package band.kessoku.lib.event;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class KessokuEventBase {
public static final String MOD_ID = "kessoku_event_base";
public static final Marker MARKER = MarkerFactory.getMarker("[KessokuEventBase]");
}
1 change: 1 addition & 0 deletions event-base-fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ group = "band.kessoku.lib.event"
base.archivesName = rootProject.name + "-event-base"

dependencies {
moduleImplementation(project(":base-common"))
common(project(path: ':event-base-common', configuration: 'namedElements')) { transitive false }
shadowBundle project(path: ':event-base-common', configuration: 'transformProductionFabric')
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package band.kessoku.lib.event;

import band.kessoku.lib.base.ModUtils;
import net.fabricmc.api.ModInitializer;

public class KessokuEventBaseEntrypoint implements ModInitializer {
@Override
public void onInitialize() {
ModUtils.getLogger().info(KessokuEventBase.MARKER, "KessokuLib-EventBase is Loaded!");
}
}
1 change: 1 addition & 0 deletions event-base-neo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ group = "band.kessoku.lib.event"
base.archivesName = rootProject.name + "-event-base"

dependencies {
moduleImplementation(project(":base-common"))
common(project(path: ':event-base-common', configuration: 'namedElements')) { transitive false }
shadowBundle project(path: ':event-base-common', configuration: 'transformProductionNeoForge')
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.parallel=true
org.gradle.parallel=true
fabric.loom.multiProjectOptimisation=true
2 changes: 1 addition & 1 deletion gradle/scripts/kessokulib-fabric.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ processResources {

shadowJar {
configurations = [project.configurations.shadowBundle]
archiveClassifier = 'dev-shadow'
archiveClassifier.set("dev-shadow")
}

remapJar {
Expand Down
4 changes: 2 additions & 2 deletions gradle/scripts/kessokulib-neo.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ processResources {

shadowJar {
configurations = [project.configurations.shadowBundle]
archiveClassifier = 'dev-shadow'
archiveClassifier.set("dev-shadow")
}

remapJar {
input.set shadowJar.archiveFile
inputFile.set shadowJar.archiveFile
}
8 changes: 8 additions & 0 deletions keybind-common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apply from: rootProject.file("gradle/scripts/kessokulib-common.gradle")

group = "band.kessoku.lib.keybind"
base.archivesName = rootProject.name + "-keybind"

dependencies {
moduleImplementation(project(":base-common"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package band.kessoku.lib.keybind;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class KessokuKeybind {
public static final String MOD_ID = "kessoku_keybind";
public static final Marker MARKER = MarkerFactory.getMarker("[KessokuKeybind]");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package band.kessoku.lib.keybind.api;

import band.kessoku.lib.keybind.impl.KessokuKeyBindService;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

public interface KeyBindRegister {
static KeyBindRegister getInstance() {
return KessokuKeyBindService.getRegister();
}

boolean addCategory(String categoryTranslationKey);
KeyBinding register(KeyBinding keyBinding);
InputUtil.Key getBoundKey(KeyBinding keyBinding);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package band.kessoku.lib.keybind.impl;

import band.kessoku.lib.base.ModUtils;
import band.kessoku.lib.keybind.api.KeyBindRegister;

public class KessokuKeyBindService {
private static final KeyBindRegister register = ModUtils.loadService(KeyBindRegister.class);

public static KeyBindRegister getRegister() {
return register;
}
}
11 changes: 11 additions & 0 deletions keybind-fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply from: rootProject.file("gradle/scripts/kessokulib-fabric.gradle")

group = "band.kessoku.lib.keybind"
base.archivesName = rootProject.name + "-keybind"

dependencies {
moduleImplementation(project(":base-common"))

common(project(path: ':keybind-common', configuration: 'namedElements')) { transitive false }
shadowBundle(project(path: ':keybind-common', configuration: 'transformProductionFabric'))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package band.kessoku.lib.keybind;

import band.kessoku.lib.base.ModUtils;
import net.fabricmc.api.ClientModInitializer;

public class KessokuKeybindEntrypoint implements ClientModInitializer {
@Override
public void onInitializeClient() {
ModUtils.getLogger().info(KessokuKeybind.MARKER, "KessokuKeybind is loaded!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package band.kessoku.lib.keybind.impl;

import band.kessoku.lib.keybind.api.KeyBindRegister;
import com.google.auto.service.AutoService;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.impl.client.keybinding.KeyBindingRegistryImpl;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

@AutoService(KeyBindRegister.class)
public class KeyBindRegisterImpl implements KeyBindRegister {
@Override
public boolean addCategory(String categoryTranslationKey) {
return KeyBindingRegistryImpl.addCategory(categoryTranslationKey);
}

@Override
public KeyBinding register(KeyBinding keyBinding) {
return KeyBindingHelper.registerKeyBinding(keyBinding);
}

@Override
public InputUtil.Key getBoundKey(KeyBinding keyBinding) {
return KeyBindingHelper.getBoundKeyOf(keyBinding);
}
}
12 changes: 12 additions & 0 deletions keybind-neo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply from: rootProject.file("gradle/scripts/kessokulib-neo.gradle")

group = "band.kessoku.lib.keybind"
base.archivesName = rootProject.name + "-keybind"

dependencies {
moduleImplementation(project(":base-common"))
moduleImplementation(project(":event-base-neo"))

common(project(path: ':keybind-common', configuration: 'namedElements')) { transitive false }
shadowBundle(project(path: ':keybind-common', configuration: 'transformProductionNeoForge'))
}
1 change: 1 addition & 0 deletions keybind-neo/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
loom.platform=neoforge
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package band.kessoku.lib.keybind;

import band.kessoku.lib.base.ModUtils;
import band.kessoku.lib.keybind.impl.KeyBindRegisterImpl;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLLoader;

@Mod(value = KessokuKeybind.MOD_ID, dist = Dist.CLIENT)
public class KessokuKeyBindEntrypoint {
public KessokuKeyBindEntrypoint(IEventBus modEventBus) {
if (FMLLoader.getDist().isClient()) {
ModUtils.getLogger().info(KessokuKeybind.MARKER, "KessokuKeybind is loaded!");
KeyBindRegisterImpl.registerEvent(modEventBus);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package band.kessoku.lib.keybind.impl;

import band.kessoku.lib.event.util.NeoEventUtils;
import band.kessoku.lib.keybind.api.KeyBindRegister;
import band.kessoku.lib.keybind.mixin.KeyBindingAccessor;
import com.google.auto.service.AutoService;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@AutoService(KeyBindRegister.class)
public class KeyBindRegisterImpl implements KeyBindRegister {
private static final List<KeyBinding> KEY_BINDINGS = new ReferenceArrayList<>();
private static boolean processed;

private static Map<String, Integer> getCategoryMap() {
return KeyBindingAccessor.kessoku$getCategoryMap();
}

@Override
public boolean addCategory(String categoryTranslationKey) {
Map<String, Integer> map = getCategoryMap();

if (map.containsKey(categoryTranslationKey)) {
return false;
}

Optional<Integer> largest = map.values().stream().max(Integer::compareTo);
int largestInt = largest.orElse(0);
map.put(categoryTranslationKey, largestInt + 1);
return true;
}

@Override
public KeyBinding register(KeyBinding keyBinding) {
if (processed) {
throw new IllegalStateException("Key bindings have already been processed");
}

for (KeyBinding existingKeyBindings : KEY_BINDINGS) {
if (existingKeyBindings == keyBinding) {
throw new IllegalArgumentException("Attempted to register a key binding twice: " + keyBinding.getTranslationKey());
} else if (existingKeyBindings.getTranslationKey().equals(keyBinding.getTranslationKey())) {
throw new IllegalArgumentException("Attempted to register two key bindings with equal ID: " + keyBinding.getTranslationKey() + "!");
}
}

// This will do nothing if the category already exists.
addCategory(keyBinding.getCategory());
KEY_BINDINGS.add(keyBinding);
return keyBinding;
}

@Override
public InputUtil.Key getBoundKey(KeyBinding keyBinding) {
return ((KeyBindingAccessor) keyBinding).kessoku$getBoundKey();
}

public static void registerEvent(IEventBus eventBus) {
NeoEventUtils.registerEvent(eventBus, RegisterKeyMappingsEvent.class, event -> {
KEY_BINDINGS.forEach(event::register);
processed = true;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package band.kessoku.lib.keybind.mixin;

import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Map;

@Mixin(KeyBinding.class)
public interface KeyBindingAccessor {
@Accessor("CATEGORY_ORDER_MAP")
static Map<String, Integer> kessoku$getCategoryMap() {
throw new AssertionError();
}

@Accessor("boundKey")
InputUtil.Key kessoku$getBoundKey();
}
Loading

0 comments on commit c3cfccb

Please sign in to comment.