Skip to content

Commit

Permalink
Update for 1.17.1, now with simultaneous Forge and Fabric support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkhax committed Dec 17, 2021
1 parent 2ede9fb commit 10781d4
Show file tree
Hide file tree
Showing 41 changed files with 1,866 additions and 488 deletions.
34 changes: 18 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#IntelliJ IDEA
*.class
*.iml
# eclipse
bin
*.launch
.settings
.metadata
.classpath
.project

# idea
out
*.ipr
*.iws
out/
*.iml
.idea

#ForgeGradle
build/
.gradle/
run/
logs/
# gradle
build
.gradle

#Eclipse
.classpath
.metadata/
.project
.settings/
*.launch
/bin/
# other
eclipse
run
69 changes: 69 additions & 0 deletions Common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
plugins {
id 'org.spongepowered.gradle.vanilla' version '0.2.1-SNAPSHOT'
}

apply from: '../gradle/property_helper.gradle'

archivesBaseName = "${mod_name}-Common-${minecraft_version}"

minecraft {
version(minecraft_version)
}

dependencies {
compileOnly group:'org.spongepowered', name:'mixin', version:'0.8.4'
}

processResources {

def buildProps = project.properties.clone()

filesMatching(['pack.mcmeta']) {

expand buildProps
}
}

// -- MAVEN PUBLISHING --
project.publishing {

publications {

mavenJava(MavenPublication) {

artifactId project.archivesBaseName

// Base mod archive.
artifact jar

// Adds the sources as an artifact.
artifact project.sourcesJar {
classifier 'sources'
}

// Adds the javadocs as an artifact.
artifact project.javadocJar {
classifier 'javadoc'
}
}
}

repositories {

maven {

// Sets maven credentials if they are provided. This is generally
// only used for external/remote uploads.
if (project.hasProperty('mavenUsername') && project.hasProperty('mavenPassword')) {

credentials {

username findProperty('mavenUsername')
password findProperty('mavenPassword')
}
}

url getDefaultString('mavenURL', 'undefined', true)
}
}
}
42 changes: 42 additions & 0 deletions Common/src/main/java/net/darkhax/attributefix/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.darkhax.attributefix;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Constants {

public static final String MOD_ID = "attributefix";
public static final String MOD_NAME = "AttributeFix";
public static final Logger LOG = LogManager.getLogger(MOD_NAME);
public static final Gson GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().registerTypeAdapter(Double.class, new DoubleJsonSerializer()).create();
public static final DecimalFormat FORMAT = new DecimalFormat("#.##");

private static final class DoubleJsonSerializer implements JsonSerializer<Double> {

@Override
public JsonElement serialize(final Double src, final Type typeOfSrc, final JsonSerializationContext context) {

BigDecimal value = BigDecimal.valueOf(src);

try {
value = new BigDecimal(value.toBigIntegerExact());
}

catch (ArithmeticException e) {
// NO-OP
}

return new JsonPrimitive(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package net.darkhax.attributefix.config;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.darkhax.attributefix.Constants;
import net.darkhax.attributefix.mixin.AccessorRangedAttribute;
import net.darkhax.attributefix.temp.RegistryHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AttributeConfig {

@Expose
private Map<String, Entry> attributes = new HashMap<>();

public void applyChanges(RegistryHelper<Attribute> registry) {


Constants.LOG.info("Applying changes for {} attributes.", attributes.size());
for (Map.Entry<String, Entry> configEntry : attributes.entrySet()) {

final ResourceLocation attributeId = ResourceLocation.tryParse(configEntry.getKey());

if (attributeId != null && registry.exists(attributeId)) {

final Attribute attribute = registry.get(attributeId);

if (attribute instanceof RangedAttribute ranged) {

final double minValue = configEntry.getValue().min.value;
final double maxValue = configEntry.getValue().max.value;

if (minValue > maxValue) {

Constants.LOG.error("Attribute {} was configured to have a minimum value higher than it's maximum. This is not permitted!", attributeId);
continue;
}

final AccessorRangedAttribute accessor = (AccessorRangedAttribute)(Object)attribute;

if (minValue != ranged.getMinValue()) {

Constants.LOG.debug("Modifying minimum value for {} from {} to {}.", attributeId, Constants.FORMAT.format(ranged.getMinValue()), Constants.FORMAT.format(minValue));
accessor.attributefix$setMinValue(minValue);
}

if (maxValue != ranged.getMaxValue()) {

Constants.LOG.debug("Modifying maximum value for {} from {} to {}.", attributeId, Constants.FORMAT.format(ranged.getMaxValue()), Constants.FORMAT.format(maxValue));
accessor.attributefix$setMaxValue(maxValue);
}
}
}
}
}

public static AttributeConfig load(File configFile, RegistryHelper<Attribute> registry) {

final AttributeConfig config = new AttributeConfig();

// Load/Generate the default values.
for (Attribute attribute : registry.getValues()) {

if (attribute instanceof RangedAttribute ranged) {

final ResourceLocation id = registry.getId(attribute);
config.attributes.put(id.toString(), new Entry(id, ranged));
}
}

Constants.LOG.info("Loaded values for {} compatible attributes.", config.attributes.size());

// Attempt to load existing config file
if (configFile.exists()) {

try (FileReader reader = new FileReader(configFile)) {

final Map<String, Entry> configValues = Constants.GSON.fromJson(reader, AttributeConfig.class).attributes;

for (Map.Entry<String, Entry> configEntry : configValues.entrySet()) {

final ResourceLocation attributeId = ResourceLocation.tryParse(configEntry.getKey());

if (attributeId == null) {

Constants.LOG.error("Attribute ID '{}' is not a valid. This entry will be ignored.", configEntry.getKey());
}

else if (!registry.exists(attributeId)) {

Constants.LOG.error("Attribute ID '{}' does not belong to a known attribute. This entry will be ignored.", configEntry.getKey());
}

if (configEntry.getValue().min.value > configEntry.getValue().max.value) {

Constants.LOG.error("Attribute ID '{}' has a max value that is less than its minimum value!", configEntry.getKey());
}

// Prevent data loss by including the user data even if it's invalid. Additional checks will be required when applying this later on.
config.attributes.put(configEntry.getKey(), configEntry.getValue());
}

Constants.LOG.info("Loaded {} values from config.", configValues.size());
}

catch (IOException e) {

Constants.LOG.error("Could not read config file {}. Defaults will be used.", configFile.getAbsolutePath());
Constants.LOG.catching(e);
}
}

else {

Constants.LOG.info("Creating a new config file at {}.", configFile.getAbsolutePath());
configFile.getParentFile().mkdirs();
}

try (FileWriter writer = new FileWriter(configFile)) {

Constants.GSON.toJson(config, writer);
Constants.LOG.info("Saving config file. {} entries.", config.attributes.size());
}

catch (IOException e) {

Constants.LOG.error("Could not write config file '{}'!", configFile.getAbsolutePath());
Constants.LOG.catching(e);
}


return config;
}

public static class Entry {

@Expose
private boolean enabled;

@Expose
private DoubleValue min;

@Expose
private DoubleValue max;

public Entry(ResourceLocation id, RangedAttribute attribute) {

this.enabled = "minecraft".equals(id.getNamespace());
this.min = new DoubleValue(attribute.getMinValue(), attribute.getMinValue());
this.max = new DoubleValue(attribute.getMaxValue(), 10_000_000d);
}

public boolean isEnabled() {

return this.isEnabled();
}
}

public static class DoubleValue {

@Expose
@SerializedName("default")
private double defaultValue;

@Expose
private double value;

public DoubleValue(double defaultValue, double value) {

this.defaultValue = defaultValue;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.darkhax.attributefix.mixin;

import net.darkhax.attributefix.Constants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(RangedAttribute.class)
public interface AccessorRangedAttribute {

@Accessor("minValue")
@Mutable
void attributefix$setMinValue(double minValue);

@Accessor("maxValue")
@Mutable
void attributefix$setMaxValue(double maxValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.darkhax.attributefix.temp;

import net.minecraft.resources.ResourceLocation;

import java.util.Collection;

public interface RegistryHelper<T> {

T get(ResourceLocation id);

ResourceLocation getId(T value);

boolean isRegistered(T value);

boolean exists(ResourceLocation id);

Collection<T> getValues();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Common/src/main/resources/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "${mod_description}",
"pack_format": 8
}
}
Loading

0 comments on commit 10781d4

Please sign in to comment.