refactor: use BlockBox for core #3
6 changed files with 89 additions and 64 deletions
src/main/java/land/chipmunk/chipmunkmod
|
@ -3,6 +3,7 @@ package land.chipmunk.chipmunkmod;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import land.chipmunk.chipmunkmod.modules.KaboomCheck;
|
import land.chipmunk.chipmunkmod.modules.KaboomCheck;
|
||||||
import land.chipmunk.chipmunkmod.modules.SelfCare;
|
import land.chipmunk.chipmunkmod.modules.SelfCare;
|
||||||
|
import land.chipmunk.chipmunkmod.util.gson.BlockBoxTypeAdapter;
|
||||||
import land.chipmunk.chipmunkmod.util.gson.BlockPosTypeAdapter;
|
import land.chipmunk.chipmunkmod.util.gson.BlockPosTypeAdapter;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -16,6 +17,7 @@ import java.io.IOException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import net.minecraft.util.math.BlockBox;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -55,6 +57,7 @@ public class ChipmunkMod implements ModInitializer {
|
||||||
|
|
||||||
final Gson gson = new GsonBuilder()
|
final Gson gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter())
|
.registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter())
|
||||||
|
.registerTypeAdapter(BlockBox.class, new BlockBoxTypeAdapter())
|
||||||
.create();
|
.create();
|
||||||
final File file = CONFIG_FILE;
|
final File file = CONFIG_FILE;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package land.chipmunk.chipmunkmod;
|
package land.chipmunk.chipmunkmod;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import land.chipmunk.chipmunkmod.data.BlockArea;
|
import net.minecraft.util.math.BlockBox;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ public class Configuration {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CommandCore {
|
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 {
|
public static class Bots {
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,10 +1,8 @@
|
||||||
package land.chipmunk.chipmunkmod.modules;
|
package land.chipmunk.chipmunkmod.modules;
|
||||||
|
|
||||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||||
import land.chipmunk.chipmunkmod.data.BlockArea;
|
|
||||||
import land.chipmunk.chipmunkmod.listeners.Listener;
|
import land.chipmunk.chipmunkmod.listeners.Listener;
|
||||||
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
|
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
|
||||||
import land.chipmunk.chipmunkmod.util.MathUtilities;
|
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.CommandBlock;
|
import net.minecraft.block.CommandBlock;
|
||||||
import net.minecraft.block.entity.CommandBlockBlockEntity;
|
import net.minecraft.block.entity.CommandBlockBlockEntity;
|
||||||
|
@ -14,6 +12,7 @@ import net.minecraft.client.world.ClientWorld;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import net.minecraft.network.packet.c2s.play.UpdateCommandBlockC2SPacket;
|
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.BlockPos;
|
||||||
import net.minecraft.util.math.Vec3d;
|
import net.minecraft.util.math.Vec3d;
|
||||||
import net.minecraft.world.dimension.DimensionType;
|
import net.minecraft.world.dimension.DimensionType;
|
||||||
|
@ -26,9 +25,9 @@ public class CommandCore {
|
||||||
private final MinecraftClient client;
|
private final MinecraftClient client;
|
||||||
public boolean ready = false;
|
public boolean ready = false;
|
||||||
public BlockPos origin;
|
public BlockPos origin;
|
||||||
public BlockArea noPos;
|
public BlockBox noPos;
|
||||||
public BlockPos block;
|
public BlockPos block;
|
||||||
public BlockArea withPos;
|
public BlockBox withPos;
|
||||||
|
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
|
|
||||||
|
@ -103,9 +102,9 @@ public class CommandCore {
|
||||||
if (networkHandler == null || withPos == null || !ready) return;
|
if (networkHandler == null || withPos == null || !ready) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (int x = withPos.start.getX(); x <= withPos.end.getX(); x++) {
|
for (int x = withPos.getMinX(); x <= withPos.getMaxX(); x++) {
|
||||||
for (int y = withPos.start.getY(); y <= withPos.end.getY(); y++) {
|
for (int y = withPos.getMinY(); y <= withPos.getMaxY(); y++) {
|
||||||
for (int z = withPos.start.getZ(); z <= withPos.end.getZ(); z++) {
|
for (int z = withPos.getMaxZ(); z <= withPos.getMaxZ(); z++) {
|
||||||
final BlockPos pos = new BlockPos(x, y, z);
|
final BlockPos pos = new BlockPos(x, y, z);
|
||||||
|
|
||||||
final ClientWorld world = client.world;
|
final ClientWorld world = client.world;
|
||||||
|
@ -129,31 +128,27 @@ public class CommandCore {
|
||||||
|
|
||||||
public void move (Vec3d position) {
|
public void move (Vec3d position) {
|
||||||
final ClientWorld world = client.world;
|
final ClientWorld world = client.world;
|
||||||
|
|
||||||
if (world == null || noPos == null) return;
|
if (world == null || noPos == null) return;
|
||||||
|
|
||||||
final DimensionType dimension = world.getDimension();
|
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(
|
origin = new BlockPos(
|
||||||
((int) position.getX() / 16) * 16,
|
((int) position.getX() / 16) * 16,
|
||||||
(int) MathUtilities.clamp(noPos.start.getY(), dimension.minY(), dimension.height()),
|
noPos.getMinY() + yOffset,
|
||||||
((int) position.getZ() / 16) * 16
|
((int) position.getZ() / 16) * 16
|
||||||
);
|
);
|
||||||
|
|
||||||
withPos = new BlockArea(
|
withPos = noPos.offset(origin.getX(), yOffset, origin.getZ());
|
||||||
new BlockPos(
|
block = new BlockPos(withPos.getMinX(), withPos.getMinY(), withPos.getMinZ());
|
||||||
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);
|
|
||||||
refill();
|
refill();
|
||||||
|
|
||||||
for (Listener listener : ListenerManager.listeners) listener.coreMoved();
|
for (Listener listener : ListenerManager.listeners) listener.coreMoved();
|
||||||
|
@ -171,13 +166,13 @@ public class CommandCore {
|
||||||
KaboomCheck.INSTANCE.isKaboom ?
|
KaboomCheck.INSTANCE.isKaboom ?
|
||||||
"fill %s %s %s %s %s %s repeating_command_block replace" :
|
"fill %s %s %s %s %s %s repeating_command_block replace" :
|
||||||
"fill %s %s %s %s %s %s command_block",
|
"fill %s %s %s %s %s %s command_block",
|
||||||
withPos.start.getX(),
|
withPos.getMinX(),
|
||||||
withPos.start.getY(),
|
withPos.getMinY(),
|
||||||
withPos.start.getZ(),
|
withPos.getMinZ(),
|
||||||
|
|
||||||
withPos.end.getX(),
|
withPos.getMaxX(),
|
||||||
withPos.end.getY(),
|
withPos.getMaxY(),
|
||||||
withPos.end.getZ()
|
withPos.getMaxZ()
|
||||||
);
|
);
|
||||||
|
|
||||||
client.getNetworkHandler().sendChatCommand(command);
|
client.getNetworkHandler().sendChatCommand(command);
|
||||||
|
@ -185,32 +180,26 @@ public class CommandCore {
|
||||||
|
|
||||||
public void incrementCurrentBlock () {
|
public void incrementCurrentBlock () {
|
||||||
if (withPos == null) return;
|
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 x = block.getX();
|
||||||
int y = block.getY();
|
int y = block.getY();
|
||||||
int z = block.getZ();
|
int z = block.getZ();
|
||||||
|
|
||||||
x++;
|
x++;
|
||||||
|
|
||||||
if (x > end.getX()) {
|
if (x > withPos.getMaxX()) {
|
||||||
x = start.getX();
|
x = withPos.getMinX();
|
||||||
z++;
|
z++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (z > end.getZ()) {
|
if (z > withPos.getMaxZ()) {
|
||||||
z = start.getZ();
|
z = withPos.getMinZ();
|
||||||
y++;
|
y++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y > end.getY()) {
|
if (y > withPos.getMaxY()) {
|
||||||
x = start.getX();
|
x = withPos.getMinX();
|
||||||
y = start.getY();
|
y = withPos.getMinY();
|
||||||
z = start.getZ();
|
z = withPos.getMinZ();
|
||||||
}
|
}
|
||||||
|
|
||||||
block = new BlockPos(x, y, z);
|
block = new BlockPos(x, y, z);
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -20,11 +20,12 @@ public class BlockPosTypeAdapter extends TypeAdapter<BlockPos> {
|
||||||
if (reader.peek().equals(JsonToken.NAME)) {
|
if (reader.peek().equals(JsonToken.NAME)) {
|
||||||
String name = reader.nextName();
|
String name = reader.nextName();
|
||||||
|
|
||||||
// ? Is there a better way to do this?
|
switch (name) {
|
||||||
if (name.equals("x")) x = reader.nextInt();
|
case "x" -> x = reader.nextInt();
|
||||||
else if (name.equals("y")) y = reader.nextInt();
|
case "y" -> y = reader.nextInt();
|
||||||
else if (name.equals("z")) z = reader.nextInt();
|
case "z" -> z = reader.nextInt();
|
||||||
else reader.skipValue();
|
default -> reader.skipValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue