Skip to content

Commit

Permalink
1201
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasualix committed Mar 13, 2024
1 parent ca7aa54 commit a89296f
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 106 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
options.release.set(8)
options.release.set(17)
}

java {
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ org.gradle.parallel=true
org.gradle.caching=true

loom.platform=forge
minecraft_version=1.16.5
forge_version=36.2.39
parchment_version=2022.03.06
minecraft_version=1.20.1
forge_version=47.2.20
parchment_version=2023.09.03
yarn_mappings=10
cf_project_id=912104

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.teampotato.sparsestructuresreforged;

public class CustomSpreadFactors {
public String structure;
public double factor;

public CustomSpreadFactors(String structure, double factor) {
this.structure = structure;
this.factor = factor;
}

public String structure() {
return this.structure;
}

public double factor() {
return this.factor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.teampotato.sparsestructuresreforged;

import com.google.gson.Gson;
import net.minecraftforge.fml.common.Mod;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Mod(SparseStructures.MOD_ID)
public class SparseStructures {
public static final String MOD_ID = "sparsestructuresreforged";
private static final String CONFIG_RESOURCE_NAME = "sparse-structures-default-config.json5";
private static final String CONFIG_FILENAME = "sparsestructures.json5";
private static final Path CONFIG_FILE_PATH = Paths.get("config", CONFIG_FILENAME);
public static SparseStructuresConfig config;

public SparseStructures() {
if (!CONFIG_FILE_PATH.toFile().exists()) {
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(CONFIG_RESOURCE_NAME)) {
if (in == null) throw new IllegalStateException("Failed to load SparseStructure's default config \"" + CONFIG_RESOURCE_NAME +"\"");
Files.createDirectories(CONFIG_FILE_PATH);
Files.copy(in, CONFIG_FILE_PATH, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
try (final InputStream in = Files.newInputStream(CONFIG_FILE_PATH)) {
config = new Gson().fromJson(new InputStreamReader(in), SparseStructuresConfig.class);
} catch (Exception e) {
throw new RuntimeException("SparseStructure's config file is malformed! If you don't know what's causing this, delete the config file and restart the game.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.teampotato.sparsestructuresreforged;

import java.util.List;

public class SparseStructuresConfig {
public double spreadFactor;
public List<CustomSpreadFactors> customSpreadFactors;

public SparseStructuresConfig(double spreadFactor, List<CustomSpreadFactors> customSpreadFactors) {
this.spreadFactor = spreadFactor;
this.customSpreadFactors = customSpreadFactors;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.teampotato.sparsestructuresreforged.mixin;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Decoder;
import com.teampotato.sparsestructuresreforged.CustomSpreadFactors;
import com.teampotato.sparsestructuresreforged.SparseStructures;
import net.minecraft.core.Registry;
import net.minecraft.core.WritableRegistry;
import net.minecraft.resources.*;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.io.Reader;
import java.util.Iterator;
import java.util.Map;

@Mixin(RegistryDataLoader.class)
public abstract class RegistryDataLoaderMixin {
@Inject(method = "loadRegistryContents", at = @At(value = "INVOKE", remap = false, target = "Lcom/mojang/serialization/Decoder;parse(Lcom/mojang/serialization/DynamicOps;Ljava/lang/Object;)Lcom/mojang/serialization/DataResult;"), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
private static <E> void ssr$init(RegistryOps.RegistryInfoLookup lookup, ResourceManager manager, ResourceKey<? extends Registry<E>> registryKey, WritableRegistry<E> registry, Decoder<E> decoder, Map<ResourceKey<?>, Exception> exceptions, CallbackInfo ci, String string, FileToIdConverter converter, RegistryOps<JsonElement> registryOps, Iterator<Map.Entry<ResourceLocation, Resource>> iterator, Map.Entry<ResourceLocation, Resource> entry, ResourceLocation resourcelocation, ResourceKey<E> resourceKey, Resource resource, Reader reader, JsonElement jsonElement) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonObject placement = jsonObject.getAsJsonObject("placement");
if (string.equals("worldgen/structure_set") && !placement.get("type").getAsString().equals("minecraft:concentric_rings")) {
double factor = SparseStructures.config.customSpreadFactors.stream().filter(s -> {
if (s == null) return false;
String structure_set = resourceKey.location().toString();
return structure_set.equals(s.structure()) || jsonObject.getAsJsonArray("structures").asList().stream().anyMatch(p -> p.getAsJsonObject().get("structure").getAsString().equals(s.structure()));
}).findFirst().orElse(new CustomSpreadFactors("", SparseStructures.config.spreadFactor)).factor();

int spacing;
int separation;

if (placement.get("spacing") == null) spacing = 1;
else spacing = (int)(Math.min(placement.get("spacing").getAsDouble() * factor, 4096.0));
if (placement.get("separation") == null) separation = 1;
else separation = (int)(Math.min(placement.get("separation").getAsDouble() * factor, 4096.0));
if (separation >= spacing) {
if (spacing == 0) spacing = 1;
separation = spacing - 1;
}

placement.addProperty("spacing", spacing);
placement.addProperty("separation", separation);
}
}
}

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/resources/sparse-structures-default-config.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ### THE MOD REQUIRES A RESTART OF THE GAME TO APPLY CHANGES ###
{
// this is the main spread factor (default is 2)
//
// tips : a spread factor can be a decimal number (such as 1.5)
// a spread factor of 1 means all structure's placement are not modified (useful if you want to use only custom spread factors)
// a spread factor above 1 means all structures are rarer
// a spread factor below 1 means all structure are more common
"spreadFactor": 2,

// this is a list of custom spread factors
"customSpreadFactors": [
// example of the mansion being doubled in rarity (the mod's default)
{
"structure": "minecraft:mansion",
"factor": 2
},
// add the structures you want to modify in the format:
// {
// "structure": "namespace:structure_name",
// "factor": number
// },
// where "structure" is a structure_set or the name of a structure
// /!\ if you put the name of a structure, all structures in its set will be modified
// (example : "minecraft:village_plains" will modify all structures in the "villages" set)
// see https://minecraft.wiki/w/Tutorials/Custom_structures#Structure_Set for more info
//
// tip : the same spread factors rules apply here
]
}
4 changes: 2 additions & 2 deletions src/main/resources/sparsestructuresreforged.mixins.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"required": true,
"package": "com.teampotato.sparsestructuresreforged.mixin",
"compatibilityLevel": "JAVA_8",
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"StructureFeatureConfigurationMixin"
"RegistryDataLoaderMixin"
]
}

0 comments on commit a89296f

Please sign in to comment.