|
| 1 | +package dev.xpple.seedmapper.command.arguments; |
| 2 | + |
| 3 | +import com.github.cubiomes.Cubiomes; |
| 4 | +import com.github.cubiomes.TerrainNoiseParameters; |
| 5 | +import com.google.common.collect.ImmutableMap; |
| 6 | +import com.mojang.brigadier.StringReader; |
| 7 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 8 | +import com.mojang.brigadier.context.CommandContext; |
| 9 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 10 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 11 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 12 | +import dev.xpple.seedmapper.command.CommandExceptions; |
| 13 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 14 | +import net.minecraft.commands.SharedSuggestionProvider; |
| 15 | + |
| 16 | +import java.lang.foreign.MemorySegment; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | + |
| 22 | +public class DensityFunctionArgument implements ArgumentType<DensityFunctionArgument.DensityFunction> { |
| 23 | + |
| 24 | + private static final Collection<String> EXAMPLES = Arrays.asList("overworld", "the_nether", "the_end"); |
| 25 | + |
| 26 | + public static final Map<String, DensityFunction> DENSITY_FUNCTIONS = ImmutableMap.<String, DensityFunction>builder() |
| 27 | + .put("overworld.base_3d_noise", (params, x, y, z) -> Cubiomes.sampleBase3dNoise(TerrainNoiseParameters.bln(params), x, y, z)) |
| 28 | + .put("overworld.caves.spaghetti_roughness_function", Cubiomes::sampleSpaghettiRoughness) |
| 29 | + .put("overworld.caves.spaghetti_2d_thickness_modulator", Cubiomes::sampleSpaghetti2dThicknessModulator) |
| 30 | + .put("overworld.caves.spaghetti_2d", Cubiomes::sampleSpaghetti2d) |
| 31 | + .put("spaghetti_3d", Cubiomes::sampleSpaghetti3d) |
| 32 | + .put("cave_entrance", Cubiomes::sampleCaveEntrance) |
| 33 | + .put("overworld.caves.entrances", (params, x, y, z) -> Cubiomes.sampleEntrances(params, x, y, z, Cubiomes.sampleSpaghettiRoughness(params, x, y, z))) |
| 34 | + .put("cave_layer", Cubiomes::sampleCaveLayer) |
| 35 | + .put("overworld.sloped_cheese", Cubiomes::sampleSlopedCheese) |
| 36 | + .put("cave_cheese", (params, x, y, z) -> Cubiomes.sampleCaveCheese(params, x, y, z, Cubiomes.sampleSlopedCheese(params, x, y, z))) |
| 37 | + .put("overworld.caves.pillars", Cubiomes::samplePillars) |
| 38 | + .put("overworld.caves.noodle", Cubiomes::sampleNoodle) |
| 39 | + .put("underground", (params, x, y, z) -> { |
| 40 | + double spaghettiRoughness = Cubiomes.sampleSpaghettiRoughness(params, x, y, z); |
| 41 | + return Cubiomes.sampleUnderground(params, x, y, z, spaghettiRoughness, Cubiomes.sampleEntrances(params, x, y, z, spaghettiRoughness), Cubiomes.sampleSlopedCheese(params, x, y, z)); |
| 42 | + }) |
| 43 | + .put("final_density", (params, x, y, z) -> { |
| 44 | + double spaghettiRoughness = Cubiomes.sampleSpaghettiRoughness(params, x, y, z); |
| 45 | + return Cubiomes.sampleFinalDensity(params, x, y, z, spaghettiRoughness, Cubiomes.sampleEntrances(params, x, y, z, spaghettiRoughness), Cubiomes.sampleSlopedCheese(params, x, y, z)); |
| 46 | + }) |
| 47 | + .put("preliminary_surface_level", (params, x, _, z) -> Cubiomes.samplePreliminarySurfaceLevel(params, x, z)) |
| 48 | + .build(); |
| 49 | + |
| 50 | + public static DensityFunctionArgument densityFunction() { |
| 51 | + return new DensityFunctionArgument(); |
| 52 | + } |
| 53 | + |
| 54 | + public static DensityFunction getDensityFunction(CommandContext<FabricClientCommandSource> context, String name) { |
| 55 | + return context.getArgument(name, DensityFunction.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public DensityFunction parse(StringReader reader) throws CommandSyntaxException { |
| 60 | + int cursor = reader.getCursor(); |
| 61 | + String densityFunctionString = reader.readUnquotedString(); |
| 62 | + DensityFunction densityFunction = DENSITY_FUNCTIONS.get(densityFunctionString); |
| 63 | + if (densityFunction == null) { |
| 64 | + reader.setCursor(cursor); |
| 65 | + throw CommandExceptions.UNKNOWN_DENSITY_FUNCTION_EXCEPTION.create(densityFunctionString); |
| 66 | + } |
| 67 | + return densityFunction; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 72 | + return SharedSuggestionProvider.suggest(DENSITY_FUNCTIONS.keySet(), builder); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Collection<String> getExamples() { |
| 77 | + return EXAMPLES; |
| 78 | + } |
| 79 | + |
| 80 | + @FunctionalInterface |
| 81 | + public interface DensityFunction { |
| 82 | + double compute(MemorySegment terrainNoiseParameters, int x, int y, int z); |
| 83 | + } |
| 84 | +} |
0 commit comments