Skip to content

Commit 1d960d9

Browse files
committed
Convert to Kotlin
1 parent d16e389 commit 1d960d9

20 files changed

+588
-681
lines changed

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'fabric-loom' version '1.7-SNAPSHOT'
33
id 'maven-publish'
4+
id 'org.jetbrains.kotlin.jvm'
45
}
56

67
version = project.mod_version
@@ -18,6 +19,7 @@ repositories {
1819
// for more information about repositories.
1920

2021
maven { url = 'https://maven.nucleoid.xyz/' }
22+
mavenCentral()
2123
}
2224

2325
loom {
@@ -40,8 +42,11 @@ dependencies {
4042

4143
// Fabric API. This is technically optional, but you probably want it anyway.
4244
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
45+
// Kotlin
46+
modImplementation("net.fabricmc:fabric-language-kotlin:1.11.0+kotlin.2.0.0")
4347

4448
modImplementation 'xyz.nucleoid:fantasy:0.6.3+1.21'
49+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
4550
}
4651

4752
processResources {
@@ -62,8 +67,6 @@ java {
6267
// If you remove this line, sources will not be generated.
6368
withSourcesJar()
6469

65-
sourceCompatibility = JavaVersion.VERSION_21
66-
targetCompatibility = JavaVersion.VERSION_21
6770
}
6871

6972
jar {
@@ -88,4 +91,7 @@ publishing {
8891
// The repositories here will be used for publishing your artifact, not for
8992
// retrieving dependencies.
9093
}
94+
}
95+
kotlin {
96+
jvmToolchain(21)
9197
}

settings.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ pluginManagement {
77
mavenCentral()
88
gradlePluginPortal()
99
}
10+
plugins {
11+
id 'org.jetbrains.kotlin.jvm' version '2.0.0'
12+
}
13+
}
14+
plugins {
15+
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
1016
}
Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,64 @@
1-
package fr.unreal852.quantum;
2-
3-
import fr.unreal852.quantum.command.CommandRegistration;
4-
import net.fabricmc.api.ModInitializer;
5-
6-
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
7-
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
8-
import net.fabricmc.loader.api.FabricLoader;
9-
import net.minecraft.block.SignBlock;
10-
import net.minecraft.util.ActionResult;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
13-
14-
import java.nio.file.Path;
15-
16-
import static fr.unreal852.quantum.QuantumManager.loadExistingWorlds;
17-
18-
public class Quantum implements ModInitializer
19-
{
20-
// Mappings
21-
// class_3218 = ServerWorld
22-
// class_2960 = Identifier
23-
// class_2961 = Identifier.Serializer
24-
25-
// TODO: Cleanup & Refactor Project
26-
// TODO: Try to switch to Kotlin
27-
28-
public static final String MOD_ID = "quantum";
29-
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
30-
public static final Path CONFIG_FOLDER = FabricLoader.getInstance().getConfigDir().resolve(MOD_ID);
31-
32-
@Override
33-
public void onInitialize()
34-
{
1+
package fr.unreal852.quantum
2+
3+
import fr.unreal852.quantum.command.CommandRegistration
4+
import net.fabricmc.api.ModInitializer
5+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
6+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents.ServerStarted
7+
import net.fabricmc.fabric.api.event.player.UseBlockCallback
8+
import net.fabricmc.loader.api.FabricLoader
9+
import net.minecraft.block.SignBlock
10+
import net.minecraft.entity.player.PlayerEntity
11+
import net.minecraft.server.MinecraftServer
12+
import net.minecraft.util.ActionResult
13+
import net.minecraft.util.Hand
14+
import net.minecraft.util.hit.BlockHitResult
15+
import net.minecraft.world.World
16+
import org.slf4j.Logger
17+
import org.slf4j.LoggerFactory
18+
import java.nio.file.Path
19+
20+
class Quantum : ModInitializer {
21+
override fun onInitialize() {
3522
// This code runs as soon as Minecraft is in a mod-load-ready state.
3623
// However, some things (like resources) may still be uninitialized.
3724
// Proceed with mild caution.
3825

39-
CommandRegistration.RegisterCommands();
26+
CommandRegistration.registerCommands()
4027

41-
ServerLifecycleEvents.SERVER_STARTED.register(server ->
42-
{
28+
ServerLifecycleEvents.SERVER_STARTED.register(ServerStarted { server: MinecraftServer? ->
4329
// TODO: load portals
44-
loadExistingWorlds(server);
45-
});
30+
if (server != null) {
31+
QuantumManager.loadExistingWorlds(server)
32+
}
33+
})
4634

47-
UseBlockCallback.EVENT.register(((player, world, hand, hitResult) ->
48-
{
35+
UseBlockCallback.EVENT.register((UseBlockCallback { player: PlayerEntity, world: World, hand: Hand?, hitResult: BlockHitResult ->
4936

5037
// TODO: Use signs instead of portals for now.
38+
if (world.isClient) return@UseBlockCallback ActionResult.PASS
39+
if (player.isSpectator || player.isSneaking) return@UseBlockCallback ActionResult.PASS
5140

52-
if (world.isClient)
53-
return ActionResult.PASS;
54-
if (player.isSpectator() || player.isSneaking())
55-
return ActionResult.PASS;
41+
val blockState = world.getBlockState(hitResult.blockPos)
42+
val block = blockState.block
5643

57-
var blockState = world.getBlockState(hitResult.getBlockPos());
58-
var block = blockState.getBlock();
59-
60-
if (block instanceof SignBlock)
61-
{
62-
Quantum.LOGGER.info("Sign block found.");
44+
if (block is SignBlock) {
45+
LOGGER.info("Sign block found.")
6346
}
64-
65-
return ActionResult.PASS;
66-
}));
47+
ActionResult.PASS
48+
}))
6749
}
6850

51+
companion object {
52+
// Mappings
53+
// class_3218 = ServerWorld
54+
// class_2960 = Identifier
55+
// class_2961 = Identifier.Serializer
56+
// TODO: Cleanup & Refactor Project
57+
// TODO: Try to switch to Kotlin
58+
const val MOD_ID: String = "quantum"
59+
60+
@JvmField
61+
val LOGGER: Logger = LoggerFactory.getLogger(MOD_ID)
62+
val CONFIG_FOLDER: Path = FabricLoader.getInstance().configDir.resolve(MOD_ID)
63+
}
6964
}

src/main/java/fr/unreal852/quantum/QuantumManager.kt

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,83 @@
1-
package fr.unreal852.quantum;
2-
3-
import fr.unreal852.quantum.world.QuantumWorld;
4-
import fr.unreal852.quantum.world.QuantumWorldData;
5-
import fr.unreal852.quantum.world.QuantumWorldPersistentState;
6-
import net.minecraft.registry.RegistryKey;
7-
import net.minecraft.registry.RegistryKeys;
8-
import net.minecraft.server.MinecraftServer;
9-
import net.minecraft.util.Identifier;
10-
import net.minecraft.world.GameRules;
11-
import xyz.nucleoid.fantasy.Fantasy;
12-
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
13-
14-
import java.util.Map;
15-
import java.util.concurrent.ConcurrentHashMap;
16-
17-
public final class QuantumManager
18-
{
19-
private static final Map<Identifier, QuantumWorld> WORLDS = new ConcurrentHashMap<>();
20-
21-
public static QuantumWorld getWorld(Identifier identifier)
22-
{
23-
return WORLDS.get(identifier);
1+
package fr.unreal852.quantum
2+
3+
import fr.unreal852.quantum.world.QuantumWorld
4+
import fr.unreal852.quantum.world.QuantumWorldData
5+
import fr.unreal852.quantum.world.QuantumWorldPersistentState
6+
import net.minecraft.registry.RegistryKey
7+
import net.minecraft.registry.RegistryKeys
8+
import net.minecraft.server.MinecraftServer
9+
import net.minecraft.util.Identifier
10+
import net.minecraft.world.GameRules
11+
import xyz.nucleoid.fantasy.Fantasy
12+
import xyz.nucleoid.fantasy.RuntimeWorldConfig
13+
import java.util.concurrent.ConcurrentHashMap
14+
15+
object QuantumManager {
16+
private val WORLDS: MutableMap<Identifier, QuantumWorld> = ConcurrentHashMap()
17+
18+
@JvmStatic
19+
fun getWorld(identifier: Identifier): QuantumWorld? {
20+
return WORLDS[identifier]
2421
}
2522

26-
public static QuantumWorld getWorld(String worldName)
27-
{
28-
return getWorld(Identifier.of("quantum", worldName));
23+
@JvmStatic
24+
fun getWorld(worldName: String): QuantumWorld? {
25+
return getWorld(Identifier.of("quantum", worldName))
2926
}
3027

31-
public static QuantumWorld getOrOpenPersistentWorld(MinecraftServer server, QuantumWorldData worldData, boolean saveToDisk)
32-
{
33-
if (WORLDS.containsKey(worldData.getWorldId()))
34-
return WORLDS.get(worldData.getWorldId());
28+
@JvmStatic
29+
fun getOrOpenPersistentWorld(
30+
server: MinecraftServer,
31+
worldData: QuantumWorldData,
32+
saveToDisk: Boolean
33+
): QuantumWorld? {
34+
if (WORLDS.containsKey(worldData.worldId)) return WORLDS[worldData.worldId]
3535

36-
var fantasy = Fantasy.get(server);
37-
var runtimeWorldConfig = getOrCreateRuntimeWorldConfig(server, worldData);
38-
var runtimeWorldHandle = fantasy.getOrOpenPersistentWorld(worldData.getWorldId(), runtimeWorldConfig);
36+
val fantasy = Fantasy.get(server)
37+
val runtimeWorldConfig = getOrCreateRuntimeWorldConfig(server, worldData)
38+
val runtimeWorldHandle = fantasy.getOrOpenPersistentWorld(worldData.worldId, runtimeWorldConfig)
3939

4040
// TODO: CustomPortalsMod.dims.put(worldConfig.getWorldId(), runtimeWorldHandle.getRegistryKey());
41+
val world = QuantumWorld(runtimeWorldHandle, worldData)
42+
WORLDS[worldData.worldId] = world
4143

42-
QuantumWorld world = new QuantumWorld(runtimeWorldHandle, worldData);
43-
WORLDS.put(worldData.getWorldId(), world);
44+
if (saveToDisk) QuantumWorldPersistentState.getQuantumState(server).addWorldData(worldData)
4445

45-
if (saveToDisk)
46-
QuantumWorldPersistentState.getQuantumState(server).addWorldData(worldData);
47-
48-
return world;
46+
return world
4947
}
5048

51-
public static RuntimeWorldConfig getOrCreateRuntimeWorldConfig(MinecraftServer server, QuantumWorldData worldData)
52-
{
53-
if (worldData.getRuntimeWorldConfig() != null)
54-
return worldData.getRuntimeWorldConfig();
49+
fun getOrCreateRuntimeWorldConfig(server: MinecraftServer, worldData: QuantumWorldData): RuntimeWorldConfig {
50+
if (worldData.runtimeWorldConfig != null) return worldData.runtimeWorldConfig
5551

56-
var runtimeWorldConfig = new RuntimeWorldConfig();
57-
var serverWorld = server.getWorld(RegistryKey.of(RegistryKeys.WORLD, worldData.getDimensionId()));
52+
val runtimeWorldConfig = RuntimeWorldConfig()
53+
val serverWorld = server.getWorld(RegistryKey.of(RegistryKeys.WORLD, worldData.dimensionId))
5854

59-
if (serverWorld != null)
60-
{
55+
if (serverWorld != null) {
6156
runtimeWorldConfig
62-
.setDimensionType(serverWorld.getDimensionEntry())
63-
.setGenerator(serverWorld.getChunkManager().getChunkGenerator());
57+
.setDimensionType(serverWorld.dimensionEntry)
58+
.setGenerator(serverWorld.chunkManager.chunkGenerator)
6459
}
6560

66-
if (runtimeWorldConfig.getGenerator() == null)
67-
{
68-
runtimeWorldConfig.setGenerator(server.getOverworld().getChunkManager().getChunkGenerator());
69-
Quantum.LOGGER.warn("The config has no generator, setting the generator to the default one.");
61+
if (runtimeWorldConfig.generator == null) {
62+
runtimeWorldConfig.setGenerator(server.overworld.chunkManager.chunkGenerator)
63+
Quantum.LOGGER.warn("The config has no generator, setting the generator to the default one.")
7064
}
7165

72-
runtimeWorldConfig.setGameRule(GameRules.DO_DAYLIGHT_CYCLE, true);
66+
runtimeWorldConfig.setGameRule(GameRules.DO_DAYLIGHT_CYCLE, true)
7367

74-
return runtimeWorldConfig;
68+
return runtimeWorldConfig
7569
}
7670

77-
public static void loadExistingWorlds(MinecraftServer server)
78-
{
79-
var state = QuantumWorldPersistentState.getQuantumState(server);
71+
fun loadExistingWorlds(server: MinecraftServer) {
72+
val state = QuantumWorldPersistentState.getQuantumState(server)
8073

81-
for (var world : state.getWorlds())
82-
{
74+
for (world in state.getWorlds()) {
8375
if (!world.isEnabled())
84-
continue;
85-
getOrOpenPersistentWorld(server, world, false);
86-
Quantum.LOGGER.info("Found enabled world '{}', loading it.", world.getWorldId());
76+
continue
77+
getOrOpenPersistentWorld(server, world, false)
78+
Quantum.LOGGER.info("Found enabled world '{}', loading it.", world.worldId)
8779
}
88-
}
89-
90-
// Old Portal code. =====================================
80+
} // Old Portal code. =====================================
9181
// public static PortalLink createPortal(MinecraftServer server, QuantumWorldPortalConfig portalConfig, boolean saveToDisk) {
9282
// class_1792 item = (class_1792)class_2378.field_11142.method_10223(portalConfig.getPortalIgniteItemId());
9383
// CustomPortalBuilder portalBuilder = CustomPortalBuilder.beginPortal().destDimID(portalConfig.getDestinationId()).frameBlock(portalConfig.getPortalBlockId()).tintColor(portalConfig.getPortalColor());
@@ -107,7 +97,6 @@ public final class QuantumManager
10797
//
10898
// return portalLink;
10999
// }
110-
111100
// public static void loadPortals(MinecraftServer server) {
112101
// try {
113102
// File directory = PORTAL_FOLDER.toFile();

0 commit comments

Comments
 (0)