Skip to content

Commit

Permalink
add repair_walls subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Dec 18, 2024
1 parent f919cc6 commit 2920380
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ public class RegeneratePocketDimensionCommand {
public static void register(RequiredArgumentBuilder<ServerCommandSource, EntitySelector> builder) {
builder.then(CommandManager.literal("regenerate")
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(CommandManager.ADMIN_PERMISSION_LEVEL))
.executes(context -> RegeneratePocketDimensionCommand.regeneratePocket(context, PocketDimensionCommand.getPlayerProfile(context)))
.executes(context -> RegeneratePocketDimensionCommand.regeneratePocket(context, PocketDimensionCommand.getPlayerProfile(context), PocketDimensionComponent.RegenerateType.FULL))
);
builder.then(CommandManager.literal("repair_walls")
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(CommandManager.ADMIN_PERMISSION_LEVEL))
.executes(context -> RegeneratePocketDimensionCommand.regeneratePocket(context, PocketDimensionCommand.getPlayerProfile(context), PocketDimensionComponent.RegenerateType.WALLS_ONLY))
);
}

public static int regeneratePocket(CommandContext<ServerCommandSource> context, GameProfile target) throws CommandSyntaxException {
public static int regeneratePocket(CommandContext<ServerCommandSource> context, GameProfile target, PocketDimensionComponent.RegenerateType regenerateType) throws CommandSyntaxException {
var server = context.getSource().getServer();
var pocketDimension = server.getWorld(PocketDimensionComponent.POCKET_DIM);
var component = PocketDimensionComponent.get(server);

if (!component.replacePlotSpace(target.getId(), pocketDimension, true)) {
if (!component.replacePlotSpace(target.getId(), pocketDimension, regenerateType)) {
context.getSource().sendError(Text.literal("Pocket dimension location not found for player %s (%s)".formatted(target.getName(), target.getId())));
return 0;
}

context.getSource().sendFeedback(() -> Text.literal("Regenerated %s's pocket dimension".formatted(target.getName())), false);
var message = switch (regenerateType) {
case WALLS_ONLY -> "Repaired the walls of %s's pocket dimension".formatted(target.getName());
case FULL -> "Regenerated %s's pocket dimension".formatted(target.getName());
default -> throw new UnsupportedOperationException();
};
context.getSource().sendFeedback(() -> Text.literal(message), false);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public void teleportToPocketDimension(GameProfile pocketOwner, Entity entity) {
var plot = getAssignedPlotSpace(pocketOwner.getId());
if (plot == null) {
plot = assignNewPlot(pocketDim, pocketOwner, entity.getWorld().getRandom());
replacePlotSpace(pocketOwner.getId(), pocketDim, false);
replacePlotSpace(pocketOwner.getId(), pocketDim, RegenerateType.FULL);
} else if (!chunksExist(plot, pocketDim)) {
Arcanus.LOGGER.warn("Pocket dimension plot for player {} ({}) failed integrity check! regenerating boundary...", pocketOwner.getName(), pocketOwner.getId());
replacePlotSpace(pocketOwner.getId(), pocketDim, false);
replacePlotSpace(pocketOwner.getId(), pocketDim, RegenerateType.WALLS_ONLY);
}

var bottomCenterPos = Vec3d.ofBottomCenter(plot.getBounds().getCenter().withY(plot.min().getY() + 1));
Expand Down Expand Up @@ -235,20 +235,34 @@ public PocketDimensionPlot getAssignedPlotSpace(UUID target) {
return existingPlots.get(target);
}

public enum RegenerateType {
INTERIOR_ONLY,
WALLS_ONLY,
FULL;

public boolean placeWalls() {
return this != INTERIOR_ONLY;
}

public boolean clearInterior() {
return this != WALLS_ONLY;
}
}

/**
* fills the plot with empty space and builds walls around it
*
* @param clear whether to remove existing blocks and entities
* @param regenerateType whether to force-replace the interior space and/or walls
*/
public boolean replacePlotSpace(UUID target, ServerWorld pocketDim, boolean clear) {
public boolean replacePlotSpace(UUID target, ServerWorld pocketDim, RegenerateType regenerateType) {
var plot = getAssignedPlotSpace(target);

// might happen if the command is ran before a player first enters their pocket dimension
if (plot == null) {
return false;
}

if (clear) {
if (regenerateType.clearInterior()) {
pocketDim.getNonSpectatingEntities(Entity.class, Box.from(plot.getBounds())).forEach(entity -> {
if (PlayerHelper.isFakePlayer(entity) || !(entity instanceof ServerPlayerEntity player)) {
entity.discard();
Expand All @@ -266,12 +280,15 @@ public boolean replacePlotSpace(UUID target, ServerWorld pocketDim, boolean clea
BlockPos.stream(plot.min(), plot.max()).forEach(pos -> {
var isNotWall = pos.getX() != plot.min().getX() && pos.getX() != plot.max().getX() && pos.getY() != plot.min().getY() && pos.getY() != plot.max().getY() && pos.getZ() != plot.min().getZ() && pos.getZ() != plot.max().getZ();
if (isNotWall) {
if (clear) {
if (regenerateType.clearInterior()) {
Clearable.clear(pocketDim.getBlockEntity(pos));
pocketDim.setBlockState(pos, Blocks.AIR.getDefaultState(), REPLACE_FLAGS);
}
return;
}
else if(!regenerateType.placeWalls()) {
return;
}

pocketDim.setBlockState(pos, ArcanusBlocks.UNBREAKABLE_MAGIC_BLOCK.get().getDefaultState(), REPLACE_FLAGS);

Expand All @@ -280,41 +297,41 @@ public boolean replacePlotSpace(UUID target, ServerWorld pocketDim, boolean clea
.ifPresent(component -> component.setSourceId(target));
});

var center = plot.getBounds().getCenter().withY(plot.min().getY());
var pos = center.mutableCopy();
for (int x = 0; x < 4; x++) {
for (int z = 0; z < 4; z++) {
pos.set(center.getX() + x - 2, center.getY(), center.getZ() + z - 2);

if (x == 0) {
switch (z) {
case 0 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
case 1, 2 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.WEST), REPLACE_FLAGS);
case 3 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.WEST).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
}
} else if (x == 3) {
switch (z) {
case 0 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.EAST).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
case 1, 2 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.EAST));
case 3 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.SOUTH).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
if(regenerateType.placeWalls()) {
var center = plot.getBounds().getCenter().withY(plot.min().getY());
var pos = center.mutableCopy();
for (int x = 0; x < 4; x++) {
for (int z = 0; z < 4; z++) {
pos.set(center.getX() + x - 2, center.getY(), center.getZ() + z - 2);

if (x == 0) {
switch (z) {
case 0 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
case 1, 2 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.WEST), REPLACE_FLAGS);
case 3 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.WEST).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
}
} else if (x == 3) {
switch (z) {
case 0 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.EAST).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
case 1, 2 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.EAST));
case 3 ->
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.SOUTH).with(SpatialRiftExitEdgeBlock.CORNER, true), REPLACE_FLAGS);
}
} else if (z == 0) {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.NORTH), REPLACE_FLAGS);
} else if (z == 3) {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.SOUTH), REPLACE_FLAGS);
} else {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT.get().getDefaultState().with(SpatialRiftExitBlock.ACTIVE, x == 1 && z == 1), REPLACE_FLAGS);
}
} else if (z == 0) {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.NORTH), REPLACE_FLAGS);
} else if (z == 3) {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT_EDGE.get().getDefaultState().with(SpatialRiftExitEdgeBlock.FACING, Direction.SOUTH), REPLACE_FLAGS);
} else {
pocketDim.setBlockState(pos, ArcanusBlocks.SPATIAL_RIFT_EXIT.get().getDefaultState().with(SpatialRiftExitBlock.ACTIVE, x == 1 && z == 1), REPLACE_FLAGS);
}

Optional.ofNullable(pocketDim.getBlockEntity(pos))
.flatMap(ArcanusComponents.MAGIC_COLOR::maybeGet)
.ifPresent(component -> component.setSourceId(target));
Optional.ofNullable(pocketDim.getBlockEntity(pos)).flatMap(ArcanusComponents.MAGIC_COLOR::maybeGet).ifPresent(component -> component.setSourceId(target));
}
}
}

Expand Down

0 comments on commit 2920380

Please sign in to comment.