Skip to content

Commit 54a7060

Browse files
committed
Implement new fixbiome command
1 parent 5c252b9 commit 54a7060

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ minecraft {
4242
mappings channel: 'official', version: mappings_version
4343
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
4444

45-
// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
45+
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
4646

4747
// Default run configurations.
4848
// These can be tweaked, removed, or duplicated as needed.

src/main/java/dev/compactmods/machines/api/core/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public abstract class Messages {
88
public static final ResourceLocation MACHINE_SPAWNPOINT_SET = new ResourceLocation(Constants.MOD_ID, "spawnpoint_set");
99
public static final ResourceLocation TELEPORT_OUT_OF_BOUNDS = new ResourceLocation(Constants.MOD_ID, "teleport_oob");
1010
public static final ResourceLocation HOW_DID_YOU_GET_HERE = new ResourceLocation(Constants.MOD_ID, "how_did_you_get_here");
11+
public static final ResourceLocation FIXBIOME_IN_BAD_DIMENSION = new ResourceLocation(Constants.MOD_ID, "fixbiome_bad_dim");
1112
}

src/main/java/dev/compactmods/machines/command/CMCommandRoot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class CMCommandRoot {
1010
public static void register(CommandDispatcher<CommandSource> dispatcher) {
1111
final LiteralArgumentBuilder<CommandSource> root = LiteralArgumentBuilder.literal(CompactMachines.MOD_ID);
1212
root.then(CMEjectSubcommand.register());
13+
root.then(CMFixBiomeSubcommand.register());
1314
dispatcher.register(root);
1415
}
1516
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.compactmods.machines.command;
2+
3+
import java.util.Arrays;
4+
import com.mojang.brigadier.builder.ArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import dev.compactmods.machines.CompactMachines;
7+
import dev.compactmods.machines.api.core.Messages;
8+
import dev.compactmods.machines.core.Registration;
9+
import dev.compactmods.machines.util.TranslationUtil;
10+
import net.minecraft.command.CommandSource;
11+
import net.minecraft.command.Commands;
12+
import net.minecraft.command.arguments.ResourceLocationArgument;
13+
import net.minecraft.command.arguments.SuggestionProviders;
14+
import net.minecraft.network.play.server.SChunkDataPacket;
15+
import net.minecraft.server.MinecraftServer;
16+
import net.minecraft.util.RegistryKey;
17+
import net.minecraft.util.ResourceLocation;
18+
import net.minecraft.util.math.BlockPos;
19+
import net.minecraft.util.math.vector.Vector3d;
20+
import net.minecraft.util.registry.Registry;
21+
import net.minecraft.world.biome.Biome;
22+
import net.minecraft.world.biome.BiomeContainer;
23+
import net.minecraft.world.biome.Biomes;
24+
import net.minecraft.world.chunk.Chunk;
25+
import net.minecraft.world.server.ServerWorld;
26+
import net.minecraftforge.fml.network.PacketDistributor;
27+
28+
public class CMFixBiomeSubcommand {
29+
public static ArgumentBuilder<CommandSource, ?> register() {
30+
return Commands.literal("fixbiome")
31+
.executes(CMFixBiomeSubcommand::fixBiomeDefault)
32+
.then(
33+
Commands.argument("biome", ResourceLocationArgument.id())
34+
.suggests(SuggestionProviders.AVAILABLE_BIOMES)
35+
.executes(CMFixBiomeSubcommand::specificBiome)
36+
);
37+
}
38+
39+
40+
private static int specificBiome(CommandContext<CommandSource> ctx) {
41+
if(!ctx.getSource().getLevel().dimension().equals(Registration.COMPACT_DIMENSION)) {
42+
ctx.getSource().sendFailure(TranslationUtil.message(Messages.FIXBIOME_IN_BAD_DIMENSION));
43+
return -1;
44+
}
45+
46+
final ResourceLocation biomeId = ResourceLocationArgument.getId(ctx, "biome");
47+
updateBiomeData(ctx, biomeId);
48+
return 0;
49+
}
50+
51+
52+
private static int fixBiomeDefault(CommandContext<CommandSource> ctx) {
53+
if(!ctx.getSource().getLevel().dimension().equals(Registration.COMPACT_DIMENSION)) {
54+
ctx.getSource().sendFailure(TranslationUtil.message(Messages.FIXBIOME_IN_BAD_DIMENSION));
55+
return -1;
56+
}
57+
58+
final ResourceLocation biomeId = Biomes.PLAINS.location();
59+
updateBiomeData(ctx, biomeId);
60+
return 0;
61+
}
62+
63+
private static void updateBiomeData(CommandContext<CommandSource> ctx, ResourceLocation biomeId) {
64+
final MinecraftServer server = ctx.getSource().getServer();
65+
final ServerWorld level = server.getLevel(Registration.COMPACT_DIMENSION);
66+
if (level == null) {
67+
CompactMachines.LOGGER.error("Error: Compact dimension not registered.");
68+
return;
69+
}
70+
71+
final Vector3d position = ctx.getSource().getPosition();
72+
final BlockPos excAt = new BlockPos(position.x, position.y, position.z);
73+
final Chunk chunkAt = level.getChunkAt(excAt);
74+
75+
final BiomeContainer biomes = chunkAt.getBiomes();
76+
if (biomes != null) {
77+
final Biome newBiome = server.registryAccess()
78+
.registryOrThrow(Registry.BIOME_REGISTRY)
79+
.get(RegistryKey.create(Registry.BIOME_REGISTRY, biomeId));
80+
81+
Arrays.fill(biomes.biomes, newBiome);
82+
83+
chunkAt.setUnsaved(true);
84+
85+
SChunkDataPacket pkt = new SChunkDataPacket(chunkAt, 65535);
86+
87+
PacketDistributor.TRACKING_CHUNK
88+
.with(() -> level.getChunkAt(excAt))
89+
.send(pkt);
90+
}
91+
}
92+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.compactmods.machines.level;
2+
3+
import net.minecraft.util.math.BlockPos;
4+
import net.minecraft.util.math.MathHelper;
5+
import net.minecraft.world.biome.BiomeContainer;
6+
7+
public class BiomeHelper {
8+
private static final int WIDTH_BITS = (int)Math.round(Math.log(16.0D) / Math.log(2.0D)) - 2;
9+
10+
public static int biomeIndex(BlockPos pos) {
11+
int l = (pos.getX() >> 2) & BiomeContainer.HORIZONTAL_MASK;
12+
int m = MathHelper.clamp(pos.getY() >> 2, 0, BiomeContainer.VERTICAL_MASK);
13+
int n = (pos.getZ() >> 2) & BiomeContainer.HORIZONTAL_MASK;
14+
return m << WIDTH_BITS + WIDTH_BITS
15+
| n << WIDTH_BITS
16+
| l;
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public net.minecraft.world.biome.BiomeContainer field_227054_f_ # biomes

src/main/resources/assets/compactmachines/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"message.compactmachines.spawnpoint_set": "New spawn point set.",
1717
"message.compactmachines.no_machine_data": "No machine data loaded; report this.",
1818
"message.compactmachines.teleport_oob": "An otherworldly force prevents your teleportation.",
19+
"message.compactmachines.fixbiome_bad_dim": "Cannot use fixbiome command outside of a machine room.",
1920

2021
"block.compactmachines.machine": "Compact Machine",
2122
"block.compactmachines.machine_tiny": "Compact Machine (Tiny)",

0 commit comments

Comments
 (0)