diff --git a/dependencies.gradle b/dependencies.gradle index 4d42819..2382aed 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,7 +1,7 @@ dependencies { compileOnly(libs.jetbrains.annotations) - testJarJar(testApi(libs.testframework.get())) + testImplementation(libs.testframework.get()) // Recipe Viewers compileOnly(forge.bundles.jei) diff --git a/gradle.properties b/gradle.properties index 99e33fa..06606c1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.parallel = true org.gradle.caching = true # Mod Info -mod_id = synceddata +mod_id = synced mod_name = SyncedData mod_version = 0.0.2 mod_description = diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6748278..6f23033 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] minecraft = "1.26.1.1" -neoForge = "26.1.0.19-beta" +neoForge = "26.1.1.15-beta" loader = "4" shadow = "9.4.0" spotless = "8.4.0" diff --git a/gradle/scripts/moddevgradle.gradle b/gradle/scripts/moddevgradle.gradle index d8b43c9..8ecc601 100644 --- a/gradle/scripts/moddevgradle.gradle +++ b/gradle/scripts/moddevgradle.gradle @@ -42,6 +42,8 @@ neoForge { sourceSet = sourceSets.clientExtra ideName = "Client" + + gameDirectory.set(file('run/client')) systemProperty('neoforge.enabledGameTestNamespaces', project.mod_id) } diff --git a/gradle/scripts/spotless.gradle b/gradle/scripts/spotless.gradle index 9a522b8..5ecb0f9 100644 --- a/gradle/scripts/spotless.gradle +++ b/gradle/scripts/spotless.gradle @@ -38,7 +38,7 @@ spotless { targetExclude 'src/generated/resources/**' biome() trimTrailingWhitespace() - leadingSpacesToTabs(2) + leadingSpacesToTabs() endWithNewline() } } diff --git a/src/main/java/com/modularmc/synceddata/SyncedData.java b/src/main/java/com/modularmc/synceddata/SyncedData.java new file mode 100644 index 0000000..e4a4d09 --- /dev/null +++ b/src/main/java/com/modularmc/synceddata/SyncedData.java @@ -0,0 +1,159 @@ +package com.modularmc.synceddata; + +import com.modularmc.synceddata.utils.FormattingUtil; + +import net.minecraft.client.Minecraft; +import net.minecraft.resources.Identifier; +import net.minecraft.server.MinecraftServer; +import net.neoforged.fml.ModList; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.fml.loading.FMLPaths; +import net.neoforged.neoforge.data.loading.DatagenModLoader; +import net.neoforged.neoforge.server.ServerLifecycleHooks; + +import com.mojang.serialization.Codec; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.nio.file.Path; + +@Mod(SyncedData.MOD_ID) +public class SyncedData { + + public static final String MOD_ID = "synced"; + public static final String MOD_NAME = "SyncedData"; + public static final Logger LOGGER = LogManager.getLogger(MOD_NAME); + public static final Codec GTCEU_ID = Codec.STRING.comapFlatMap( + str -> Identifier.read(appendIdString(str)), + s -> s.getNamespace().equals(MOD_ID) ? s.getPath() : s.toString()); + public static final Path SYNCED_FOLDER = getGameDir().resolve("synced"); + private static final Identifier TEMPLATE_LOCATION = Identifier.fromNamespaceAndPath(MOD_ID, ""); + + public static Identifier id(String path) { + if (path.isBlank()) { + return TEMPLATE_LOCATION; + } + + int i = path.indexOf(':'); + if (i > 0) { + return Identifier.tryParse(path); + } else if (i == 0) { + path = path.substring(i + 1); + } + // only convert it to camel_case if it has any uppercase to begin with + if (FormattingUtil.hasUpperCase(path)) { + path = FormattingUtil.toLowerCaseUnderscore(path); + } + return TEMPLATE_LOCATION.withPath(path); + } + + public static String appendIdString(String id) { + int i = id.indexOf(':'); + if (i > 0) { + return id; + } else if (i == 0) { + return MOD_ID + id; + } else { + return MOD_ID + ":" + id; + } + } + + /** + * 是否在生产环境中运行 + * + * @return if we're running in a production environment + */ + public static boolean isProd() { + return FMLEnvironment.isProduction(); + } + + /** + * 是否不在生产环境中运行 + * + * @return if we're not running in a production environment + */ + public static boolean isDev() { + return !isProd() || isDataGen(); + } + + /** + * 是否正在运行数据生成 + * + * @return if we're running data generation + */ + public static boolean isDataGen() { + return DatagenModLoader.isRunningDataGen(); + } + + /** + * 一个友好的提醒:服务器实例仅在服务器端填充,因此请进行空/端检查! + * A friendly reminder that the server instance is populated on the server side only, so null/side check it! + * + * @return 当前的Minecraft服务器实例 the current minecraft server instance + */ + public static MinecraftServer getMinecraftServer() { + return ServerLifecycleHooks.getCurrentServer(); + } + + /** + * 检查指定mod是否已加载 + * + * @param modId 要检查的mod ID the mod id to check for + * @return 指定ID的mod是否已加载 if the mod whose id is {@code modId} is loaded or not + */ + public static boolean isModLoaded(String modId) { + return ModList.get().isLoaded(modId); + } + + /** + * 对于异步操作使用此方法,否则使用 {@link SyncedData isClientSide} + * For async stuff use this, otherwise use {@link SyncedData isClientSide} + * + * @return 当前线程是否为客户端线程 if the current thread is the client thread + */ + public static boolean isClientThread() { + return isClientSide() && Minecraft.getInstance().isSameThread(); + } + + /** + * 游戏是否为物理客户端,例如不是专用服务器 + * + * @return if the game is the PHYSICAL client, e.g. not a dedicated server. + * @apiNote 不要使用此方法来检查你是否在服务器线程上执行端特定操作!它不适用于此。请使用 {@link #isClientThread()} 代替。 + * Do not use this to check if you're currently on the server thread for side-specific actions! + * It does NOT work for that. Use {@link #isClientThread()} instead. + * @see #isClientThread() + */ + public static boolean isClientSide() { + return FMLEnvironment.getDist().isClient(); + } + + /** + * 此检查在客户端和服务器上并不相同! + * This check isn't the same for client and server! + * + * @return 在客户端上访问当前实例{@link net.minecraft.world.level.Level Level}是否安全,或者在服务器上访问任何level是否安全。 + * if it's safe to access the current instance {@link net.minecraft.world.level.Level Level} on client or if + * it's safe to access any level on server. + */ + public static boolean canGetServerLevel() { + if (isClientSide()) { + return Minecraft.getInstance().level != null; + } + var server = getMinecraftServer(); + return server != null && + !(server.isStopped() || server.isShutdown() || !server.isRunning() || server.isCurrentlySaving()); + } + + /** + * 获取Minecraft实例目录的路径 + * + * @return Minecraft实例目录的路径 the path to the minecraft instance directory + */ + public static Path getGameDir() { + return FMLPaths.GAMEDIR.get(); + } + + public static class Mods {} +} diff --git a/src/main/java/com/modularmc/synceddata/Synceddata.java b/src/main/java/com/modularmc/synceddata/Synceddata.java deleted file mode 100644 index 9f1e05a..0000000 --- a/src/main/java/com/modularmc/synceddata/Synceddata.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.modularmc.synceddata; - -import com.mojang.logging.LogUtils; -import net.minecraft.client.Minecraft; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.core.registries.Registries; -import net.minecraft.network.chat.Component; -import net.minecraft.world.food.FoodProperties; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.CreativeModeTab; -import net.minecraft.world.item.CreativeModeTabs; -import net.minecraft.world.item.Item; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.state.BlockBehaviour; -import net.minecraft.world.level.material.MapColor; -import net.neoforged.api.distmarker.Dist; -import net.neoforged.bus.api.IEventBus; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.ModContainer; -import net.neoforged.fml.common.EventBusSubscriber; -import net.neoforged.fml.common.Mod; -import net.neoforged.fml.config.ModConfig; -import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; -import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -import net.neoforged.neoforge.common.NeoForge; -import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; -import net.neoforged.neoforge.event.server.ServerStartingEvent; -import net.neoforged.neoforge.registries.DeferredBlock; -import net.neoforged.neoforge.registries.DeferredHolder; -import net.neoforged.neoforge.registries.DeferredItem; -import net.neoforged.neoforge.registries.DeferredRegister; -import org.slf4j.Logger; - -// The value here should match an entry in the META-INF/neoforge.mods.toml file -@Mod(Synceddata.MODID) -public class Synceddata { - // Define mod id in a common place for everything to reference - public static final String MODID = "synceddata"; - // Directly reference a slf4j logger - private static final Logger LOGGER = LogUtils.getLogger(); -} diff --git a/src/main/java/com/modularmc/synceddata/core/mixins/SyncedMixinPlugin.java b/src/main/java/com/modularmc/synceddata/core/mixins/SyncedMixinPlugin.java new file mode 100644 index 0000000..713c1bd --- /dev/null +++ b/src/main/java/com/modularmc/synceddata/core/mixins/SyncedMixinPlugin.java @@ -0,0 +1,3 @@ +package com.modularmc.synceddata.core.mixins; + +public class SyncedMixinPlugin {} diff --git a/src/main/java/com/modularmc/synceddata/utils/FormattingUtil.java b/src/main/java/com/modularmc/synceddata/utils/FormattingUtil.java new file mode 100644 index 0000000..83e02bc --- /dev/null +++ b/src/main/java/com/modularmc/synceddata/utils/FormattingUtil.java @@ -0,0 +1,49 @@ +package com.modularmc.synceddata.utils; + +public class FormattingUtil { + + /** + * Check if {@code string} has any uppercase characters. + * + * @param string the string to check + * @return if the string has any uppercase characters. + */ + public static boolean hasUpperCase(String string) { + for (int i = 0; i < string.length(); i++) { + char ch = string.charAt(i); + if (Character.isUpperCase(ch)) return true; + } + return false; + } + + /** + * Does almost the same thing as {@code UPPER_CAMEL.to(LOWER_UNDERSCORE, string)}, + * but it also inserts underscores between words and numbers. + * + * @param string Any string with ASCII characters. + * @return A string that is all lowercase, with underscores inserted before word/number boundaries: + * + *
+     *         
{@code "maragingSteel300" -> "maraging_steel_300"} + *
{@code "gtceu:maraging_steel_300" -> "gtceu:maraging_steel_300"} + *
{@code "maragingSteel_300" -> "maraging_steel_300"} + *
{@code "maragingSTEEL_300" -> "maraging_steel_300"} + *
{@code "MARAGING_STEEL_300" -> "maraging_steel_300"} + *
+ */ + public static String toLowerCaseUnderscore(String string) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + char curChar = string.charAt(i); + result.append(Character.toLowerCase(curChar)); + if (i == string.length() - 1) break; + + char nextChar = string.charAt(i + 1); + if (curChar == '_' || nextChar == '_') continue; + boolean nextIsUpper = Character.isUpperCase(nextChar); + if (Character.isUpperCase(curChar) && nextIsUpper) continue; + if (nextIsUpper || Character.isDigit(curChar) ^ Character.isDigit(nextChar)) result.append('_'); + } + return result.toString(); + } +} diff --git a/src/main/resources/META-INF/enum_extensions.json b/src/main/resources/META-INF/enum_extensions.json index 3fde4b2..14aa6f5 100644 --- a/src/main/resources/META-INF/enum_extensions.json +++ b/src/main/resources/META-INF/enum_extensions.json @@ -1,4 +1,3 @@ { - "entries": [ - ] + "entries": [] } diff --git a/src/main/resources/assets/synceddata/lang/en_us.json b/src/main/resources/assets/synceddata/lang/en_us.json index a6c3585..cc4c85b 100644 --- a/src/main/resources/assets/synceddata/lang/en_us.json +++ b/src/main/resources/assets/synceddata/lang/en_us.json @@ -1,5 +1,5 @@ { - "itemGroup.synceddata": "Example Mod Tab", - "block.synceddata.example_block": "Example Block", - "item.synceddata.example_item": "Example Item" + "itemGroup.synceddata": "Example Mod Tab", + "block.synceddata.example_block": "Example Block", + "item.synceddata.example_item": "Example Item" } diff --git a/src/main/resources/synced.mixins.json b/src/main/resources/synced.mixins.json new file mode 100644 index 0000000..0687f8b --- /dev/null +++ b/src/main/resources/synced.mixins.json @@ -0,0 +1,14 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "com.modularmc.synceddata.core.mixins", + "compatibilityLevel": "JAVA_25", + "mixins": [], + "client": [], + "injectors": { + "defaultRequire": 1 + }, + "overwrites": { + "requireAnnotations": true + } +} diff --git a/src/main/resources/synceddata.mixins.json b/src/main/resources/synceddata.mixins.json deleted file mode 100644 index d51419e..0000000 --- a/src/main/resources/synceddata.mixins.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "required": true, - "minVersion": "0.8", - "package": "com.modularmc.synceddata.core.mixin", - "compatibilityLevel": "JAVA_25", - "mixins": [ - ], - "client": [ - ], - "injectors": { - "defaultRequire": 1 - }, - "overwrites": { - "requireAnnotations": true - } -}