Skip to content

Commit

Permalink
fix(creator_markers): only allow placement in blueprint editor worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jan 12, 2025
1 parent c068565 commit 5fc1701
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.mcbrawls.blueprint.block.region

import net.mcbrawls.blueprint.block.entity.RegionIdBlockEntity
import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
import net.mcbrawls.blueprint.region.serialization.SerializableRegion
import net.mcbrawls.blueprint.structure.Blueprint.Companion.openInputGui
import net.minecraft.block.BlockState
Expand All @@ -17,6 +18,7 @@ import net.minecraft.util.Formatting
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.minecraft.world.WorldView

abstract class RegionBlock(settings: Settings) : BlockWithEntity(settings) {
abstract fun saveRegion(
Expand Down Expand Up @@ -67,6 +69,10 @@ abstract class RegionBlock(settings: Settings) : BlockWithEntity(settings) {
return ActionResult.PASS
}

override fun canPlaceAt(state: BlockState, world: WorldView, pos: BlockPos): Boolean {
return world is BlueprintEditorWorld
}

override fun createBlockEntity(pos: BlockPos, state: BlockState): BlockEntity {
return RegionIdBlockEntity(pos, state)
}
Expand Down Expand Up @@ -103,7 +109,7 @@ abstract class RegionBlock(settings: Settings) : BlockWithEntity(settings) {
*/
fun openRegionIdEditorGui(player: ServerPlayerEntity, blockEntity: RegionIdBlockEntity) {
val regionId = blockEntity.getOrCreateId()
openInputGui(player, Text.literal("Region ID"), regionId) { input ->
openInputGui(player, Text.literal("Set Region ID").formatted(Formatting.BOLD), regionId) { input ->
if (input != regionId) {
if (input.isBlank()) {
val id = blockEntity.id
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/net/mcbrawls/blueprint/entity/AnchorEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mcbrawls.blueprint.entity

import eu.pb4.polymer.core.api.entity.PolymerEntity
import net.mcbrawls.blueprint.anchor.Anchor
import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
import net.mcbrawls.blueprint.structure.Blueprint
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityPose
Expand Down Expand Up @@ -36,6 +37,11 @@ class AnchorEntity(type: EntityType<*>, world: World) : InteractionEntity(type,
}

override fun tick() {
if (world !is BlueprintEditorWorld) {
discard()
return
}

super.tick()

val world = world
Expand All @@ -59,14 +65,14 @@ class AnchorEntity(type: EntityType<*>, world: World) : InteractionEntity(type,
}

fun openAnchorIdEditor(player: ServerPlayerEntity) {
openInputGui(player, Text.literal("Anchor ID"), getOrCreateId(), "anchor ID") { input, dataName ->
openInputGui(player, Text.literal("Set Anchor ID").formatted(Formatting.BOLD), getOrCreateId(), "anchor ID") { input, dataName ->
id = input.trim()
player.sendMessage(Text.literal("Set $dataName: \"$id\"").formatted(Formatting.GREEN))
}
}

fun openAnchorDataEditor(player: ServerPlayerEntity) {
openInputGui(player, Text.literal("Anchor Data"), data ?: "", "data", canBeBlank = true) { input, dataName ->
openInputGui(player, Text.literal("Set Anchor Data").formatted(Formatting.BOLD), data ?: "", "data", canBeBlank = true) { input, dataName ->
data = input.trim()
player.sendMessage(Text.literal("Set $dataName: \"$data\"").formatted(Formatting.GREEN))
}
Expand Down
27 changes: 17 additions & 10 deletions src/main/kotlin/net/mcbrawls/blueprint/item/AnchorItem.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.mcbrawls.blueprint.item

import eu.pb4.polymer.core.api.item.PolymerItem
import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
import net.mcbrawls.blueprint.entity.BlueprintEntityTypes
import net.minecraft.entity.SpawnReason
import net.minecraft.item.Item
Expand All @@ -14,17 +15,23 @@ import xyz.nucleoid.packettweaker.PacketContext

class AnchorItem(settings: Settings) : Item(settings), PolymerItem {
override fun useOnBlock(context: ItemUsageContext): ActionResult {
val world = context.world
if (world !is BlueprintEditorWorld) {
return ActionResult.PASS
}

val player = context.player
if (player is ServerPlayerEntity) {
val world = context.world
BlueprintEntityTypes.ANCHOR.create(world, SpawnReason.COMMAND)?.also { anchorEntity ->
anchorEntity.setPosition(context.hitPos)
anchorEntity.rotate(context.playerYaw, 0.0f)
world.spawnEntity(anchorEntity)
anchorEntity.openAnchorIdEditor(player)

return ActionResult.SUCCESS
}
if (player !is ServerPlayerEntity) {
return ActionResult.PASS
}

BlueprintEntityTypes.ANCHOR.create(world, SpawnReason.COMMAND)?.also { anchorEntity ->
anchorEntity.setPosition(context.hitPos)
anchorEntity.rotate(context.playerYaw, 0.0f)
world.spawnEntity(anchorEntity)
anchorEntity.openAnchorIdEditor(player)

return ActionResult.SUCCESS
}

return ActionResult.PASS
Expand Down

0 comments on commit 5fc1701

Please sign in to comment.