Update to Iris 0.4.0, code cleanup

This commit is contained in:
Motschen
2021-06-06 08:24:16 +02:00
parent b167872a6b
commit 2e7c504e72
6 changed files with 38 additions and 391 deletions

0
README.md Normal file → Executable file
View File

View File

@@ -1,5 +1,5 @@
plugins { plugins {
id 'fabric-loom' version '0.6-SNAPSHOT' id 'fabric-loom' version '0.7-SNAPSHOT'
id 'maven-publish' id 'maven-publish'
} }
@@ -24,9 +24,9 @@ dependencies {
//to change the versions see the gradle.properties file //to change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}" minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modCompile "net.fabricmc:fabric-loader:${project.loader_version}" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation "com.terraformersmc:modmenu:${project.mod_menu_version}" modImplementation "com.terraformersmc:modmenu:${project.mod_menu_version}"
modImplementation ("com.github.TeamMidnightDust:CullLeaves:${project.cull_leaves_version}"){ modImplementation ("com.github.TeamMidnightDust:CullLeaves:${project.cull_leaves_version}"){
@@ -39,7 +39,7 @@ dependencies {
modImplementation ("com.github.LambdAurora:LambdaBetterGrass:${project.lbg_version}") { modImplementation ("com.github.LambdAurora:LambdaBetterGrass:${project.lbg_version}") {
exclude module: "modmenu" exclude module: "modmenu"
} }
modImplementation ("com.github.The-HyperZone:Iris:${project.iris_version}") { modImplementation ("com.github.IrisShaders:Iris:${project.iris_version}") {
exclude module: "modmenu" exclude module: "modmenu"
} }
modImplementation ("com.github.PepperCode1:ConnectedTexturesMod-Fabric:${project.ctmf_version}") { modImplementation ("com.github.PepperCode1:ConnectedTexturesMod-Fabric:${project.ctmf_version}") {
@@ -53,33 +53,33 @@ dependencies {
processResources { processResources {
inputs.property "version", project.version inputs.property "version", project.version
from(sourceSets.main.resources.srcDirs) { filesMatching("fabric.mod.json") {
include "fabric.mod.json"
expand "version": project.version expand "version": project.version
} }
from(sourceSets.main.resources.srcDirs) {
exclude "fabric.mod.json"
}
} }
// ensure that the encoding is set to UTF-8, no matter what the system default is tasks.withType(JavaCompile).configureEach {
// this fixes some edge cases with special characters not displaying correctly // ensure that the encoding is set to UTF-8, no matter what the system default is
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html // this fixes some edge cases with special characters not displaying correctly
tasks.withType(JavaCompile) { // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
options.encoding = "UTF-8" // If Javadoc is generated, this must be specified in that task too.
it.options.encoding = "UTF-8"
// Minecraft 1.17 (21w19a) upwards uses Java 16.
//it.options.release = 8
} }
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task java {
// if it is present. // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// If you remove this task, sources will not be generated. // if it is present.
task sourcesJar(type: Jar, dependsOn: classes) { // If you remove this line, sources will not be generated.
classifier = "sources" withSourcesJar()
from sourceSets.main.allSource
} }
jar { jar {
from "LICENSE" from("LICENSE") {
rename { "${it}_${project.archivesBaseName}"}
}
} }
// configure the maven publication // configure the maven publication
@@ -96,9 +96,11 @@ publishing {
} }
} }
// select the repositories you want to publish to // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories { repositories {
// uncomment to publish to the local maven // Add repositories to publish to here.
// mavenLocal() // Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
} }
} }

View File

