From 94ba89e611c35256794b2833603c3eaf52e6e6f9 Mon Sep 17 00:00:00 2001 From: ziro Date: Tue, 20 Jun 2023 12:20:54 +0700 Subject: [PATCH 01/13] feat: Obsidian --- run/server/server.properties | 2 +- .../github/null2264/cobblegen/CobbleGen.java | 13 +++++++ .../cobblegen/config/AdvancedGen.java | 1 + .../null2264/cobblegen/config/Config.java | 4 ++ .../null2264/cobblegen/config/ConfigData.java | 5 ++- .../cobblegen/config/ConfigHelper.java | 11 +++--- .../cobblegen/config/ConfigMetaData.java | 9 +++++ .../data/generator/BasaltGenerator.java | 2 +- .../data/generator/CobbleGenerator.java | 37 +++++++++++++++---- .../data/generator/StoneGenerator.java | 8 +++- .../data/model/BuiltInGenerator.java | 22 ++++++++--- .../cobblegen/data/model/Generator.java | 13 +++++-- .../cobblegen/integration/BuiltInPlugin.java | 7 ++-- 13 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 src/main/java/io/github/null2264/cobblegen/config/Config.java create mode 100644 src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java diff --git a/run/server/server.properties b/run/server/server.properties index 39ce1c6b..8f2586fe 100644 --- a/run/server/server.properties +++ b/run/server/server.properties @@ -1,5 +1,5 @@ #Minecraft server properties -#Mon Jun 19 11:47:28 ICT 2023 +#Mon Jun 19 12:26:15 ICT 2023 enable-jmx-monitoring=false rcon.port=25575 level-seed= diff --git a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java index 91cdd86d..aa54c202 100644 --- a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java +++ b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java @@ -1,9 +1,17 @@ package io.github.null2264.cobblegen; +import io.github.null2264.cobblegen.compat.Loader; +import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.data.FluidInteractionHelper; import io.github.null2264.cobblegen.data.model.CGRegistry; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.nio.file.Path; + +import static io.github.null2264.cobblegen.config.ConfigHelper.loadConfig; //#if FABRIC<=0 //$$ @net.minecraftforge.fml.common.Mod(CobbleGen.MOD_ID) @@ -21,6 +29,11 @@ public class CobbleGen implements net.fabricmc.api.ModInitializer @Deprecated @ApiStatus.Internal public static final FluidInteractionHelper FLUID_INTERACTION = new FluidInteractionHelper(); + private static final Path configPath = Loader.getConfigDir(); + private static final File configFile = new File(configPath + File.separator + MOD_ID + "-meta.json5"); + @ApiStatus.Internal + @Nullable + public static ConfigMetaData META_CONFIG = loadConfig(false, configFile, null, new ConfigMetaData(), ConfigMetaData.class); //#if FABRIC>=1 @Override diff --git a/src/main/java/io/github/null2264/cobblegen/config/AdvancedGen.java b/src/main/java/io/github/null2264/cobblegen/config/AdvancedGen.java index bfc42260..86b9e507 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/AdvancedGen.java +++ b/src/main/java/io/github/null2264/cobblegen/config/AdvancedGen.java @@ -8,4 +8,5 @@ public class AdvancedGen public boolean silent = false; public Map> results = Map.of(); public Map> resultsFromTop = Map.of(); + public Map> obsidian = Map.of(); } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/config/Config.java b/src/main/java/io/github/null2264/cobblegen/config/Config.java new file mode 100644 index 00000000..928b6732 --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/config/Config.java @@ -0,0 +1,4 @@ +package io.github.null2264.cobblegen.config; + +public class Config { +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java index c8cffe50..38c5f46c 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; -public class ConfigData +public class ConfigData extends Config { @Nullable @Comment(value = """ @@ -60,6 +60,9 @@ public class ConfigData @Nullable public Map> advanced; + @Comment(value="Enable Experimental Features") + public Boolean enableExperimentalFeatures = false; + public static ConfigData defaultConfig() { ConfigData config = new ConfigData(); config.cobbleGen = List.of(new WeightedBlock( diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java index c413ef34..51da20ba 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java @@ -39,12 +39,12 @@ private static JsonElement filter(JsonElement json) { } @ApiStatus.Internal - public static ConfigData loadConfig(boolean reload, File configFile, ConfigData workingConfig) { + public static T loadConfig(boolean reload, File configFile, T workingConfig, T defaultConfig, Class clazz) { String string = reload ? "reload" : "load"; try { CGLog.info("Trying to " + string + " config file..."); JsonObject json = jankson.load(configFile); - return gson.fromJson(json.toJson(JsonGrammar.COMPACT), ConfigData.class); + return gson.fromJson(json.toJson(JsonGrammar.COMPACT), clazz); } catch (Exception e) { CGLog.error("There was an error while " + string + "ing the config file!\n" + e); @@ -53,16 +53,15 @@ public static ConfigData loadConfig(boolean reload, File configFile, ConfigData return workingConfig; } - val newConfig = ConfigData.defaultConfig(); if (!configFile.exists()) { - saveConfig(newConfig, configFile); + saveConfig(defaultConfig, configFile); } CGLog.warn("Falling back to default config..."); - return newConfig; + return defaultConfig; } } - private static void saveConfig(ConfigData config, File configFile) { + private static void saveConfig(T config, File configFile) { try { CGLog.info("Trying to create config file..."); FileWriter fw = new FileWriter(configFile); diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java new file mode 100644 index 00000000..c910a544 --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java @@ -0,0 +1,9 @@ +package io.github.null2264.cobblegen.config; + +import blue.endless.jankson.Comment; + +public class ConfigMetaData extends Config +{ + @Comment(value="Enable Experimental Features") + public Boolean enableExperimentalFeatures = false; +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java index 7afc3e3c..c9c94c58 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java @@ -66,7 +66,7 @@ public boolean isSilent() { public Optional tryGenerate(LevelAccessor level, BlockPos pos, BlockState state, Direction direction) { BlockPos blockPos = pos.relative(direction.getOpposite()); if (level.getBlockState(blockPos).getBlock() == getBlock()) - return getBlockCandidate(level, pos); + return getBlockCandidate(level, pos, getOutput()); return Optional.empty(); } diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java index 4d0a8833..ee4c1374 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java @@ -1,5 +1,6 @@ package io.github.null2264.cobblegen.data.generator; +import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.config.WeightedBlock; import io.github.null2264.cobblegen.data.model.BlockGenerator; import io.github.null2264.cobblegen.data.model.Generator; @@ -20,19 +21,26 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; public class CobbleGenerator extends BlockGenerator { private final Map> possibleBlocks; + private final Map> obsidianReplacements; private Fluid fluid; private final boolean silent; public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent) { - this(Map.of("*", possibleBlocks), fluid, silent); + this(Map.of("*", possibleBlocks), fluid, silent, Map.of()); } - public CobbleGenerator(Map> possibleBlocks, Fluid fluid, boolean silent) { + public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { + this(Map.of("*", possibleBlocks), fluid, silent, obsidianReplacements); + } + + public CobbleGenerator(Map> possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { this.possibleBlocks = possibleBlocks; + this.obsidianReplacements = obsidianReplacements; this.fluid = fluid; this.silent = silent; } @@ -42,6 +50,11 @@ public CobbleGenerator(Map> possibleBlocks, Fluid fl return possibleBlocks; } + @Override + public Map> getObsidianOutput() { + return obsidianReplacements; + } + @Override public @NotNull GeneratorType getType() { return GeneratorType.COBBLE; @@ -76,10 +89,15 @@ public Optional tryGenerate(LevelAccessor level, BlockPos pos, Block @Override public Optional tryGenerate(LevelAccessor level, BlockPos pos, FluidState source, FluidState neighbour) { if (Generator.getStillFluid(neighbour) == getFluid()) { - if (source.getType() == Fluids.LAVA && source.isSource()) + AtomicReference isExperimentalEnabled = new AtomicReference<>(false); + Util.optional(CobbleGen.META_CONFIG).ifPresent((e) -> isExperimentalEnabled.set(e.enableExperimentalFeatures)); + if (source.getType() == Fluids.LAVA && source.isSource()) { + if (isExperimentalEnabled.get()) + return getBlockCandidate(level, pos, getObsidianOutput(), Blocks.OBSIDIAN); return Optional.of(Blocks.OBSIDIAN.defaultBlockState()); + } - return getBlockCandidate(level, pos); + return getBlockCandidate(level, pos, getOutput()); } return Optional.empty(); } @@ -91,9 +109,12 @@ public void toPacket(FriendlyByteBuf buf) { buf.writeResourceLocation(Util.getFluidId(fluid)); buf.writeBoolean(silent); - val outMap = getOutput(); buf.writeMap( - outMap, + getOutput(), + FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) + ); + buf.writeMap( + getObsidianOutput(), FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) ); } @@ -105,7 +126,9 @@ public static Generator fromPacket(FriendlyByteBuf buf) { Map> outMap = buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket)); + Map> obiMap = + buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket)); - return new CobbleGenerator(outMap, fluid, silent); + return new CobbleGenerator(outMap, fluid, silent, obiMap); } } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java index 64219897..09b40259 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java @@ -10,6 +10,7 @@ import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; @@ -39,6 +40,11 @@ public StoneGenerator(Map> possibleBlocks, Fluid flu return possibleBlocks; } + @Override + public Map> getObsidianOutput() { + return Map.of("*", List.of(WeightedBlock.fromBlock(Blocks.STONE, 100D))); + } + @Override public @NotNull GeneratorType getType() { return GeneratorType.STONE; @@ -73,7 +79,7 @@ public Optional tryGenerate(LevelAccessor level, BlockPos pos, Block public Optional tryGenerate(LevelAccessor level, BlockPos pos, FluidState source, FluidState neighbour) { Fluid fluid = Generator.getStillFluid(neighbour); if (getFluid() == fluid) { - return getBlockCandidate(level, pos); + return getBlockCandidate(level, pos, getOutput()); } return Optional.empty(); diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java index fcff0ca1..adf0b9e9 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; @@ -19,10 +20,10 @@ public interface BuiltInGenerator extends Generator { // https://stackoverflow.com/a/6737362 - private String randomizeBlockId(Block key, String dim, Integer yLevel) { - val blockIds = getOutput().getOrDefault( + private String randomizeBlockId(Block key, String dim, Integer yLevel, Map> candidates) { + val blockIds = candidates.getOrDefault( Util.getBlockId(key).toString(), - getOutput().getOrDefault("*", List.of()) + candidates.getOrDefault("*", List.of()) ); if (blockIds.isEmpty()) return null; @@ -63,14 +64,23 @@ private String randomizeBlockId(Block key, String dim, Integer yLevel) { return filteredBlockIds.get(idx).id; } - default Optional getBlockCandidate(LevelAccessor level, BlockPos pos) { + default Optional getBlockCandidate(LevelAccessor level, BlockPos pos, Map> candidates) { + return getBlockCandidate(level, pos, candidates, null); + } + + default Optional getBlockCandidate(LevelAccessor level, BlockPos pos, Map> candidates, Block defaultBlock) { val replacementId = randomizeBlockId( level.getBlockState(pos.below()).getBlock(), Util.getDimension(level), - pos.getY() + pos.getY(), + candidates ); - if (replacementId == null) return Optional.empty(); + if (replacementId == null) { + if (defaultBlock != null) + return Optional.of(defaultBlock.defaultBlockState()); + return Optional.empty(); + } return Optional.of(Util.getBlock(new ResourceLocation(replacementId)).defaultBlockState()); } diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java b/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java index d7d7df22..02b98ee5 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java @@ -42,8 +42,6 @@ else if (fluid instanceof FlowingFluid) return fluid; } - ; - Optional tryGenerate(LevelAccessor level, BlockPos pos, BlockState state); /** @@ -56,11 +54,18 @@ default Optional tryGenerate(LevelAccessor level, BlockPos pos, Flui @NotNull Map> getOutput(); + /** + * @return The output block when a source fluid met another fluid (e.g. Water -> Stone / Lava -> Obsidian) + */ + default Map> getObsidianOutput() { + return Map.of(); + } + @NotNull GeneratorType getType(); /** - * The neighbour {@link Fluid}, for Stone/Cobble-like generators. + * @return The neighbour {@link Fluid}, for Stone/Cobble-like generators. */ @Nullable Fluid getFluid(); @@ -69,7 +74,7 @@ default Optional tryGenerate(LevelAccessor level, BlockPos pos, Flui default void setFluid(Fluid fluid) {}; /** - * The neighbour {@link Block}, for Basalt-like generators. + * @return The neighbour {@link Block}, for Basalt-like generators. */ @Nullable Block getBlock(); diff --git a/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java index a54189d8..38fe44f6 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java @@ -54,7 +54,7 @@ private Block getBlockFromString(String string) { @Override public void registerInteraction(CGRegistry registry) { CGLog.info((!isReload ? "L" : "Rel") + "oading config..."); - if (config == null || isReload) config = loadConfig(isReload, configFile, config); + if (config == null || isReload) config = loadConfig(isReload, configFile, config, ConfigData.defaultConfig(), ConfigData.class); if (config == null) throw new RuntimeException("How?"); AtomicInteger count = new AtomicInteger(); @@ -78,6 +78,7 @@ public void registerInteraction(CGRegistry registry) { Fluid actualFluid = getFluidFromString(fluid); value.forEach((neighbour, gen) -> { val results = gen.results; + val obi = gen.obsidian; boolean isNeighbourBlock = neighbour.startsWith("b:"); if (isNeighbourBlock) neighbour = neighbour.substring(2); @@ -99,7 +100,7 @@ public void registerInteraction(CGRegistry registry) { if (isNeighbourBlock) generator = new BasaltGenerator(results, getBlockFromString(neighbour), gen.silent); else - generator = new CobbleGenerator(results, getFluidFromString(neighbour), gen.silent); + generator = new CobbleGenerator(results, getFluidFromString(neighbour), gen.silent, obi); registry.addGenerator(actualFluid, generator); count.getAndIncrement(); @@ -108,7 +109,7 @@ public void registerInteraction(CGRegistry registry) { }); registry.addGenerator(Fluids.LAVA, new StoneGenerator(stoneGen, Fluids.WATER, false)); - registry.addGenerator(Fluids.LAVA, new CobbleGenerator(cobbleGen, Fluids.WATER, false)); + registry.addGenerator(Fluids.LAVA, new CobbleGenerator(cobbleGen, Fluids.WATER, false, Map.of())); registry.addGenerator(Fluids.LAVA, new BasaltGenerator(basaltGen, Blocks.BLUE_ICE, false)); count.addAndGet(3); From ee55e60a1f2e6a37c0218e310da8a33049c30554 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani <46041660+null2264@users.noreply.github.com> Date: Wed, 21 Jun 2023 07:30:26 +0700 Subject: [PATCH 02/13] fix: This flag should be in ConfigMetaData only --- .../java/io/github/null2264/cobblegen/config/ConfigData.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java index 38c5f46c..956b47c9 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java @@ -60,9 +60,6 @@ public class ConfigData extends Config @Nullable public Map> advanced; - @Comment(value="Enable Experimental Features") - public Boolean enableExperimentalFeatures = false; - public static ConfigData defaultConfig() { ConfigData config = new ConfigData(); config.cobbleGen = List.of(new WeightedBlock( @@ -110,4 +107,4 @@ public static ConfigData defaultConfig() { ); return config; } -} \ No newline at end of file +} From d5ec6048e7023980ff5fdee1f2f08c8286cd616c Mon Sep 17 00:00:00 2001 From: ziro Date: Wed, 21 Jun 2023 09:00:06 +0700 Subject: [PATCH 03/13] chore: Use LoaderCompat --- src/main/java/io/github/null2264/cobblegen/CobbleGen.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java index aa54c202..de873ada 100644 --- a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java +++ b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java @@ -1,6 +1,6 @@ package io.github.null2264.cobblegen; -import io.github.null2264.cobblegen.compat.Loader; +import io.github.null2264.cobblegen.compat.LoaderCompat; import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.data.FluidInteractionHelper; import io.github.null2264.cobblegen.data.model.CGRegistry; @@ -29,7 +29,7 @@ public class CobbleGen implements net.fabricmc.api.ModInitializer @Deprecated @ApiStatus.Internal public static final FluidInteractionHelper FLUID_INTERACTION = new FluidInteractionHelper(); - private static final Path configPath = Loader.getConfigDir(); + private static final Path configPath = LoaderCompat.getConfigDir(); private static final File configFile = new File(configPath + File.separator + MOD_ID + "-meta.json5"); @ApiStatus.Internal @Nullable @@ -44,4 +44,4 @@ public enum Channel { PING, SYNC, } -} \ No newline at end of file +} From 46bd113f1c376a1dc4a1ed2f79d0281eec6a7fb5 Mon Sep 17 00:00:00 2001 From: ziro Date: Fri, 23 Jun 2023 16:56:22 +0700 Subject: [PATCH 04/13] feat: EMI merge toggle --- .../null2264/cobblegen/config/Config.java | 3 +- .../null2264/cobblegen/config/ConfigData.java | 4 +- .../cobblegen/config/ConfigHelper.java | 3 +- .../cobblegen/config/ConfigMetaData.java | 5 +- .../integration/viewer/emi/CGEMIPlugin.java | 187 +++++++++--------- 5 files changed, 104 insertions(+), 98 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/config/Config.java b/src/main/java/io/github/null2264/cobblegen/config/Config.java index 928b6732..56deb619 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/Config.java +++ b/src/main/java/io/github/null2264/cobblegen/config/Config.java @@ -1,4 +1,3 @@ package io.github.null2264.cobblegen.config; -public class Config { -} \ No newline at end of file +public interface Config {} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java index 956b47c9..1b19cf92 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; -public class ConfigData extends Config +public class ConfigData implements Config { @Nullable @Comment(value = """ @@ -107,4 +107,4 @@ public static ConfigData defaultConfig() { ); return config; } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java index 51da20ba..f944fe46 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigHelper.java @@ -3,7 +3,6 @@ import blue.endless.jankson.*; import com.google.gson.Gson; import io.github.null2264.cobblegen.util.CGLog; -import lombok.val; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; @@ -61,7 +60,7 @@ public static T loadConfig(boolean reload, File configFile, T } } - private static void saveConfig(T config, File configFile) { + private static void saveConfig(Config config, File configFile) { try { CGLog.info("Trying to create config file..."); FileWriter fw = new FileWriter(configFile); diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java index c910a544..216eadc3 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java @@ -2,8 +2,11 @@ import blue.endless.jankson.Comment; -public class ConfigMetaData extends Config +public class ConfigMetaData implements Config { @Comment(value="Enable Experimental Features") public Boolean enableExperimentalFeatures = false; + + @Comment(value="Merge CobbleGen recipe categories into EMI's World Interaction category") + public Boolean mergeEMIRecipeCategory = true; } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java index ae27832f..dfbddcde 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java @@ -6,7 +6,9 @@ import dev.emi.emi.api.recipe.EmiRecipeCategory; import dev.emi.emi.api.recipe.EmiWorldInteractionRecipe; import dev.emi.emi.api.stack.EmiStack; +import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.compat.TextCompat; +import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.config.WeightedBlock; import io.github.null2264.cobblegen.util.Util; import lombok.val; @@ -58,108 +60,111 @@ public class CGEMIPlugin implements EmiPlugin @Override public void register(EmiRegistry registry) { - /* For future feature (meta config) - FLUID_INTERACTION_CATEGORIES.forEach((ignored, category) -> registry.addCategory(category)); - FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( - (modifierId, blocks) -> { - Block modifier = null; - if (!Objects.equals(modifierId, "*")) - modifier = Util.getBlock(new ResourceLocation(modifierId)); - for (WeightedBlock block : blocks) - registry.addRecipe( - new FluidInteractionRecipe( - fluid, - Util.notNullOr(generator.getFluid(), Fluids.EMPTY), - Util.notNullOr(generator.getBlock(), Blocks.AIR), - block, - generator.getType(), - Util.notNullOr(modifier, Blocks.AIR) - ) - ); - }))); - */ - Minecraft minecraft = Minecraft.getInstance(); - FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( - (modifierRawId, blocks) -> { - ResourceLocation modifierId = new ResourceLocation("none"); - Optional modifier = Optional.empty(); - if (!Objects.equals(modifierRawId, "*")) { - modifierId = new ResourceLocation(modifierRawId); - modifier = Optional.of(Util.getBlock(modifierId)); - } - - for (WeightedBlock block : blocks) { - ResourceLocation resultId = new ResourceLocation(block.id); - ResourceLocation source = Util.getFluidId(fluid); - EmiStack neighbour; - ResourceLocation neighbourId; - if (generator.getFluid() == null) { - neighbourId = Util.getBlockId(generator.getBlock()); - neighbour = EmiStack.of(Objects.requireNonNull(generator.getBlock())); - } else { - neighbourId = Util.getFluidId(generator.getFluid()); - neighbour = EmiStack.of(Objects.requireNonNull(generator.getFluid())); + if (Util.optional(CobbleGen.META_CONFIG).orElse(new ConfigMetaData()).mergeEMIRecipeCategory) { + FLUID_INTERACTION_CATEGORIES.forEach((ignored, category) -> registry.addCategory(category)); + FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( + (modifierId, blocks) -> { + Block modifier = null; + if (!Objects.equals(modifierId, "*")) + modifier = Util.getBlock(new ResourceLocation(modifierId)); + for (WeightedBlock block : blocks) + registry.addRecipe( + new FluidInteractionRecipe( + fluid, + Util.notNullOr(generator.getFluid(), Fluids.EMPTY), + Util.notNullOr(generator.getBlock(), Blocks.AIR), + block, + generator.getType(), + Util.notNullOr(modifier, Blocks.AIR) + ) + ); + }))); + } else { + Minecraft minecraft = Minecraft.getInstance(); + FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( + (modifierRawId, blocks) -> { + ResourceLocation modifierId = new ResourceLocation("none"); + Optional modifier = Optional.empty(); + if (!Objects.equals(modifierRawId, "*")) { + modifierId = new ResourceLocation(modifierRawId); + modifier = Optional.of(Util.getBlock(modifierId)); } - val id = Util.identifierOf(CGEMIPlugin.ID_PREFIX + generator.getType().name() - .toLowerCase() + "-" + source.toDebugFileName() + "-" + resultId.toDebugFileName() + "-" + neighbourId.toDebugFileName() + "-" + modifierId.toDebugFileName()); - EmiWorldInteractionRecipe.Builder recipe = EmiWorldInteractionRecipe.builder() - .id(id) - .leftInput(EmiStack.of(fluid)); + for (WeightedBlock block : blocks) { + ResourceLocation resultId = new ResourceLocation(block.id); + ResourceLocation source = Util.getFluidId(fluid); + EmiStack neighbour; + ResourceLocation neighbourId; + if (generator.getFluid() == null) { + neighbourId = Util.getBlockId(generator.getBlock()); + neighbour = EmiStack.of(Objects.requireNonNull(generator.getBlock())); + } else { + neighbourId = Util.getFluidId(generator.getFluid()); + neighbour = EmiStack.of(Objects.requireNonNull(generator.getFluid())); + } + val id = Util.identifierOf(CGEMIPlugin.ID_PREFIX + generator.getType().name() + .toLowerCase() + "-" + source.toDebugFileName() + "-" + resultId.toDebugFileName() + "-" + neighbourId.toDebugFileName() + "-" + modifierId.toDebugFileName()); - if (modifier.isPresent()) - recipe.rightInput(EmiStack.of(modifier.get()), false, - s -> s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.soul_soil").withStyle(ChatFormatting.GREEN))) - .rightInput(neighbour, false, - s -> generator.getBlock() != null ? s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.blue_ice").withStyle(ChatFormatting.GREEN)) : s); - else { - recipe.rightInput(neighbour, false); - } + EmiWorldInteractionRecipe.Builder recipe = EmiWorldInteractionRecipe.builder() + .id(id) + .leftInput(EmiStack.of(fluid)); - recipe.output(EmiStack.of(Util.getBlock(resultId)), - s -> { - var minY = block.minY; - if (minY == null) minY = minecraft.level != null ? minecraft.level.getMinBuildHeight() : 0; - var maxY = block.maxY; - if (maxY == null) maxY = minecraft.level != null ? minecraft.level.getMaxBuildHeight() : 256; + if (modifier.isPresent()) + recipe.rightInput(EmiStack.of(modifier.get()), false, + s -> s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.soul_soil").withStyle(ChatFormatting.GREEN))) + .rightInput(neighbour, false, + s -> generator.getBlock() != null ? s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.blue_ice").withStyle(ChatFormatting.GREEN)) : s); + else { + recipe.rightInput(neighbour, false); + } - s.appendTooltip(TextCompat.translatable("cobblegen.info.weight") - .append(Component.nullToEmpty(block.weight.toString()))); - s.appendTooltip(TextCompat.translatable("cobblegen.info.minY") - .append(Component.nullToEmpty(minY.toString()))); - s.appendTooltip(TextCompat.translatable("cobblegen.info.maxY") - .append(Component.nullToEmpty(maxY.toString()))); + recipe.output(EmiStack.of(Util.getBlock(resultId)), + s -> { + var minY = block.minY; + if (minY == null) + minY = minecraft.level != null ? minecraft.level.getMinBuildHeight() : 0; + var maxY = block.maxY; + if (maxY == null) + maxY = minecraft.level != null ? minecraft.level.getMaxBuildHeight() : 256; - s.appendTooltip(TextCompat.translatable("cobblegen.info.blacklistedDim").withStyle(ChatFormatting.GREEN)); - List recipeBlacklist = block.excludedDimensions; - try { - for (String dim : recipeBlacklist) { - ResourceLocation dimId = new ResourceLocation(dim); - s.appendTooltip(TextCompat.literal("- " + dimId)); + s.appendTooltip(TextCompat.translatable("cobblegen.info.weight") + .append(Component.nullToEmpty(block.weight.toString()))); + s.appendTooltip(TextCompat.translatable("cobblegen.info.minY") + .append(Component.nullToEmpty(minY.toString()))); + s.appendTooltip(TextCompat.translatable("cobblegen.info.maxY") + .append(Component.nullToEmpty(maxY.toString()))); + + s.appendTooltip(TextCompat.translatable("cobblegen.info.blacklistedDim").withStyle(ChatFormatting.GREEN)); + List recipeBlacklist = block.excludedDimensions; + try { + for (String dim : recipeBlacklist) { + ResourceLocation dimId = new ResourceLocation(dim); + s.appendTooltip(TextCompat.literal("- " + dimId)); + } + } catch (NullPointerException ignored) { + s.appendTooltip(TextCompat.literal("- ") + .append(TextCompat.translatable("cobblegen.dim.none"))); } - } catch (NullPointerException ignored) { - s.appendTooltip(TextCompat.literal("- ") - .append(TextCompat.translatable("cobblegen.dim.none"))); - } - s.appendTooltip(TextCompat.translatable("cobblegen.info.whitelistedDim").withStyle(ChatFormatting.GREEN)); - List recipeWhitelist = block.dimensions; - try { - for (String dim : recipeWhitelist) { - ResourceLocation dimId = new ResourceLocation(dim); - s.appendTooltip(TextCompat.literal("- " + dimId)); + s.appendTooltip(TextCompat.translatable("cobblegen.info.whitelistedDim").withStyle(ChatFormatting.GREEN)); + List recipeWhitelist = block.dimensions; + try { + for (String dim : recipeWhitelist) { + ResourceLocation dimId = new ResourceLocation(dim); + s.appendTooltip(TextCompat.literal("- " + dimId)); + } + } catch (NullPointerException ignored) { + s.appendTooltip(TextCompat.literal("- ") + .append(TextCompat.translatable("cobblegen.dim.any"))); } - } catch (NullPointerException ignored) { - s.appendTooltip(TextCompat.literal("- ") - .append(TextCompat.translatable("cobblegen.dim.any"))); + return s; } - return s; - } - ); - registry.addRecipe(recipe.build()); + ); + registry.addRecipe(recipe.build()); + } } - } - ))); + ))); + } } } //#endif \ No newline at end of file From 8c7e15ef3e74e06b324411844be751499dc23532 Mon Sep 17 00:00:00 2001 From: ziro Date: Fri, 23 Jun 2023 16:59:30 +0700 Subject: [PATCH 05/13] feat: Optional getter --- .../null2264/cobblegen/config/WeightedBlock.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java b/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java index 3b378c8e..c2c5f2ed 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java +++ b/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java @@ -58,6 +58,10 @@ public static WeightedBlock fromBlock(Block block, Double weight) { return fromBlock(block, weight, null, null, null, null); } + public Optional> getDimensions() { + return Util.optional(dimensions); + } + public static WeightedBlock fromBlock( Block block, Double weight, @@ -74,6 +78,18 @@ public Block getBlock() { return Util.getBlock(ResourceLocation.tryParse(id)); } + public Optional> getExcludedDimensions() { + return Util.optional(excludedDimensions); + } + + public Optional getMaxY() { + return Util.optional(maxY); + } + + public Optional getMinY() { + return Util.optional(minY); + } + @Override public void toPacket(FriendlyByteBuf buf) { buf.writeUtf(id); From 49ba599e367ff1cb8465ad9b91f4e0370b102777 Mon Sep 17 00:00:00 2001 From: ziro Date: Fri, 23 Jun 2023 17:10:36 +0700 Subject: [PATCH 06/13] refactor: Simplify code --- .../null2264/cobblegen/data/generator/CobbleGenerator.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java index ee4c1374..d4f4352f 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java @@ -1,6 +1,7 @@ package io.github.null2264.cobblegen.data.generator; import io.github.null2264.cobblegen.CobbleGen; +import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.config.WeightedBlock; import io.github.null2264.cobblegen.data.model.BlockGenerator; import io.github.null2264.cobblegen.data.model.Generator; @@ -31,7 +32,7 @@ public class CobbleGenerator extends BlockGenerator private final boolean silent; public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent) { - this(Map.of("*", possibleBlocks), fluid, silent, Map.of()); + this(possibleBlocks, fluid, silent, Map.of()); } public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { @@ -89,10 +90,8 @@ public Optional tryGenerate(LevelAccessor level, BlockPos pos, Block @Override public Optional tryGenerate(LevelAccessor level, BlockPos pos, FluidState source, FluidState neighbour) { if (Generator.getStillFluid(neighbour) == getFluid()) { - AtomicReference isExperimentalEnabled = new AtomicReference<>(false); - Util.optional(CobbleGen.META_CONFIG).ifPresent((e) -> isExperimentalEnabled.set(e.enableExperimentalFeatures)); if (source.getType() == Fluids.LAVA && source.isSource()) { - if (isExperimentalEnabled.get()) + if (Util.optional(CobbleGen.META_CONFIG).orElse(new ConfigMetaData()).enableExperimentalFeatures) return getBlockCandidate(level, pos, getObsidianOutput(), Blocks.OBSIDIAN); return Optional.of(Blocks.OBSIDIAN.defaultBlockState()); } From 7c3db062d2917a3848d169b33950db3746ba8424 Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 08:31:14 +0700 Subject: [PATCH 07/13] feat: Add more option to meta config --- .../cobblegen/config/ConfigMetaData.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java index 216eadc3..a3336a32 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigMetaData.java @@ -1,12 +1,30 @@ package io.github.null2264.cobblegen.config; import blue.endless.jankson.Comment; +import org.jetbrains.annotations.NotNull; public class ConfigMetaData implements Config { + @Comment(value="Enable Recipe Viewer support (EMI/REI/JEI)") + @NotNull + public Boolean enableRecipeViewer = true; + @Comment(value="Enable Experimental Features") + @NotNull public Boolean enableExperimentalFeatures = false; @Comment(value="Merge CobbleGen recipe categories into EMI's World Interaction category") + @NotNull public Boolean mergeEMIRecipeCategory = true; + + @Comment(value="EMI related config, used when mergeEMIRecipeCategory is set to 'true'") + public EMIData emi = new EMIData(); + + public static class EMIData { + @Comment(value="Add CobbleGen tooltip when CobbleGen is merged with EMI's World Interaction category") + public Boolean addTooltip = true; + + @Comment(value="Remove overlapping recipe between CobbleGen and EMI when CobbleGen is merged with EMI's World Interaction category") + public Boolean removeOverlaps = true; + } } \ No newline at end of file From 96af5468122a8a8c3ee7cc96fce19b0bf1437593 Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 08:32:55 +0700 Subject: [PATCH 08/13] feat(EMI): Toggles - Ability to toggle EMI support - Ability to toggle CobbleGen tooltip (#41) --- src/main/java/io/github/null2264/cobblegen/CobbleGen.java | 3 +-- .../cobblegen/integration/viewer/emi/CGEMIPlugin.java | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java index 5dca1d87..8073d52c 100644 --- a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java +++ b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java @@ -32,7 +32,6 @@ public class CobbleGen implements net.fabricmc.api.ModInitializer private static final Path configPath = LoaderCompat.getConfigDir(); private static final File configFile = new File(configPath + File.separator + MOD_ID + "-meta.json5"); @ApiStatus.Internal - @Nullable public static ConfigMetaData META_CONFIG = loadConfig(false, configFile, null, new ConfigMetaData(), ConfigMetaData.class); //#if FABRIC>=1 @@ -44,4 +43,4 @@ public enum Channel { PING, SYNC, } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java index dfbddcde..908cde3d 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java @@ -60,7 +60,10 @@ public class CGEMIPlugin implements EmiPlugin @Override public void register(EmiRegistry registry) { - if (Util.optional(CobbleGen.META_CONFIG).orElse(new ConfigMetaData()).mergeEMIRecipeCategory) { + if (!CobbleGen.META_CONFIG.enableRecipeViewer) + return; + + if (CobbleGen.META_CONFIG.mergeEMIRecipeCategory) { FLUID_INTERACTION_CATEGORIES.forEach((ignored, category) -> registry.addCategory(category)); FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( (modifierId, blocks) -> { @@ -120,6 +123,8 @@ public void register(EmiRegistry registry) { recipe.output(EmiStack.of(Util.getBlock(resultId)), s -> { + if (!CobbleGen.META_CONFIG.emi.addTooltip) return s; + var minY = block.minY; if (minY == null) minY = minecraft.level != null ? minecraft.level.getMinBuildHeight() : 0; From a7f8dd543fbcc065ae7e29cea99ddd7592be08e9 Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 10:36:12 +0700 Subject: [PATCH 09/13] feat(EMI): Toggle to remove overlapping recipe(s) (#40) --- gradle.properties | 2 +- .../null2264/cobblegen/CobbleGenPlugin.java | 6 +++ .../cobblegen/data/model/CGRegistry.java | 5 +++ .../integration/viewer/emi/CGEMIPlugin.java | 40 ++++++++++++++----- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index d34b9740..4bd07788 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ loader_version_1_18_2=0.14.14 fabric_version_1_18_2=0.76.0+1.18.2 # Runtime Properties -recipe_viewer=rei +recipe_viewer=emi # Dependencies jankson_version=1.2.2 diff --git a/src/main/java/io/github/null2264/cobblegen/CobbleGenPlugin.java b/src/main/java/io/github/null2264/cobblegen/CobbleGenPlugin.java index 9ce1f7b6..553dbf3a 100644 --- a/src/main/java/io/github/null2264/cobblegen/CobbleGenPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/CobbleGenPlugin.java @@ -4,8 +4,14 @@ public interface CobbleGenPlugin { + /** + * Register new Fluid Interaction + */ void registerInteraction(CGRegistry registry); + /** + * Function that will be run when config is being reloaded + */ default void onReload() { } } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/CGRegistry.java b/src/main/java/io/github/null2264/cobblegen/data/model/CGRegistry.java index 4acf7aca..f694a2b9 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/CGRegistry.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/CGRegistry.java @@ -3,5 +3,10 @@ import net.minecraft.world.level.material.Fluid; public interface CGRegistry { + /** + * Add new custom generator + * @param fluid A fluid that will touch neighbouring fluid(s) + * @param generator The generator + */ void addGenerator(Fluid fluid, Generator generator); } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java index 908cde3d..1774195c 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java @@ -7,24 +7,24 @@ import dev.emi.emi.api.recipe.EmiWorldInteractionRecipe; import dev.emi.emi.api.stack.EmiStack; import io.github.null2264.cobblegen.CobbleGen; +import io.github.null2264.cobblegen.compat.LoaderCompat; import io.github.null2264.cobblegen.compat.TextCompat; import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.config.WeightedBlock; +import io.github.null2264.cobblegen.util.CGLog; import io.github.null2264.cobblegen.util.Util; import lombok.val; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Items; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.material.Fluids; import org.jetbrains.annotations.Nullable; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import static io.github.null2264.cobblegen.CobbleGen.FLUID_INTERACTION; @@ -58,12 +58,17 @@ public class CGEMIPlugin implements EmiPlugin ) ); + // To merge EMI + private static ResourceLocation synthetic(String type, String name) { + return new ResourceLocation("emi", "/" + type + "/" + name); + } + @Override public void register(EmiRegistry registry) { if (!CobbleGen.META_CONFIG.enableRecipeViewer) return; - if (CobbleGen.META_CONFIG.mergeEMIRecipeCategory) { + if (!CobbleGen.META_CONFIG.mergeEMIRecipeCategory) { FLUID_INTERACTION_CATEGORIES.forEach((ignored, category) -> registry.addCategory(category)); FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( (modifierId, blocks) -> { @@ -86,6 +91,8 @@ public void register(EmiRegistry registry) { Minecraft minecraft = Minecraft.getInstance(); FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( (modifierRawId, blocks) -> { + EmiStack trigger = EmiStack.of(fluid, LoaderCompat.isForge() ? 1_000 : 81_000); + ResourceLocation modifierId = new ResourceLocation("none"); Optional modifier = Optional.empty(); if (!Objects.equals(modifierRawId, "*")) { @@ -95,6 +102,8 @@ public void register(EmiRegistry registry) { for (WeightedBlock block : blocks) { ResourceLocation resultId = new ResourceLocation(block.id); + EmiStack output = EmiStack.of(Util.getBlock(resultId)); + ResourceLocation source = Util.getFluidId(fluid); EmiStack neighbour; ResourceLocation neighbourId; @@ -103,28 +112,37 @@ public void register(EmiRegistry registry) { neighbour = EmiStack.of(Objects.requireNonNull(generator.getBlock())); } else { neighbourId = Util.getFluidId(generator.getFluid()); - neighbour = EmiStack.of(Objects.requireNonNull(generator.getFluid())); + neighbour = EmiStack.of(Objects.requireNonNull(generator.getFluid()), LoaderCompat.isForge() ? 1_000 : 81_000); } + + if (CobbleGen.META_CONFIG.emi.removeOverlaps) { + registry.removeRecipes(r -> + new HashSet<>(r.getInputs()).containsAll(List.of(neighbour, trigger)) && r.getOutputs().contains(output) && r.getId().toString().startsWith("emi") + ); + } + val id = Util.identifierOf(CGEMIPlugin.ID_PREFIX + generator.getType().name() .toLowerCase() + "-" + source.toDebugFileName() + "-" + resultId.toDebugFileName() + "-" + neighbourId.toDebugFileName() + "-" + modifierId.toDebugFileName()); EmiWorldInteractionRecipe.Builder recipe = EmiWorldInteractionRecipe.builder() .id(id) - .leftInput(EmiStack.of(fluid)); + .leftInput(trigger.copy().setRemainder(trigger)); + + EmiStack neighbourRemainder = neighbour.isEmpty() ? neighbour : neighbour.copy().setRemainder(neighbour); if (modifier.isPresent()) recipe.rightInput(EmiStack.of(modifier.get()), false, s -> s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.soul_soil").withStyle(ChatFormatting.GREEN))) - .rightInput(neighbour, false, + .rightInput(neighbourRemainder, false, s -> generator.getBlock() != null ? s.appendTooltip(TextCompat.translatable("tooltip.emi.fluid_interaction.basalt.blue_ice").withStyle(ChatFormatting.GREEN)) : s); else { - recipe.rightInput(neighbour, false); + recipe.rightInput(neighbourRemainder, false); } - recipe.output(EmiStack.of(Util.getBlock(resultId)), + recipe.output(output, s -> { if (!CobbleGen.META_CONFIG.emi.addTooltip) return s; - + var minY = block.minY; if (minY == null) minY = minecraft.level != null ? minecraft.level.getMinBuildHeight() : 0; From 0250f0ab7033bd5832169a4cded5757df92eb1ad Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 10:37:02 +0700 Subject: [PATCH 10/13] chore: Remove unused function --- .../cobblegen/integration/viewer/emi/CGEMIPlugin.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java index 1774195c..1d87ae30 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/emi/CGEMIPlugin.java @@ -58,11 +58,6 @@ public class CGEMIPlugin implements EmiPlugin ) ); - // To merge EMI - private static ResourceLocation synthetic(String type, String name) { - return new ResourceLocation("emi", "/" + type + "/" + name); - } - @Override public void register(EmiRegistry registry) { if (!CobbleGen.META_CONFIG.enableRecipeViewer) From 1c2e409e7d4b2b8af0dd4f467a7abed831a3033b Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 10:38:21 +0700 Subject: [PATCH 11/13] feat: Recipe Viewer support toggle --- .../cobblegen/integration/viewer/jei/CGJEIPlugin.java | 7 +++++++ .../cobblegen/integration/viewer/rei/CGREIPlugin.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/jei/CGJEIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/jei/CGJEIPlugin.java index 1fe111d6..17833b77 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/jei/CGJEIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/jei/CGJEIPlugin.java @@ -1,5 +1,6 @@ package io.github.null2264.cobblegen.integration.viewer.jei; +import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.config.WeightedBlock; import io.github.null2264.cobblegen.integration.viewer.FluidInteractionRecipeHolder; import io.github.null2264.cobblegen.util.GeneratorType; @@ -35,6 +36,9 @@ public class CGJEIPlugin implements IModPlugin @Override public void registerCategories(IRecipeCategoryRegistration registration) { + if (!CobbleGen.META_CONFIG.enableRecipeViewer) + return; + IJeiHelpers jeiHelpers = registration.getJeiHelpers(); IGuiHelper guiHelper = jeiHelpers.getGuiHelper(); IPlatformFluidHelper fluidHelper = jeiHelpers.getPlatformFluidHelper(); @@ -47,6 +51,9 @@ public void registerCategories(IRecipeCategoryRegistration registration) { @Override public void registerRecipes(IRecipeRegistration registration) { + if (!CobbleGen.META_CONFIG.enableRecipeViewer) + return; + FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( (modifierId, blocks) -> { val recipes = new ArrayList(); diff --git a/src/main/java/io/github/null2264/cobblegen/integration/viewer/rei/CGREIPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/viewer/rei/CGREIPlugin.java index 95a5577c..08bc0902 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/viewer/rei/CGREIPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/viewer/rei/CGREIPlugin.java @@ -1,5 +1,6 @@ package io.github.null2264.cobblegen.integration.viewer.rei; +import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.config.WeightedBlock; import io.github.null2264.cobblegen.util.GeneratorType; import io.github.null2264.cobblegen.util.Util; @@ -23,6 +24,9 @@ public class CGREIPlugin implements REIClientPlugin { @Override public void registerCategories(CategoryRegistry registry) { + if (!CobbleGen.META_CONFIG.enableRecipeViewer) + return; + for (GeneratorType generator : GeneratorType.values()) { val category = new FluidInteractionCategory(generator); registry.add(category); @@ -31,6 +35,9 @@ public void registerCategories(CategoryRegistry registry) { @Override public void registerDisplays(DisplayRegistry registry) { + if (!CobbleGen.META_CONFIG.enableRecipeViewer) + return; + FLUID_INTERACTION.getGenerators().forEach((fluid, generators) -> generators.forEach(generator -> generator.getOutput().forEach( (modifierId, blocks) -> { Block modifier = null; From f153bcb043b7cceb554444cbc079045561d05beb Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 10:58:09 +0700 Subject: [PATCH 12/13] chore: Initial support for "Easy generators" --- .../null2264/cobblegen/config/ConfigData.java | 5 +++-- .../null2264/cobblegen/config/WeightedBlock.java | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java index 1b19cf92..0540bf1d 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java +++ b/src/main/java/io/github/null2264/cobblegen/config/ConfigData.java @@ -68,8 +68,9 @@ public static ConfigData defaultConfig() { null, null, null, - 0 - ), new WeightedBlock("minecraft:cobbled_deepslate", 100.0, null, null, 0, null)); + 0, + null + ), new WeightedBlock("minecraft:cobbled_deepslate", 100.0, null, null, 0, null, null)); config.stoneGen = List.of(new WeightedBlock("minecraft:stone", 100.0)); config.basaltGen = List.of(new WeightedBlock("minecraft:basalt", 100.0)); config.customGen = new CustomGen( diff --git a/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java b/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java index c2c5f2ed..63276c6a 100644 --- a/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java +++ b/src/main/java/io/github/null2264/cobblegen/config/WeightedBlock.java @@ -25,6 +25,8 @@ public class WeightedBlock implements PacketSerializable public Integer maxY; @Nullable public Integer minY; + @Nullable + public List neighbours; public WeightedBlock(String id, Double weight) { this(id, weight, null, null); @@ -35,7 +37,7 @@ public WeightedBlock(String id, Double weight, List dimIds) { } public WeightedBlock(String id, Double weight, List dimIds, List excludedDimensions) { - this(id, weight, dimIds, excludedDimensions, null, null); + this(id, weight, dimIds, excludedDimensions, null, null, null); } public WeightedBlock( @@ -44,7 +46,8 @@ public WeightedBlock( @Nullable List dimIds, @Nullable List excludedDimensions, @Nullable Integer maxY, - @Nullable Integer minY + @Nullable Integer minY, + @Nullable List neighbours ) { this.id = id; this.weight = weight; @@ -52,6 +55,7 @@ public WeightedBlock( this.excludedDimensions = excludedDimensions; this.maxY = maxY; this.minY = minY; + this.neighbours = neighbours; } public static WeightedBlock fromBlock(Block block, Double weight) { @@ -71,7 +75,7 @@ public static WeightedBlock fromBlock( Integer minY ) { val id = Util.getBlockId(block).toString(); - return new WeightedBlock(id, weight, dimIds, excludedDimensions, maxY, minY); + return new WeightedBlock(id, weight, dimIds, excludedDimensions, maxY, minY, null); } public Block getBlock() { @@ -118,7 +122,8 @@ public static WeightedBlock fromPacket(FriendlyByteBuf buf) { dimensions.orElse(null), excludedDimensions.orElse(null), maxY.orElse(null), - minY.orElse(null) + minY.orElse(null), + null ); } } \ No newline at end of file From d4752141b3fc4bcb715651acbfe40d357c3d8823 Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 12 Nov 2023 12:34:22 +0700 Subject: [PATCH 13/13] feat: Add command to reload CobbleGen meta config --- build.gradle | 2 +- .../github/null2264/cobblegen/CobbleGen.java | 20 ++++++++++++- .../cobblegen/mixin/CommandsMixin.java | 28 +++++++++++++++++++ src/main/resources/cobblegen.mixins.json | 5 ++-- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/null2264/cobblegen/mixin/CommandsMixin.java diff --git a/build.gradle b/build.gradle index 0b554504..42945908 100644 --- a/build.gradle +++ b/build.gradle @@ -93,7 +93,7 @@ dependencies { mappings loom.officialMojangMappings() if (isFabric) { - if (mcVersion <= 11902) + if (mcVersion <= 11902 && project.recipe_viewer == "rei") modImplementation "net.fabricmc:fabric-loader:0.14.14" // I don't get it, REI hate 0.14.21 in 1.19.2 or lower, wtf? else if (mcVersion <= 12001) modImplementation "net.fabricmc:fabric-loader:0.14.21" diff --git a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java index 8073d52c..4c2e5f8e 100644 --- a/src/main/java/io/github/null2264/cobblegen/CobbleGen.java +++ b/src/main/java/io/github/null2264/cobblegen/CobbleGen.java @@ -1,12 +1,16 @@ package io.github.null2264.cobblegen; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import io.github.null2264.cobblegen.compat.LoaderCompat; +import io.github.null2264.cobblegen.compat.TextCompat; import io.github.null2264.cobblegen.config.ConfigMetaData; import io.github.null2264.cobblegen.data.FluidInteractionHelper; import io.github.null2264.cobblegen.data.model.CGRegistry; +import io.github.null2264.cobblegen.util.CGLog; +import net.minecraft.commands.CommandSourceStack; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.nio.file.Path; @@ -39,6 +43,20 @@ public class CobbleGen implements net.fabricmc.api.ModInitializer public void onInitialize() {} //#endif + public static void initCommands(CommandDispatcher dispatcher) { + CGLog.info("Registering command..."); + dispatcher.register( + LiteralArgumentBuilder.literal("cobblegen") + .then(LiteralArgumentBuilder.literal("reload-meta").executes(c -> { + CGLog.info("Reloading meta config..."); + META_CONFIG = loadConfig(true, configFile, META_CONFIG, new ConfigMetaData(), ConfigMetaData.class); + c.getSource().sendSuccess(TextCompat.literal("Meta config has been reloaded"), false); + CGLog.info("Meta config has been reloaded"); + return 0; + })) + ); + } + public enum Channel { PING, SYNC, diff --git a/src/main/java/io/github/null2264/cobblegen/mixin/CommandsMixin.java b/src/main/java/io/github/null2264/cobblegen/mixin/CommandsMixin.java new file mode 100644 index 00000000..13b4cfb9 --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/mixin/CommandsMixin.java @@ -0,0 +1,28 @@ +package io.github.null2264.cobblegen.mixin; + +import com.mojang.brigadier.CommandDispatcher; +import io.github.null2264.cobblegen.CobbleGen; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(Commands.class) +public abstract class CommandsMixin { + @Shadow @Final private CommandDispatcher dispatcher; + + @Inject(method = "", at = @At("TAIL")) + private void registerCustomCommands( + Commands.CommandSelection commandSelection, + //#if MC>1.18.2 + //$$ net.minecraft.commands.CommandBuildContext commandBuildContext, + //#endif + CallbackInfo ci + ) { + CobbleGen.initCommands(this.dispatcher); + } +} \ No newline at end of file diff --git a/src/main/resources/cobblegen.mixins.json b/src/main/resources/cobblegen.mixins.json index 5f74ff20..f63d956c 100644 --- a/src/main/resources/cobblegen.mixins.json +++ b/src/main/resources/cobblegen.mixins.json @@ -4,15 +4,16 @@ "package": "io.github.null2264.cobblegen.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "CommandsMixin", "MinecraftServerMixin", "fluid.CreateFluidReactionsMixin", "fluid.FluidEventMixin", "fluid.LavaEventMixin" ], "client": [ - "network.ConnectionMixin", "network.ClientboundCustomPayloadPacketMixin", - "network.ClientCommonPacketListenerMixin" + "network.ClientCommonPacketListenerMixin", + "network.ConnectionMixin" ], "server": [ "network.PlayerManagerMixin",