From 8cda5ac0436113dcb024323c2318876d14b4d8d2 Mon Sep 17 00:00:00 2001 From: Up Date: Wed, 6 Mar 2024 19:49:45 +0100 Subject: [PATCH] dont reassign plot pos when regenerating --- .../level/PocketDimensionComponent.java | 43 ++++++++++++------- .../common/registry/ArcanusCommands.java | 10 ++++- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/level/PocketDimensionComponent.java b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/level/PocketDimensionComponent.java index 79e6edf..a977b72 100644 --- a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/level/PocketDimensionComponent.java +++ b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/level/PocketDimensionComponent.java @@ -98,19 +98,15 @@ public void writeToNbt(NbtCompound tag) { tag.put("ExitSpots", exitNbtList); } - private static boolean chunksExist(Box plot, PlayerEntity owner, ServerWorld pocketDim) { + // FIXME this check is too eager sometimes. + // not too bad for the moment but should be looked into. + private static boolean chunksExist(Box plot, ServerWorld pocketDim) { var chunkManager = pocketDim.getChunkManager(); - var passed = BlockPos.stream(plot) + return BlockPos.stream(plot) .map(ChunkPos::new).distinct() .map(cPos -> chunkManager.getWorldChunk(cPos.x, cPos.z, false)) .noneMatch(Objects::isNull); - - if (!passed) { - Arcanus.LOGGER.warn("Pocket dimension plot for player {} failed integrity check! regenerating...", owner.getGameProfile().getName()); - } - - return passed; } public void teleportToPocketDimension(PlayerEntity ownerOfPocket, Entity entity) { @@ -118,10 +114,14 @@ public void teleportToPocketDimension(PlayerEntity ownerOfPocket, Entity entity) ServerWorld pocketDim = entity.getServer().getWorld(POCKET_DIM); if (pocketDim != null) { Box plot = existingPlots.get(ownerOfPocket.getUuid()); - if (plot == null || !chunksExist(plot, ownerOfPocket, pocketDim)) { - generateNewPlot(ownerOfPocket, pocketDim); + if (plot == null) { + plot = assignNewPlot(ownerOfPocket, pocketDim); + generatePlotSpace(ownerOfPocket, pocketDim); + } + else if (!chunksExist(plot, pocketDim)) { + Arcanus.LOGGER.warn("Pocket dimension plot for player {} failed integrity check! regenerating boundary...", ownerOfPocket.getGameProfile().getName()); + generatePlotSpace(ownerOfPocket, pocketDim); } - plot = existingPlots.get(ownerOfPocket.getUuid()); QuiltDimensions.teleport(entity, pocketDim, new TeleportTarget(plot.getCenter().subtract(0, 11, 0), Vec3d.ZERO, entity.getYaw(), entity.getPitch())); } @@ -152,7 +152,7 @@ public void teleportOutOfPocketDimension(ServerPlayerEntity player) { } } - public void generateNewPlot(PlayerEntity player, ServerWorld pocketDim) { + public Box assignNewPlot(PlayerEntity player, ServerWorld pocketDim) { int pocketWidth = Math.round(ArcanusConfig.UtilityEffects.SpatialRiftEffectProperties.pocketWidth / 2f) + 1; int pocketHeight = Math.round(ArcanusConfig.UtilityEffects.SpatialRiftEffectProperties.pocketHeight / 2f) + 1; @@ -165,8 +165,19 @@ public void generateNewPlot(PlayerEntity player, ServerWorld pocketDim) { existingPlots.put(player.getUuid(), boxContainer.box); - for (BlockPos pos : BlockPos.iterate((int) Math.round(boxContainer.box.minX), (int) Math.round(boxContainer.box.minY), (int) Math.round(boxContainer.box.minZ), (int) Math.round(boxContainer.box.maxX - 1), (int) Math.round(boxContainer.box.maxY - 1), (int) Math.round(boxContainer.box.maxZ - 1))) { - if (pos.getX() > boxContainer.box.minX && pos.getX() < boxContainer.box.maxX - 1 && pos.getY() > boxContainer.box.minY && pos.getY() < boxContainer.box.maxY - 1 && pos.getZ() > boxContainer.box.minZ && pos.getZ() < boxContainer.box.maxZ - 1) + return boxContainer.box; + } + + public boolean generatePlotSpace(PlayerEntity player, ServerWorld pocketDim) { + var box = existingPlots.get(player.getUuid()); + + // might happen if the command is ran before a player first enters their pocket dimension + if(box == null) { + return false; + } + + for (BlockPos pos : BlockPos.iterate((int) Math.round(box.minX), (int) Math.round(box.minY), (int) Math.round(box.minZ), (int) Math.round(box.maxX - 1), (int) Math.round(box.maxY - 1), (int) Math.round(box.maxZ - 1))) { + if (pos.getX() > box.minX && pos.getX() < box.maxX - 1 && pos.getY() > box.minY && pos.getY() < box.maxY - 1 && pos.getZ() > box.minZ && pos.getZ() < box.maxZ - 1) continue; pocketDim.setBlockState(pos, ArcanusBlocks.UNBREAKABLE_MAGIC_BLOCK.get().getDefaultState()); @@ -177,7 +188,7 @@ public void generateNewPlot(PlayerEntity player, ServerWorld pocketDim) { for (int x = 0; x < 4; x++) { for (int z = 0; z < 4; z++) { - BlockPos pos = new BlockPos((int) Math.round(boxContainer.box.getCenter().getX()) + (x - 2), (int) Math.round(boxContainer.box.minY), (int) Math.round(boxContainer.box.getCenter().getZ()) + (z - 2)); + BlockPos pos = new BlockPos((int) Math.round(box.getCenter().getX()) + (x - 2), (int) Math.round(box.minY), (int) Math.round(box.getCenter().getZ()) + (z - 2)); if (x == 0) { switch (z) { @@ -212,6 +223,8 @@ public void generateNewPlot(PlayerEntity player, ServerWorld pocketDim) { magicBlock.setColour(Arcanus.getMagicColour(player.getGameProfile().getId())); } } + + return true; } public void setExit(PlayerEntity ownerOfPocket, World world, Vec3d pos) { diff --git a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/registry/ArcanusCommands.java b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/registry/ArcanusCommands.java index 80db9da..d579fe7 100644 --- a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/registry/ArcanusCommands.java +++ b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/registry/ArcanusCommands.java @@ -59,9 +59,15 @@ public static int setLevel(CommandContext context, ServerPl private static class RegenPocketCommand { public static int regeneratePocket(CommandContext context, ServerPlayerEntity player) throws CommandSyntaxException { - context.getSource().getWorld().getProperties().getComponent(ArcanusComponents.POCKET_DIMENSION_COMPONENT).generateNewPlot(player, context.getSource().getServer().getWorld(PocketDimensionComponent.POCKET_DIM)); - context.getSource().sendFeedback(() -> Text.literal(String.format("Regenerated %s's pocket dimension", player.getEntityName())), false); + var pocketDimension = context.getSource().getServer().getWorld(PocketDimensionComponent.POCKET_DIM); + var component = context.getSource().getWorld().getProperties().getComponent(ArcanusComponents.POCKET_DIMENSION_COMPONENT); + if(!component.generatePlotSpace(player, pocketDimension)) { + context.getSource().sendError(Text.literal("Pocket dimension location not found for player %s".formatted(player.getEntityName()))); + return 0; + } + + context.getSource().sendFeedback(() -> Text.literal("Regenerated %s's pocket dimension".formatted(player.getEntityName())), false); return Command.SINGLE_SUCCESS; } }