|
| 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 | +} |
0 commit comments