@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3 loader_version=0.11.3
# Mod Properties # Mod Properties
mod_version = 0.1.0 mod_version = 0.2.0
maven_group = eu.midnightdust maven_group = eu.midnightdust
archives_base_name = puzzle archives_base_name = puzzle
@@ -21,5 +21,5 @@ org.gradle.jvmargs=-Xmx1G
ldl_version = 1.3.4-1.16 ldl_version = 1.3.4-1.16
lbg_version = 1.0.3-1.16 lbg_version = 1.0.3-1.16
lc_version = 1.6.0-1.16 lc_version = 1.6.0-1.16
iris_version = 807b026 iris_version = v0.4.0
ctmf_version = v0.4.0 ctmf_version = v0.4.0

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.1-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@@ -1,340 +0,0 @@
package eu.midnightdust.puzzle.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
// MidnightConfig v0.1.2
// Changelog:
// - The config screen no longer shows the entries of all instances of MidnightConfig
// - Compatible with servers!
/** Based on https://github.com/Minenash/TinyConfig
* Credits to Minenash - CC0-1.0 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class MidnightConfig {
private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)");
private static final Pattern DECIMAL_ONLY = Pattern.compile("-?([\\d]+\\.?[\\d]*|[\\d]*\\.?[\\d]+|\\.)");
private static final List<EntryInfo> entries = new ArrayList<>();
protected static class EntryInfo {
Field field;
Object widget;
int width;
Method dynamicTooltip;
Map.Entry<TextFieldWidget,Text> error;
Object defaultValue;
Object value;
String tempValue;
boolean inLimits = true;
String id;
}
private static final Map<String,Class> configClass = new HashMap();
private static String translationPrefix;
private static Path path;
private static final Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.setPrettyPrinting()
.create();
public static void init(String modid, Class<?> config) {
translationPrefix = modid + ".midnightconfig.";
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
configClass.put(modid, config);
for (Field field : config.getFields()) {
Class<?> type = field.getType();
EntryInfo info = new EntryInfo();
Entry e;
try { e = field.getAnnotation(Entry.class); }
catch (Exception ignored) { continue; }
info.width = e.width();
info.field = field;
info.id = modid;
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(),false);
else if (type == String.class) textField(info, String::length, null, Math.min(e.min(),0), Math.max(e.max(),1),true);
else if (type == boolean.class) {
Function<Object,Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
info.value = !(Boolean) info.value;
button.setMessage(func.apply(info.value));
}, func);
}
else if (type.isEnum()) {
List<?> values = Arrays.asList(field.getType().getEnumConstants());
Function<Object,Text> func = value -> new TranslatableText(translationPrefix + "enum." + type.getSimpleName() + "." + info.value.toString());
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object,Text>>( button -> {
int index = values.indexOf(info.value) + 1;
info.value = values.get(index >= values.size()? 0 : index);
button.setMessage(func.apply(info.value));
}, func);
}
else
continue;
entries.add(info);
try { info.defaultValue = field.get(null); }
catch (IllegalAccessException ignored) {}
try {
info.dynamicTooltip = config.getMethod(e.dynamicTooltip());
info.dynamicTooltip.setAccessible(true);
} catch (Exception ignored) {}
}
try { gson.fromJson(Files.newBufferedReader(path), config); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored) {}
}
}
public static void initServer(String modid, Class<?> config) {
translationPrefix = modid + ".midnightconfig.";
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
configClass.put(modid,config);
try { gson.fromJson(Files.newBufferedReader(path), config); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored) {}
}
}
private static void textField(EntryInfo info, Function<String,Number> f, Pattern pattern, double min, double max, boolean cast) {
boolean isNumber = pattern != null;
info.widget = (BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) (t, b) -> s -> {
s = s.trim();
if (!(s.isEmpty() || !isNumber || pattern.matcher(s).matches()))
return false;
Number value = 0;
boolean inLimits = false;
System.out.println(((isNumber ^ s.isEmpty())));
System.out.println(!s.equals("-") && !s.equals("."));
info.error = null;
if (!(isNumber && s.isEmpty()) && !s.equals("-") && !s.equals(".")) {
value = f.apply(s);
inLimits = value.doubleValue() >= min && value.doubleValue() <= max;
info.error = inLimits? null : new AbstractMap.SimpleEntry<>(t, new LiteralText(value.doubleValue() < min ?
"§cMinimum " + (isNumber? "value" : "length") + (cast? " is " + (int)min : " is " + min) :
"§cMaximum " + (isNumber? "value" : "length") + (cast? " is " + (int)max : " is " + max)));
}
info.tempValue = s;
t.setEditableColor(inLimits? 0xFFFFFFFF : 0xFFFF7777);
info.inLimits = inLimits;
b.active = entries.stream().allMatch(e -> e.inLimits);
if (inLimits)
info.value = isNumber? value : s;
return true;
};
}
public static void write(String modid) {
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
try {
if (!Files.exists(path)) Files.createFile(path);
Files.write(path, gson.toJson(configClass.get(modid).newInstance()).getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void update(String modid, Class<?> config) {
write(modid);
try { gson.fromJson(Files.newBufferedReader(path), config); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored) {}
}
}
@Environment(EnvType.CLIENT)
public static Screen getScreen(Screen parent, String modid) {
return new TinyConfigScreen(parent, modid);
}
@Environment(EnvType.CLIENT)
private static class TinyConfigScreen extends Screen {
protected TinyConfigScreen(Screen parent, String modid) {
super(new TranslatableText(modid + ".midnightconfig." + "title"));
this.parent = parent;
this.modid = modid;
this.translationPrefix = modid + ".midnightconfig.";
}
private final Screen parent;
private final String modid;
private final String translationPrefix;
// Real Time config update //
@Override
public void tick() {
for (EntryInfo info : entries)
try { info.field.set(null, info.value); }
catch (IllegalAccessException ignore) {}
}
@Override
protected void init() {
super.init();
this.addButton(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> {
try { gson.fromJson(Files.newBufferedReader(path), configClass.get(modid)); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored) {}
}
Objects.requireNonNull(client).openScreen(parent);
}));
ButtonWidget done = this.addButton(new ButtonWidget(this.width / 2 + 4, this.height - 28, 150, 20, ScreenTexts.DONE, (button) -> {
for (EntryInfo info : entries)
if (info.id.equals(modid)) {
try {
info.field.set(null, info.value);
} catch (IllegalAccessException ignore) {
}
}
write(modid);
Objects.requireNonNull(client).openScreen(parent);
}));
int y = 45;
for (EntryInfo info : entries) {
if (info.id.equals(modid)) {
addButton(new ButtonWidget(width - 155, y, 40, 20, new LiteralText("Reset").formatted(Formatting.RED), (button -> {
info.value = info.defaultValue;
info.tempValue = info.value.toString();
Objects.requireNonNull(client).openScreen(this);
})));
if (info.widget instanceof Map.Entry) {
Map.Entry<ButtonWidget.PressAction, Function<Object, Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
addButton(new ButtonWidget(width - 110, y, info.width, 20, widget.getValue().apply(info.value), widget.getKey()));
} else {
TextFieldWidget widget = addButton(new TextFieldWidget(textRenderer, width - 110, y, info.width, 20, null));
widget.setText(info.tempValue);
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget, done);
widget.setTextPredicate(processor);
children.add(widget);
}
y += 25;
}
}
}
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices);
int stringWidth = (int) (title.getString().length() * 2.75f);
this.fillGradient(matrices, this.width / 2 - stringWidth, 10, this.width /2 + stringWidth, 29, -1072689136, -804253680);
this.fillGradient(matrices, 0, 35, width, this.height - 40, -1072689136, -804253680);
super.render(matrices, mouseX, mouseY, delta);
//renderTooltip(matrices, title, width/2 - stringWidth, 27);
drawCenteredText(matrices, textRenderer, title, width/2, 15, 0xFFFFFF);
int y = 40;
for (EntryInfo info : entries) {
if (info.id.equals(modid)) {
drawTextWithShadow(matrices, textRenderer, new TranslatableText(translationPrefix + info.field.getName()), 12, y + 10, 0xFFFFFF);
if (info.error != null && info.error.getKey().isMouseOver(mouseX, mouseY))
renderTooltip(matrices, info.error.getValue(), mouseX, mouseY);
else if (mouseY >= y && mouseY < (y + 25)) {
if (info.dynamicTooltip != null) {
try {
renderTooltip(matrices, (List<Text>) info.dynamicTooltip.invoke(null, entries), mouseX, mouseY);
y += 25;
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
String key = translationPrefix + info.field.getName() + ".tooltip";
if (I18n.hasTranslation(key)) {
List<Text> list = new ArrayList<>();
for (String str : I18n.translate(key).split("\n"))
list.add(new LiteralText(str));
renderTooltip(matrices, list, mouseX, mouseY);
}
}
y += 25;
}
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Entry {
String dynamicTooltip() default "";
int width() default 100;
double min() default Double.MIN_NORMAL;
double max() default Double.MAX_VALUE;
}
}

View File

@@ -4,14 +4,13 @@ import eu.midnightdust.puzzle.screen.page.GraphicsPage;
import eu.midnightdust.puzzle.screen.page.MiscPage; import eu.midnightdust.puzzle.screen.page.MiscPage;
import eu.midnightdust.puzzle.screen.page.PerformancePage; import eu.midnightdust.puzzle.screen.page.PerformancePage;
import eu.midnightdust.puzzle.screen.page.TexturesPage; import eu.midnightdust.puzzle.screen.page.TexturesPage;
import net.coderbot.iris.gui.ShaderPackScreen; import net.coderbot.iris.gui.screen.ShaderPackScreen;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts; import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import org.apache.logging.log4j.LogManager;
import java.util.Objects; import java.util.Objects;
@@ -31,33 +30,19 @@ public class PuzzleOptionsScreen extends Screen {
PerformancePage performancePage = new PerformancePage(this); PerformancePage performancePage = new PerformancePage(this);
TexturesPage texturesPage = new TexturesPage(this); TexturesPage texturesPage = new TexturesPage(this);
this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 48 - 6, 150, 20, graphicsPage.getTitle().copy().append("..."), (button) -> { this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 48 - 6, 150, 20, graphicsPage.getTitle().copy().append("..."), (button) -> Objects.requireNonNull(client).openScreen(graphicsPage)));
Objects.requireNonNull(client).openScreen(graphicsPage); this.addButton(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 48 - 6, 150, 20, texturesPage.getTitle().copy().append("..."), (button) -> Objects.requireNonNull(client).openScreen(texturesPage)));
})); this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 72 - 6, 150, 20, performancePage.getTitle().copy().append("..."), (button) -> Objects.requireNonNull(client).openScreen(performancePage)));
this.addButton(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 48 - 6, 150, 20, texturesPage.getTitle().copy().append("..."), (button) -> { this.addButton(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 72 - 6, 150, 20, miscPage.getTitle().copy().append("..."), (button) -> Objects.requireNonNull(client).openScreen(miscPage)));
Objects.requireNonNull(client).openScreen(texturesPage);
}));
this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 72 - 6, 150, 20, performancePage.getTitle().copy().append("..."), (button) -> {
Objects.requireNonNull(client).openScreen(performancePage);
}));
this.addButton(new ButtonWidget(this.width / 2 + 5, this.height / 6 + 72 - 6, 150, 20, miscPage.getTitle().copy().append("..."), (button) -> {
Objects.requireNonNull(client).openScreen(miscPage);
}));
if (FabricLoader.getInstance().isModLoaded("iris")) { if (FabricLoader.getInstance().isModLoaded("iris")) {
try { try {
ShaderPackScreen shaderPackPage = new ShaderPackScreen(this); ShaderPackScreen shaderPackPage = new ShaderPackScreen(this);
this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 96 - 6, 150, 20, shaderPackPage.getTitle().copy().append("..."), (button) -> { this.addButton(new ButtonWidget(this.width / 2 - 155, this.height / 6 + 96 - 6, 150, 20, shaderPackPage.getTitle().copy().append("..."), (button) -> Objects.requireNonNull(client).openScreen(shaderPackPage)));
Objects.requireNonNull(client).openScreen(shaderPackPage);
}));
} }
catch (NoClassDefFoundError e) { catch (Exception | Error ignored) {
LogManager.getLogger("Puzzle").info("The shaderpack selection screen is not present, not adding it.");
} }
} }
this.addButton(new ButtonWidget(this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE, (button) -> Objects.requireNonNull(client).openScreen(parent)));
this.addButton(new ButtonWidget(this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE, (button) -> {
Objects.requireNonNull(client).openScreen(parent);
}));
} }
@Override @Override