Skip to content

Commit

Permalink
Delete useless classes
Browse files Browse the repository at this point in the history
Move QuantumManager to Quantum
  • Loading branch information
Unreal852 committed Jul 31, 2024
1 parent f7401d8 commit 3f5f45c
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 189 deletions.
163 changes: 155 additions & 8 deletions src/main/java/fr/unreal852/quantum/Quantum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ package fr.unreal852.quantum
import fr.unreal852.quantum.callback.PlayerRespawnHandler
import fr.unreal852.quantum.callback.PlayerUseSignHandler
import fr.unreal852.quantum.command.CommandRegistration
import fr.unreal852.quantum.utils.Extensions.getWorldByIdentifier
import fr.unreal852.quantum.world.QuantumWorld
import fr.unreal852.quantum.world.QuantumWorldData
import fr.unreal852.quantum.world.state.QuantumPersistentState
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents.ServerStarted
import net.fabricmc.fabric.api.event.player.UseBlockCallback
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.server.MinecraftServer
import net.minecraft.util.Identifier
import net.minecraft.world.GameRules
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import xyz.nucleoid.fantasy.Fantasy
import xyz.nucleoid.fantasy.RuntimeWorldConfig
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap


class Quantum : ModInitializer {
override fun onInitialize() {
Expand All @@ -22,11 +32,9 @@ class Quantum : ModInitializer {

CommandRegistration.registerCommands()

ServerLifecycleEvents.SERVER_STARTED.register(ServerStarted { server: MinecraftServer? ->
// TODO: load portals
if (server != null) {
QuantumManager.loadExistingWorlds(server)
}
ServerLifecycleEvents.SERVER_STARTED.register(ServerStarted { server: MinecraftServer ->

loadExistingWorlds(server)
})

UseBlockCallback.EVENT.register(PlayerUseSignHandler())
Expand All @@ -35,9 +43,148 @@ class Quantum : ModInitializer {

companion object {

const val MOD_ID: String = "quantum"

const val MOD_ID = "quantum"
val LOGGER: Logger = LoggerFactory.getLogger(MOD_ID)
val CONFIG_FOLDER: Path = FabricLoader.getInstance().configDir.resolve(MOD_ID)

private val WORLDS: MutableMap<Identifier, QuantumWorld> = ConcurrentHashMap()

fun getWorld(identifier: Identifier): QuantumWorld? {
return WORLDS[identifier]
}

fun worldExists(identifier: Identifier): Boolean {
return WORLDS.containsKey(identifier)
}

fun getOrOpenPersistentWorld(server: MinecraftServer, worldData: QuantumWorldData, saveToDisk: Boolean): QuantumWorld? {
if (WORLDS.containsKey(worldData.worldId))
return WORLDS[worldData.worldId]

val fantasy = Fantasy.get(server)
val runtimeWorldConfig = getOrCreateRuntimeWorldConfig(server, worldData)
val runtimeWorldHandle = fantasy.getOrOpenPersistentWorld(worldData.worldId, runtimeWorldConfig)

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

if (saveToDisk)
QuantumPersistentState.getQuantumState(server).addWorldData(worldData)

return world
}

fun getOrCreateRuntimeWorldConfig(server: MinecraftServer, worldData: QuantumWorldData): RuntimeWorldConfig? {

val runtimeWorldConfig = RuntimeWorldConfig()
val serverWorld = server.getWorldByIdentifier(worldData.worldId)

if (serverWorld != null) {
runtimeWorldConfig.setDimensionType(serverWorld.dimensionEntry).setGenerator(serverWorld.chunkManager.chunkGenerator)
}

if (runtimeWorldConfig.generator == null) {
runtimeWorldConfig.setGenerator(server.overworld.chunkManager.chunkGenerator)
LOGGER.warn("The config has no generator, setting the generator to the default one.")
}

runtimeWorldConfig.setGameRule(GameRules.DO_DAYLIGHT_CYCLE, true)

return runtimeWorldConfig
}

fun deleteWorld(identifier: Identifier): Boolean {
val world = WORLDS.getOrDefault(identifier, null) ?: return false
val server = world.serverWorld.server
val fantasy = Fantasy.get(world.serverWorld.server)

if (!fantasy.tickDeleteWorld(world.serverWorld))
return false

val state = QuantumPersistentState.getQuantumState(server)
state.removeWorldData(world.worldData)
WORLDS.remove(identifier)

return true
}

fun loadExistingWorlds(server: MinecraftServer) {
val state = QuantumPersistentState.getQuantumState(server)

for (world in state.getWorlds()) {
if (!world.enabled)
continue
getOrOpenPersistentWorld(server, world, false)
LOGGER.info("Found enabled world '{}', loading it.", world.worldId)
}
}
}
}
}

// Old Portal code. =====================================
// public static PortalLink createPortal(MinecraftServer server, QuantumWorldPortalConfig portalConfig, boolean saveToDisk) {
// class_1792 item = (class_1792)class_2378.field_11142.method_10223(portalConfig.getPortalIgniteItemId());
// CustomPortalBuilder portalBuilder = CustomPortalBuilder.beginPortal().destDimID(portalConfig.getDestinationId()).frameBlock(portalConfig.getPortalBlockId()).tintColor(portalConfig.getPortalColor());
// if (item == class_1802.field_8705) {
// portalBuilder.lightWithWater();
// } else if (item == class_1802.field_8187) {
// portalBuilder.lightWithFluid(class_3612.field_15908);
// } else if (item != class_1802.field_8884) {
// portalBuilder.lightWithItem(item);
// }
//
// PortalLink portalLink = portalBuilder.registerPortal();
// PortalRegistrySync.syncLinkToAllPlayers(portalLink, server);
// if (saveToDisk) {
// FilesUtils.writeObjectToJsonFile((Path)PORTAL_FOLDER.resolve(UUID.randomUUID() + ".json"), portalConfig);
// }
//
// return portalLink;
// }
// public static void loadPortals(MinecraftServer server) {
// try {
// File directory = PORTAL_FOLDER.toFile();
// if (!directory.exists()) {
// return;
// }
//
// File[] var2 = FilesUtils.listFiles(directory);
// int var3 = var2.length;
//
// for (int var4 = 0; var4 < var3; ++var4) {
// File file = var2[var4];
// if (file.isDirectory()) {
// Quantum.LOGGER.error("The specified file is a directory ! It should be a file");
// } else {
// QuantumWorldPortalConfig config = (QuantumWorldPortalConfig) FilesUtils.readObjectFromJsonFile(QuantumWorldPortalConfig.class, file);
// if (config == null) {
// Quantum.LOGGER.warn("Failed to load portal '" + file.getName() + "'");
// } else if (config.isEnabled()) {
// createPortal(server, config, false);
// Quantum.LOGGER.info("Portal '" + file.getName() + "' loaded !");
// }
// }
// }
// } catch (Exception var7) {
// Quantum.LOGGER.error(var7);
// var7.printStackTrace();
// }
//
// }
//fun test() {
// val registryManager: DynamicRegistryManager = server.getCombinedDynamicRegistries().getCombinedRegistryManager()
// val dimensionRegistry: Registry<DimensionOptions> = registryManager.get(RegistryKeys.DIMENSION)
//
// // iterate all registered dimensions
// for (key in dimensionsRegistry.getKeys()) {
// val id = key.value
//
// // filter the dimension by mod namespace
// if (id.namespace == "target_mod_id") {
// // do something with it, e.g. get the world
// val wKey = RegistryKey.of(RegistryKeys.WORLD, id)
// val world: ServerWorld = server.getWorld(wKey)
// }
// }
//}
147 changes: 0 additions & 147 deletions src/main/java/fr/unreal852/quantum/QuantumManager.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import fr.unreal852.quantum.Quantum
import fr.unreal852.quantum.QuantumManager
import fr.unreal852.quantum.command.suggestion.DifficultySuggestionProvider
import fr.unreal852.quantum.command.suggestion.WorldsDimensionSuggestionProvider
import fr.unreal852.quantum.utils.CommandArgumentsUtils.getEnumArgument
Expand Down Expand Up @@ -37,7 +36,7 @@ class CreateWorldCommand : Command<ServerCommandSource> {
val worldName = StringArgumentType.getString(context, WORLD_NAME_ARG).lowercase()
val worldIdentifier = Identifier.of(Quantum.MOD_ID, worldName)

if (QuantumManager.worldExists(worldIdentifier)) {
if (Quantum.worldExists(worldIdentifier)) {
context.source.sendError(Text.translatable("quantum.text.cmd.world.exists", worldName))
return 0
}
Expand All @@ -55,7 +54,7 @@ class CreateWorldCommand : Command<ServerCommandSource> {

val quantumWorldData = QuantumWorldData(worldIdentifier, dimensionIdentifier, worldConfig)

QuantumManager.getOrOpenPersistentWorld(server, quantumWorldData, true)
Quantum.getOrOpenPersistentWorld(server, quantumWorldData, true)

context.source.sendMessage(Text.translatable("quantum.text.cmd.world.created", worldName))
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.mojang.brigadier.Command
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import fr.unreal852.quantum.Quantum
import fr.unreal852.quantum.QuantumManager
import fr.unreal852.quantum.command.suggestion.WorldsDimensionSuggestionProvider
import net.minecraft.command.argument.DimensionArgumentType
import net.minecraft.command.argument.IdentifierArgumentType
Expand All @@ -22,12 +21,12 @@ class DeleteWorldCommand : Command<ServerCommandSource> {

val worldName = IdentifierArgumentType.getIdentifier(context, WORLD_NAME_ARG)

if (!QuantumManager.worldExists(worldName)) {
if (!Quantum.worldExists(worldName)) {
context.source.sendError(Text.translatable("quantum.text.cmd.world.notexists.unspecified"))
return 0
}

if (QuantumManager.deleteWorld(worldName)) {
if (Quantum.deleteWorld(worldName)) {
context.source.sendMessage(Text.translatable("quantum.text.cmd.world.deleted", worldName.toString()))
}
} catch (e: Exception) {
Expand Down
Loading

0 comments on commit 3f5f45c

Please sign in to comment.