refactor: use BlockBox for core #3

Manually merged
amy merged 1 commit from amy/chomensmod:1.20.1 into 1.20.1 2024-12-28 20:11:33 -05:00
6 changed files with 89 additions and 64 deletions

View file

@ -3,6 +3,7 @@ package land.chipmunk.chipmunkmod;
import com.google.gson.GsonBuilder;
import land.chipmunk.chipmunkmod.modules.KaboomCheck;
import land.chipmunk.chipmunkmod.modules.SelfCare;
import land.chipmunk.chipmunkmod.util.gson.BlockBoxTypeAdapter;
import land.chipmunk.chipmunkmod.util.gson.BlockPosTypeAdapter;
import net.fabricmc.api.ModInitializer;
import java.io.InputStream;
@ -16,6 +17,7 @@ import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,6 +57,7 @@ public class ChipmunkMod implements ModInitializer {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter())
.registerTypeAdapter(BlockBox.class, new BlockBoxTypeAdapter())
.create();
final File file = CONFIG_FILE;

View file

@ -1,7 +1,7 @@
package land.chipmunk.chipmunkmod;
import com.google.gson.JsonObject;
import land.chipmunk.chipmunkmod.data.BlockArea;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
@ -18,7 +18,7 @@ public class Configuration {
}
public static class CommandCore {
public BlockArea relativeArea = new BlockArea(new BlockPos(0, 0, 0), new BlockPos(15, 0, 15));
public BlockBox relativeArea = BlockBox.create(new BlockPos(0, 0, 0), new BlockPos(15, 0, 15));
}
public static class Bots {

View file

@ -1,14 +0,0 @@
package land.chipmunk.chipmunkmod.data;
import net.minecraft.util.math.BlockPos;
// ? Am I reinventing the wheel here?
public class BlockArea {
public BlockPos start;
public BlockPos end;
public BlockArea (BlockPos start, BlockPos end) {
this.start = start;
this.end = end;
}
}

View file

@ -1,10 +1,8 @@
package land.chipmunk.chipmunkmod.modules;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.data.BlockArea;
import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import land.chipmunk.chipmunkmod.util.MathUtilities;
import net.minecraft.block.Block;
import net.minecraft.block.CommandBlock;
import net.minecraft.block.entity.CommandBlockBlockEntity;
@ -14,6 +12,7 @@ import net.minecraft.client.world.ClientWorld;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.UpdateCommandBlockC2SPacket;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.dimension.DimensionType;
@ -26,9 +25,9 @@ public class CommandCore {
private final MinecraftClient client;
public boolean ready = false;
public BlockPos origin;
public BlockArea noPos;
public BlockBox noPos;
public BlockPos block;
public BlockArea withPos;
public BlockBox withPos;
private Timer timer;
@ -103,9 +102,9 @@ public class CommandCore {
if (networkHandler == null || withPos == null || !ready) return;
try {
for (int x = withPos.start.getX(); x <= withPos.end.getX(); x++) {
for (int y = withPos.start.getY(); y <= withPos.end.getY(); y++) {
for (int z = withPos.start.getZ(); z <= withPos.end.getZ(); z++) {
for (int x = withPos.getMinX(); x <= withPos.getMaxX(); x++) {
for (int y = withPos.getMinY(); y <= withPos.getMaxY(); y++) {
for (int z = withPos.getMaxZ(); z <= withPos.getMaxZ(); z++) {
final BlockPos pos = new BlockPos(x, y, z);
final ClientWorld world = client.world;
@ -129,31 +128,27 @@ public class CommandCore {
public void move (Vec3d position) {
final ClientWorld world = client.world;
if (world == null || noPos == null) return;
final DimensionType dimension = world.getDimension();
final int dimMinY = dimension.minY();
final int dimMaxY = dimension.height() + dimMinY - 1; // -1 accounts for block at Y=0
int yOffset = 0;
if (noPos.getMinY() < dimMinY) {
yOffset = dimMinY - noPos.getMinY();
} else if (noPos.getMaxY() > dimMaxY) {
yOffset = dimMaxY - noPos.getMaxY();
}
origin = new BlockPos(
((int) position.getX() / 16) * 16,
(int) MathUtilities.clamp(noPos.start.getY(), dimension.minY(), dimension.height()),
noPos.getMinY() + yOffset,
((int) position.getZ() / 16) * 16
);
withPos = new BlockArea(
new BlockPos(
noPos.start.getX() + origin.getX(),
(int) MathUtilities.clamp(noPos.start.getY(), dimension.minY(), dimension.height()),
noPos.start.getZ() + origin.getZ()
),
new BlockPos(
noPos.end.getX() + origin.getX(),
(int) MathUtilities.clamp(noPos.end.getY(), dimension.minY(), dimension.height()),
noPos.end.getZ() + origin.getZ()
)
);
block = new BlockPos(withPos.start);
withPos = noPos.offset(origin.getX(), yOffset, origin.getZ());
block = new BlockPos(withPos.getMinX(), withPos.getMinY(), withPos.getMinZ());
refill();
for (Listener listener : ListenerManager.listeners) listener.coreMoved();
@ -171,13 +166,13 @@ public class CommandCore {
KaboomCheck.INSTANCE.isKaboom ?
"fill %s %s %s %s %s %s repeating_command_block replace" :
"fill %s %s %s %s %s %s command_block",
withPos.start.getX(),
withPos.start.getY(),
withPos.start.getZ(),
withPos.getMinX(),
withPos.getMinY(),
withPos.getMinZ(),
withPos.end.getX(),
withPos.end.getY(),
withPos.end.getZ()
withPos.getMaxX(),
withPos.getMaxY(),
withPos.getMaxZ()
);
client.getNetworkHandler().sendChatCommand(command);
@ -185,32 +180,26 @@ public class CommandCore {
public void incrementCurrentBlock () {
if (withPos == null) return;
final BlockPos start = withPos.start;
final BlockPos end = withPos.end;
if (start == null || end == null) return;
int x = block.getX();
int y = block.getY();
int z = block.getZ();
x++;
if (x > end.getX()) {
x = start.getX();
if (x > withPos.getMaxX()) {
x = withPos.getMinX();
z++;
}
if (z > end.getZ()) {
z = start.getZ();
if (z > withPos.getMaxZ()) {
z = withPos.getMinZ();
y++;
}
if (y > end.getY()) {
x = start.getX();
y = start.getY();
z = start.getZ();
if (y > withPos.getMaxY()) {
x = withPos.getMinX();
y = withPos.getMinY();
z = withPos.getMinZ();
}
block = new BlockPos(x, y, z);

View file

@ -0,0 +1,46 @@
package land.chipmunk.chipmunkmod.util.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import java.io.IOException;
public class BlockBoxTypeAdapter extends TypeAdapter<BlockBox> {
private final BlockPosTypeAdapter blockPosAdapter = new BlockPosTypeAdapter();
@Override
public BlockBox read(JsonReader reader) throws IOException {
BlockPos start = null;
BlockPos end = null;
reader.beginObject();
while (!reader.peek().equals(JsonToken.END_OBJECT)) {
if (reader.peek().equals(JsonToken.NAME)) {
String name = reader.nextName();
switch (name) {
case "start" -> start = blockPosAdapter.read(reader);
case "end" -> end = blockPosAdapter.read(reader);
default -> reader.skipValue();
}
}
}
reader.endObject();
if (start == null || end == null) return null;
return BlockBox.create(start, end);
}
@Override
public void write(JsonWriter writer, BlockBox box) {
// TODO
}
}

View file

@ -20,11 +20,12 @@ public class BlockPosTypeAdapter extends TypeAdapter<BlockPos> {
if (reader.peek().equals(JsonToken.NAME)) {
String name = reader.nextName();
// ? Is there a better way to do this?
if (name.equals("x")) x = reader.nextInt();
else if (name.equals("y")) y = reader.nextInt();
else if (name.equals("z")) z = reader.nextInt();
else reader.skipValue();
switch (name) {
case "x" -> x = reader.nextInt();
case "y" -> y = reader.nextInt();
case "z" -> z = reader.nextInt();
default -> reader.skipValue();
}
}
}