-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...print/environment/BlueprintEnvironment.kt → ...rint/editor/BlueprintEditorEnvironment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package net.mcbrawls.blueprint.editor | ||
|
||
import com.google.common.base.Preconditions | ||
import dev.andante.bubble.BubbleManager | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents | ||
import net.mcbrawls.blueprint.BlueprintMod | ||
import net.minecraft.registry.RegistryKey | ||
import net.minecraft.registry.RegistryKeys | ||
import net.minecraft.server.MinecraftServer | ||
import net.minecraft.util.Identifier | ||
|
||
/** | ||
* Manages all blueprint editor environments for a server. | ||
*/ | ||
class BlueprintEditors private constructor(private val server: MinecraftServer) { | ||
/** | ||
* All active blueprint editor environments. | ||
*/ | ||
private val environments: MutableMap<Identifier, BlueprintEditorEnvironment> = mutableMapOf() | ||
|
||
fun tick() { | ||
} | ||
|
||
fun clean() { | ||
environments.values.forEach(BubbleManager.getOrCreate(server)::remove) | ||
} | ||
|
||
/** | ||
* Gets or creates a blueprint environment for the given blueprint id. | ||
* @return a blueprint environment | ||
* @throws IllegalStateException if a world exists for the given blueprint id but that world is not an environment | ||
*/ | ||
operator fun get(blueprintId: Identifier): BlueprintEditorEnvironment { | ||
val blueprintNamespace = blueprintId.namespace | ||
val blueprintPath = blueprintId.path | ||
val worldId = Identifier(BlueprintMod.MOD_ID, "blueprint/$blueprintNamespace/$blueprintPath") | ||
val worldKey = RegistryKey.of(RegistryKeys.WORLD, worldId) | ||
|
||
val environmentWorld = server.getWorld(worldKey) | ||
|
||
// get or create environment | ||
val environment = if (environmentWorld != null) { | ||
if (environmentWorld is BlueprintEditorEnvironment) { | ||
environmentWorld | ||
} else { | ||
throw IllegalStateException("World existed for blueprint but was not environment: $blueprintId") | ||
} | ||
} else { | ||
val bubbleManager = BubbleManager.getOrCreate(server) | ||
val environment = bubbleManager.createAndInitialize( | ||
identifier = worldId, | ||
factory = { server, key, options -> BlueprintEditorEnvironment(blueprintId, server, key, options) } | ||
) | ||
|
||
environments[blueprintId] = environment | ||
environment | ||
} | ||
|
||
return environment | ||
} | ||
|
||
companion object { | ||
/** | ||
* All blueprint environment managers. | ||
*/ | ||
private val EDITORS = mutableMapOf<MinecraftServer, BlueprintEditors>() | ||
|
||
init { | ||
// register server events | ||
ServerTickEvents.START_SERVER_TICK.register { server -> EDITORS[server]?.tick() } | ||
ServerLifecycleEvents.SERVER_STOPPING.register { server -> EDITORS[server]?.clean() } | ||
} | ||
|
||
/** | ||
* Gets an environment manager for the server or creates one if not present. | ||
*/ | ||
operator fun get(server: MinecraftServer): BlueprintEditors { | ||
Preconditions.checkState(server.isOnThread, "Cannot create blueprint environment manager off-thread") | ||
return EDITORS.computeIfAbsent(server, ::BlueprintEditors) | ||
} | ||
|
||
/** | ||
* Clears the environment manager for the given server. | ||
*/ | ||
fun clear(server: MinecraftServer): BlueprintEditors? { | ||
EDITORS[server]?.let(BlueprintEditors::clean) | ||
return EDITORS.remove(server) | ||
} | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
src/main/kotlin/net/mcbrawls/blueprint/environment/BlueprintEnvironmentManager.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters