Skip to content

Commit 17e07be

Browse files
committed
Add generate sphere
1 parent 16951b0 commit 17e07be

3 files changed

Lines changed: 86 additions & 2 deletions

File tree

src/client/java/com/tpexcotblu/litematicgen/LitematicGeneratorClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public class LitematicGeneratorClient implements ClientModInitializer {
77
@Override
88
public void onInitializeClient() {
9-
SchemeCommandRegistrar.registerCircleCommand();
9+
SchemeCommandRegistrar.registerAllCommands();
1010
}
1111
}

src/client/java/com/tpexcotblu/litematicgen/commands/SchemeCommandRegistrar.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mojang.brigadier.arguments.StringArgumentType;
55
import com.mojang.brigadier.suggestion.SuggestionProvider;
66
import com.tpexcotblu.litematicgen.generators.SchemeGenerator;
7+
import com.tpexcotblu.litematicgen.generators.SphereGenerator;
78
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
89
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
910
import net.minecraft.nbt.NbtCompound;
@@ -58,7 +59,7 @@ private static int saveScheme(FabricClientCommandSource source, SchemeGenerator
5859
return 1;
5960
}
6061

61-
public static void registerCircleCommand() {
62+
private static void registerCircleCommand() {
6263
SuggestionProvider<FabricClientCommandSource> orientationSuggestions = (context, builder) -> {
6364
return builder.suggest("horizontal").suggest("vertical").buildFuture();
6465
};
@@ -80,4 +81,24 @@ public static void registerCircleCommand() {
8081
);
8182
});
8283
}
84+
85+
private static void registerSphereCommand() {
86+
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
87+
dispatcher.register(
88+
literal("generatesphere")
89+
.then(argument("radius", IntegerArgumentType.integer(1, 128))
90+
.executes(ctx -> {
91+
int radius = IntegerArgumentType.getInteger(ctx, "radius");
92+
SphereGenerator generator = new SphereGenerator(radius);
93+
return saveScheme(ctx.getSource(), generator);
94+
})
95+
)
96+
);
97+
});
98+
}
99+
100+
public static void registerAllCommands() {
101+
registerCircleCommand();
102+
registerSphereCommand();
103+
}
83104
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.tpexcotblu.litematicgen.generators;
2+
3+
import net.minecraft.nbt.NbtCompound;
4+
import net.minecraft.nbt.NbtInt;
5+
import net.minecraft.nbt.NbtList;
6+
7+
public class SphereGenerator implements SchemeGenerator {
8+
private final int radius;
9+
10+
public SphereGenerator(int radius) {
11+
this.radius = radius;
12+
}
13+
14+
@Override
15+
public NbtList generateBlocks() {
16+
int size = radius * 2 + 1;
17+
NbtList blocks = new NbtList();
18+
19+
for (int y = 0; y < size; y++) {
20+
for (int z = 0; z < size; z++) {
21+
for (int x = 0; x < size; x++) {
22+
int dx = x - radius;
23+
int dy = y - radius;
24+
int dz = z - radius;
25+
double dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
26+
27+
if (Math.abs(dist - radius) < 0.5) {
28+
NbtCompound block = new NbtCompound();
29+
NbtList posList = new NbtList();
30+
posList.add(NbtInt.of(x));
31+
posList.add(NbtInt.of(y));
32+
posList.add(NbtInt.of(z));
33+
block.put("pos", posList);
34+
block.putInt("state", 1);
35+
blocks.add(block);
36+
}
37+
}
38+
}
39+
}
40+
41+
return blocks;
42+
}
43+
44+
@Override
45+
public NbtList generateSize() {
46+
int size = radius * 2 + 1;
47+
NbtList sizeList = new NbtList();
48+
sizeList.add(NbtInt.of(size));
49+
sizeList.add(NbtInt.of(size));
50+
sizeList.add(NbtInt.of(size));
51+
return sizeList;
52+
}
53+
54+
@Override
55+
public String getFilename() {
56+
return "sphere_" + radius + ".nbt";
57+
}
58+
59+
@Override
60+
public String getSuccessMessage() {
61+
return "The sphere is saved";
62+
}
63+
}

0 commit comments

Comments
 (0)