Skip to content

Commit

Permalink
Optimize code (#14)
Browse files Browse the repository at this point in the history
* 💎📕

* fix

* Even more changes

* move NeoEventUtils

* final

* interfaces

* remove unused code

* Some changes

* 💎

* more changes

* 秦始皇统一度量衡 be like

* 扔掉group

* 💎 💢

* clean

* So faq loom

* everything is fine 🔥

---------

Co-authored-by: TexTrue <[email protected]>
  • Loading branch information
RawDiamondMC and TexBlock authored Oct 4, 2024
1 parent 29e5e62 commit 765092f
Show file tree
Hide file tree
Showing 260 changed files with 2,693 additions and 1,424 deletions.
2 changes: 0 additions & 2 deletions base/common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
apply from: rootProject.file("gradle/scripts/klib-common.gradle")

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

dependencies {

}
68 changes: 68 additions & 0 deletions base/common/src/main/java/band/kessoku/lib/api/KessokuLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024 KessokuTeaTime
*
* Licensed under the GNU Lesser General Pubic License, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.api;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;

import org.jetbrains.annotations.UnmodifiableView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class KessokuLib {
private static final List<Class<?>> initializedModules = new ArrayList<>();

private KessokuLib() {
}

public static void loadModule(Class<?> moduleCommonClass) {
// Try to get module name
String moduleName;
try {
Field field = moduleCommonClass.getField("NAME");
if (!Modifier.isStatic(field.getModifiers()))
throw new IllegalArgumentException("NAME in " + moduleCommonClass.getPackageName() + " is not static!");
field.setAccessible(true);
moduleName = (String) field.get(null);
} catch (NoSuchFieldException e) {
getLogger().warn("no NAME found for {}! Using package name", moduleCommonClass.getPackageName());
moduleName = moduleCommonClass.getPackageName();
} catch (IllegalAccessException e) {
// Already set accessible, shouldn't be called
moduleName = moduleCommonClass.getPackageName();
}
initializedModules.add(moduleCommonClass);
getLogger().info("{} loaded!", moduleName);
}

public static boolean isModuleLoaded(Class<?> moduleCommonClass) {
return initializedModules.contains(moduleCommonClass);
}

@UnmodifiableView
public static List<Class<?>> getActiveModules() {
return Collections.unmodifiableList(initializedModules);
}

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

public static Logger getLogger() {
return LoggerFactory.getLogger("[Kessoku Lib]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.base;
package band.kessoku.lib.api.base;

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

public class KessokuBase {
public final class KessokuBase {
public static final String MOD_ID = "kessoku_base";
public static final Marker MARKER = MarkerFactory.getMarker("[KessokuBase]");
public static final String NAME = "Kessoku Base API";
public static final Marker MARKER = MarkerFactory.getMarker("[" + NAME +"]");
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package band.kessoku.lib.api.base;

import java.lang.reflect.Constructor;
import java.util.List;
import java.util.ServiceLoader;
import java.util.*;

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()));
}
import org.jetbrains.annotations.NotNull;

public static Logger getLogger() {
return LoggerFactory.getLogger("[KessokuLib]");
public final class KessokuUtils {
private KessokuUtils() {
}

public static <T> boolean isType(List<?> list, Class<T> type) {
for (Object element : list) {
public static <T> boolean isType(final List<?> list, final Class<T> type) {
for (final Object element : list) {
if (!(type.isInstance(element))) {
return false;
}
}
return true;
}
public static <V> V constructUnsafely(Class<V> cls) {

public static <V> V constructUnsafely(final Class<V> cls) {
try {
Constructor<V> constructor = cls.getDeclaredConstructor();
final Constructor<V> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

public static <K, V> @NotNull Set<K> getKeysByValue(final Map<K, V> map, final V value) {
final Set<K> result = new HashSet<>();
map.forEach((k, v) -> {
if (v.equals(value)) result.add(k);
});
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ApiStatus.Internal
package band.kessoku.lib.impl;

import org.jetbrains.annotations.ApiStatus;
1 change: 0 additions & 1 deletion base/fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import net.fabricmc.loom.util.ModPlatform

apply from: rootProject.file("gradle/scripts/klib-fabric.gradle")

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

kessoku {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.config;
package band.kessoku.lib.impl.base.fabric;

import band.kessoku.lib.api.KessokuLib;
import band.kessoku.lib.api.base.KessokuBase;

import net.fabricmc.api.ModInitializer;

public class KessokuConfigEntrypoint implements ModInitializer {
public final class KessokuBaseFabric implements ModInitializer {
@Override
public void onInitialize() {
KessokuLib.loadModule(KessokuBase.class);
}
}
4 changes: 2 additions & 2 deletions base/fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "kessoku_base",
"version": "${version}",
"name": "Kessoku Base",
"description": "Provide some basic API.",
"description": "Kessoku base API.",
"authors": [
"Kessoku Tea Time"
],
Expand All @@ -16,7 +16,7 @@
"icon": "icon.png",
"entrypoints": {
"main": [
"band.kessoku.lib.base.KessokuBaseEntrypoint"
"band.kessoku.lib.impl.base.fabric.KessokuBaseFabric"
]
},
"environment": "*",
Expand Down
1 change: 0 additions & 1 deletion base/neo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import net.fabricmc.loom.util.ModPlatform

apply from: rootProject.file("gradle/scripts/klib-neo.gradle")

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

kessoku {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.event.util;
package band.kessoku.lib.api.base.neoforge;

import java.util.function.Consumer;

import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.IEventBus;

import java.util.function.Consumer;
public final class NeoEventUtils {
private NeoEventUtils() {
}

public class NeoEventUtils {
public static <T extends Event> void registerEvent(IEventBus eventBus, Class<T> eventClass, Consumer<T> consumer) {
eventBus.addListener(EventPriority.HIGHEST, eventClass, consumer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 KessokuTeaTime
*
* Licensed under the GNU Lesser General Pubic License, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package band.kessoku.lib.impl.base.neoforge;

import band.kessoku.lib.api.KessokuLib;
import band.kessoku.lib.api.base.KessokuBase;

import net.neoforged.fml.common.Mod;

@Mod(KessokuBase.MOD_ID)
public final class KessokuBaseNeoforge {
public KessokuBaseNeoforge() {
KessokuLib.loadModule(KessokuBase.class);
}
}
2 changes: 1 addition & 1 deletion base/neo/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ modId = "kessoku_base"
version = "${version}"
displayName = "Kessoku Base"
description = '''
Provide some basic API.
Kessoku base API.
'''
logoFile = "icon.png"
authors = "Kessoku Tea Time"
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ subprojects {

allprojects {
apply plugin: "com.diffplug.spotless"
group = "band.kessoku.lib"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@
import java.util.List;
import java.util.Map;

public abstract class KesssokuExtension {
public abstract class KessokuExtension {
@Inject
protected abstract Project getProject();

protected static final List<String> MODULES = List.of(
"base",
"command",
"config",
"data",
"entity-events",
"entrypoint",
"event-base",
"keybinding",
"lifecycle-events",
"platform",
"registry"
);

public List<String> getModuleList() {
return MODULES;
}

public void library(String lib) {
Project project = this.getProject();
DependencyHandler dependencies = project.getDependencies();
Expand Down Expand Up @@ -77,13 +95,6 @@ public void moduleInclude(String name, String plat) {
"configuration", "namedElements"
));
dependencies.add("include", dependency);

LoomGradleExtensionAPI loom = project.getExtensions().getByType(LoomGradleExtensionAPI.class);
loom.mods(mods -> mods.register("kessoku-" + name + "-" + plat, settings -> {
Project depProject = project.project(":" + name + "-" + plat);
SourceSetContainer sourceSets = depProject.getExtensions().getByType(SourceSetContainer.class);
settings.sourceSet(sourceSets.getByName("main"), depProject);
}));
}

public void common(String name, ModPlatform platform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

import java.util.List;

public class KessokuGradlePlugin implements Plugin<PluginAware> {
public final class KessokuGradlePlugin implements Plugin<PluginAware> {

private static final List<String> NEO_GROUPS = List.of(
"net.neoforged",
"cpw.mods",
Expand All @@ -28,14 +29,15 @@ public void apply(@NotNull PluginAware target) {
settings.getGradle().getPluginManager().apply(KessokuGradlePlugin.class);
}
case Project project -> {
project.getExtensions().create("kessoku", KesssokuExtension.class);
project.getExtensions().create("kessoku", KessokuExtension.class);

additionalRepositories(project.getRepositories());
}
case Gradle gradle -> {
return;
}
default -> throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass());
default ->
throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package band.kessoku.gradle.plugin;

import net.fabricmc.loom.util.ModPlatform;

import java.util.Locale;

public enum PlatformIdentifier {
FABRIC("Fabric", ModPlatform.FABRIC),
NEO("Neo", ModPlatform.NEOFORGE)
;

private final String displayName;
private final ModPlatform modPlatform;

PlatformIdentifier(String displayName, ModPlatform modPlatform) {
this.displayName = displayName;
this.modPlatform = modPlatform;
}

/**
* Returns the lowercase ID of this mod platform.
*/
public String id() {
return name().toLowerCase(Locale.ROOT);
}

public String displayName() {
return displayName;
}
}
2 changes: 0 additions & 2 deletions command/common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
apply from: rootProject.file("gradle/scripts/klib-common.gradle")

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

kessoku {
modules(["base", "event-base"], "common")

testModules(["base", "event-base"], "common")
}
Loading

0 comments on commit 765092f

Please sign in to comment.