Skip to content

Commit

Permalink
command changes (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
screret authored Sep 9, 2024
1 parent 02bc041 commit b0bae42
Show file tree
Hide file tree
Showing 18 changed files with 649 additions and 281 deletions.
4 changes: 4 additions & 0 deletions src/generated/resources/assets/gtceu/lang/en_ud.json
Original file line number Diff line number Diff line change
Expand Up @@ -1730,10 +1730,13 @@
"block.gtceu.zpm_world_accelerator": "ɹ§III ɹoʇɐɹǝןǝɔɔⱯ pןɹoM ǝʇıןƎɔ§",
"block.sterilizing_filter_casing.tooltip": "ʇuǝɯuoɹıʌuǝ ㄥ§pǝzıןıɹǝʇSɐ§ ɐ sǝʇɐǝɹƆ",
"block.surface_rock": "ʞɔoᴚ ǝɔɐɟɹnS %s",
"command.gtceu.dump_data.success": "%s oʇ %s ʎɹʇsıbǝɹ ɯoɹɟ sǝɔɹnosǝɹ %s pǝdɯnᗡ",
"command.gtceu.medical_condition.get": ":suoıʇıpuoɔ ןɐɔıpǝɯ ǝsǝɥʇ sɐɥ %s ɹǝʎɐןԀ",
"command.gtceu.medical_condition.get.element": "spuoɔǝs %s :ɹ§%s uoıʇıpuoƆ",
"command.gtceu.medical_condition.get.element.permanent": ")ʇuǝuɐɯɹǝd( spuoɔǝs %s :ɹ§%s uoıʇıpuoƆ",
"command.gtceu.medical_condition.get.empty": "˙suoıʇıpuoɔ ןɐɔıpǝɯ ou sɐɥ %s ɹǝʎɐןԀ",
"command.gtceu.place_vein.failure": "%s uoıʇısod ʇɐ %s uıǝʌ ǝɔɐןd oʇ pǝןıɐℲ",
"command.gtceu.place_vein.success": "%s uoıʇısod ʇɐ %s uıǝʌ pǝɔɐןԀ",
"compass.node.gtceu.batteries/energy_cluster": "ɹǝʇsnןƆ ʎbɹǝuƎ",
"compass.node.gtceu.batteries/energy_crystal": "ןɐʇsʎɹƆ ʎbɹǝuƎ",
"compass.node.gtceu.batteries/energy_module": "ǝןnpoW ʎbɹǝuƎ",
Expand Down Expand Up @@ -5245,6 +5248,7 @@
"material.gtceu.einsteinium": "ɯnıuıǝʇsuıƎ",
"material.gtceu.electrotine": "ǝuıʇoɹʇɔǝןƎ",
"material.gtceu.electrum": "ɯnɹʇɔǝןƎ",
"material.gtceu.elementium": "ɯnıʇuǝɯǝןƎ",
"material.gtceu.emerald": "pןɐɹǝɯƎ",
"material.gtceu.ender_air": "ɹıⱯ ɹǝpuƎ",
"material.gtceu.ender_eye": "ǝʎƎ ɹǝpuƎ",
Expand Down
4 changes: 4 additions & 0 deletions src/generated/resources/assets/gtceu/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1730,10 +1730,13 @@
"block.gtceu.zpm_world_accelerator": "§cElite World Accelerator III§r",
"block.sterilizing_filter_casing.tooltip": "Creates a §aSterilized§7 environment",
"block.surface_rock": "%s Surface Rock",
"command.gtceu.dump_data.success": "Dumped %s resources from registry %s to %s",
"command.gtceu.medical_condition.get": "Player %s has these medical conditions:",
"command.gtceu.medical_condition.get.element": "Condition %s§r: %s seconds",
"command.gtceu.medical_condition.get.element.permanent": "Condition %s§r: %s seconds (permanent)",
"command.gtceu.medical_condition.get.empty": "Player %s has no medical conditions.",
"command.gtceu.place_vein.failure": "Failed to place vein %s at position %s",
"command.gtceu.place_vein.success": "Placed vein %s at position %s",
"compass.node.gtceu.batteries/energy_cluster": "Energy Cluster",
"compass.node.gtceu.batteries/energy_crystal": "Energy Crystal",
"compass.node.gtceu.batteries/energy_module": "Energy Module",
Expand Down Expand Up @@ -5245,6 +5248,7 @@
"material.gtceu.einsteinium": "Einsteinium",
"material.gtceu.electrotine": "Electrotine",
"material.gtceu.electrum": "Electrum",
"material.gtceu.elementium": "Elementium",
"material.gtceu.emerald": "Emerald",
"material.gtceu.ender_air": "Ender Air",
"material.gtceu.ender_eye": "Ender Eye",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Getter;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -48,6 +49,10 @@ public Map<BlockPos, OreBlockPlacer> consumeOres(ChunkPos chunk) {
return this.generatedOres.getOrDefault(chunk, Map.of());
}

public Set<ChunkPos> getGeneratedChunks() {
return generatedOres.keySet();
}

@Override
public String toString() {
return "GeneratedVein[origin=" + origin + ", chunks={" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Getter;
import org.apache.commons.lang3.mutable.MutableInt;

import java.util.List;
Expand All @@ -30,6 +31,7 @@
@ParametersAreNonnullByDefault
public class OreGenCache {

@Getter
private final OreGenerator oreGenerator = new OreGenerator();

private final int oreGenerationCacheSize = ConfigHolder.INSTANCE != null ?
Expand Down Expand Up @@ -61,7 +63,7 @@ private List<GeneratedVeinMetadata> getOrCreateVeinMetadata(WorldGenLevel level,
return veinMetadataByOrigin
.get(chunkPos, () -> oreGenerator.generateMetadata(level, generator, chunkPos));
} catch (ExecutionException e) {
GTCEu.LOGGER.error("Cannot create vein position in chunk " + chunkPos, e);
GTCEu.LOGGER.error("Cannot create vein position in chunk {}", chunkPos, e);
return List.of();
}
}
Expand All @@ -81,7 +83,7 @@ public List<GeneratedVein> consumeChunkVeins(WorldGenLevel level, ChunkGenerator
getOrCreateVeinMetadata(level, generator, chunkPos), chunkPos))
.stream();
} catch (ExecutionException e) {
GTCEu.LOGGER.error("Cannot create vein in chunk " + chunkPos, e);
GTCEu.LOGGER.error("Cannot create vein in chunk {}", chunkPos, e);
return Stream.empty();
}
}).filter(Objects::nonNull).toList();
Expand All @@ -103,7 +105,7 @@ public List<GeneratedIndicators> consumeChunkIndicators(WorldGenLevel level, Chu
getOrCreateVeinMetadata(level, generator, chunkPos), chunkPos))
.stream();
} catch (ExecutionException e) {
GTCEu.LOGGER.error("Cannot create vein in chunk " + chunkPos, e);
GTCEu.LOGGER.error("Cannot create vein in chunk {}", chunkPos, e);
return Stream.empty();
}
}).filter(Objects::nonNull).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@MethodsReturnNonnullByDefault
public class OreGenerator {

private record VeinConfiguration(GeneratedVeinMetadata data, RandomSource random) {
public record VeinConfiguration(GeneratedVeinMetadata data, RandomSource random) {

public RandomSource newRandom() {
return new XoroshiroRandomSource(random.nextLong());
Expand Down Expand Up @@ -96,7 +96,7 @@ public List<GeneratedVein> generateOres(WorldGenLevel level, List<GeneratedVeinM
.toList();
}

private Optional<GeneratedVein> generateOres(VeinConfiguration config, WorldGenLevel level, ChunkPos chunkPos) {
public Optional<GeneratedVein> generateOres(VeinConfiguration config, WorldGenLevel level, ChunkPos chunkPos) {
GTOreDefinition definition = config.data.definition();
Map<BlockPos, OreBlockPlacer> generatedVeins = definition.veinGenerator()
.generate(level, config.newRandom(), definition, config.data.center());
Expand Down Expand Up @@ -162,15 +162,15 @@ private static Optional<BlockPos> computeVeinOrigin(WorldGenLevel level, ChunkGe

private static VeinConfiguration logVeinGeneration(VeinConfiguration config) {
if (ConfigHolder.INSTANCE.dev.debugWorldgen) {
GTCEu.LOGGER.debug("Generating vein " + config.data.id() + " at " + config.data.center());
GTCEu.LOGGER.debug("Generating vein {} at {}", config.data.id(), config.data.center());
}

return config;
}

private static void logEmptyVein(VeinConfiguration config) {
if (ConfigHolder.INSTANCE.dev.debugWorldgen) {
GTCEu.LOGGER.debug("No blocks generated for vein " + config.data.id() + " at " + config.data.center());
GTCEu.LOGGER.debug("No blocks generated for vein {} at {}", config.data.id(), config.data.center());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.chunk.BulkSectionAccess;
import net.minecraft.world.level.chunk.ChunkAccess;
Expand All @@ -12,9 +13,12 @@
import net.minecraft.world.level.levelgen.XoroshiroRandomSource;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;

import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

/**
Expand All @@ -28,6 +32,7 @@
@ParametersAreNonnullByDefault
public class OrePlacer {

@Getter
private final OreGenCache oreGenCache = new OreGenCache();

/**
Expand All @@ -43,14 +48,14 @@ public void placeOres(WorldGenLevel level, ChunkGenerator chunkGenerator, ChunkA
var generatedIndicators = oreGenCache.consumeChunkIndicators(level, chunkGenerator, chunk);

try (BulkSectionAccess access = new BulkSectionAccess(level)) {
generatedVeins.forEach(generatedVein -> placeVein(chunk, random, access, generatedVein));
generatedVeins.forEach(generatedVein -> placeVein(chunk.getPos(), random, access, generatedVein, null));
generatedIndicators.forEach(generatedIndicator -> placeIndicators(chunk, access, generatedIndicator));
}
}

private void placeVein(ChunkAccess chunk, RandomSource random, BulkSectionAccess access,
GeneratedVein generatedVein) {
RuleTest layerTarget = generatedVein.getLayer().getTarget();
public void placeVein(ChunkPos chunk, RandomSource random, BulkSectionAccess access,
GeneratedVein generatedVein, @Nullable RuleTest targetOverride) {
RuleTest layerTarget = targetOverride != null ? targetOverride : generatedVein.getLayer().getTarget();

resolvePlacerLists(chunk, generatedVein).forEach(((sectionPos, placers) -> {
LevelChunkSection section = access.getSection(sectionPos.origin());
Expand All @@ -70,8 +75,8 @@ private void placeVein(ChunkAccess chunk, RandomSource random, BulkSectionAccess
}));
}

private Map<SectionPos, Map<BlockPos, OreBlockPlacer>> resolvePlacerLists(ChunkAccess chunk, GeneratedVein vein) {
return vein.consumeOres(chunk.getPos()).entrySet().stream()
private Map<SectionPos, Map<BlockPos, OreBlockPlacer>> resolvePlacerLists(ChunkPos chunk, GeneratedVein vein) {
return vein.consumeOres(chunk).entrySet().stream()
.collect(Collectors.groupingBy(
entry -> SectionPos.of(entry.getKey()),
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
Expand Down
152 changes: 152 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/common/commands/GTCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.gregtechceu.gtceu.common.commands;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.data.worldgen.GTOreDefinition;
import com.gregtechceu.gtceu.api.data.worldgen.bedrockfluid.BedrockFluidDefinition;
import com.gregtechceu.gtceu.api.data.worldgen.bedrockore.BedrockOreDefinition;
import com.gregtechceu.gtceu.api.data.worldgen.ores.GeneratedVeinMetadata;
import com.gregtechceu.gtceu.api.data.worldgen.ores.OreGenerator;
import com.gregtechceu.gtceu.api.data.worldgen.ores.OrePlacer;
import com.gregtechceu.gtceu.api.gui.factory.GTUIEditorFactory;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.api.registry.GTRegistry;
import com.gregtechceu.gtceu.common.commands.arguments.GTRegistryArgument;
import com.gregtechceu.gtceu.data.loader.BedrockFluidLoader;
import com.gregtechceu.gtceu.data.loader.BedrockOreLoader;
import com.gregtechceu.gtceu.data.loader.GTOreLoader;
import com.gregtechceu.gtceu.data.pack.GTDynamicDataPack;

import com.lowdragmc.lowdraglib.Platform;

import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.BulkSectionAccess;
import net.minecraft.world.level.levelgen.structure.templatesystem.AlwaysTrueTest;

import com.google.gson.JsonElement;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;

import java.nio.file.Path;

import static net.minecraft.commands.Commands.*;

/**
* @author KilaBash
* @date 2023/2/9
* @implNote GTCommands
*/
public class GTCommands {

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext buildContext) {
dispatcher.register(
literal("gtceu")
.requires(source -> source.hasPermission(3))
.then(literal("ui_editor")
.executes(context -> {
GTUIEditorFactory.INSTANCE.openUI(GTUIEditorFactory.INSTANCE,
context.getSource().getPlayerOrException());
return 1;
}))
.then(literal("check_recipes_valid")
.executes(context -> {
for (Recipe<?> recipe : context.getSource().getServer().getRecipeManager()
.getRecipes()) {
if (recipe instanceof GTRecipe gtRecipe && !gtRecipe.checkRecipeValid()) {
context.getSource().sendSuccess(
() -> Component
.literal("recipe %s is invalid".formatted(gtRecipe.id)),
false);
}
}
return 1;
}))
.then(literal("dump_data")
.then(literal("bedrock_fluid_veins")
.executes(context -> dumpDataRegistry(context,
GTRegistries.BEDROCK_FLUID_DEFINITIONS,
BedrockFluidDefinition.FULL_CODEC,
BedrockFluidLoader.FOLDER)))
.then(literal("bedrock_ore_veins")
.executes(context -> dumpDataRegistry(context,
GTRegistries.BEDROCK_ORE_DEFINITIONS,
BedrockOreDefinition.FULL_CODEC,
BedrockOreLoader.FOLDER)))
.then(literal("ore_veins")
.executes(context -> dumpDataRegistry(context,
GTRegistries.ORE_VEINS,
GTOreDefinition.FULL_CODEC,
GTOreLoader.FOLDER))))
.then(literal("place_vein")
.then(argument("vein",
GTRegistryArgument.registry(GTRegistries.ORE_VEINS, ResourceLocation.class))
.executes(context -> GTCommands.placeVein(context,
context.getSource().getEntityOrException().blockPosition()))
.then(argument("position", BlockPosArgument.blockPos())
.executes(context -> GTCommands.placeVein(context,
BlockPosArgument.getBlockPos(context, "position")))))));
}

private static <T> int dumpDataRegistry(CommandContext<CommandSourceStack> context,
GTRegistry<ResourceLocation, T> registry, Codec<T> codec, String folder) {
Path parent = Platform.getGamePath().resolve("gtceu/dumped/data");
var ops = RegistryOps.create(JsonOps.INSTANCE, context.getSource().registryAccess());
int dumpedCount = 0;
for (ResourceLocation id : registry.keys()) {
T entry = registry.get(id);
JsonElement json = codec.encodeStart(ops, entry).getOrThrow(false, GTCEu.LOGGER::error);
GTDynamicDataPack.writeJson(id, folder, parent, json);
dumpedCount++;
}
final int result = dumpedCount;
context.getSource().sendSuccess(
() -> Component.translatable("command.gtceu.dump_data.success", result,
registry.getRegistryName().toString(), parent.toString()),
true);
return result;
}

private static int placeVein(CommandContext<CommandSourceStack> context, BlockPos sourcePos) {
GTOreDefinition vein = context.getArgument("vein", GTOreDefinition.class);
ResourceLocation id = GTRegistries.ORE_VEINS.getKey(vein);

ChunkPos chunkPos = new ChunkPos(sourcePos);
ServerLevel level = context.getSource().getLevel();

GeneratedVeinMetadata metadata = new GeneratedVeinMetadata(id, chunkPos, sourcePos, vein);
RandomSource random = level.random;

OrePlacer placer = new OrePlacer();
OreGenerator generator = placer.getOreGenCache().getOreGenerator();

try (BulkSectionAccess access = new BulkSectionAccess(level)) {
var generated = generator.generateOres(new OreGenerator.VeinConfiguration(metadata, random), level,
chunkPos);
if (generated.isEmpty()) {
throw new CommandRuntimeException(Component.translatable("command.gtceu.place_vein.failure",
id.toString(), sourcePos.toString()));
}
for (ChunkPos pos : generated.get().getGeneratedChunks()) {
placer.placeVein(pos, random, access, generated.get(), AlwaysTrueTest.INSTANCE);
level.getChunk(pos.x, pos.z).setUnsaved(true);
}
context.getSource().sendSuccess(() -> Component.translatable("command.gtceu.place_vein.success",
id.toString(), sourcePos.toString()), true);
}

return 1;
}
}
Loading

0 comments on commit b0bae42

Please sign in to comment.