Skip to content

Commit

Permalink
Merge pull request #4 from teaconmc/feat/claim-multiple-chunk
Browse files Browse the repository at this point in the history
/ac claim chunk <x-expand-amount> <z-expand-amount>
  • Loading branch information
3TUSK authored Jul 22, 2022
2 parents 7988903 + 29abe07 commit 43342b4
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/teacon/areacontrol/AreaControlCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -53,6 +54,9 @@ public AreaControlCommand(CommandDispatcher<CommandSourceStack> dispatcher) {
.executes(AreaControlCommand::claimMarked))
.then(Commands.literal("chunk")
.requires(check(AreaControlPermissions.CLAIM_CHUNK_AREA))
.then(Commands.argument("x", IntegerArgumentType.integer())
.then(Commands.argument("z", IntegerArgumentType.integer())
.executes(AreaControlCommand::claimChunkWithSize)))
.executes(AreaControlCommand::claimChunk)))
.then(Commands.literal("current")
.then(Commands.literal("friends")
Expand Down Expand Up @@ -152,6 +156,75 @@ private static int claimChunk(CommandContext<CommandSourceStack> context) throws
}
}

private static int claimChunkWithSize(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
final var src = context.getSource();
final var claimer = src.getPlayerOrException();
final var corner = new ChunkPos(new BlockPos(src.getPosition()));
int xOffset = context.getArgument("x", Integer.class), zOffset = context.getArgument("z", Integer.class);
final BlockPos chunkStart, chunkEnd;
if (xOffset > 0) {
if (zOffset > 0) {
// Corner is at northwest
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 0);
chunkEnd = new ChunkPos(corner.x + xOffset - 1, corner.z + zOffset - 1).getBlockAt(15, Integer.MAX_VALUE, 15);
} else if (zOffset == 0) {
// Corner is at northwest
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 0);
chunkEnd = new ChunkPos(corner.x + xOffset - 1, corner.z).getBlockAt(15, Integer.MAX_VALUE, 15);
} else {
// Corner is at southwest
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 15);
chunkEnd = new ChunkPos(corner.x + xOffset - 1, corner.z + zOffset + 1).getBlockAt(15, Integer.MAX_VALUE, 0);
}
} else if (xOffset == 0) {
if (zOffset > 0) {
// Corner is at northwest
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 0);
chunkEnd = new ChunkPos(corner.x, corner.z + zOffset - 1).getBlockAt(15, Integer.MAX_VALUE, 15);
} else if (zOffset == 0) {
// Corner is at northwest, just one chunk
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 0);
chunkEnd = corner.getBlockAt(15, Integer.MAX_VALUE, 15);
} else {
// Corner is at southwest
chunkStart = corner.getBlockAt(0, Integer.MIN_VALUE, 15);
chunkEnd = new ChunkPos(corner.x, corner.z + zOffset + 1).getBlockAt(15, Integer.MAX_VALUE, 0);
}
} else {
if (zOffset > 0) {
// Corner is at northeast
chunkStart = corner.getBlockAt(15, Integer.MIN_VALUE, 0);
chunkEnd = new ChunkPos(corner.x + xOffset + 1, corner.z + zOffset - 1).getBlockAt(0, Integer.MAX_VALUE, 15);
} else if (zOffset == 0) {
// Corner is at northeast
chunkStart = corner.getBlockAt(15, Integer.MIN_VALUE, 0);
chunkEnd = new ChunkPos(corner.x + xOffset + 1, corner.z).getBlockAt(0, Integer.MAX_VALUE, 15);
} else {
// Corner is southeast
chunkStart = corner.getBlockAt(15, Integer.MIN_VALUE, 15);
chunkEnd = new ChunkPos(corner.x + xOffset + 1, corner.z + zOffset + 1).getBlockAt(0, Integer.MAX_VALUE, 0);
}
}
final var range = new AABB(Vec3.atCenterOf(chunkStart), Vec3.atCenterOf(chunkEnd));
if (!range.expandTowards(0.5, 0.5, 0.5).contains(claimer.position())) {
src.sendFailure(new TranslatableComponent("area_control.error.outside_selection"));
return -1;
}
final Area area = Util.createArea(chunkStart, chunkEnd);
final var worldIndex = src.getLevel().dimension();
final UUID claimerUUID = claimer.getGameProfile().getId();
if (claimerUUID != null) {
area.owner = claimerUUID;
}
if (AreaManager.INSTANCE.add(area, worldIndex)) {
src.sendSuccess(new TranslatableComponent("area_control.claim.created", area.name, Util.toGreenText(area)), true);
return Command.SINGLE_SUCCESS;
} else {
src.sendFailure(new TranslatableComponent("area_control.error.overlap"));
return -2;
}
}

private static int claimMarked(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
final var src = context.getSource();
final var claimer = src.getPlayerOrException();
Expand Down

0 comments on commit 43342b4

Please sign in to comment.