Skip to content

Commit

Permalink
do not load client class in server context
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Aug 20, 2022
1 parent 506bdb4 commit 38e7c68
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package possible_triangle.skygrid.world

import net.minecraft.core.RegistryAccess
import net.minecraft.server.dedicated.DedicatedServerProperties
import net.minecraft.world.level.levelgen.WorldGenSettings
import possible_triangle.skygrid.SkygridMod
import kotlin.random.Random

object ServerProvider {

fun fromServerProperties(registries: RegistryAccess, properties: DedicatedServerProperties.WorldGenProperties): WorldGenSettings? {
val levelType = properties.levelType
if (levelType != SkygridMod.MOD_ID) return null

val seed = properties.levelSeed.takeIf { it.isNotEmpty() }?.let {
try {
it.toLong()
} catch (ex: NumberFormatException) {
it.hashCode().toLong()
}
}.takeIf { it != 0L } ?: Random.nextLong()

return SkygridChunkGenerator.createSettings(registries, seed, false)
}

}
2 changes: 1 addition & 1 deletion Fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
minecraft "com.mojang:minecraft:${minecraft_version}"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_api_version}"
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
implementation project(":Common")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import possible_triangle.skygrid.world.ServerProvider;
import possible_triangle.skygrid.world.SkygridGenerator;

@Mixin(WorldGenSettings.class)
public class WorldGenSettingsMixin {

@Inject(at = @At("RETURN"), method = "create(Lnet/minecraft/core/RegistryAccess;Lnet/minecraft/server/dedicated/DedicatedServerProperties$WorldGenProperties;)Lnet/minecraft/world/level/levelgen/WorldGenSettings;", cancellable = true)
private static void createSettings(RegistryAccess registries, DedicatedServerProperties.WorldGenProperties properties, CallbackInfoReturnable<WorldGenSettings> callback) {
var settings = SkygridGenerator.INSTANCE.fromServerProperties(registries, properties);
var settings = ServerProvider.INSTANCE.fromServerProperties(registries, properties);
if (settings != null) callback.setReturnValue(settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import possible_triangle.skygrid.world.SkygridGenerator
object SkygridFabric : ModInitializer, ClientModInitializer {

override fun onInitialize() {
SkygridMod.LOGGER.info("Common Initialize")
SkygridMod.init()
SkygridMod.setup()
FabricPlatformHelper.register()


XMLResource.register {
val listener = FabricReloadListener(it, it.path)
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(listener)
Expand All @@ -38,7 +36,6 @@ object SkygridFabric : ModInitializer, ClientModInitializer {
}

override fun onInitializeClient() {
SkygridMod.LOGGER.info("Client Initialize")
WorldPresetAccessor.presets().add(SkygridGenerator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ import kotlin.random.Random

object SkygridGenerator : WorldPreset("${MOD_ID}.${MOD_ID}") {

fun fromServerProperties(registries: RegistryAccess, properties: DedicatedServerProperties.WorldGenProperties): WorldGenSettings? {
val levelType = properties.levelType
if (levelType != MOD_ID) return null

val seed = properties.levelSeed.takeIf { it.isNotEmpty() }?.let {
try {
it.toLong()
} catch (ex: NumberFormatException) {
it.hashCode().toLong()
}
}.takeIf { it != 0L } ?: Random.nextLong()

return SkygridChunkGenerator.createSettings(registries, seed, false)
}

override fun generator(registries: RegistryAccess, seed: Long): ChunkGenerator {
val biomes = registries.registryOrThrow(
Registry.BIOME_REGISTRY)
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ forge_version=40.1.60
#forge_ats_enabled=true

# Fabric
fabric_version=0.58.0+1.18.2
fabric_loader_version=0.14.8
fabric_api_version=0.58.0+1.18.2
fabric_loader_version=0.14.9

# Mod options
mod_name=Skygrid
Expand Down

0 comments on commit 38e7c68

Please sign in to comment.