Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Meta Config and Experimental Features #39

Merged
merged 16 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/github/null2264/cobblegen/CobbleGen.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
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 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)
//$$ public class CobbleGen
Expand All @@ -21,12 +33,30 @@ public class CobbleGen implements net.fabricmc.api.ModInitializer
@Deprecated
@ApiStatus.Internal
public static final FluidInteractionHelper FLUID_INTERACTION = new FluidInteractionHelper();
private static final Path configPath = LoaderCompat.getConfigDir();
private static final File configFile = new File(configPath + File.separator + MOD_ID + "-meta.json5");
@ApiStatus.Internal
public static ConfigMetaData META_CONFIG = loadConfig(false, configFile, null, new ConfigMetaData(), ConfigMetaData.class);

//#if FABRIC>=1
@Override
public void onInitialize() {}
//#endif

public static void initCommands(CommandDispatcher<CommandSourceStack> dispatcher) {
CGLog.info("Registering command...");
dispatcher.register(
LiteralArgumentBuilder.<CommandSourceStack>literal("cobblegen")
.then(LiteralArgumentBuilder.<CommandSourceStack>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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class AdvancedGen
public boolean silent = false;
public Map<String, List<WeightedBlock>> results = Map.of();
public Map<String, List<WeightedBlock>> resultsFromTop = Map.of();
public Map<String, List<WeightedBlock>> obsidian = Map.of();
}
3 changes: 3 additions & 0 deletions src/main/java/io/github/null2264/cobblegen/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.null2264.cobblegen.config;

public interface Config {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;
import java.util.Map;

public class ConfigData
public class ConfigData implements Config
{
@Nullable
@Comment(value = """
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -39,12 +38,12 @@ private static JsonElement filter(JsonElement json) {
}

@ApiStatus.Internal
public static ConfigData loadConfig(boolean reload, File configFile, ConfigData workingConfig) {
public static <T extends Config> T loadConfig(boolean reload, File configFile, T workingConfig, T defaultConfig, Class<T> 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);

Expand All @@ -53,16 +52,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(Config config, File configFile) {
try {
CGLog.info("Trying to create config file...");
FileWriter fw = new FileWriter(configFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class WeightedBlock implements PacketSerializable<WeightedBlock>
public Integer maxY;
@Nullable
public Integer minY;
@Nullable
public List<String> neighbours;

public WeightedBlock(String id, Double weight) {
this(id, weight, null, null);
Expand All @@ -35,7 +37,7 @@ public WeightedBlock(String id, Double weight, List<String> dimIds) {
}

public WeightedBlock(String id, Double weight, List<String> dimIds, List<String> excludedDimensions) {
this(id, weight, dimIds, excludedDimensions, null, null);
this(id, weight, dimIds, excludedDimensions, null, null, null);
}

public WeightedBlock(
Expand All @@ -44,20 +46,26 @@ public WeightedBlock(
@Nullable List<String> dimIds,
@Nullable List<String> excludedDimensions,
@Nullable Integer maxY,
@Nullable Integer minY
@Nullable Integer minY,
@Nullable List<String> neighbours
) {
this.id = id;
this.weight = weight;
this.dimensions = dimIds;
this.excludedDimensions = excludedDimensions;
this.maxY = maxY;
this.minY = minY;
this.neighbours = neighbours;
}

public static WeightedBlock fromBlock(Block block, Double weight) {
return fromBlock(block, weight, null, null, null, null);
}

public Optional<List<String>> getDimensions() {
return Util.optional(dimensions);
}

public static WeightedBlock fromBlock(
Block block,
Double weight,
Expand All @@ -67,13 +75,25 @@ 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() {
return Util.getBlock(ResourceLocation.tryParse(id));
}

public Optional<List<String>> getExcludedDimensions() {
return Util.optional(excludedDimensions);
}

public Optional<Integer> getMaxY() {
return Util.optional(maxY);
}

public Optional<Integer> getMinY() {
return Util.optional(minY);
}

@Override
public void toPacket(FriendlyByteBuf buf) {
buf.writeUtf(id);
Expand Down Expand Up @@ -102,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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean isSilent() {
public Optional<BlockState> 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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +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;
Expand All @@ -20,19 +22,26 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class CobbleGenerator extends BlockGenerator
{
private final Map<String, List<WeightedBlock>> possibleBlocks;
private final Map<String, List<WeightedBlock>> obsidianReplacements;
private Fluid fluid;
private final boolean silent;

public CobbleGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent) {
this(Map.of("*", possibleBlocks), fluid, silent);
this(possibleBlocks, fluid, silent, Map.of());
}

public CobbleGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent) {
public CobbleGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent, Map<String, List<WeightedBlock>> obsidianReplacements) {
this(Map.of("*", possibleBlocks), fluid, silent, obsidianReplacements);
}

public CobbleGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent, Map<String, List<WeightedBlock>> obsidianReplacements) {
this.possibleBlocks = possibleBlocks;
this.obsidianReplacements = obsidianReplacements;
this.fluid = fluid;
this.silent = silent;
}
Expand All @@ -42,6 +51,11 @@ public CobbleGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fl
return possibleBlocks;
}

@Override
public Map<String, List<WeightedBlock>> getObsidianOutput() {
return obsidianReplacements;
}

@Override
public @NotNull GeneratorType getType() {
return GeneratorType.COBBLE;
Expand Down Expand Up @@ -76,10 +90,13 @@ public Optional<BlockState> tryGenerate(LevelAccessor level, BlockPos pos, Block
@Override
public Optional<BlockState> tryGenerate(LevelAccessor level, BlockPos pos, FluidState source, FluidState neighbour) {
if (Generator.getStillFluid(neighbour) == getFluid()) {
if (source.getType() == Fluids.LAVA && source.isSource())
if (source.getType() == Fluids.LAVA && source.isSource()) {
if (Util.optional(CobbleGen.META_CONFIG).orElse(new ConfigMetaData()).enableExperimentalFeatures)
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();
}
Expand All @@ -91,9 +108,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))
);
}
Expand All @@ -105,7 +125,9 @@ public static Generator fromPacket(FriendlyByteBuf buf) {

Map<String, List<WeightedBlock>> outMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<String, List<WeightedBlock>> obiMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));

return new CobbleGenerator(outMap, fluid, silent);
return new CobbleGenerator(outMap, fluid, silent, obiMap);
}
}
Loading
Loading