Skip to content

Commit

Permalink
dont reassign plot pos when regenerating
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Mar 6, 2024
1 parent 6e30371 commit 8cda5ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,30 @@ 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) {
if (!entity.getWorld().isClient()) {
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()));
}
Expand Down Expand Up @@ -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;

Expand All @@ -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());
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ public static int setLevel(CommandContext<ServerCommandSource> context, ServerPl

private static class RegenPocketCommand {
public static int regeneratePocket(CommandContext<ServerCommandSource> 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;
}
}
Expand Down

0 comments on commit 8cda5ac

Please sign in to comment.