From c3c547c6f950ebbc047cd31672f6a794483a9d0e Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 21 Jan 2022 10:33:11 +0200 Subject: Added separate project for defaults & set up building --- .../resources/assets/citresewn-defaults/logo.png | Bin 0 -> 99334 bytes .../resources/citresewn-defaults.accesswidener | 1 + .../main/resources/citresewn-defaults.mixins.json | 11 ++++++++ defaults/src/main/resources/fabric.mod.json | 30 +++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 defaults/src/main/resources/assets/citresewn-defaults/logo.png create mode 100644 defaults/src/main/resources/citresewn-defaults.accesswidener create mode 100644 defaults/src/main/resources/citresewn-defaults.mixins.json create mode 100644 defaults/src/main/resources/fabric.mod.json (limited to 'defaults/src/main') diff --git a/defaults/src/main/resources/assets/citresewn-defaults/logo.png b/defaults/src/main/resources/assets/citresewn-defaults/logo.png new file mode 100644 index 0000000..35859ac Binary files /dev/null and b/defaults/src/main/resources/assets/citresewn-defaults/logo.png differ diff --git a/defaults/src/main/resources/citresewn-defaults.accesswidener b/defaults/src/main/resources/citresewn-defaults.accesswidener new file mode 100644 index 0000000..3fde517 --- /dev/null +++ b/defaults/src/main/resources/citresewn-defaults.accesswidener @@ -0,0 +1 @@ +accessWidener v1 named diff --git a/defaults/src/main/resources/citresewn-defaults.mixins.json b/defaults/src/main/resources/citresewn-defaults.mixins.json new file mode 100644 index 0000000..b5c307d --- /dev/null +++ b/defaults/src/main/resources/citresewn-defaults.mixins.json @@ -0,0 +1,11 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "shcm.shsupercm.fabric.citresewn.defaults.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/defaults/src/main/resources/fabric.mod.json b/defaults/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..004b7a8 --- /dev/null +++ b/defaults/src/main/resources/fabric.mod.json @@ -0,0 +1,30 @@ +{ + "schemaVersion": 1, + "id": "citresewn-defaults", + "version": "${version}", + "name": "CIT Resewn - Defaults", + "description": "Default types and conditions for CIT Resewn", + "authors": [ + "SHsuperCM" + ], + "license": "MIT", + "icon": "assets/citresewn-defaults/logo.png", + + "environment": "client", + "entrypoints": { + }, + "accessWidener" : "citresewn-defaults.accesswidener", + "mixins": [ + "citresewn-defaults.mixins.json" + ], + "custom": { + "modmenu": { + "parent": { + "id": "citresewn" + } + } + }, + "depends": { + "citresewn": "${version}" + } +} -- cgit From c48e23a8949eaa89d06908201357b53fa7eaedbe Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 21 Jan 2022 15:50:24 +0200 Subject: Added config/screen for defaults --- .../defaults/config/CITResewnDefaultsConfig.java | 49 ++++++++++++++++++ .../CITResewnDefaultsConfigScreenFactory.java | 27 ++++++++++ .../defaults/config/CITResewnDefaultsModMenu.java | 25 +++++++++ .../main/java/shcm/shsupercm/util/logic/Loops.java | 60 ++++++++++++++++++++++ .../assets/citresewn-defaults/lang/en_us.json | 4 ++ defaults/src/main/resources/fabric.mod.json | 8 ++- .../config/CITResewnConfigScreenFactory.java | 21 +++++++- .../fabric/citresewn/config/CITResewnModMenu.java | 9 +--- src/main/java/shcm/shsupercm/util/logic/Loops.java | 60 ---------------------- .../resources/assets/citresewn/lang/en_us.json | 4 +- 10 files changed, 194 insertions(+), 73 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java create mode 100644 defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java create mode 100644 defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json delete mode 100644 src/main/java/shcm/shsupercm/util/logic/Loops.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java new file mode 100644 index 0000000..0787fba --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java @@ -0,0 +1,49 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.CITResewn; + +import java.io.*; + +public class CITResewnDefaultsConfig { + + + private static final File FILE = new File("config/citresewn-defaults.json"); + + public static final CITResewnDefaultsConfig INSTANCE = read(); + + public static CITResewnDefaultsConfig read() { + if (!FILE.exists()) + return new CITResewnDefaultsConfig().write(); + + Reader reader = null; + try { + return new Gson().fromJson(reader = new FileReader(FILE), CITResewnDefaultsConfig.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(reader); + } + } + + public CITResewnDefaultsConfig write() { + Gson gson = new Gson(); + JsonWriter writer = null; + try { + writer = gson.newJsonWriter(new FileWriter(FILE)); + writer.setIndent(" "); + + gson.toJson(gson.toJsonTree(this, CITResewnDefaultsConfig.class), writer); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't save defaults config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(writer); + } + return this; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java new file mode 100644 index 0000000..889c465 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java @@ -0,0 +1,27 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; + +public class CITResewnDefaultsConfigScreenFactory { + public static Screen create(Screen parent) { + CITResewnDefaultsConfig currentConfig = CITResewnDefaultsConfig.INSTANCE, defaultConfig = new CITResewnDefaultsConfig(); + + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(parent) + .setTitle(new TranslatableText("config.citresewn.defaults.title")) + .setSavingRunnable(currentConfig::write); + + ConfigCategory category = builder.getOrCreateCategory(new LiteralText("")); + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + + + return builder.build(); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java new file mode 100644 index 0000000..cba1592 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java @@ -0,0 +1,25 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.NoticeScreen; +import net.minecraft.text.Text; + +public class CITResewnDefaultsModMenu implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + if (FabricLoader.getInstance().isModLoaded("cloth-config2")) + return new ClothConfigOpenImpl().getModConfigScreenFactory(); + + return parent -> new NoticeScreen(() -> MinecraftClient.getInstance().setScreen(parent), Text.of("CIT Resewn: Defaults"), Text.of("CIT Resewn requires Cloth Config to be able to show the config.")); + } + + private static class ClothConfigOpenImpl implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + return CITResewnDefaultsConfigScreenFactory::create; + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java b/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java new file mode 100644 index 0000000..6c18244 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java @@ -0,0 +1,60 @@ +package shcm.shsupercm.util.logic; + +import java.util.*; + +/** + * This class(or class portion) is a part of SHCM's utilities. Feel free to use without credit. + */ +public class Loops { + /** + * Creates a loop of T with linked intensities allowing for fading between the elements. + * @param items list of items and pause durations(in time units) ordered as they are in the loop + * @param fade time in units to fade between each item + * @param ticks positive raising counter + * @param tpu the amount of ticks per time unit + * @param element type + * @return map of elements and their respective intensities(between 0.0f and 1.0f) + */ + public static Map statelessFadingLoop(List> items, float fade, int ticks, int tpu) { + Map itemValues = new HashMap<>(); + + if (items == null || items.size() == 0) + return itemValues; + + if (items.size() == 1) { + itemValues.put(items.get(0).getKey(), 1f); + return itemValues; + } + + float totalUnitsInLoop = 0f; + for (Map.Entry item : items) { + itemValues.put(item.getKey(), 0f); + totalUnitsInLoop += item.getValue() + fade; + } + + float unitInLoop = (ticks % (tpu * totalUnitsInLoop)) / tpu; + + for (int i = 0; i < items.size(); i++) { + Map.Entry item = items.get(i); + if (unitInLoop < item.getValue()) { + itemValues.put(item.getKey(), 1f); + break; + } else + unitInLoop -= item.getValue(); + + if (unitInLoop < fade) { + Map.Entry nextItem = items.get(i + 1 >= items.size() ? 0 : i + 1); + + unitInLoop /= fade; + + itemValues.put(item.getKey(), 1f - unitInLoop); + itemValues.put(nextItem.getKey(), unitInLoop); + + break; + } else + unitInLoop -= fade; + } + + return itemValues; + } +} \ No newline at end of file diff --git a/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json new file mode 100644 index 0000000..72784ed --- /dev/null +++ b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "config.citresewn.defaults.title": "CIT Resewn: Defaults", + "config.citresewn.defaults.tooltip": "Go to the defaults' config menu" +} \ No newline at end of file diff --git a/defaults/src/main/resources/fabric.mod.json b/defaults/src/main/resources/fabric.mod.json index 004b7a8..0f8e566 100644 --- a/defaults/src/main/resources/fabric.mod.json +++ b/defaults/src/main/resources/fabric.mod.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "citresewn-defaults", "version": "${version}", - "name": "CIT Resewn - Defaults", + "name": "CIT Resewn: Defaults", "description": "Default types and conditions for CIT Resewn", "authors": [ "SHsuperCM" @@ -12,6 +12,12 @@ "environment": "client", "entrypoints": { + "modmenu": [ + "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsModMenu" + ], + "citresewn-defaults:config_screen": [ + "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsConfigScreenFactory::create" + ] }, "accessWidener" : "citresewn-defaults.accesswidener", "mixins": [ diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java index 33f4950..57ae2c3 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -3,12 +3,16 @@ package shcm.shsupercm.fabric.citresewn.config; import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.entrypoint.EntrypointContainer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import java.util.function.Function; + public class CITResewnConfigScreenFactory { public static Screen create(Screen parent) { CITResewnConfig currentConfig = CITResewnConfig.INSTANCE, defaultConfig = new CITResewnConfig(); @@ -32,6 +36,21 @@ public class CITResewnConfigScreenFactory { .setDefaultValue(defaultConfig.enabled) .build()); + class CurrentScreen { Screen screen; boolean prevToggle = false; } final CurrentScreen currentScreen = new CurrentScreen(); + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.defaults.title"), false) + .setTooltip(new TranslatableText("config.citresewn.defaults.tooltip")) + .setYesNoTextSupplier((b) -> { + if (b != currentScreen.prevToggle) { + //noinspection unchecked + MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints("citresewn-defaults:config_screen", Function.class).stream().findAny().orElseThrow().apply(currentScreen.screen)); + + currentScreen.prevToggle = b; + } + + return new TranslatableText("config.citresewn.configure"); + }) + .build()); + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.mute_errors.title"), currentConfig.mute_errors) .setTooltip(new TranslatableText("config.citresewn.mute_errors.tooltip")) .setSaveConsumer(newConfig -> currentConfig.mute_errors = newConfig) @@ -70,6 +89,6 @@ public class CITResewnConfigScreenFactory { .requireRestart() .build()); - return builder.build(); + return currentScreen.screen = builder.build(); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java index cf6d9bd..378350a 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java @@ -11,15 +11,8 @@ public class CITResewnModMenu implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { if (FabricLoader.getInstance().isModLoaded("cloth-config2")) - return new ClothConfigOpenImpl().getModConfigScreenFactory(); + return CITResewnConfigScreenFactory::create; return parent -> new NoticeScreen(() -> MinecraftClient.getInstance().setScreen(parent), Text.of("CIT Resewn"), Text.of("CIT Resewn requires Cloth Config to be able to show the config.")); } - - private static class ClothConfigOpenImpl implements ModMenuApi { - @Override - public ConfigScreenFactory getModConfigScreenFactory() { - return CITResewnConfigScreenFactory::create; - } - } } diff --git a/src/main/java/shcm/shsupercm/util/logic/Loops.java b/src/main/java/shcm/shsupercm/util/logic/Loops.java deleted file mode 100644 index 6c18244..0000000 --- a/src/main/java/shcm/shsupercm/util/logic/Loops.java +++ /dev/null @@ -1,60 +0,0 @@ -package shcm.shsupercm.util.logic; - -import java.util.*; - -/** - * This class(or class portion) is a part of SHCM's utilities. Feel free to use without credit. - */ -public class Loops { - /** - * Creates a loop of T with linked intensities allowing for fading between the elements. - * @param items list of items and pause durations(in time units) ordered as they are in the loop - * @param fade time in units to fade between each item - * @param ticks positive raising counter - * @param tpu the amount of ticks per time unit - * @param element type - * @return map of elements and their respective intensities(between 0.0f and 1.0f) - */ - public static Map statelessFadingLoop(List> items, float fade, int ticks, int tpu) { - Map itemValues = new HashMap<>(); - - if (items == null || items.size() == 0) - return itemValues; - - if (items.size() == 1) { - itemValues.put(items.get(0).getKey(), 1f); - return itemValues; - } - - float totalUnitsInLoop = 0f; - for (Map.Entry item : items) { - itemValues.put(item.getKey(), 0f); - totalUnitsInLoop += item.getValue() + fade; - } - - float unitInLoop = (ticks % (tpu * totalUnitsInLoop)) / tpu; - - for (int i = 0; i < items.size(); i++) { - Map.Entry item = items.get(i); - if (unitInLoop < item.getValue()) { - itemValues.put(item.getKey(), 1f); - break; - } else - unitInLoop -= item.getValue(); - - if (unitInLoop < fade) { - Map.Entry nextItem = items.get(i + 1 >= items.size() ? 0 : i + 1); - - unitInLoop /= fade; - - itemValues.put(item.getKey(), 1f - unitInLoop); - itemValues.put(nextItem.getKey(), unitInLoop); - - break; - } else - unitInLoop -= fade; - } - - return itemValues; - } -} \ No newline at end of file diff --git a/src/main/resources/assets/citresewn/lang/en_us.json b/src/main/resources/assets/citresewn/lang/en_us.json index 2f90a92..3164cb5 100644 --- a/src/main/resources/assets/citresewn/lang/en_us.json +++ b/src/main/resources/assets/citresewn/lang/en_us.json @@ -1,5 +1,6 @@ { "config.citresewn.title": "CIT Resewn Configuration", + "config.citresewn.configure": "Configure", "config.citresewn.enabled.title": "Enabled", "config.citresewn.enabled.tooltip": "Should CIT Resewn load and inject cits from resourcepacks", @@ -10,9 +11,6 @@ "config.citresewn.mute_warns.title": "Mute loading warnings", "config.citresewn.mute_warns.tooltip": "Should CIT Resewn not log cit warnings", - "config.citresewn.citenchantment_scroll_multiplier.title": "CIT Enchantment: Scroll Multiplier", - "config.citresewn.citenchantment_scroll_multiplier.tooltip": "General multiplier for the scroll speed of the glint textures added by enchantment CITs.", - "config.citresewn.broken_paths.title": "Allow broken paths in resourcepacks", "config.citresewn.broken_paths.tooltip": "Allows packs with illegal resource paths to load.\nMay cause issues!", -- cgit From 818f912fc038b84a3dd855892d619af3325bc163 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 21 Jan 2022 18:22:27 +0200 Subject: Improved mod metadata --- defaults/src/main/resources/fabric.mod.json | 8 ++++++++ src/main/resources/fabric.mod.json | 14 +++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/resources/fabric.mod.json b/defaults/src/main/resources/fabric.mod.json index 0f8e566..c140556 100644 --- a/defaults/src/main/resources/fabric.mod.json +++ b/defaults/src/main/resources/fabric.mod.json @@ -9,6 +9,11 @@ ], "license": "MIT", "icon": "assets/citresewn-defaults/logo.png", + "contact": { + "homepage": "https://citresewn.shcm.io/", + "sources": "https://github.com/SHsuperCM/CITResewn", + "issues": "https://github.com/SHsuperCM/CITResewn/issues" + }, "environment": "client", "entrypoints": { @@ -32,5 +37,8 @@ }, "depends": { "citresewn": "${version}" + }, + "conflicts": { + "eatinganimationid": "*" } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 3ab2b9b..b00baa4 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -2,13 +2,18 @@ "schemaVersion": 1, "id": "citresewn", "version": "${version}", - "name": "CITResewn", + "name": "CIT Resewn", "description": "Re-implements MCPatcher's CIT", "authors": [ "SHsuperCM" ], "license": "MIT", "icon": "assets/citresewn/logo.png", + "contact": { + "homepage": "https://citresewn.shcm.io/", + "sources": "https://github.com/SHsuperCM/CITResewn", + "issues": "https://github.com/SHsuperCM/CITResewn/issues" + }, "environment": "client", "entrypoints": { @@ -27,5 +32,12 @@ "fabric": "*", "fabricloader": "*", "minecraft": "1.18.x" + }, + "recommends": { + "modmenu": "*", + "cloth-config2": "*" + }, + "breaks": { + "canvas": "*" } } -- cgit From 1a9e72178881ca41321c730eb69ce28f0b6ad171 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 5 Feb 2022 07:46:52 +0200 Subject: Migrated to using Fletching Table --- build.gradle | 11 ++++++++++- .../defaults/config/CITResewnDefaultsConfigScreenFactory.java | 3 +++ .../citresewn/defaults/config/CITResewnDefaultsModMenu.java | 2 ++ defaults/src/main/resources/citresewn-defaults.mixins.json | 2 -- defaults/src/main/resources/fabric.mod.json | 8 -------- gradle.properties | 4 ++-- src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java | 8 ++++---- .../fabric/citresewn/config/CITResewnConfigScreenFactory.java | 4 +++- .../shsupercm/fabric/citresewn/config/CITResewnModMenu.java | 2 ++ .../java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java | 2 +- .../citresewn/registry/api/GlobalPropertiesHandler.java | 4 +++- .../java/shcm/shsupercm/fabric/citresewn/util/Disposable.java | 2 ++ src/main/resources/citresewn.mixins.json | 3 --- src/main/resources/fabric.mod.json | 8 -------- 14 files changed, 32 insertions(+), 31 deletions(-) (limited to 'defaults/src/main') diff --git a/build.gradle b/build.gradle index 64a0be9..5774182 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ plugins { - id 'fabric-loom' version '0.10-SNAPSHOT' apply false + id 'fabric-loom' version '0.11-SNAPSHOT' apply false + id 'io.shcm.shsupercm.fabric.fletchingtable' version '1.4' apply false id 'maven-publish' } @@ -8,6 +9,7 @@ group = project.maven_group allprojects { apply plugin: "fabric-loom" + apply plugin: "io.shcm.shsupercm.fabric.fletchingtable" repositories { maven { url "https://maven.shedaniel.me/" } @@ -33,6 +35,13 @@ allprojects { } modCompileOnly("curse.maven:cosmeticarmor-436659:${project.cosmetic_armor}") + + + fletchingTable.shutUpDrasil18() + } + + fletchingTable { + defaultMixinEnvironment = "auto" } processResources { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java index 889c465..6d89bc6 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn.defaults.config; +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; @@ -7,8 +8,10 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfigScreenFactory; public class CITResewnDefaultsConfigScreenFactory { + @Entrypoint(CITResewnConfigScreenFactory.DEFAULTS_CONFIG_ENTRYPOINT) public static Screen create(Screen parent) { CITResewnDefaultsConfig currentConfig = CITResewnDefaultsConfig.INSTANCE, defaultConfig = new CITResewnDefaultsConfig(); diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java index cba1592..48edf33 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java @@ -2,11 +2,13 @@ package shcm.shsupercm.fabric.citresewn.defaults.config; import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.NoticeScreen; import net.minecraft.text.Text; +@Entrypoint("modmenu") public class CITResewnDefaultsModMenu implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { diff --git a/defaults/src/main/resources/citresewn-defaults.mixins.json b/defaults/src/main/resources/citresewn-defaults.mixins.json index b5c307d..536380b 100644 --- a/defaults/src/main/resources/citresewn-defaults.mixins.json +++ b/defaults/src/main/resources/citresewn-defaults.mixins.json @@ -3,8 +3,6 @@ "minVersion": "0.8", "package": "shcm.shsupercm.fabric.citresewn.defaults.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [ - ], "injectors": { "defaultRequire": 1 } diff --git a/defaults/src/main/resources/fabric.mod.json b/defaults/src/main/resources/fabric.mod.json index c140556..f35e7bb 100644 --- a/defaults/src/main/resources/fabric.mod.json +++ b/defaults/src/main/resources/fabric.mod.json @@ -16,14 +16,6 @@ }, "environment": "client", - "entrypoints": { - "modmenu": [ - "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsModMenu" - ], - "citresewn-defaults:config_screen": [ - "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsConfigScreenFactory::create" - ] - }, "accessWidener" : "citresewn-defaults.accesswidener", "mixins": [ "citresewn-defaults.mixins.json" diff --git a/gradle.properties b/gradle.properties index bc89793..ebfce68 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,10 +5,10 @@ org.gradle.jvmargs=-Xmx1G # check these on https://modmuss50.me/fabric.html minecraft_version=1.18.1 minecraft_version_compat=1.18.x -yarn_mappings=1.18.1+build.18 +yarn_mappings=1.18.1+build.22 loader_version=0.12.12 -fabric_api=0.45.0+1.18 +fabric_api=0.46.4+1.18 modmenu=3.0.1 cloth=6.1.48 cosmetic_armor=3544655 diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index 0a0051f..807aa97 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn; +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.fabricmc.api.ClientModInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -7,13 +8,12 @@ import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; public class CITResewn implements ClientModInitializer { public static final Logger LOG = LogManager.getLogger("CITResewn"); - public static CITResewn INSTANCE; - - public boolean processingBrokenPaths = false; + @Entrypoint(Entrypoint.CLIENT) + public static final CITResewn INSTANCE = new CITResewn(); @Override public void onInitializeClient() { - INSTANCE = this; + info("init"); } public static void info(String message) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java index 57ae2c3..76576ba 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -14,6 +14,8 @@ import net.minecraft.util.Formatting; import java.util.function.Function; public class CITResewnConfigScreenFactory { + public static final String DEFAULTS_CONFIG_ENTRYPOINT = "citresewn-defaults:config_screen"; + public static Screen create(Screen parent) { CITResewnConfig currentConfig = CITResewnConfig.INSTANCE, defaultConfig = new CITResewnConfig(); @@ -42,7 +44,7 @@ public class CITResewnConfigScreenFactory { .setYesNoTextSupplier((b) -> { if (b != currentScreen.prevToggle) { //noinspection unchecked - MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints("citresewn-defaults:config_screen", Function.class).stream().findAny().orElseThrow().apply(currentScreen.screen)); + MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints(DEFAULTS_CONFIG_ENTRYPOINT, Function.class).stream().findAny().orElseThrow().apply(currentScreen.screen)); currentScreen.prevToggle = b; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java index 378350a..521c3b6 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java @@ -2,11 +2,13 @@ package shcm.shsupercm.fabric.citresewn.config; import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.NoticeScreen; import net.minecraft.text.Text; +@Entrypoint("modmenu") public class CITResewnModMenu implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java index 8e5faa3..5523c84 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java @@ -37,7 +37,7 @@ public class ActiveCITs implements Disposable { private ActiveCITs() {} @Override public void dispose() { - for (Disposable disposable : FabricLoader.getInstance().getEntrypoints("citresewn:dispose", Disposable.class)) + for (Disposable disposable : FabricLoader.getInstance().getEntrypoints(Disposable.ENTRYPOINT, Disposable.class)) disposable.dispose(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/registry/api/GlobalPropertiesHandler.java b/src/main/java/shcm/shsupercm/fabric/citresewn/registry/api/GlobalPropertiesHandler.java index e5a2820..9b1ad6f 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/registry/api/GlobalPropertiesHandler.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/registry/api/GlobalPropertiesHandler.java @@ -4,5 +4,7 @@ import shcm.shsupercm.fabric.citresewn.format.PropertyValue; @FunctionalInterface public interface GlobalPropertiesHandler { - void globalProperty(String key, PropertyValue value); + String ENTRYPOINT = "citresewn:global_property"; + + boolean globalProperty(String key, PropertyValue value); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/util/Disposable.java b/src/main/java/shcm/shsupercm/fabric/citresewn/util/Disposable.java index 84c65b2..7878e16 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/util/Disposable.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/util/Disposable.java @@ -2,5 +2,7 @@ package shcm.shsupercm.fabric.citresewn.util; @FunctionalInterface public interface Disposable { + String ENTRYPOINT = "citresewn:dispose"; + void dispose(); } diff --git a/src/main/resources/citresewn.mixins.json b/src/main/resources/citresewn.mixins.json index 39343fb..394941e 100644 --- a/src/main/resources/citresewn.mixins.json +++ b/src/main/resources/citresewn.mixins.json @@ -3,9 +3,6 @@ "minVersion": "0.8", "package": "shcm.shsupercm.fabric.citresewn.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [ - "ModelLoaderMixin" - ], "injectors": { "defaultRequire": 1 } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index b00baa4..a8e2234 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -16,14 +16,6 @@ }, "environment": "client", - "entrypoints": { - "client": [ - "shcm.shsupercm.fabric.citresewn.CITResewn" - ], - "modmenu": [ - "shcm.shsupercm.fabric.citresewn.config.CITResewnModMenu" - ] - }, "accessWidener" : "citresewn.accesswidener", "mixins": [ "citresewn.mixins.json" -- cgit From 2f39cd8e2ad0d1009220bf83e50e8e4c6e885f36 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 12 Feb 2022 09:22:01 +0200 Subject: Created item type to test type api --- .../fabric/citresewn/defaults/types/TypeItem.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java new file mode 100644 index 0000000..c8ef04b --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java @@ -0,0 +1,28 @@ +package shcm.shsupercm.fabric.citresewn.defaults.types; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; + +import java.util.Collection; + +public class TypeItem extends CITType { + @Entrypoint(CITTypeContainer.ENTRYPOINT) + public static final Container CONTAINER = new Container(); + + public static class Container extends CITTypeContainer { + public Container() { + super(TypeItem.class, TypeItem::new, "item"); + } + + @Override + public void load(Collection parsedCITs) { + } + + @Override + public void dispose() { + + } + } +} \ No newline at end of file -- cgit From 3ca2dde4a8e70bd3f8f11b5d7dbad9ddbc9792af Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 12 Feb 2022 14:28:07 +0200 Subject: Created condition container api/parsing and fixed some stuff --- .../fabric/citresewn/defaults/types/TypeItem.java | 10 ++++ .../citresewn/api/CITConditionContainer.java | 18 +++++++ .../fabric/citresewn/pack/cit/CITCondition.java | 5 +- .../fabric/citresewn/pack/cit/CITRegistry.java | 61 ++++++++++++++++++++-- .../fabric/citresewn/pack/cit/CITType.java | 8 +-- 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java index c8ef04b..ec2491f 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java @@ -2,15 +2,25 @@ package shcm.shsupercm.fabric.citresewn.defaults.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import java.util.Collection; +import java.util.List; public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); + @Override + public void load(List conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + + } + public static class Container extends CITTypeContainer { public Container() { super(TypeItem.class, TypeItem::new, "item"); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java new file mode 100644 index 0000000..d307b1a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java @@ -0,0 +1,18 @@ +package shcm.shsupercm.fabric.citresewn.api; + +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; + +import java.util.function.Supplier; + +public abstract class CITConditionContainer { + public static final String ENTRYPOINT = "citresewn:condition"; + public final Class condition; + public final Supplier createCondition; + public final String[] aliases; + + protected CITConditionContainer(Class condition, Supplier createCondition, String... aliases) { + this.condition = condition; + this.createCondition = createCondition; + this.aliases = aliases; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java index e117133..f05d4d1 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java @@ -1,12 +1,13 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.Collections; import java.util.Set; public abstract class CITCondition { - public abstract void load(String keyMetadata, PropertyValue value) throws Exception; + public abstract void load(PropertyValue value) throws CITParsingException; public Set> acceptedTypes() { return null; @@ -16,7 +17,7 @@ public abstract class CITCondition { return Collections.emptySet(); } - public T modifySibling(T sibling) { + public T modifySibling(Class siblingType, T sibling) { return sibling; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java index 9dd423b..2f34de3 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java @@ -2,21 +2,35 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.util.Identifier; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import static shcm.shsupercm.fabric.citresewn.CITResewn.info; +import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading; public class CITRegistry { private static final Map> TYPES = new HashMap<>(); + private static final Map> CONDITIONS = new HashMap<>(); public static void registerAll() { + info("Registering CIT Conditions"); + for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) { + String namespace = entrypointContainer.getProvider().getMetadata().getId(); + if (namespace.equals("citresewn-defaults")) + namespace = "citresewn"; + + for (String alias : entrypointContainer.getEntrypoint().aliases) + CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer) entrypointContainer.getEntrypoint()); + } + info("Registering CIT Types"); for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) { String namespace = entrypointContainer.getProvider().getMetadata().getId(); @@ -27,7 +41,46 @@ public class CITRegistry { } } - public static CITTypeContainer parseType(PropertyGroup properties) throws CITParsingException { + public static CIT load(PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + CITType citType = parseType(properties); + + ArrayList conditions = new ArrayList<>(); + + for (Map.Entry> entry : properties.properties.entrySet()) { + if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) + continue; + + for (PropertyValue value : entry.getValue()) + conditions.add(parseCondition(properties, entry.getKey(), value)); + } + + for (CITCondition condition : new ArrayList<>(conditions)) + for (Class siblingConditionType : condition.siblingConditions()) + conditions.replaceAll( + siblingCondition -> siblingConditionType == siblingCondition.getClass() ? + condition.modifySibling(siblingConditionType, siblingCondition) : + siblingCondition); + + conditions.removeIf(Objects::isNull); + + citType.load(conditions, properties, globalProperties); + + return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0])); + } + + public static CITCondition parseCondition(PropertyGroup properties, PropertyKey key, PropertyValue value) throws CITParsingException { + CITConditionContainer conditionContainer = CONDITIONS.get(key); + if (conditionContainer == null) { + logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); + return null; + } + + CITCondition condition = conditionContainer.createCondition.get(); + condition.load(value); + return condition; + } + + public static CITType parseType(PropertyGroup properties) throws CITParsingException { Identifier type = new Identifier("citresewn", "item"); PropertyValue propertiesType = properties.getLast("citresewn", "type"); @@ -43,6 +96,6 @@ public class CITRegistry { // assert (propertiesType != null) because the default citresewn:item should always be registered throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position()); - return typeContainer; + return typeContainer.createType.get(); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java index 557a34c..bbfa0e9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java @@ -1,9 +1,11 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; + import java.util.List; public abstract class CITType { - public List modifyConditions(List conditions) { - return conditions; - } + public abstract void load(List conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; } -- cgit From f681f4f8811881b6a7b349f3b215c74a27a7ed4d Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 12 Feb 2022 18:26:07 +0200 Subject: Implemented CIT parsing/loading --- .../fabric/citresewn/defaults/types/TypeItem.java | 6 +- settings.gradle | 2 +- .../fabric/citresewn/api/CITTypeContainer.java | 4 +- .../fabric/citresewn/builtin/WeightCondition.java | 3 +- .../fabric/citresewn/pack/PackParser.java | 71 ++++++++++++++++++++-- .../fabric/citresewn/pack/cit/ActiveCITs.java | 13 ++++ .../fabric/citresewn/pack/cit/CITCondition.java | 7 +-- .../fabric/citresewn/pack/cit/CITRegistry.java | 44 +++----------- .../fabric/citresewn/pack/cit/CITType.java | 3 +- 9 files changed, 95 insertions(+), 58 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java index ec2491f..855b90e 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java @@ -3,13 +3,11 @@ package shcm.shsupercm.fabric.citresewn.defaults.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import java.util.Collection; import java.util.List; public class TypeItem extends CITType { @@ -17,7 +15,7 @@ public class TypeItem extends CITType { public static final Container CONTAINER = new Container(); @Override - public void load(List conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + public void load(List conditions, PropertyGroup properties) throws CITParsingException { } @@ -27,7 +25,7 @@ public class TypeItem extends CITType { } @Override - public void load(Collection parsedCITs) { + public void load(List parsedCITs) { } @Override diff --git a/settings.gradle b/settings.gradle index 7a1d91c..4ab5170 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,4 +8,4 @@ pluginManagement { } } -include 'defaults', 'srcOld' \ No newline at end of file +include 'defaults' \ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java index d691ab2..e207d00 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.api; import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; -import java.util.Collection; +import java.util.List; import java.util.function.Supplier; public abstract class CITTypeContainer implements CITDisposable { @@ -18,5 +18,5 @@ public abstract class CITTypeContainer implements CITDisposab this.id = id; } - public abstract void load(Collection parsedCITs); + public abstract void load(List parsedCITs); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java index 9882d7b..f258df4 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java @@ -3,7 +3,6 @@ package shcm.shsupercm.fabric.citresewn.builtin; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; @@ -16,7 +15,7 @@ public class WeightCondition extends CITCondition { public int weight = 0; @Override - public void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { this.weight = parseInteger(value, properties); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 4945120..c82b505 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -2,16 +2,25 @@ package shcm.shsupercm.fabric.citresewn.pack; import net.fabricmc.fabric.impl.resource.loader.GroupResourcePack; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceNotFoundException; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourceType; +import net.minecraft.resource.*; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.mixin.GroupResourcePackAccessor; +import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITRegistry; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -47,4 +56,58 @@ public class PackParser { } }); } + + public static List loadCITs(ResourceManager resourceManager) { + List cits = new ArrayList<>(); + + for (String root : ROOTS) + for (Identifier identifier : resourceManager.findResources(root + "/cit", s -> s.endsWith(".properties"))) { + String packName = null; + try (Resource resource = resourceManager.getResource(identifier)) { + cits.add(parseCIT(PropertyGroup.tryParseGroup(packName = resource.getResourcePackName(), identifier, resource.getInputStream()))); + } catch (Exception e) { + CITResewn.logErrorLoading("Errored while loading cit: " + identifier + (packName == null ? "" : " from " + packName)); + e.printStackTrace(); + } + } + + return cits; + } + + + public static CIT parseCIT(PropertyGroup properties) throws CITParsingException { + CITType citType = CITRegistry.parseType(properties); + + ArrayList conditions = new ArrayList<>(); + + for (Map.Entry> entry : properties.properties.entrySet()) { + if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) + continue; + + for (PropertyValue value : entry.getValue()) + conditions.add(CITRegistry.parseCondition(entry.getKey(), value, properties)); + } + + for (CITCondition condition : new ArrayList<>(conditions)) + for (Class siblingConditionType : condition.siblingConditions()) + conditions.replaceAll( + siblingCondition -> siblingConditionType == siblingCondition.getClass() ? + condition.modifySibling(siblingConditionType, siblingCondition) : + siblingCondition); + + WeightCondition weight = new WeightCondition(); + + conditions.removeIf(condition -> { + if (condition instanceof WeightCondition weightCondition) { + weight.weight = weightCondition.weight; + return true; + } + + return condition == null; + }); + + citType.load(conditions, properties); + + return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java index 62f011e..afc228c 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java @@ -7,6 +7,8 @@ import shcm.shsupercm.fabric.citresewn.api.CITDisposable; import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.PackParser; +import java.util.*; + public class ActiveCITs implements CITDisposable { private ActiveCITs() {} private static ActiveCITs active = null; @@ -20,6 +22,8 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} public final GlobalProperties globalProperties = new GlobalProperties(); + public final Map, List> cits = new IdentityHashMap<>(); + public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) { profiler.push("citresewn:disposing"); if (active != null) { @@ -32,6 +36,15 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} profiler.swap("citresewn:load_global_properties"); PackParser.loadGlobalProperties(resourceManager, active.globalProperties); active.globalProperties.callHandlers(); + + profiler.swap("citresewn:load_cits"); + for (CIT cit : PackParser.loadCITs(resourceManager)) + active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit); + for (Map.Entry, List> entry : active.cits.entrySet()) { + entry.getValue().sort(Comparator.comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); + CITRegistry.load(entry.getKey(), entry.getValue()); + } + profiler.pop(); return ActiveCITs.active = active; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java index 2de5be1..b5aa315 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java @@ -1,7 +1,6 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -9,11 +8,7 @@ import java.util.Collections; import java.util.Set; public abstract class CITCondition { - public abstract void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; - - public Set> acceptedTypes() { - return null; - } + public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException; public Set> siblingConditions() { return Collections.emptySet(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java index fe0bcc7..e24e598 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java @@ -4,10 +4,8 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -42,43 +40,15 @@ public class CITRegistry { } } - public static CIT load(PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { - CITType citType = parseType(properties); - - ArrayList conditions = new ArrayList<>(); - - for (Map.Entry> entry : properties.properties.entrySet()) { - if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) - continue; - - for (PropertyValue value : entry.getValue()) - conditions.add(parseCondition(entry.getKey(), value, properties, globalProperties)); - } - - for (CITCondition condition : new ArrayList<>(conditions)) - for (Class siblingConditionType : condition.siblingConditions()) - conditions.replaceAll( - siblingCondition -> siblingConditionType == siblingCondition.getClass() ? - condition.modifySibling(siblingConditionType, siblingCondition) : - siblingCondition); - - WeightCondition weight = new WeightCondition(); - - conditions.removeIf(condition -> { - if (condition instanceof WeightCondition weightCondition) { - weight.weight = weightCondition.weight; - return true; + public static void load(Class type, List cits) { + for (CITTypeContainer typeContainer : TYPES.values()) + if (typeContainer.type == type) { + typeContainer.load(cits); + break; } - - return condition == null; - }); - - citType.load(conditions, properties, globalProperties); - - return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); } - public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { CITConditionContainer conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); @@ -86,7 +56,7 @@ public class CITRegistry { } CITCondition condition = conditionContainer.createCondition.get(); - condition.load(value, properties, globalProperties); + condition.load(value, properties); return condition; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java index bbfa0e9..4504c01 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java @@ -1,11 +1,10 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import java.util.List; public abstract class CITType { - public abstract void load(List conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; + public abstract void load(List conditions, PropertyGroup properties) throws CITParsingException; } -- cgit From a48789c45fb474a953df65e342a3bad27fd89870 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 07:20:35 +0200 Subject: Implemented items condition --- .../defaults/cit/conditions/ConditionItems.java | 54 ++++++++++++++++++++++ .../citresewn/defaults/cit/types/TypeItem.java | 36 +++++++++++++++ .../fabric/citresewn/defaults/types/TypeItem.java | 36 --------------- .../fabric/citresewn/builtin/WeightCondition.java | 3 +- .../fabric/citresewn/pack/cit/CITCondition.java | 5 ++ 5 files changed, 97 insertions(+), 37 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java delete mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java new file mode 100644 index 0000000..e010a58 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java @@ -0,0 +1,54 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.item.Item; +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; +import net.minecraft.util.registry.Registry; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.LinkedHashSet; +import java.util.Set; + +public class ConditionItems extends CITCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionItems.class, ConditionItems::new, + "items", "matchItems"); + + public Item[] items = new Item[0]; + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + Set items = new LinkedHashSet<>(); + + for (String entry : value.value().split(" ")) + if (!entry.isEmpty()) + try { + Identifier identifier = new Identifier(entry); + if (Registry.ITEM.containsId(identifier)) + items.add(Registry.ITEM.get(identifier)); + else { + items.add(null); + warn(identifier + " is not in the item registry", value, properties); + } + } catch (InvalidIdentifierException e) { + throw new CITParsingException("Invalid item identifier \"" + entry + "\"", properties, value.position()); + } + + this.items = items.toArray(new Item[0]); + } + + @Override + public boolean test(CITContext context) { + for (Item item : this.items) + if (context.stack.getItem() == item) + return true; + + return false; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java new file mode 100644 index 0000000..58460aa --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -0,0 +1,36 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.types; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; + +import java.util.List; + +public class TypeItem extends CITType { + @Entrypoint(CITTypeContainer.ENTRYPOINT) + public static final Container CONTAINER = new Container(); + + @Override + public void load(List conditions, PropertyGroup properties) throws CITParsingException { + + } + + public static class Container extends CITTypeContainer { + public Container() { + super(TypeItem.class, TypeItem::new, "item"); + } + + @Override + public void load(List parsedCITs) { + } + + @Override + public void dispose() { + + } + } +} \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java deleted file mode 100644 index 855b90e..0000000 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/types/TypeItem.java +++ /dev/null @@ -1,36 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.defaults.types; - -import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; -import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; - -import java.util.List; - -public class TypeItem extends CITType { - @Entrypoint(CITTypeContainer.ENTRYPOINT) - public static final Container CONTAINER = new Container(); - - @Override - public void load(List conditions, PropertyGroup properties) throws CITParsingException { - - } - - public static class Container extends CITTypeContainer { - public Container() { - super(TypeItem.class, TypeItem::new, "item"); - } - - @Override - public void load(List parsedCITs) { - } - - @Override - public void dispose() { - - } - } -} \ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java index f258df4..4161a55 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java @@ -10,7 +10,8 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; public class WeightCondition extends CITCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) - public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, "weight"); + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, + "weight"); public int weight = 0; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java index b5aa315..c16d96a 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; +import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -20,6 +21,10 @@ public abstract class CITCondition { public abstract boolean test(CITContext context); + protected void warn(String message, PropertyValue value, PropertyGroup properties) { + CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); + } + protected int parseInteger(PropertyValue value, PropertyGroup properties) throws CITParsingException { try { return Integer.parseInt(value.value()); -- cgit From 8823e77b989ab1035fa8813120177dd3a95ce863 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 07:49:24 +0200 Subject: Item type storage --- .../fabric/citresewn/defaults/cit/types/TypeItem.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 58460aa..02c09d6 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -1,14 +1,16 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.item.Item; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import java.util.List; +import java.util.*; public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) @@ -24,13 +26,21 @@ public class TypeItem extends CITType { super(TypeItem.class, TypeItem::new, "item"); } + public Map> loaded = new IdentityHashMap<>(); + @Override public void load(List parsedCITs) { + for (CIT cit : parsedCITs) + for (CITCondition condition : cit.conditions) + if (condition instanceof ConditionItems items) + for (Item item : items.items) + if (item != null) + loaded.computeIfAbsent(item, i -> new LinkedHashSet<>()).add(cit); } @Override public void dispose() { - + loaded.clear(); } } } \ No newline at end of file -- cgit From 49318a4452f629274e96001700006e0b4a1464a5 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 07:52:10 +0200 Subject: Moved some stuff around --- .../defaults/cit/conditions/ConditionItems.java | 4 +- .../citresewn/defaults/cit/types/TypeItem.java | 6 +- .../shcm/shsupercm/fabric/citresewn/CITResewn.java | 2 +- .../citresewn/api/CITConditionContainer.java | 2 +- .../fabric/citresewn/api/CITTypeContainer.java | 4 +- .../fabric/citresewn/builtin/WeightCondition.java | 27 -------- .../shsupercm/fabric/citresewn/cit/ActiveCITs.java | 66 +++++++++++++++++++ .../shcm/shsupercm/fabric/citresewn/cit/CIT.java | 27 ++++++++ .../fabric/citresewn/cit/CITCondition.java | 35 +++++++++++ .../shsupercm/fabric/citresewn/cit/CITContext.java | 32 ++++++++++ .../fabric/citresewn/cit/CITRegistry.java | 73 ++++++++++++++++++++++ .../shsupercm/fabric/citresewn/cit/CITType.java | 10 +++ .../citresewn/cit/builtin/WeightCondition.java | 27 ++++++++ .../fabric/citresewn/mixin/ModelLoaderMixin.java | 2 +- .../fabric/citresewn/pack/PackParser.java | 10 +-- .../fabric/citresewn/pack/cit/ActiveCITs.java | 66 ------------------- .../shsupercm/fabric/citresewn/pack/cit/CIT.java | 27 -------- .../fabric/citresewn/pack/cit/CITCondition.java | 35 ----------- .../fabric/citresewn/pack/cit/CITContext.java | 32 ---------- .../fabric/citresewn/pack/cit/CITRegistry.java | 73 ---------------------- .../fabric/citresewn/pack/cit/CITType.java | 10 --- 21 files changed, 285 insertions(+), 285 deletions(-) delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITContext.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java index e010a58..ddd6220 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java @@ -7,8 +7,8 @@ import net.minecraft.util.InvalidIdentifierException; import net.minecraft.util.registry.Registry; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 02c09d6..8e3bcbc 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -5,9 +5,9 @@ import net.minecraft.item.Item; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import java.util.*; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index ce908ed..482d757 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -5,7 +5,7 @@ import net.fabricmc.api.ClientModInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITRegistry; +import shcm.shsupercm.fabric.citresewn.cit.CITRegistry; public class CITResewn implements ClientModInitializer { public static final Logger LOG = LogManager.getLogger("CITResewn"); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java index 6803526..56ac01f 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java @@ -1,6 +1,6 @@ package shcm.shsupercm.fabric.citresewn.api; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; import java.util.function.Supplier; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java index e207d00..8c375bb 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -1,7 +1,7 @@ package shcm.shsupercm.fabric.citresewn.api; -import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITType; import java.util.List; import java.util.function.Supplier; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java deleted file mode 100644 index 4161a55..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java +++ /dev/null @@ -1,27 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.builtin; - -import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; -import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; - -public class WeightCondition extends CITCondition { - @Entrypoint(CITConditionContainer.ENTRYPOINT) - public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, - "weight"); - - public int weight = 0; - - @Override - public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { - this.weight = parseInteger(value, properties); - } - - @Override - public boolean test(CITContext context) { - return true; - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java new file mode 100644 index 0000000..d24faf8 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java @@ -0,0 +1,66 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.profiler.Profiler; +import shcm.shsupercm.fabric.citresewn.api.CITDisposable; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; +import shcm.shsupercm.fabric.citresewn.pack.PackParser; + +import java.util.*; + +public class ActiveCITs implements CITDisposable { private ActiveCITs() {} + private static ActiveCITs active = null; + + public static ActiveCITs getActive() { + return active; + } + + public static boolean isActive() { + return active != null; + } + + public final GlobalProperties globalProperties = new GlobalProperties(); + + public final Map, List> cits = new IdentityHashMap<>(); + + public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) { + profiler.push("citresewn:disposing"); + if (active != null) { + active.dispose(); + active = null; + } + + ActiveCITs active = new ActiveCITs(); + + profiler.swap("citresewn:load_global_properties"); + PackParser.loadGlobalProperties(resourceManager, active.globalProperties); + active.globalProperties.callHandlers(); + + profiler.swap("citresewn:load_cits"); + for (CIT cit : PackParser.loadCITs(resourceManager)) + active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit); + for (Map.Entry, List> entry : active.cits.entrySet()) { + entry.getValue().sort(Comparator.comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); + for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) + if (typeContainer.type == entry.getKey()) { + typeContainer.load(entry.getValue()); + break; + } + } + + profiler.pop(); + + return ActiveCITs.active = active; + } + + @Override + public void dispose() { + for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class)) + disposable.dispose(); + + for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) + typeContainer.dispose(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java new file mode 100644 index 0000000..ea75c04 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java @@ -0,0 +1,27 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import net.minecraft.util.Identifier; + +public class CIT { + public final Identifier propertiesIdentifier; + public final String packName; + public final CITType type; + public final CITCondition[] conditions; + public final int weight; + + public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions, int weight) { + this.propertiesIdentifier = propertiesIdentifier; + this.packName = packName; + this.type = type; + this.conditions = conditions; + this.weight = weight; + } + + public boolean test(CITContext context) { + for (CITCondition condition : conditions) + if (!condition.test(context)) + return false; + + return true; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java new file mode 100644 index 0000000..7640d3c --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java @@ -0,0 +1,35 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.Collections; +import java.util.Set; + +public abstract class CITCondition { + public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException; + + public Set> siblingConditions() { + return Collections.emptySet(); + } + + public T modifySibling(Class siblingType, T sibling) { + return sibling; + } + + public abstract boolean test(CITContext context); + + protected void warn(String message, PropertyValue value, PropertyGroup properties) { + CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); + } + + protected int parseInteger(PropertyValue value, PropertyGroup properties) throws CITParsingException { + try { + return Integer.parseInt(value.value()); + } catch (NumberFormatException e) { + throw new CITParsingException("\"" + value.value() + "\" is not a valid integer", properties, value.position()); + } + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java new file mode 100644 index 0000000..99cdbf0 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java @@ -0,0 +1,32 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +import java.util.Objects; + +public class CITContext { + public final ItemStack stack; + public final World world; + public final LivingEntity entity; + + public CITContext(ItemStack stack, World world, LivingEntity entity) { + this.stack = stack; + this.world = world; + this.entity = entity; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof CITContext that && + Objects.equals(this.stack, that.stack) && + Objects.equals(this.world, that.world) && + Objects.equals(this.entity, that.entity); + } + + @Override + public int hashCode() { + return Objects.hash(stack, world, entity); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java new file mode 100644 index 0000000..33f6367 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java @@ -0,0 +1,73 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.util.Identifier; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.*; + +import static shcm.shsupercm.fabric.citresewn.CITResewn.info; +import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading; + +public class CITRegistry { + public static final Map> TYPES = new HashMap<>(); + public static final Map> CONDITIONS = new HashMap<>(); + + public static void registerAll() { + info("Registering CIT Conditions"); + for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) { + String namespace = entrypointContainer.getProvider().getMetadata().getId(); + if (namespace.equals("citresewn-defaults")) + namespace = "citresewn"; + + for (String alias : entrypointContainer.getEntrypoint().aliases) + CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer) entrypointContainer.getEntrypoint()); + } + + info("Registering CIT Types"); + for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) { + String namespace = entrypointContainer.getProvider().getMetadata().getId(); + if (namespace.equals("citresewn-defaults")) + namespace = "citresewn"; + + TYPES.put(new Identifier(namespace, entrypointContainer.getEntrypoint().id), (CITTypeContainer) entrypointContainer.getEntrypoint()); + } + } + + public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { + CITConditionContainer conditionContainer = CONDITIONS.get(key); + if (conditionContainer == null) { + logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); + return null; + } + + CITCondition condition = conditionContainer.createCondition.get(); + condition.load(value, properties); + return condition; + } + + public static CITType parseType(PropertyGroup properties) throws CITParsingException { + Identifier type = new Identifier("citresewn", "item"); + + PropertyValue propertiesType = properties.getLast("citresewn", "type"); + if (propertiesType != null) { + String value = propertiesType.value(); + if (!value.contains(":")) + value = "citresewn:" + value; + type = new Identifier(value); + } + + CITTypeContainer typeContainer = TYPES.get(type); + if (typeContainer == null) + // assert (propertiesType != null) because the default citresewn:item should always be registered + throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position()); + + return typeContainer.createType.get(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java new file mode 100644 index 0000000..abbe755 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -0,0 +1,10 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; + +import java.util.List; + +public abstract class CITType { + public abstract void load(List conditions, PropertyGroup properties) throws CITParsingException; +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java new file mode 100644 index 0000000..ec0a5d7 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java @@ -0,0 +1,27 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public class WeightCondition extends CITCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, + "weight"); + + public int weight = 0; + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + this.weight = parseInteger(value, properties); + } + + @Override + public boolean test(CITContext context) { + return true; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java index e5e000d..ea1d5ab 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java @@ -8,7 +8,7 @@ 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 shcm.shsupercm.fabric.citresewn.pack.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; @Mixin(ModelLoader.class) public class ModelLoaderMixin { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index c82b505..35e8c87 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -5,13 +5,13 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.resource.*; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; +import shcm.shsupercm.fabric.citresewn.cit.builtin.WeightCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.mixin.GroupResourcePackAccessor; -import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITRegistry; -import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITRegistry; +import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java deleted file mode 100644 index 4d9377c..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java +++ /dev/null @@ -1,66 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceManager; -import net.minecraft.util.profiler.Profiler; -import shcm.shsupercm.fabric.citresewn.api.CITDisposable; -import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; -import shcm.shsupercm.fabric.citresewn.pack.PackParser; - -import java.util.*; - -public class ActiveCITs implements CITDisposable { private ActiveCITs() {} - private static ActiveCITs active = null; - - public static ActiveCITs getActive() { - return active; - } - - public static boolean isActive() { - return active != null; - } - - public final GlobalProperties globalProperties = new GlobalProperties(); - - public final Map, List> cits = new IdentityHashMap<>(); - - public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) { - profiler.push("citresewn:disposing"); - if (active != null) { - active.dispose(); - active = null; - } - - ActiveCITs active = new ActiveCITs(); - - profiler.swap("citresewn:load_global_properties"); - PackParser.loadGlobalProperties(resourceManager, active.globalProperties); - active.globalProperties.callHandlers(); - - profiler.swap("citresewn:load_cits"); - for (CIT cit : PackParser.loadCITs(resourceManager)) - active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit); - for (Map.Entry, List> entry : active.cits.entrySet()) { - entry.getValue().sort(Comparator.comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); - for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) - if (typeContainer.type == entry.getKey()) { - typeContainer.load(entry.getValue()); - break; - } - } - - profiler.pop(); - - return ActiveCITs.active = active; - } - - @Override - public void dispose() { - for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class)) - disposable.dispose(); - - for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) - typeContainer.dispose(); - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java deleted file mode 100644 index b4aa9fb..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java +++ /dev/null @@ -1,27 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import net.minecraft.util.Identifier; - -public class CIT { - public final Identifier propertiesIdentifier; - public final String packName; - public final CITType type; - public final CITCondition[] conditions; - public final int weight; - - public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions, int weight) { - this.propertiesIdentifier = propertiesIdentifier; - this.packName = packName; - this.type = type; - this.conditions = conditions; - this.weight = weight; - } - - public boolean test(CITContext context) { - for (CITCondition condition : conditions) - if (!condition.test(context)) - return false; - - return true; - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java deleted file mode 100644 index c16d96a..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ /dev/null @@ -1,35 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; - -import java.util.Collections; -import java.util.Set; - -public abstract class CITCondition { - public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException; - - public Set> siblingConditions() { - return Collections.emptySet(); - } - - public T modifySibling(Class siblingType, T sibling) { - return sibling; - } - - public abstract boolean test(CITContext context); - - protected void warn(String message, PropertyValue value, PropertyGroup properties) { - CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); - } - - protected int parseInteger(PropertyValue value, PropertyGroup properties) throws CITParsingException { - try { - return Integer.parseInt(value.value()); - } catch (NumberFormatException e) { - throw new CITParsingException("\"" + value.value() + "\" is not a valid integer", properties, value.position()); - } - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITContext.java deleted file mode 100644 index d8d66ad..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITContext.java +++ /dev/null @@ -1,32 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import net.minecraft.entity.LivingEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; - -import java.util.Objects; - -public class CITContext { - public final ItemStack stack; - public final World world; - public final LivingEntity entity; - - public CITContext(ItemStack stack, World world, LivingEntity entity) { - this.stack = stack; - this.world = world; - this.entity = entity; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof CITContext that && - Objects.equals(this.stack, that.stack) && - Objects.equals(this.world, that.world) && - Objects.equals(this.entity, that.entity); - } - - @Override - public int hashCode() { - return Objects.hash(stack, world, entity); - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java deleted file mode 100644 index ba61757..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java +++ /dev/null @@ -1,73 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.util.Identifier; -import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; -import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; - -import java.util.*; - -import static shcm.shsupercm.fabric.citresewn.CITResewn.info; -import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading; - -public class CITRegistry { - public static final Map> TYPES = new HashMap<>(); - public static final Map> CONDITIONS = new HashMap<>(); - - public static void registerAll() { - info("Registering CIT Conditions"); - for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) { - String namespace = entrypointContainer.getProvider().getMetadata().getId(); - if (namespace.equals("citresewn-defaults")) - namespace = "citresewn"; - - for (String alias : entrypointContainer.getEntrypoint().aliases) - CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer) entrypointContainer.getEntrypoint()); - } - - info("Registering CIT Types"); - for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) { - String namespace = entrypointContainer.getProvider().getMetadata().getId(); - if (namespace.equals("citresewn-defaults")) - namespace = "citresewn"; - - TYPES.put(new Identifier(namespace, entrypointContainer.getEntrypoint().id), (CITTypeContainer) entrypointContainer.getEntrypoint()); - } - } - - public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { - CITConditionContainer conditionContainer = CONDITIONS.get(key); - if (conditionContainer == null) { - logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); - return null; - } - - CITCondition condition = conditionContainer.createCondition.get(); - condition.load(value, properties); - return condition; - } - - public static CITType parseType(PropertyGroup properties) throws CITParsingException { - Identifier type = new Identifier("citresewn", "item"); - - PropertyValue propertiesType = properties.getLast("citresewn", "type"); - if (propertiesType != null) { - String value = propertiesType.value(); - if (!value.contains(":")) - value = "citresewn:" + value; - type = new Identifier(value); - } - - CITTypeContainer typeContainer = TYPES.get(type); - if (typeContainer == null) - // assert (propertiesType != null) because the default citresewn:item should always be registered - throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position()); - - return typeContainer.createType.get(); - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java deleted file mode 100644 index 4504c01..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java +++ /dev/null @@ -1,10 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.pack.cit; - -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; - -import java.util.List; - -public abstract class CITType { - public abstract void load(List conditions, PropertyGroup properties) throws CITParsingException; -} -- cgit From fe02178b220ebf57225bd092bfdb0d8b88067c33 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 11:01:45 +0200 Subject: Generified CITs to their type --- .../fabric/citresewn/defaults/cit/types/TypeItem.java | 6 +++--- .../shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java | 7 ++++++- .../java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java | 10 +++++----- src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java | 6 +++--- .../fabric/citresewn/config/CITResewnConfigScreenFactory.java | 1 - .../java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java | 8 ++++---- 6 files changed, 21 insertions(+), 17 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 8e3bcbc..1b0e98e 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -26,11 +26,11 @@ public class TypeItem extends CITType { super(TypeItem.class, TypeItem::new, "item"); } - public Map> loaded = new IdentityHashMap<>(); + public Map>> loaded = new IdentityHashMap<>(); @Override - public void load(List parsedCITs) { - for (CIT cit : parsedCITs) + public void load(List> parsedCITs) { + for (CIT cit : parsedCITs) for (CITCondition condition : cit.conditions) if (condition instanceof ConditionItems items) for (Item item : items.items) diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java index 8c375bb..a9666d3 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -18,5 +18,10 @@ public abstract class CITTypeContainer implements CITDisposab this.id = id; } - public abstract void load(List parsedCITs); + public abstract void load(List> parsedCITs); + + @SuppressWarnings("unchecked") + public final void loadUntyped(List parsedCITs) { + load((List>) parsedCITs); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java index d24faf8..f54cc54 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java @@ -23,7 +23,7 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} public final GlobalProperties globalProperties = new GlobalProperties(); - public final Map, List> cits = new IdentityHashMap<>(); + public final Map, List>> cits = new IdentityHashMap<>(); public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) { profiler.push("citresewn:disposing"); @@ -39,13 +39,13 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} active.globalProperties.callHandlers(); profiler.swap("citresewn:load_cits"); - for (CIT cit : PackParser.loadCITs(resourceManager)) + for (CIT cit : PackParser.loadCITs(resourceManager)) active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit); - for (Map.Entry, List> entry : active.cits.entrySet()) { - entry.getValue().sort(Comparator.comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); + for (Map.Entry, List>> entry : active.cits.entrySet()) { + entry.getValue().sort(Comparator.>comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) if (typeContainer.type == entry.getKey()) { - typeContainer.load(entry.getValue()); + typeContainer.loadUntyped(entry.getValue()); break; } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java index ea75c04..7cc5724 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java @@ -2,14 +2,14 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.util.Identifier; -public class CIT { +public class CIT { public final Identifier propertiesIdentifier; public final String packName; - public final CITType type; + public final T type; public final CITCondition[] conditions; public final int weight; - public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions, int weight) { + public CIT(Identifier propertiesIdentifier, String packName, T type, CITCondition[] conditions, int weight) { this.propertiesIdentifier = propertiesIdentifier; this.packName = packName; this.type = type; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java index 76576ba..2d584aa 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -4,7 +4,6 @@ import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.entrypoint.EntrypointContainer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.LiteralText; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 35e8c87..7555866 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -57,8 +57,8 @@ public class PackParser { }); } - public static List loadCITs(ResourceManager resourceManager) { - List cits = new ArrayList<>(); + public static List> loadCITs(ResourceManager resourceManager) { + List> cits = new ArrayList<>(); for (String root : ROOTS) for (Identifier identifier : resourceManager.findResources(root + "/cit", s -> s.endsWith(".properties"))) { @@ -75,7 +75,7 @@ public class PackParser { } - public static CIT parseCIT(PropertyGroup properties) throws CITParsingException { + public static CIT parseCIT(PropertyGroup properties) throws CITParsingException { CITType citType = CITRegistry.parseType(properties); ArrayList conditions = new ArrayList<>(); @@ -108,6 +108,6 @@ public class PackParser { citType.load(conditions, properties); - return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); + return new CIT<>(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); } } -- cgit From cf735c9f3205208801a9675e6b6217dc84b1ea3c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 12:49:48 +0200 Subject: Implemented caching --- .../citresewn/defaults/cit/types/TypeItem.java | 22 ++++++++-- .../defaults/mixin/types/item/ItemStackMixin.java | 16 +++++++ .../shsupercm/fabric/citresewn/cit/CITCache.java | 50 ++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 1b0e98e..730c713 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -3,11 +3,9 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.minecraft.item.Item; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.cit.*; import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import java.util.*; @@ -42,5 +40,23 @@ public class TypeItem extends CITType { public void dispose() { loaded.clear(); } + + public CIT getCIT(CITContext context) { + return ((CITCacheItem) (Object) context.stack).citresewn$getCacheTypeItem().get(context).get(); + } + + public CIT getRealTimeCIT(CITContext context) { + Set> loadedForItemType = loaded.get(context.stack.getItem()); + if (loadedForItemType != null) + for (CIT cit : loadedForItemType) + if (cit.test(context)) + return cit; + + return null; + } + } + + public interface CITCacheItem { + CITCache.Single citresewn$getCacheTypeItem(); } } \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java new file mode 100644 index 0000000..450d8a3 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; + +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import shcm.shsupercm.fabric.citresewn.cit.CITCache; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; + +@Mixin(ItemStack.class) +public class ItemStackMixin implements TypeItem.CITCacheItem { + private final CITCache.Single citresewn$cacheTypeItem = new CITCache.Single<>(TypeItem.CONTAINER::getRealTimeCIT); + + @Override + public CITCache.Single citresewn$getCacheTypeItem() { + return this.citresewn$cacheTypeItem; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java new file mode 100644 index 0000000..5fc6f5a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java @@ -0,0 +1,50 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public abstract class CITCache { + public long lastCachedStamp = 0; + + public static class Single extends CITCache { + public WeakReference> cit = null; + public final Function> realtime; + + public Single(Function> realtime) { + this.realtime = realtime; + } + + public WeakReference> get(CITContext context) { + if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { + this.cit = new WeakReference<>(this.realtime.apply(context)); + this.lastCachedStamp = System.currentTimeMillis(); + } + + return this.cit; + } + } + + public static class MultiList extends CITCache { + public List>> cit = null; + public final Function>> realtime; + + public MultiList(Function>> realtime) { + this.realtime = realtime; + } + + public List>> get(CITContext context) { + if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { + this.cit = new ArrayList<>(); + for (CIT realtimeCIT : this.realtime.apply(context)) + this.cit.add(new WeakReference<>(realtimeCIT)); + this.lastCachedStamp = System.currentTimeMillis(); + } + + return cit; + } + } +} -- cgit From 972e6c330d03f205ab738ed28c4e47496f30e92d Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Tue, 15 Feb 2022 12:21:57 +0200 Subject: Ported item type (missing asset resolution) --- .../citresewn/defaults/cit/types/TypeItem.java | 440 ++++++++++++++++++++- .../defaults/common/ResewnItemModelIdentifier.java | 16 + .../defaults/common/ResewnTextureIdentifier.java | 12 + .../mixin/common/SpriteAtlasTextureMixin.java | 18 + .../mixin/types/item/ItemRendererMixin.java | 83 ++++ .../defaults/mixin/types/item/ItemStackMixin.java | 12 + .../mixin/types/item/JsonUnbakedModelAccessor.java | 22 ++ .../mixin/types/item/ModelLoaderMixin.java | 165 ++++++++ .../resources/citresewn-defaults.accesswidener | 26 ++ .../shsupercm/fabric/citresewn/cit/CITContext.java | 3 +- .../fabric/citresewn/cit/CITRegistry.java | 2 +- .../shsupercm/fabric/citresewn/cit/CITType.java | 6 + .../fabric/citresewn/ex/CITParsingException.java | 2 +- .../citresewn/pack/format/PropertyGroup.java | 6 +- src/main/resources/citresewn.accesswidener | 30 +- 15 files changed, 807 insertions(+), 36 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnTextureIdentifier.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/SpriteAtlasTextureMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/JsonUnbakedModelAccessor.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 730c713..14b8813 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -1,22 +1,449 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; +import com.google.common.collect.ImmutableMap; +import com.mojang.datafixers.util.Either; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.client.render.model.json.ModelOverrideList; +import net.minecraft.client.render.model.json.ModelTransformation; +import net.minecraft.client.texture.SpriteAtlasTexture; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.client.world.ClientWorld; import net.minecraft.item.Item; +import net.minecraft.item.Items; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.resource.ResourceType; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.cit.*; import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item.JsonUnbakedModelAccessor; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.stream.Collectors; public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); + private static final String GENERATED_SUB_CITS_PREFIX = "sub_cititem_generated_"; + public static final Set GENERATED_SUB_CITS_SEEN = new HashSet<>(); + + private final List items = new ArrayList<>(); + + public Map assetIdentifiers = new LinkedHashMap<>(); + public Map, JsonUnbakedModel> unbakedAssets = new LinkedHashMap<>(); + private Map> textureOverrideMap = new HashMap<>(); + private boolean isTexture = false; + + public BakedModel bakedModel = null; + public CITOverrideList bakedSubModels = new CITOverrideList(); + @Override public void load(List conditions, PropertyGroup properties) throws CITParsingException { + for (CITCondition condition : conditions) + if (condition instanceof ConditionItems conditionItems) + items.addAll(Arrays.asList(conditionItems.items)); + + if (this.items.size() == 0) + throw new CITParsingException("Not targeting any item type", properties, -1); + + Identifier assetIdentifier; + PropertyValue modelProp = properties.getLastWithoutMetadata("citresewn", "model"); + boolean containsTexture = modelProp == null && !properties.get("citresewn", "texture", "tile").isEmpty(); + + if (!containsTexture) { + assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (assetIdentifier != null) + assetIdentifiers.put(null, assetIdentifier); + else if (modelProp != null) { + assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (assetIdentifier != null) + assetIdentifiers.put(null, assetIdentifier); + } + } + + for (PropertyValue property : properties.get("citresewn", "model")) { + Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (subIdentifier == null) + throw new CITParsingException("Cannot resolve path", properties, property.position()); + + String subItem = property.keyMetadata(); + Identifier subItemIdentifier = fixDeprecatedSubItem(subItem, properties, property.position()); + assetIdentifiers.put(subItemIdentifier == null ? new Identifier("minecraft", "item/" + subItem) : subItemIdentifier, subIdentifier); + } + if (assetIdentifiers.size() == 0) { // attempt to load texture + isTexture = true; + PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); + assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (assetIdentifier != null) + assetIdentifiers.put(null, assetIdentifier); + + for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { + Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (subIdentifier == null) + throw new CITParsingException("Cannot resolve path", properties, property.position()); + + String subItem = property.keyMetadata(); + Identifier subItemIdentifier = fixDeprecatedSubItem(subItem, properties, property.position()); + assetIdentifiers.put(subItemIdentifier == null ? new Identifier("minecraft", "item/" + subItem) : subItemIdentifier, subIdentifier); + } + } else { // attempt to load textureOverrideMap from textures + PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); + if (textureProp != null) { + assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (assetIdentifier != null) + textureOverrideMap.put(null, Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(assetIdentifier)))); + else + throw new CITParsingException("Cannot resolve path", properties, textureProp.position()); + } + + for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { + textureProp = property; + Identifier subIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (subIdentifier == null) + throw new CITParsingException("Cannot resolve path", properties, property.position()); + + textureOverrideMap.put(property.keyMetadata(), Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(subIdentifier)))); + } + } + + if (assetIdentifiers.size() == 0) + throw new CITParsingException("Could not resolve a replacement model/texture", properties, -1); + } + + public void loadUnbakedAssets(ResourceManager resourceManager) throws Exception { + try { + if (isTexture) { + JsonUnbakedModel itemJson = getModelForFirstItemType(resourceManager); + if (((JsonUnbakedModelAccessor) itemJson).getTextureMap().size() > 1) { // use(some/all of) the asset identifiers to build texture override in layered models + textureOverrideMap = ((JsonUnbakedModelAccessor) itemJson).getTextureMap(); + Identifier defaultAsset = assetIdentifiers.get(null); + textureOverrideMap.replaceAll((layerName, originalTextureEither) -> { + Identifier textureIdentifier = assetIdentifiers.remove(originalTextureEither.map(SpriteIdentifier::getTextureId, Identifier::new)); + if (textureIdentifier != null) + return Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(textureIdentifier))); + if (defaultAsset != null) + return Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(defaultAsset))); + return null; + }); + + if (assetIdentifiers.size() == 0 || (assetIdentifiers.size() == 1 && assetIdentifiers.containsKey(null))) { + unbakedAssets.put(null, itemJson); + return; + } + } + + Identifier baseIdentifier = assetIdentifiers.remove(null); + + if (baseIdentifier != null) + unbakedAssets.put(null, loadUnbakedAsset(resourceManager, baseIdentifier)); + + if (!assetIdentifiers.isEmpty()) { // contains sub models + LinkedHashMap> overrideConditions = new LinkedHashMap<>(); + for (Item item : this.items) { + Identifier itemIdentifier = Registry.ITEM.getId(item); + overrideConditions.put(new Identifier(itemIdentifier.getNamespace(), "item/" + itemIdentifier.getPath()), Collections.emptyList()); + + Identifier itemModelIdentifier = new Identifier(itemIdentifier.getNamespace(), "models/item/" + itemIdentifier.getPath() + ".json"); + try (Resource itemModelResource = resourceManager.getResource(itemModelIdentifier); Reader resourceReader = new InputStreamReader(itemModelResource.getInputStream())) { + JsonUnbakedModel itemModelJson = JsonUnbakedModel.deserialize(resourceReader); + + if (itemModelJson.getOverrides() != null && !itemModelJson.getOverrides().isEmpty()) + for (ModelOverride override : itemModelJson.getOverrides()) + overrideConditions.put(override.getModelId(), override.streamConditions().toList()); + } + } + + ArrayList overrideModels = new ArrayList<>(overrideConditions.keySet()); + Collections.reverse(overrideModels); + + for (Identifier overrideModel : overrideModels) { + Identifier replacement = assetIdentifiers.remove(overrideModel); + if (replacement == null) + continue; + + List conditions = overrideConditions.get(overrideModel); + unbakedAssets.put(conditions, loadUnbakedAsset(resourceManager, replacement)); + } + } + } else { // isModel + Identifier baseIdentifier = assetIdentifiers.remove(null); + + if (baseIdentifier != null) { + if (!GENERATED_SUB_CITS_SEEN.add(baseIdentifier)) // cit generated duplicate + baseIdentifier = new Identifier(baseIdentifier.getNamespace(), GENERATED_SUB_CITS_PREFIX + GENERATED_SUB_CITS_SEEN.size() + "_" + baseIdentifier.getPath()); + GENERATED_SUB_CITS_SEEN.add(baseIdentifier); + + JsonUnbakedModel model = loadUnbakedAsset(resourceManager, baseIdentifier); + unbakedAssets.put(null, model); + + if (model.getOverrides().size() > 0 && textureOverrideMap.size() > 0) { + LinkedHashMap> overrideConditions = new LinkedHashMap<>(); + + for (ModelOverride override : model.getOverrides()) + overrideConditions.put(override.getModelId(), override.streamConditions().toList()); + + ArrayList overrideModels = new ArrayList<>(overrideConditions.keySet()); + Collections.reverse(overrideModels); + + for (Identifier overrideModel : overrideModels) { + Identifier replacement = resolvePath(baseIdentifier, overrideModel.toString(), ".json", resourceManager::containsResource); + if (replacement != null) { + String subTexturePath = replacement.toString().substring(0, replacement.toString().lastIndexOf('.')); + final String subTextureName = subTexturePath.substring(subTexturePath.lastIndexOf('/') + 1); + + replacement = baseIdentifier; + if (!GENERATED_SUB_CITS_SEEN.add(replacement)) // cit generated duplicate + replacement = new Identifier(replacement.getNamespace(), GENERATED_SUB_CITS_PREFIX + GENERATED_SUB_CITS_SEEN.size() + "_" + replacement.getPath()); + GENERATED_SUB_CITS_SEEN.add(replacement); + + JsonUnbakedModel jsonModel = loadUnbakedAsset(resourceManager, replacement); + jsonModel.getOverrides().clear(); + + ((JsonUnbakedModelAccessor) jsonModel).getTextureMap().replaceAll((layerName, texture) -> { + if (layerName != null) + try { + for (String subTexture : textureOverrideMap.keySet()) + if (subTextureName.equals(subTexture)) + return textureOverrideMap.get(subTexture); + } catch (Exception ignored) { } + return texture; + }); + + unbakedAssets.put(overrideConditions.get(overrideModel), jsonModel); + } + } + } + } + + if (!assetIdentifiers.isEmpty()) { // contains sub models + LinkedHashMap> overrideConditions = new LinkedHashMap<>(); + for (Item item : this.items) { + Identifier itemIdentifier = Registry.ITEM.getId(item); + overrideConditions.put(new Identifier(itemIdentifier.getNamespace(), "item/" + itemIdentifier.getPath()), Collections.emptyList()); + + Identifier itemModelIdentifier = new Identifier(itemIdentifier.getNamespace(), "models/item/" + itemIdentifier.getPath() + ".json"); + try (Resource itemModelResource = resourceManager.getResource(itemModelIdentifier); Reader resourceReader = new InputStreamReader(itemModelResource.getInputStream())) { + JsonUnbakedModel itemModelJson = JsonUnbakedModel.deserialize(resourceReader); + + if (itemModelJson.getOverrides() != null && !itemModelJson.getOverrides().isEmpty()) + for (ModelOverride override : itemModelJson.getOverrides()) + overrideConditions.put(override.getModelId(), override.streamConditions().toList()); + } + } + + ArrayList overrideModels = new ArrayList<>(overrideConditions.keySet()); + Collections.reverse(overrideModels); + + for (Identifier overrideModel : overrideModels) { + Identifier replacement = assetIdentifiers.remove(overrideModel); + if (replacement == null) + continue; + + if (!GENERATED_SUB_CITS_SEEN.add(replacement)) // cit generated duplicate + replacement = new Identifier(replacement.getNamespace(), GENERATED_SUB_CITS_PREFIX + GENERATED_SUB_CITS_SEEN.size() + "_" + replacement.getPath()); + GENERATED_SUB_CITS_SEEN.add(replacement); + + List conditions = overrideConditions.get(overrideModel); + unbakedAssets.put(conditions, loadUnbakedAsset(resourceManager, replacement)); + } + } + } + } finally { + assetIdentifiers = null; + textureOverrideMap = null; + } + } + + private JsonUnbakedModel loadUnbakedAsset(ResourceManager resourceManager, Identifier assetIdentifier) throws Exception { + final Identifier identifier; + { + Identifier possibleIdentifier = assetIdentifier; + while (possibleIdentifier.getPath().startsWith(GENERATED_SUB_CITS_PREFIX)) + possibleIdentifier = new Identifier(possibleIdentifier.getNamespace(), possibleIdentifier.getPath().substring(possibleIdentifier.getPath().substring(GENERATED_SUB_CITS_PREFIX.length()).indexOf('_') + GENERATED_SUB_CITS_PREFIX.length() + 1)); + identifier = possibleIdentifier; + } + JsonUnbakedModel json; + if (identifier.getPath().endsWith(".json")) { + InputStream is = null; + Resource resource = null; + try { + json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(identifier)).getInputStream(), StandardCharsets.UTF_8)); + json.id = assetIdentifier.toString(); + json.id = json.id.substring(0, json.id.length() - 5); + + ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { + Optional left = original.left(); + if (left.isPresent()) { + Identifier resolvedIdentifier = resolvePath(identifier, left.get().getTextureId().getPath(), ".png", resourceManager::containsResource); + if (resolvedIdentifier != null) + return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); + } + return original; + }); + + if (textureOverrideMap.size() > 0) { + Map> jsonTextureMap = ((JsonUnbakedModelAccessor) json).getTextureMap(); + if (jsonTextureMap.size() == 0) + jsonTextureMap.put("layer0", null); + + final Either defaultTextureOverride = textureOverrideMap.get(null); + if (defaultTextureOverride != null) + jsonTextureMap.replaceAll((layerName, spriteIdentifierStringEither) -> defaultTextureOverride); + + //jsonTextureMap.putAll(textureOverrideMap); + jsonTextureMap.replaceAll((layerName, texture) -> { + if (layerName != null) + try { + String[] split = texture.map(id -> id.getTextureId().getPath(), s -> s).split("/"); + String textureName = split[split.length - 1]; + if (textureName.endsWith(".png")) + textureName = textureName.substring(0, textureName.length() - 4); + return Objects.requireNonNull(textureOverrideMap.get(textureName)); + } catch (Exception ignored) { } + return texture; + }); + jsonTextureMap.values().removeIf(Objects::isNull); + } + + Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); + if (parentId != null) { + String[] parentIdPathSplit = parentId.getPath().split("/"); + if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { + parentId = resolvePath(identifier, parentId.getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (parentId != null) + ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); + } + } + + json.getOverrides().replaceAll(override -> { + String[] modelIdPathSplit = override.getModelId().getPath().split("/"); + if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { + Identifier resolvedOverridePath = resolvePath(identifier, override.getModelId().getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + if (resolvedOverridePath != null) + return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); + } + + return override; + }); + + return json; + } finally { + IOUtils.closeQuietly(is, resource); + } + } else if (identifier.getPath().endsWith(".png")) { + json = getModelForFirstItemType(resourceManager); + if (json == null) + json = new JsonUnbakedModel(new Identifier("minecraft", "item/generated"), new ArrayList<>(), ImmutableMap.of("layer0", Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(identifier)))), true, JsonUnbakedModel.GuiLight.ITEM, ModelTransformation.NONE, new ArrayList<>()); + json.getOverrides().clear(); + json.id = identifier.toString(); + json.id = json.id.substring(0, json.id.length() - 4); + + ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layerName, originalTextureEither) -> { + if (textureOverrideMap.size() > 0) { + Either textureOverride = textureOverrideMap.get(layerName); + if (textureOverride == null) + textureOverride = textureOverrideMap.get(null); + return textureOverride == null ? originalTextureEither : textureOverride; + } else + return Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(identifier))); + }); + return json; + } + + throw new Exception("Unknown asset type"); + } + + public Identifier fixDeprecatedSubItem(String subItem, PropertyGroup properties, int position) { + String replacement = switch (subItem) { + case "bow_pulling_standby" -> "bow"; + case "crossbow_standby" -> "crossbow"; + case "potion_bottle_drinkable" -> "potion"; + case "potion_bottle_splash" -> "splash_potion"; + case "potion_bottle_lingering" -> "lingering_potion"; + + + default -> null; + }; + + if (replacement != null) { + CITResewn.logWarnLoading(CITParsingException.descriptionOf("Warning: Using deprecated sub item id \"" + subItem + "\" instead of \"" + replacement + "\"", properties, position)); + + return new Identifier("minecraft", "item/" + replacement); + } + + return null; + } + + private JsonUnbakedModel getModelForFirstItemType(ResourceManager resourceManager) { + Identifier firstItemIdentifier = Registry.ITEM.getId(this.items.iterator().next()), firstItemModelIdentifier = new Identifier(firstItemIdentifier.getNamespace(), "models/item/" + firstItemIdentifier.getPath() + ".json"); + Resource itemModelResource = null; + try { + JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString((itemModelResource = resourceManager.getResource(firstItemModelIdentifier)).getInputStream(), StandardCharsets.UTF_8)); + + if (!GENERATED_SUB_CITS_SEEN.add(firstItemModelIdentifier)) // cit generated duplicate + firstItemModelIdentifier = new Identifier(firstItemModelIdentifier.getNamespace(), GENERATED_SUB_CITS_PREFIX + GENERATED_SUB_CITS_SEEN.size() + "_" + firstItemModelIdentifier.getPath()); + GENERATED_SUB_CITS_SEEN.add(firstItemModelIdentifier); + + json.id = firstItemModelIdentifier.toString(); + json.id = json.id.substring(0, json.id.length() - 5); + return json; + } catch (Exception e) { + return null; + } finally { + IOUtils.closeQuietly(itemModelResource); + } + } + + public BakedModel getItemModel(CITContext context, int seed) { + // get sub items or bakedModel if no sub item matches @Nullable + BakedModel bakedModel = bakedSubModels.apply(this.bakedModel, context.stack, (ClientWorld) context.world, context.entity, seed); + + // apply model overrides + if (bakedModel != null && bakedModel.getOverrides() != null) + bakedModel = bakedModel.getOverrides().apply(bakedModel, context.stack, (ClientWorld) context.world, context.entity, seed); + + return bakedModel; + } + + public static class CITOverrideList extends ModelOverrideList { + public void override(List key, BakedModel bakedModel) { + Set conditionTypes = new LinkedHashSet<>(Arrays.asList(this.conditionTypes)); + for (ModelOverride.Condition condition : key) + conditionTypes.add(condition.getType()); + this.conditionTypes = conditionTypes.toArray(new Identifier[0]); + + this.overrides = Arrays.copyOf(this.overrides, this.overrides.length + 1); + + Object2IntMap object2IntMap = new Object2IntOpenHashMap<>(); + for(int i = 0; i < this.conditionTypes.length; ++i) + object2IntMap.put(this.conditionTypes[i], i); + + this.overrides[this.overrides.length - 1] = new BakedOverride( + key.stream() + .map((condition) -> new InlinedCondition(object2IntMap.getInt(condition.getType()), condition.getThreshold())) + .toArray(InlinedCondition[]::new) + , bakedModel); + } } public static class Container extends CITTypeContainer { @@ -41,16 +468,22 @@ public class TypeItem extends CITType { loaded.clear(); } - public CIT getCIT(CITContext context) { + public CIT getCIT(CITContext context, int seed) { return ((CITCacheItem) (Object) context.stack).citresewn$getCacheTypeItem().get(context).get(); } public CIT getRealTimeCIT(CITContext context) { + ((CITCacheItem) (Object) context.stack).citresewn$setMojankCITTypeItem(false); + Set> loadedForItemType = loaded.get(context.stack.getItem()); if (loadedForItemType != null) for (CIT cit : loadedForItemType) - if (cit.test(context)) + if (cit.test(context)) { + if (context.stack.isOf(Items.TRIDENT) || context.stack.isOf(Items.SPYGLASS)) + ((CITCacheItem) (Object) context.stack).citresewn$setMojankCITTypeItem(true); + return cit; + } return null; } @@ -58,5 +491,8 @@ public class TypeItem extends CITType { public interface CITCacheItem { CITCache.Single citresewn$getCacheTypeItem(); + + boolean citresewn$isMojankCITTypeItem(); + void citresewn$setMojankCITTypeItem(boolean mojankCIT); } } \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java new file mode 100644 index 0000000..c3f30f5 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.common; + +import net.minecraft.util.Identifier; + +/** + * Marks models as cit item models. + */ +public class ResewnItemModelIdentifier extends Identifier { + public ResewnItemModelIdentifier(String id) { + super(id); + } + + public ResewnItemModelIdentifier(Identifier identifier) { + super(identifier.getNamespace(), identifier.getPath()); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnTextureIdentifier.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnTextureIdentifier.java new file mode 100644 index 0000000..a6dd2ef --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnTextureIdentifier.java @@ -0,0 +1,12 @@ +package shcm.shsupercm.fabric.citresewn.defaults.common; + +import net.minecraft.util.Identifier; + +/** + * Marks path identifiers as forced literal texture paths. + */ +public class ResewnTextureIdentifier extends Identifier { + public ResewnTextureIdentifier(Identifier identifier) { + super(identifier.getNamespace(), identifier.getPath()); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/SpriteAtlasTextureMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/SpriteAtlasTextureMixin.java new file mode 100644 index 0000000..61eff5c --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/SpriteAtlasTextureMixin.java @@ -0,0 +1,18 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.common; + +import net.minecraft.client.texture.SpriteAtlasTexture; +import net.minecraft.util.Identifier; +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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; + +@Mixin(SpriteAtlasTexture.class) +public class SpriteAtlasTextureMixin { + @Inject(method = "getTexturePath", cancellable = true, at = @At("HEAD")) + public void forceLiteralResewnTextureIdentifier(Identifier id, CallbackInfoReturnable cir) { + if (id instanceof ResewnTextureIdentifier) + cir.setReturnValue(id); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java new file mode 100644 index 0000000..9f6063b --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java @@ -0,0 +1,83 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; + +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.item.ItemModels; +import net.minecraft.client.render.item.ItemRenderer; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.json.ModelTransformation; +import net.minecraft.client.util.ModelIdentifier; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.world.World; +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.ModifyVariable; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; + +import java.lang.ref.WeakReference; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; + +@Mixin(ItemRenderer.class) +public class ItemRendererMixin { + @Shadow @Final private ItemModels models; + + private static WeakReference mojankCITModel = null; + + @Inject(method = "getModel", cancellable = true, at = @At("HEAD")) + private void citresewn$getItemModel(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { + if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + return; + + CITContext context = new CITContext(stack, world, entity); + CIT cit = CONTAINER.getCIT(context, seed); + if (cit != null) { + BakedModel citModel = cit.type.getItemModel(context, seed); + + if (citModel != null) + cir.setReturnValue(citModel); + } + } + + @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD")) + private void citresewn$fixMojankCITsContext(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { + if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + return; + + mojankCITModel = null; + if (((TypeItem.CITCacheItem) (Object) stack).citresewn$isMojankCITTypeItem()) { + boolean bl = renderMode == ModelTransformation.Mode.GUI || renderMode == ModelTransformation.Mode.GROUND || renderMode == ModelTransformation.Mode.FIXED; + if (bl) + mojankCITModel = new WeakReference<>(model); + else { // rendered in hand model of trident/spyglass + if (stack.isOf(Items.TRIDENT)) + mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); + else if (stack.isOf(Items.SPYGLASS)) + mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); + } + } else + mojankCITModel = null; + } + + @ModifyVariable(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "LOAD", ordinal = 0, target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"), argsOnly = true) + private BakedModel citresewn$fixMojankCITs(BakedModel original) { + if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + return original; + + if (mojankCITModel != null) + return mojankCITModel.get(); + + return original; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java index 450d8a3..2fb5396 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java @@ -9,8 +9,20 @@ import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; public class ItemStackMixin implements TypeItem.CITCacheItem { private final CITCache.Single citresewn$cacheTypeItem = new CITCache.Single<>(TypeItem.CONTAINER::getRealTimeCIT); + private boolean citresewn$mojankCITTypeItem = false; + @Override public CITCache.Single citresewn$getCacheTypeItem() { return this.citresewn$cacheTypeItem; } + + @Override + public boolean citresewn$isMojankCITTypeItem() { + return this.citresewn$mojankCITTypeItem; + } + + @Override + public void citresewn$setMojankCITTypeItem(boolean mojankCITTypeItem) { + this.citresewn$mojankCITTypeItem = mojankCITTypeItem; + } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/JsonUnbakedModelAccessor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/JsonUnbakedModelAccessor.java new file mode 100644 index 0000000..350d01a --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/JsonUnbakedModelAccessor.java @@ -0,0 +1,22 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; + +import com.mojang.datafixers.util.Either; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import java.util.Map; + +@Mixin(JsonUnbakedModel.class) +public interface JsonUnbakedModelAccessor { + @Accessor + Map> getTextureMap(); + + @Accessor + Identifier getParentId(); + + @Accessor + void setParentId(Identifier parentId); +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java new file mode 100644 index 0000000..22e9373 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -0,0 +1,165 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; + +import com.mojang.datafixers.util.Either; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.ModelLoader; +import net.minecraft.client.render.model.SpriteAtlasManager; +import net.minecraft.client.render.model.UnbakedModel; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.client.texture.TextureManager; +import net.minecraft.client.util.ModelIdentifier; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import net.minecraft.util.profiler.Profiler; +import org.apache.commons.io.IOUtils; +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.ModifyArg; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.stream.Collectors; + +import static shcm.shsupercm.fabric.citresewn.CITResewn.info; +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; + +@Mixin(ModelLoader.class) +public class ModelLoaderMixin { + @Shadow @Final private ResourceManager resourceManager; + @Shadow @Final private Set modelsToLoad; + @Shadow @Final private Map modelsToBake; + @Shadow @Final private Map unbakedModels; + @Shadow @Final private Map bakedModels; + + @Inject(method = "addModel", at = @At("TAIL")) + public void citresewn$addTypeItemModels(ModelIdentifier eventModelId, CallbackInfo ci) { if (eventModelId != ModelLoader.MISSING_ID) return; + if (!ActiveCITs.isActive()) + return; + + info("Loading CITItem models..."); + CONTAINER.loaded.values().stream() // todo remove streams usage + .flatMap(Collection::stream) + .distinct().forEach(cit -> { + try { + cit.type.loadUnbakedAssets(resourceManager); + + for (JsonUnbakedModel unbakedModel : cit.type.unbakedAssets.values()) { + ResewnItemModelIdentifier id = new ResewnItemModelIdentifier(unbakedModel.id); + this.unbakedModels.put(id, unbakedModel); + this.modelsToLoad.addAll(unbakedModel.getModelDependencies()); + this.modelsToBake.put(id, unbakedModel); + } + } catch (Exception e) { + CITResewn.logErrorLoading(e.getMessage()); + } + }); + + TypeItem.GENERATED_SUB_CITS_SEEN.clear(); + } + + @Inject(method = "upload", at = @At("RETURN")) + public void citresewn$linkTypeItemModels(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable cir) { + if (!ActiveCITs.isActive()) + return; + + profiler.push("citresewn:item_linking"); + info("Linking baked models to CITItems..."); + + CONTAINER.loaded.values().stream() // todo remove streams usage + .flatMap(Collection::stream) + .distinct().forEach(cit -> { + for (Map.Entry, JsonUnbakedModel> citModelEntry : cit.type.unbakedAssets.entrySet()) { + if (citModelEntry.getKey() == null) { + cit.type.bakedModel = this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id)); + } else { + BakedModel bakedModel = bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id)); + if (bakedModel == null) + CITResewn.logWarnLoading("Skipping sub cit: Failed loading model for \"" + citModelEntry.getValue().id + "\" in " + cit.propertiesIdentifier + " from " + cit.packName); + else + cit.type.bakedSubModels.override(citModelEntry.getKey(), bakedModel); + } + } + cit.type.unbakedAssets = null; + }); + + profiler.pop(); + } + + + @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD")) + public void citresewn$forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable cir) { + if (id instanceof ResewnItemModelIdentifier) { + InputStream is = null; + Resource resource = null; + try { + JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8)); + json.id = id.toString(); + json.id = json.id.substring(0, json.id.length() - 5); + + ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { + Optional left = original.left(); + if (left.isPresent()) { + String originalPath = left.get().getTextureId().getPath(); + String[] split = originalPath.split("/"); + if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { + Identifier resolvedIdentifier = CITType.resolvePath(id, originalPath, ".png", identifier -> resourceManager.containsResource(identifier)); + if (resolvedIdentifier != null) + return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); + } + } + return original; + }); + + Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); + if (parentId != null) { + String[] parentIdPathSplit = parentId.getPath().split("/"); + if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { + parentId = CITType.resolvePath(id, parentId.getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + if (parentId != null) + ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); + } + } + + json.getOverrides().replaceAll(override -> { + String[] modelIdPathSplit = override.getModelId().getPath().split("/"); + if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { + Identifier resolvedOverridePath = CITType.resolvePath(id, override.getModelId().getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + if (resolvedOverridePath != null) + return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); + } + + return override; + }); + + cir.setReturnValue(json); + } catch (Exception ignored) { + } finally { + IOUtils.closeQuietly(is, resource); + } + } + } + + @ModifyArg(method = "loadModelFromJson", at = + @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;")) + public Identifier citresewn$fixDuplicatePrefixSuffix(Identifier original) { + if (original.getPath().startsWith("models/models/") && original.getPath().endsWith(".json.json") && original.getPath().contains("cit")) + return new Identifier(original.getNamespace(), original.getPath().substring(7, original.getPath().length() - 5)); + + return original; + } +} diff --git a/defaults/src/main/resources/citresewn-defaults.accesswidener b/defaults/src/main/resources/citresewn-defaults.accesswidener index 3fde517..22b4947 100644 --- a/defaults/src/main/resources/citresewn-defaults.accesswidener +++ b/defaults/src/main/resources/citresewn-defaults.accesswidener @@ -1 +1,27 @@ accessWidener v1 named + +# TypeItem +accessible class net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride +accessible class net/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition +accessible method net/minecraft/client/render/model/json/ModelOverrideList ()V +accessible method net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride ([Lnet/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition;Lnet/minecraft/client/render/model/BakedModel;)V +accessible method net/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition (IF)V +accessible field net/minecraft/client/render/model/json/ModelOverrideList overrides [Lnet/minecraft/client/render/model/json/ModelOverrideList$BakedOverride; +mutable field net/minecraft/client/render/model/json/ModelOverrideList overrides [Lnet/minecraft/client/render/model/json/ModelOverrideList$BakedOverride; +accessible field net/minecraft/client/render/model/json/ModelOverrideList conditionTypes [Lnet/minecraft/util/Identifier; +mutable field net/minecraft/client/render/model/json/ModelOverrideList conditionTypes [Lnet/minecraft/util/Identifier; + +# TypeEnchantment +accessible class net/minecraft/client/render/RenderLayer$MultiPhaseParameters +accessible method net/minecraft/client/render/RenderLayer of (Ljava/lang/String;Lnet/minecraft/client/render/VertexFormat;Lnet/minecraft/client/render/VertexFormat$DrawMode;ILnet/minecraft/client/render/RenderLayer$MultiPhaseParameters;)Lnet/minecraft/client/render/RenderLayer$MultiPhase; +accessible class net/minecraft/client/render/RenderPhase$Shader +accessible class net/minecraft/client/render/RenderPhase$Texturing +accessible class net/minecraft/client/render/RenderPhase$Transparency +accessible class net/minecraft/client/render/RenderPhase$Texture +accessible class net/minecraft/client/render/RenderPhase$Lightmap +accessible class net/minecraft/client/render/RenderPhase$Overlay +accessible class net/minecraft/client/render/RenderPhase$Cull +accessible class net/minecraft/client/render/RenderPhase$DepthTest +accessible class net/minecraft/client/render/RenderPhase$WriteMaskState +accessible class net/minecraft/client/render/RenderPhase$Layering +accessible class net/minecraft/client/render/RenderPhase$Target \ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java index 99cdbf0..b2dd2cd 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn.cit; +import net.minecraft.client.MinecraftClient; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.world.World; @@ -13,7 +14,7 @@ public class CITContext { public CITContext(ItemStack stack, World world, LivingEntity entity) { this.stack = stack; - this.world = world; + this.world = world == null ? MinecraftClient.getInstance().world : world; this.entity = entity; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java index 33f6367..fb0f793 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java @@ -55,7 +55,7 @@ public class CITRegistry { public static CITType parseType(PropertyGroup properties) throws CITParsingException { Identifier type = new Identifier("citresewn", "item"); - PropertyValue propertiesType = properties.getLast("citresewn", "type"); + PropertyValue propertiesType = properties.getLastWithoutMetadata("citresewn", "type"); if (propertiesType != null) { String value = propertiesType.value(); if (!value.contains(":")) diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index abbe755..b618dde 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -1,10 +1,16 @@ package shcm.shsupercm.fabric.citresewn.cit; +import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.List; public abstract class CITType { public abstract void load(List conditions, PropertyGroup properties) throws CITParsingException; + + protected void warn(String message, PropertyValue value, PropertyGroup properties) { + CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java index 964b58d..384014b 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java @@ -8,6 +8,6 @@ public class CITParsingException extends Exception { } public static String descriptionOf(String message, PropertyGroup propertyGroup, int position) { - return message + " at " + position + " in " + propertyGroup.identifier.toString() + " from " + propertyGroup.packName; + return message + (position != -1 ? " line " + position : "") + " in " + propertyGroup.identifier.toString() + " from " + propertyGroup.packName; } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java index 6955ac1..b2806a2 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -40,9 +40,11 @@ public abstract class PropertyGroup { return values; } - public PropertyValue getLast(String namespace, String... pathAliases) { + public PropertyValue getLastWithoutMetadata(String namespace, String... pathAliases) { PropertyValue value = null; - for (Iterator iterator = get(namespace, pathAliases).iterator(); iterator.hasNext(); value = iterator.next()); + for (PropertyValue next : get(namespace, pathAliases)) + if (next.keyMetadata() == null) + value = next; return value; } diff --git a/src/main/resources/citresewn.accesswidener b/src/main/resources/citresewn.accesswidener index 33ad46d..dfde67a 100644 --- a/src/main/resources/citresewn.accesswidener +++ b/src/main/resources/citresewn.accesswidener @@ -1,29 +1 @@ -accessWidener v1 named - -# net.minecraft.client.render.model.json.ModelOverrideList -accessible class net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride -accessible class net/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition -accessible method net/minecraft/client/render/model/json/ModelOverrideList ()V -accessible method net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride ([Lnet/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition;Lnet/minecraft/client/render/model/BakedModel;)V -accessible method net/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition (IF)V -accessible field net/minecraft/client/render/model/json/ModelOverrideList overrides [Lnet/minecraft/client/render/model/json/ModelOverrideList$BakedOverride; -mutable field net/minecraft/client/render/model/json/ModelOverrideList overrides [Lnet/minecraft/client/render/model/json/ModelOverrideList$BakedOverride; -accessible field net/minecraft/client/render/model/json/ModelOverrideList conditionTypes [Lnet/minecraft/util/Identifier; -mutable field net/minecraft/client/render/model/json/ModelOverrideList conditionTypes [Lnet/minecraft/util/Identifier; - -# net.minecraft.client.render.RenderLayer -accessible class net/minecraft/client/render/RenderLayer$MultiPhaseParameters -accessible method net/minecraft/client/render/RenderLayer of (Ljava/lang/String;Lnet/minecraft/client/render/VertexFormat;Lnet/minecraft/client/render/VertexFormat$DrawMode;ILnet/minecraft/client/render/RenderLayer$MultiPhaseParameters;)Lnet/minecraft/client/render/RenderLayer$MultiPhase; - -# net.minecraft.client.render.RenderPhase -accessible class net/minecraft/client/render/RenderPhase$Shader -accessible class net/minecraft/client/render/RenderPhase$Texturing -accessible class net/minecraft/client/render/RenderPhase$Transparency -accessible class net/minecraft/client/render/RenderPhase$Texture -accessible class net/minecraft/client/render/RenderPhase$Lightmap -accessible class net/minecraft/client/render/RenderPhase$Overlay -accessible class net/minecraft/client/render/RenderPhase$Cull -accessible class net/minecraft/client/render/RenderPhase$DepthTest -accessible class net/minecraft/client/render/RenderPhase$WriteMaskState -accessible class net/minecraft/client/render/RenderPhase$Layering -accessible class net/minecraft/client/render/RenderPhase$Target \ No newline at end of file +accessWidener v1 named \ No newline at end of file -- cgit From 4d8c7ea53d5f4af351ec9355c55931ad4dad6715 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Tue, 15 Feb 2022 12:31:42 +0200 Subject: Pass resource manager to type loading --- .../shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java | 2 +- src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java | 3 ++- src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 14b8813..aa3562b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -57,7 +57,7 @@ public class TypeItem extends CITType { public CITOverrideList bakedSubModels = new CITOverrideList(); @Override - public void load(List conditions, PropertyGroup properties) throws CITParsingException { + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { for (CITCondition condition : conditions) if (condition instanceof ConditionItems conditionItems) items.addAll(Arrays.asList(conditionItems.items)); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index b618dde..7ee1e03 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn.cit; +import net.minecraft.resource.ResourceManager; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; @@ -8,7 +9,7 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.List; public abstract class CITType { - public abstract void load(List conditions, PropertyGroup properties) throws CITParsingException; + public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 1dbc22d..38ca718 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -65,7 +65,7 @@ public class PackParser { for (Identifier identifier : resourceManager.findResources(root + "/cit", s -> s.endsWith(".properties"))) { String packName = null; try (Resource resource = resourceManager.getResource(identifier)) { - cits.add(parseCIT(PropertyGroup.tryParseGroup(packName = resource.getResourcePackName(), identifier, resource.getInputStream()))); + cits.add(parseCIT(PropertyGroup.tryParseGroup(packName = resource.getResourcePackName(), identifier, resource.getInputStream()), resourceManager)); } catch (CITParsingException e) { CITResewn.logErrorLoading(e.getMessage()); } catch (Exception e) { @@ -77,7 +77,7 @@ public class PackParser { return cits; } - public static CIT parseCIT(PropertyGroup properties) throws CITParsingException { + public static CIT parseCIT(PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { CITType citType = CITRegistry.parseType(properties); ArrayList conditions = new ArrayList<>(); @@ -109,7 +109,7 @@ public class PackParser { return condition == null; }); - citType.load(conditions, properties); + citType.load(conditions, properties, resourceManager); return new CIT<>(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); } -- cgit From 55e32286cb149e4c20aa8466d51b4d40dae58d48 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Tue, 15 Feb 2022 12:49:37 +0200 Subject: Implemented asset resolution --- .../citresewn/defaults/cit/types/TypeItem.java | 22 +++---- .../mixin/types/item/ModelLoaderMixin.java | 6 +- .../shsupercm/fabric/citresewn/cit/CITType.java | 68 ++++++++++++++++++++++ 3 files changed, 82 insertions(+), 14 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index aa3562b..b08f0c9 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -70,18 +70,18 @@ public class TypeItem extends CITType { boolean containsTexture = modelProp == null && !properties.get("citresewn", "texture", "tile").isEmpty(); if (!containsTexture) { - assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, modelProp, "models", ".json", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); else if (modelProp != null) { - assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, modelProp, "models", ".json", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); } } for (PropertyValue property : properties.get("citresewn", "model")) { - Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, property, "models", ".json", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -93,12 +93,12 @@ public class TypeItem extends CITType { if (assetIdentifiers.size() == 0) { // attempt to load texture isTexture = true; PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); - assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { - Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, property, "textures", ".png", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -109,7 +109,7 @@ public class TypeItem extends CITType { } else { // attempt to load textureOverrideMap from textures PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); if (textureProp != null) { - assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (assetIdentifier != null) textureOverrideMap.put(null, Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(assetIdentifier)))); else @@ -118,7 +118,7 @@ public class TypeItem extends CITType { for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { textureProp = property; - Identifier subIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -206,7 +206,7 @@ public class TypeItem extends CITType { Collections.reverse(overrideModels); for (Identifier overrideModel : overrideModels) { - Identifier replacement = resolvePath(baseIdentifier, overrideModel.toString(), ".json", resourceManager::containsResource); + Identifier replacement = resolveAsset(baseIdentifier, overrideModel.toString(), "models", ".json", resourceManager); if (replacement != null) { String subTexturePath = replacement.toString().substring(0, replacement.toString().lastIndexOf('.')); final String subTextureName = subTexturePath.substring(subTexturePath.lastIndexOf('/') + 1); @@ -294,7 +294,7 @@ public class TypeItem extends CITType { ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { Optional left = original.left(); if (left.isPresent()) { - Identifier resolvedIdentifier = resolvePath(identifier, left.get().getTextureId().getPath(), ".png", resourceManager::containsResource); + Identifier resolvedIdentifier = resolveAsset(identifier, left.get().getTextureId().getPath(), "textures", ".png", resourceManager); if (resolvedIdentifier != null) return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); } @@ -329,7 +329,7 @@ public class TypeItem extends CITType { if (parentId != null) { String[] parentIdPathSplit = parentId.getPath().split("/"); if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = resolvePath(identifier, parentId.getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + parentId = resolveAsset(identifier, parentId.getPath(), "models", ".json", resourceManager); if (parentId != null) ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); } @@ -338,7 +338,7 @@ public class TypeItem extends CITType { json.getOverrides().replaceAll(override -> { String[] modelIdPathSplit = override.getModelId().getPath().split("/"); if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = resolvePath(identifier, override.getModelId().getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier resolvedOverridePath = resolveAsset(identifier, override.getModelId().getPath(), "models", ".json", resourceManager); if (resolvedOverridePath != null) return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 22e9373..c95e08a 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -117,7 +117,7 @@ public class ModelLoaderMixin { String originalPath = left.get().getTextureId().getPath(); String[] split = originalPath.split("/"); if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { - Identifier resolvedIdentifier = CITType.resolvePath(id, originalPath, ".png", identifier -> resourceManager.containsResource(identifier)); + Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); if (resolvedIdentifier != null) return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); } @@ -129,7 +129,7 @@ public class ModelLoaderMixin { if (parentId != null) { String[] parentIdPathSplit = parentId.getPath().split("/"); if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = CITType.resolvePath(id, parentId.getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); if (parentId != null) ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); } @@ -138,7 +138,7 @@ public class ModelLoaderMixin { json.getOverrides().replaceAll(override -> { String[] modelIdPathSplit = override.getModelId().getPath().split("/"); if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = CITType.resolvePath(id, override.getModelId().getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); if (resolvedOverridePath != null) return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index 7ee1e03..3618dfd 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -1,11 +1,14 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import java.util.Arrays; +import java.util.LinkedList; import java.util.List; public abstract class CITType { @@ -14,4 +17,69 @@ public abstract class CITType { protected void warn(String message, PropertyValue value, PropertyGroup properties) { CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); } + + /** + * Takes a defined path and resolves it to an identifier pointing to the resourcepack's path of the specified extension(returns null if no path can be resolved).
+ * If definedPath is null, will try to resolve a relative file with the same name as the rootIdentifier with the extension, otherwise:
+ * definedPath will be formatted to replace "\\" with "/" the extension will be appended if not there already.
+ * It will first try using definedPath as an absolute path, if it cant resolve(or definedPath starts with ./), definedPath will be considered relative.
+ * Relative paths support going to parent directories using "..". + */ + public static Identifier resolveAsset(Identifier rootIdentifier, String path, String defaultedTypeDirectory, String extension, ResourceManager resourceManager) { + if (path == null) { + path = rootIdentifier.getPath().substring(0, rootIdentifier.getPath().length() - 11); + if (!path.endsWith(extension)) + path = path + extension; + Identifier pathIdentifier = new Identifier(rootIdentifier.getNamespace(), path); + return resourceManager.containsResource(pathIdentifier) ? pathIdentifier : null; + } + + Identifier pathIdentifier = new Identifier(path); + + path = pathIdentifier.getPath().replace('\\', '/'); + if (!path.endsWith(extension)) + path = path + extension; + + if (path.startsWith("./")) + path = path.substring(2); + else if (!path.contains("..")) { + pathIdentifier = new Identifier(pathIdentifier.getNamespace(), path); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + else if (path.startsWith("assets/")) { + path = path.substring(7); + int sep = path.indexOf('/'); + pathIdentifier = new Identifier(path.substring(0, sep), path.substring(sep + 1)); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + } + pathIdentifier = new Identifier(pathIdentifier.getNamespace(), defaultedTypeDirectory + "/" + path); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + } + + LinkedList pathParts = new LinkedList<>(Arrays.asList(rootIdentifier.getPath().split("/"))); + pathParts.removeLast(); + + if (path.contains("/")) { + for (String part : path.split("/")) { + if (part.equals("..")) { + if (pathParts.size() == 0) + return null; + pathParts.removeLast(); + } else + pathParts.addLast(part); + } + } else + pathParts.addLast(path); + path = String.join("/", pathParts); + + pathIdentifier = new Identifier(rootIdentifier.getNamespace(), path); + + return resourceManager.containsResource(pathIdentifier) ? pathIdentifier : null; + } + + public static Identifier resolveAsset(Identifier rootIdentifier, PropertyValue path, String defaultedTypeDirectory, String extension, ResourceManager resourceManager) { + return resolveAsset(rootIdentifier, path == null ? null : path.value(), defaultedTypeDirectory, extension, resourceManager); + } } -- cgit From 2bda2f19a107f5a31e62b7c60e95035fcea3448e Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Wed, 16 Feb 2022 14:23:32 +0200 Subject: Fixed minor bugs --- .../shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java | 2 ++ .../citresewn/defaults/mixin/types/item/ModelLoaderMixin.java | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index b08f0c9..d9f9ba6 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -374,6 +374,8 @@ public class TypeItem extends CITType { } public Identifier fixDeprecatedSubItem(String subItem, PropertyGroup properties, int position) { + if (subItem == null) + return null; String replacement = switch (subItem) { case "bow_pulling_standby" -> "bow"; case "crossbow_standby" -> "crossbow"; diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index c95e08a..adb4963 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -1,6 +1,7 @@ package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; import com.mojang.datafixers.util.Either; +import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.ModelLoader; import net.minecraft.client.render.model.SpriteAtlasManager; @@ -46,8 +47,10 @@ public class ModelLoaderMixin { @Shadow @Final private Map unbakedModels; @Shadow @Final private Map bakedModels; - @Inject(method = "addModel", at = @At("TAIL")) - public void citresewn$addTypeItemModels(ModelIdentifier eventModelId, CallbackInfo ci) { if (eventModelId != ModelLoader.MISSING_ID) return; + @Inject(method = "", at = + @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/util/profiler/Profiler;swap(Ljava/lang/String;)V")) + public void citresewn$addTypeItemModels(ResourceManager resourceManager, BlockColors blockColors, Profiler profiler, int i, CallbackInfo ci) { + profiler.swap("citresewn:type_item_models"); if (!ActiveCITs.isActive()) return; -- cgit From 185dd9defbd5e0a511a6551e7857b01c5353dd5b Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Wed, 16 Feb 2022 14:42:15 +0200 Subject: Implemented type-specific property acknowledgement --- .../shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java | 7 ++++++- .../citresewn/defaults/mixin/types/item/ModelLoaderMixin.java | 6 +++--- src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java | 7 ++++--- src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java | 4 ++++ 4 files changed, 17 insertions(+), 7 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index d9f9ba6..9cb623e 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -17,7 +17,6 @@ import net.minecraft.item.Item; import net.minecraft.item.Items; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceType; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import org.apache.commons.io.IOUtils; @@ -30,6 +29,7 @@ import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item.JsonUnbakedModelAccessor; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.io.InputStream; @@ -56,6 +56,11 @@ public class TypeItem extends CITType { public BakedModel bakedModel = null; public CITOverrideList bakedSubModels = new CITOverrideList(); + @Override + public Set typeProperties() { + return Set.of(PropertyKey.of("model"), PropertyKey.of("texture"), PropertyKey.of("tile")); + } + @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { for (CITCondition condition : conditions) diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index adb4963..0c5f4f1 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -54,7 +54,7 @@ public class ModelLoaderMixin { if (!ActiveCITs.isActive()) return; - info("Loading CITItem models..."); + info("Loading item CIT models..."); CONTAINER.loaded.values().stream() // todo remove streams usage .flatMap(Collection::stream) .distinct().forEach(cit -> { @@ -80,8 +80,8 @@ public class ModelLoaderMixin { if (!ActiveCITs.isActive()) return; - profiler.push("citresewn:item_linking"); - info("Linking baked models to CITItems..."); + profiler.push("citresewn:type_item_linking"); + info("Linking baked models to item CITs..."); CONTAINER.loaded.values().stream() // todo remove streams usage .flatMap(Collection::stream) diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index 3618dfd..aed784a 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -5,13 +5,14 @@ import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; +import java.util.*; public abstract class CITType { + public abstract Set typeProperties(); + public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 38ca718..70e3f75 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -82,9 +82,13 @@ public class PackParser { ArrayList conditions = new ArrayList<>(); + Set ignoredProperties = citType.typeProperties(); + for (Map.Entry> entry : properties.properties.entrySet()) { if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) continue; + if (ignoredProperties.contains(entry.getKey())) + continue; for (PropertyValue value : entry.getValue()) conditions.add(CITRegistry.parseCondition(entry.getKey(), value, properties)); -- cgit From 1ec38a503f3b88c9de1e29319548b5a6e6098707 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 12:55:24 +0200 Subject: Implemented conditions stackSize, damage, damageMask --- .../defaults/cit/conditions/ConditionDamage.java | 46 ++++++++++++++++++++++ .../cit/conditions/ConditionDamageMask.java | 30 ++++++++++++++ .../cit/conditions/ConditionStackSize.java | 21 ++++++++++ 3 files changed, 97 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamage.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionStackSize.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamage.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamage.java new file mode 100644 index 0000000..c5c31f9 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamage.java @@ -0,0 +1,46 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; + +import java.util.Set; + +public class ConditionDamage extends IntegerCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionDamage.class, ConditionDamage::new, + "damage"); + + protected Integer mask = null; + + public ConditionDamage() { + super(true, false, true); + } + + @Override + protected int getValue(CITContext context) { + int value = context.stack.isDamageable() ? context.stack.getDamage() : 0; + if (mask != null) + value &= mask; + return value; + } + + @Override + protected int getPercentageTotalValue(CITContext context) { + return context.stack.isDamageable() ? context.stack.getItem().getMaxDamage() : 0; + } + + @Override + public Set> siblingConditions() { + return Set.of(ConditionDamageMask.class); + } + + @Override + public T modifySibling(T sibling) { + if (sibling instanceof ConditionDamageMask damageMask) + this.mask = damageMask.getMask(); + return null; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java new file mode 100644 index 0000000..6c8e9e0 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java @@ -0,0 +1,30 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; + +public class ConditionDamageMask extends IntegerCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionDamageMask.class, ConditionDamageMask::new, + "damageMask"); + + public ConditionDamageMask() { + super(false, false, false); + } + + @Override + protected int getValue(CITContext context) { + return 0; + } + + @Override + public boolean test(CITContext context) { + return true; + } + + public int getMask() { + return this.min; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionStackSize.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionStackSize.java new file mode 100644 index 0000000..1d22a53 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionStackSize.java @@ -0,0 +1,21 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; + +public class ConditionStackSize extends IntegerCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionStackSize.class, ConditionStackSize::new, + "stack_size", "stackSize", "amount"); + + public ConditionStackSize() { + super(true, false, false); + } + + @Override + protected int getValue(CITContext context) { + return context.stack.getCount(); + } +} -- cgit From d510448fa7e40b31d174d5e26cf601ee63a632e0 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 13:08:39 +0200 Subject: Added stubs for existing cit types --- .../citresewn/defaults/cit/types/TypeArmor.java | 45 +++++++++++++++++ .../citresewn/defaults/cit/types/TypeElytra.java | 45 +++++++++++++++++ .../defaults/cit/types/TypeEnchantment.java | 57 ++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java new file mode 100644 index 0000000..2b857fa --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java @@ -0,0 +1,45 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.types; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.resource.ResourceManager; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; + +import java.util.List; +import java.util.Set; + +public class TypeArmor extends CITType { + @Entrypoint(CITTypeContainer.ENTRYPOINT) + public static final Container CONTAINER = new Container(); + + @Override + public Set typeProperties() { + return Set.of(PropertyKey.of("texture")); + } + + @Override + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + + } + + public static class Container extends CITTypeContainer { + public Container() { + super(TypeArmor.class, TypeArmor::new, "armor"); + } + + @Override + public void load(List> parsedCITs) { + + } + + @Override + public void dispose() { + + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java new file mode 100644 index 0000000..f0f7540 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java @@ -0,0 +1,45 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.types; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.resource.ResourceManager; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; + +import java.util.List; +import java.util.Set; + +public class TypeElytra extends CITType { + @Entrypoint(CITTypeContainer.ENTRYPOINT) + public static final Container CONTAINER = new Container(); + + @Override + public Set typeProperties() { + return Set.of(PropertyKey.of("texture")); + } + + @Override + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + + } + + public static class Container extends CITTypeContainer { + public Container() { + super(TypeElytra.class, TypeElytra::new, "elytra"); + } + + @Override + public void load(List> parsedCITs) { + + } + + @Override + public void dispose() { + + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java new file mode 100644 index 0000000..3a340b1 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -0,0 +1,57 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.types; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.resource.ResourceManager; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; + +import java.util.List; +import java.util.Set; + +public class TypeEnchantment extends CITType { + @Entrypoint(CITTypeContainer.ENTRYPOINT) + public static final Container CONTAINER = new Container(); + + @Override + public Set typeProperties() { + return Set.of( + PropertyKey.of("texture"), + PropertyKey.of("layer"), + PropertyKey.of("speed"), + PropertyKey.of("rotation"), + PropertyKey.of("duration"), + PropertyKey.of("blend"), + PropertyKey.of("useGlint"), + PropertyKey.of("blur"), + PropertyKey.of("r"), + PropertyKey.of("g"), + PropertyKey.of("b"), + PropertyKey.of("a")); + } + + @Override + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + + } + + public static class Container extends CITTypeContainer { + public Container() { + super(TypeEnchantment.class, TypeEnchantment::new, "enchantment"); + } + + @Override + public void load(List> parsedCITs) { + + } + + @Override + public void dispose() { + + } + } +} -- cgit From a50fd46a00b56a15161ae3288cf2ad55d9badc17 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 15:04:18 +0200 Subject: Added more common condition types --- .../cit/conditions/ConditionDamageMask.java | 8 ++++ .../cit/builtin/conditions/BooleanCondition.java | 28 ++++++++++++ .../cit/builtin/conditions/EnumCondition.java | 50 ++++++++++++++++++++++ .../builtin/conditions/IdentifierCondition.java | 29 +++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java index 6c8e9e0..01d33fe 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java @@ -2,9 +2,12 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.cit.CITContext; import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; +import java.util.Set; + public class ConditionDamageMask extends IntegerCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionDamageMask.class, ConditionDamageMask::new, @@ -27,4 +30,9 @@ public class ConditionDamageMask extends IntegerCondition { public int getMask() { return this.min; } + + @Override + public Set> siblingConditions() { + return Set.of(ConditionDamage.class); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java new file mode 100644 index 0000000..fcf976c --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java @@ -0,0 +1,28 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public abstract class BooleanCondition extends CITCondition { + protected boolean value; + + protected abstract boolean getValue(CITContext context); + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + if (value.value().equalsIgnoreCase("true")) + this.value = true; + else if (value.value().equalsIgnoreCase("false")) + this.value = false; + else + throw new CITParsingException("Not a boolean", properties, value.position()); + } + + @Override + public boolean test(CITContext context) { + return getValue(context) == this.value; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java new file mode 100644 index 0000000..cdfd65d --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java @@ -0,0 +1,50 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.function.Supplier; + +public abstract class EnumCondition> extends CITCondition { + protected final Supplier values; + protected final boolean ignoreCase; + + protected T value; + + protected abstract T getValue(CITContext context); + + protected EnumCondition(Supplier values, boolean ignoreCase) { + this.values = values; + this.ignoreCase = ignoreCase; + } + + protected EnumCondition(Supplier values) { + this(values, true); + } + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + for (T enumConstant : values.get()) + for (String alias : ((Aliased) enumConstant).getAliases()) + if (ignoreCase ? alias.equalsIgnoreCase(value.value()) : alias.equals(value.value())) { + this.value = enumConstant; + return; + } + + throw new CITParsingException("Unrecognized value", properties, value.position()); + } + + @Override + public boolean test(CITContext context) { + return getValue(context) == this.value; + } + + public interface Aliased { + default String[] getAliases() { + return new String[] { ((Enum) this).name() }; + } + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java new file mode 100644 index 0000000..2f45f2f --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java @@ -0,0 +1,29 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public abstract class IdentifierCondition extends CITCondition { + protected Identifier value; + + protected abstract Identifier getValue(CITContext context); + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + try { + this.value = new Identifier(value.value()); + } catch (InvalidIdentifierException e) { + throw new CITParsingException(e.getMessage(), properties, value.position()); + } + } + + @Override + public boolean test(CITContext context) { + return this.value.equals(getValue(context)); + } +} -- cgit From 748dc6de2373a9bda3886a804735c872502a942c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 15:18:13 +0200 Subject: Implemented hand condition --- .../defaults/cit/conditions/ConditionHand.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java new file mode 100644 index 0000000..5325e28 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java @@ -0,0 +1,56 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.EnumCondition; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public class ConditionHand extends EnumCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionHand.class, ConditionHand::new, + "hand"); + + public ConditionHand() { + super(ConditionHand.Hand::values); + } + + @Override + protected Hand getValue(CITContext context) { + if (context.entity.getMainHandStack() == context.stack) + return Hand.MAINHAND; + if (context.entity.getOffHandStack() == context.stack) + return Hand.OFFHAND; + + return null; + } + + @Override + public boolean test(CITContext context) { + Hand hand = getValue(context); + return this.value == hand || (this.value == Hand.ANY && hand != null); + } + + protected enum Hand implements EnumCondition.Aliased { + MAINHAND { + @Override + public String[] getAliases() { + return new String[] { "main", "mainhand", "main_hand" }; + } + }, + OFFHAND { + @Override + public String[] getAliases() { + return new String[] { "off", "offhand", "off_hand" }; + } + }, + ANY { + @Override + public String[] getAliases() { + return new String[] { "any", "either", "*" }; + } + } + } +} -- cgit From ee4412ffa26ff2bfc7b649c9e0554367cf2cdb2e Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 15:53:59 +0200 Subject: Updated api usage of items condition --- .../defaults/cit/conditions/ConditionItems.java | 50 ++++++++++++++-------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java index ddd6220..73b79e0 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java @@ -3,42 +3,37 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.minecraft.item.Item; import net.minecraft.util.Identifier; -import net.minecraft.util.InvalidIdentifierException; import net.minecraft.util.registry.Registry; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IdentifierCondition; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.ListCondition; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.LinkedHashSet; import java.util.Set; -public class ConditionItems extends CITCondition { +public class ConditionItems extends ListCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionItems.class, ConditionItems::new, "items", "matchItems"); public Item[] items = new Item[0]; + public ConditionItems() { + super(ItemCondition.class, ItemCondition::new); + } + @Override public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + super.load(value, properties); + Set items = new LinkedHashSet<>(); - for (String entry : value.value().split(" ")) - if (!entry.isEmpty()) - try { - Identifier identifier = new Identifier(entry); - if (Registry.ITEM.containsId(identifier)) - items.add(Registry.ITEM.get(identifier)); - else { - items.add(null); - warn(identifier + " is not in the item registry", value, properties); - } - } catch (InvalidIdentifierException e) { - throw new CITParsingException("Invalid item identifier \"" + entry + "\"", properties, value.position()); - } + for (ItemCondition itemCondition : this.conditions) + items.add(itemCondition.item); this.items = items.toArray(new Item[0]); } @@ -51,4 +46,25 @@ public class ConditionItems extends CITCondition { return false; } + + protected static class ItemCondition extends IdentifierCondition { + public Item item = null; + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + super.load(value, properties); + + if (Registry.ITEM.containsId(this.value)) + this.item = Registry.ITEM.get(this.value); + else { + this.item = null; + warn(this.value + " is not in the item registry", value, properties); + } + } + + @Override + protected Identifier getValue(CITContext context) { + return this.value; + } + } } -- cgit From 9db8d1eafb861f6512e7ae0d8584b5ace676cc3c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 17:15:26 +0200 Subject: Implemented enchantments and enchantmentLevels conditions --- .../cit/conditions/ConditionEnchantmentLevels.java | 62 ++++++++++++++++++++++ .../cit/conditions/ConditionEnchantments.java | 59 ++++++++++++++++++++ .../shsupercm/fabric/citresewn/cit/CITContext.java | 19 +++++++ 3 files changed, 140 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantmentLevels.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantments.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantmentLevels.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantmentLevels.java new file mode 100644 index 0000000..da6b4f4 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantmentLevels.java @@ -0,0 +1,62 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.util.Identifier; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.ListCondition; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class ConditionEnchantmentLevels extends ListCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionEnchantmentLevels.class, ConditionEnchantmentLevels::new, + "enchantment_levels", "enchantmentLevels"); + + protected Set enchantments = null; + + public ConditionEnchantmentLevels() { + super(EnchantmentLevelCondition.class, EnchantmentLevelCondition::new); + } + + @Override + public Set> siblingConditions() { + return Set.of(ConditionEnchantments.class); + } + + @Override + public T modifySibling(T sibling) { + if (sibling instanceof ConditionEnchantments conditionEnchantments) { + if (enchantments == null) { + enchantments = new HashSet<>(); + for (EnchantmentLevelCondition subCondition : this.conditions) + subCondition.enchantments = enchantments; + } + enchantments.addAll(Arrays.asList(conditionEnchantments.getEnchantments())); + } + + return sibling; + } + + protected static class EnchantmentLevelCondition extends IntegerCondition { + protected Set enchantments = null; + + protected EnchantmentLevelCondition() { + super(true, false, false); + } + + @Override + public boolean test(CITContext context) { + for (Map.Entry entry : context.enchantments().entrySet()) + if ((enchantments == null || enchantments.contains(entry.getKey())) && entry.getValue() != null && (range ? min <= entry.getValue() && entry.getValue() <= max : entry.getValue() == min)) + return true; + + return false; + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantments.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantments.java new file mode 100644 index 0000000..301f9c1 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionEnchantments.java @@ -0,0 +1,59 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IdentifierCondition; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.ListCondition; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.Set; + +public class ConditionEnchantments extends ListCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionEnchantments.class, ConditionEnchantments::new, + "enchantments", "enchantmentIDs"); + + public ConditionEnchantments() { + super(EnchantmentCondition.class, EnchantmentCondition::new); + } + + public Identifier[] getEnchantments() { + Identifier[] enchantments = new Identifier[this.conditions.length]; + + for (int i = 0; i < this.conditions.length; i++) + enchantments[i] = this.conditions[i].getValue(null); + + return enchantments; + } + + @Override + public Set> siblingConditions() { + return Set.of(ConditionEnchantmentLevels.class); + } + + protected static class EnchantmentCondition extends IdentifierCondition { + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + super.load(value, properties); + + if (!Registry.ENCHANTMENT.containsId(this.value)) + warn(this.value + " is not in the enchantment registry", value, properties); + } + + @Override + public boolean test(CITContext context) { + return context.enchantments().containsKey(this.value); + } + + @Override + protected Identifier getValue(CITContext context) { + return this.value; + } + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java index b2dd2cd..c5f1922 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java @@ -1,10 +1,18 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.client.MinecraftClient; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.LivingEntity; +import net.minecraft.item.EnchantedBookItem; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.util.Identifier; import net.minecraft.world.World; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Objects; public class CITContext { @@ -12,12 +20,23 @@ public class CITContext { public final World world; public final LivingEntity entity; + private Map enchantments = null; + public CITContext(ItemStack stack, World world, LivingEntity entity) { this.stack = stack; this.world = world == null ? MinecraftClient.getInstance().world : world; this.entity = entity; } + public Map enchantments() { + if (this.enchantments == null) { + this.enchantments = new LinkedHashMap<>(); + for (NbtElement nbtElement : stack.isOf(Items.ENCHANTED_BOOK) ? EnchantedBookItem.getEnchantmentNbt(stack) : stack.getEnchantments()) + this.enchantments.put(EnchantmentHelper.getIdFromNbt((NbtCompound) nbtElement), EnchantmentHelper.getLevelFromNbt((NbtCompound) nbtElement)); + } + return this.enchantments; + } + @Override public boolean equals(Object obj) { return obj instanceof CITContext that && -- cgit From 30e64f63baca74e13b599c8bb20b5ae0f1995562 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 17:15:50 +0200 Subject: Added damage_mask alias to damageMask condition --- .../fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java index 01d33fe..6adde08 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionDamageMask.java @@ -11,7 +11,7 @@ import java.util.Set; public class ConditionDamageMask extends IntegerCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionDamageMask.class, ConditionDamageMask::new, - "damageMask"); + "damage_mask", "damageMask"); public ConditionDamageMask() { super(false, false, false); -- cgit From 540fc9187ca1d1b1b986d27add982d2026735a3a Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Feb 2022 08:15:27 +0200 Subject: Implemented nbt condition --- .../defaults/cit/conditions/ConditionNBT.java | 244 +++++++++++++++++++++ .../resources/citresewn-defaults.accesswidener | 3 + 2 files changed, 247 insertions(+) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java new file mode 100644 index 0000000..09ae08e --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java @@ -0,0 +1,244 @@ +package shcm.shsupercm.fabric.citresewn.defaults.cit.conditions; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.nbt.*; +import net.minecraft.text.Text; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.Locale; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +public class ConditionNBT extends CITCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionNBT.class, ConditionNBT::new, + "nbt"); + + protected String[] path; + + protected StringMatcher matchString = null; + protected NbtInt matchInteger = null; + protected NbtByte matchByte = null; + protected NbtFloat matchFloat = null; + protected NbtDouble matchDouble = null; + protected NbtLong matchLong = null; + protected NbtShort matchShort = null; + protected NbtCompound matchCompound = null; + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + if (value.keyMetadata() == null || value.keyMetadata().isEmpty()) + throw new CITParsingException("Missing nbt path", properties, value.position()); + + path = value.keyMetadata().split("\\."); + for (String s : path) + if (s.isEmpty()) + throw new CITParsingException("Path segment cannot be empty", properties, value.position()); + + try { + if (value.value().startsWith("regex:")) + matchString = new StringMatcher.RegexMatcher(value.value().substring(6)); + else if (value.value().startsWith("iregex:")) + matchString = new StringMatcher.IRegexMatcher(value.value().substring(7)); + else if (value.value().startsWith("pattern:")) + matchString = new StringMatcher.PatternMatcher(value.value().substring(8)); + else if (value.value().startsWith("ipattern:")) + matchString = new StringMatcher.IPatternMatcher(value.value().substring(9)); + else + matchString = new StringMatcher.DirectMatcher(value.value()); + } catch (PatternSyntaxException e) { + throw new CITParsingException("Malformatted regex expression", properties, value.position(), e); + } catch (Exception ignored) { } + try { + if (value.value().startsWith("#")) + matchInteger = NbtInt.of(Integer.parseInt(value.value().substring(1).toLowerCase(Locale.ENGLISH), 16)); + else if (value.value().startsWith("0x")) + matchInteger = NbtInt.of(Integer.parseInt(value.value().substring(2).toLowerCase(Locale.ENGLISH), 16)); + else + matchInteger = NbtInt.of(Integer.parseInt(value.value())); + } catch (Exception ignored) { } + try { + matchByte = NbtByte.of(Byte.parseByte(value.value())); + } catch (Exception ignored) { } + try { + matchFloat = NbtFloat.of(Float.parseFloat(value.value())); + } catch (Exception ignored) { } + try { + matchDouble = NbtDouble.of(Double.parseDouble(value.value())); + } catch (Exception ignored) { } + try { + matchLong = NbtLong.of(Long.parseLong(value.value())); + } catch (Exception ignored) { } + try { + matchShort = NbtShort.of(Short.parseShort(value.value())); + } catch (Exception ignored) { } + try { + matchCompound = StringNbtReader.parse(value.value()); + } catch (Exception ignored) { } + } + + @Override + public boolean test(CITContext context) { + return testPath(context.stack.getNbt(), 0); + } + + protected boolean testPath(NbtElement element, int pathIndex) { + if (element == null) + return false; + + if (pathIndex >= path.length) + return testValue(element); + + final String path = this.path[pathIndex]; + if (path.equals("*")) { + if (element instanceof NbtCompound compound) { + for (NbtElement subElement : compound.entries.values()) + if (testPath(subElement, pathIndex + 1)) + return true; + } else if (element instanceof NbtList list) { + for (NbtElement subElement : list) + if (testPath(subElement, pathIndex + 1)) + return true; + } + } else { + if (element instanceof NbtCompound compound) + return testPath(compound.get(path), pathIndex + 1); + else if (element instanceof NbtList list) + try { + return testPath(list.get(Integer.parseInt(path)), pathIndex + 1); + } catch (NumberFormatException | IndexOutOfBoundsException ignored) { } + } + + return false; + } + + private boolean testValue(NbtElement element) { + try { + if (element instanceof NbtString nbtString) //noinspection ConstantConditions + return matchString.matches(nbtString.asString()) || matchString.matches(Text.Serializer.fromJson(nbtString.asString()).getString()); + else if (element instanceof NbtInt nbtInt) + return nbtInt.equals(matchInteger); + else if (element instanceof NbtByte nbtByte) + return nbtByte.equals(matchByte); + else if (element instanceof NbtFloat nbtFloat) + return nbtFloat.equals(matchFloat); + else if (element instanceof NbtDouble nbtDouble) + return nbtDouble.equals(matchDouble); + else if (element instanceof NbtLong nbtLong) + return nbtLong.equals(matchLong); + else if (element instanceof NbtShort nbtShort) + return nbtShort.equals(matchShort); + else if (element instanceof NbtCompound nbtCompound) + return NbtHelper.matches(matchCompound, nbtCompound, true); + } catch (Exception ignored) { } + return false; + } + + protected static abstract class StringMatcher { + public abstract boolean matches(String value); + + public static class DirectMatcher extends StringMatcher { + protected final String pattern; + + public DirectMatcher(String pattern) { + this.pattern = pattern; + } + + @Override + public boolean matches(String value) { + return pattern.equals(value); + } + } + + public static class RegexMatcher extends StringMatcher { + protected final Pattern pattern; + + public RegexMatcher(String pattern) { + this(Pattern.compile(pattern)); + } + + protected RegexMatcher(Pattern pattern) { + this.pattern = pattern; + } + + @Override + public boolean matches(String value) { + return this.pattern.matcher(value).matches(); + } + } + + public static class PatternMatcher extends StringMatcher { + protected final String pattern; + + public PatternMatcher(String pattern) { + this.pattern = pattern; + } + + @Override + public boolean matches(String value) { + return matchesPattern(value, this.pattern, 0, value.length(), 0, pattern.length()); + } + + /** + * Author: Paul "prupe" Rupe
+ * Taken and modified from MCPatcher under public domain licensing.
+ * https://bitbucket.org/prupe/mcpatcher/src/1aa45839b2cd029143809edfa60ec59e5ef75f80/newcode/src/com/prupe/mcpatcher/mal/nbt/NBTRule.java#lines-269:301 + */ + protected boolean matchesPattern(String value, String pattern, int curV, int maxV, int curG, int maxG) { + for (; curG < maxG; curG++, curV++) { + char g = pattern.charAt(curG); + if (g == '*') { + while (true) { + if (matchesPattern(value, pattern, curV, maxV, curG + 1, maxG)) { + return true; + } + if (curV >= maxV) { + break; + } + curV++; + } + return false; + } else if (curV >= maxV) { + break; + } else if (g == '?') { + continue; + } + if (g == '\\' && curG + 1 < maxG) { + curG++; + g = pattern.charAt(curG); + } + + if (!charsEqual(g, value.charAt(curV))) + return false; + } + return curG == maxG && curV == maxV; + } + + protected boolean charsEqual(char p, char v) { + return p == v; + } + } + + public static class IRegexMatcher extends RegexMatcher { + public IRegexMatcher(String pattern) { + super(Pattern.compile(pattern, Pattern.CASE_INSENSITIVE)); + } + } + + public static class IPatternMatcher extends PatternMatcher { + public IPatternMatcher(String pattern) { + super(pattern.toLowerCase(Locale.ROOT)); + } + + @Override + protected boolean charsEqual(char p, char v) { + return p == v || p == Character.toLowerCase(v); + } + } + } +} diff --git a/defaults/src/main/resources/citresewn-defaults.accesswidener b/defaults/src/main/resources/citresewn-defaults.accesswidener index 22b4947..739fc5e 100644 --- a/defaults/src/main/resources/citresewn-defaults.accesswidener +++ b/defaults/src/main/resources/citresewn-defaults.accesswidener @@ -1,5 +1,8 @@ accessWidener v1 named +# ConditionNBT +accessible field net/minecraft/nbt/NbtCompound entries Ljava/util/Map; + # TypeItem accessible class net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride accessible class net/minecraft/client/render/model/json/ModelOverrideList$InlinedCondition -- cgit From 9a3952f2a17a94ff98494856b1ed9754d7b6ead6 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Feb 2022 08:31:18 +0200 Subject: Fixed ambiguous conditions list for type loading --- .../shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java | 2 +- .../shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java | 2 +- .../shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java | 2 +- .../shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java | 2 +- src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java | 2 +- src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java index 2b857fa..9f0beed 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java @@ -23,7 +23,7 @@ public class TypeArmor extends CITType { } @Override - public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java index f0f7540..1b5367b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java @@ -23,7 +23,7 @@ public class TypeElytra extends CITType { } @Override - public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index 3a340b1..566273a 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -35,7 +35,7 @@ public class TypeEnchantment extends CITType { } @Override - public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 9cb623e..02a2bf1 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -62,7 +62,7 @@ public class TypeItem extends CITType { } @Override - public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { for (CITCondition condition : conditions) if (condition instanceof ConditionItems conditionItems) items.addAll(Arrays.asList(conditionItems.items)); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index aed784a..468c1cb 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -13,7 +13,7 @@ import java.util.*; public abstract class CITType { public abstract Set typeProperties(); - public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; + public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 4c5b116..163435b 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -80,7 +80,7 @@ public class PackParser { public static CIT parseCIT(PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { CITType citType = CITRegistry.parseType(properties); - ArrayList conditions = new ArrayList<>(); + List conditions = new ArrayList<>(); Set ignoredProperties = citType.typeProperties(); -- cgit From 429b299266e9633e325e4d57e2a582fde1666e7a Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Feb 2022 08:52:52 +0200 Subject: Fixed item name from properties file name --- .../citresewn/defaults/cit/conditions/ConditionItems.java | 5 +++++ .../fabric/citresewn/defaults/cit/types/TypeItem.java | 11 ++++++++++- .../shsupercm/fabric/citresewn/pack/format/PropertyGroup.java | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java index 73b79e0..117aa47 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionItems.java @@ -26,6 +26,11 @@ public class ConditionItems extends ListCondition super(ItemCondition.class, ItemCondition::new); } + public ConditionItems(Item... items) { + this(); + this.items = items; + } + @Override public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { super.load(value, properties); diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 02a2bf1..aaca882 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -68,7 +68,16 @@ public class TypeItem extends CITType { items.addAll(Arrays.asList(conditionItems.items)); if (this.items.size() == 0) - throw new CITParsingException("Not targeting any item type", properties, -1); + try { + Identifier propertiesName = new Identifier(properties.stripName()); + if (!Registry.ITEM.containsId(propertiesName)) + throw new Exception(); + Item item = Registry.ITEM.get(propertiesName); + conditions.add(new ConditionItems(item)); + this.items.add(item); + } catch (Exception ignored) { + throw new CITParsingException("Not targeting any item type", properties, -1); + } Identifier assetIdentifier; PropertyValue modelProp = properties.getLastWithoutMetadata("citresewn", "model"); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java index b2806a2..5252330 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -49,6 +49,10 @@ public abstract class PropertyGroup { return value; } + public String stripName() { + return identifier.getPath().substring(identifier.getPath().lastIndexOf('/') + 1, identifier.getPath().length() - getExtension().length()); + } + public static PropertyGroup tryParseGroup(String packName, Identifier identifier, InputStream is) throws IOException { PropertyGroup group = null; if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) -- cgit From 6c3eb559865c291bc363f4c9697a257c4c2cc604 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Feb 2022 09:29:26 +0200 Subject: Fixed ambiguous model error log message --- .../fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 0c5f4f1..485268b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -9,7 +9,6 @@ import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.render.model.json.JsonUnbakedModel; import net.minecraft.client.render.model.json.ModelOverride; import net.minecraft.client.texture.TextureManager; -import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; @@ -68,7 +67,8 @@ public class ModelLoaderMixin { this.modelsToBake.put(id, unbakedModel); } } catch (Exception e) { - CITResewn.logErrorLoading(e.getMessage()); + CITResewn.logErrorLoading("Errored loading model in " + cit.propertiesIdentifier + " from " + cit.packName); + e.printStackTrace(); } }); -- cgit From f90a1bc5859036e6572932fbfa05b006503c1e29 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Feb 2022 10:09:43 +0200 Subject: Removed streams usage from item type --- .../citresewn/defaults/cit/types/TypeItem.java | 9 +++-- .../mixin/types/item/ModelLoaderMixin.java | 38 ++++++++++------------ 2 files changed, 23 insertions(+), 24 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index aaca882..2c431e1 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -467,21 +467,24 @@ public class TypeItem extends CITType { super(TypeItem.class, TypeItem::new, "item"); } - public Map>> loaded = new IdentityHashMap<>(); + public Set> loaded = new HashSet<>(); + public Map>> loadedTyped = new IdentityHashMap<>(); @Override public void load(List> parsedCITs) { + loaded.addAll(parsedCITs); for (CIT cit : parsedCITs) for (CITCondition condition : cit.conditions) if (condition instanceof ConditionItems items) for (Item item : items.items) if (item != null) - loaded.computeIfAbsent(item, i -> new LinkedHashSet<>()).add(cit); + loadedTyped.computeIfAbsent(item, i -> new LinkedHashSet<>()).add(cit); } @Override public void dispose() { loaded.clear(); + loadedTyped.clear(); } public CIT getCIT(CITContext context, int seed) { @@ -491,7 +494,7 @@ public class TypeItem extends CITType { public CIT getRealTimeCIT(CITContext context) { ((CITCacheItem) (Object) context.stack).citresewn$setMojankCITTypeItem(false); - Set> loadedForItemType = loaded.get(context.stack.getItem()); + Set> loadedForItemType = loadedTyped.get(context.stack.getItem()); if (loadedForItemType != null) for (CIT cit : loadedForItemType) if (cit.test(context)) { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 485268b..718cbca 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -25,6 +25,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; @@ -54,23 +55,20 @@ public class ModelLoaderMixin { return; info("Loading item CIT models..."); - CONTAINER.loaded.values().stream() // todo remove streams usage - .flatMap(Collection::stream) - .distinct().forEach(cit -> { - try { - cit.type.loadUnbakedAssets(resourceManager); - - for (JsonUnbakedModel unbakedModel : cit.type.unbakedAssets.values()) { - ResewnItemModelIdentifier id = new ResewnItemModelIdentifier(unbakedModel.id); - this.unbakedModels.put(id, unbakedModel); - this.modelsToLoad.addAll(unbakedModel.getModelDependencies()); - this.modelsToBake.put(id, unbakedModel); - } - } catch (Exception e) { - CITResewn.logErrorLoading("Errored loading model in " + cit.propertiesIdentifier + " from " + cit.packName); - e.printStackTrace(); - } - }); + for (CIT cit : CONTAINER.loaded) + try { + cit.type.loadUnbakedAssets(resourceManager); + + for (JsonUnbakedModel unbakedModel : cit.type.unbakedAssets.values()) { + ResewnItemModelIdentifier id = new ResewnItemModelIdentifier(unbakedModel.id); + this.unbakedModels.put(id, unbakedModel); + this.modelsToLoad.addAll(unbakedModel.getModelDependencies()); + this.modelsToBake.put(id, unbakedModel); + } + } catch (Exception e) { + CITResewn.logErrorLoading("Errored loading model in " + cit.propertiesIdentifier + " from " + cit.packName); + e.printStackTrace(); + } TypeItem.GENERATED_SUB_CITS_SEEN.clear(); } @@ -83,9 +81,7 @@ public class ModelLoaderMixin { profiler.push("citresewn:type_item_linking"); info("Linking baked models to item CITs..."); - CONTAINER.loaded.values().stream() // todo remove streams usage - .flatMap(Collection::stream) - .distinct().forEach(cit -> { + for (CIT cit : CONTAINER.loaded) { for (Map.Entry, JsonUnbakedModel> citModelEntry : cit.type.unbakedAssets.entrySet()) { if (citModelEntry.getKey() == null) { cit.type.bakedModel = this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id)); @@ -98,7 +94,7 @@ public class ModelLoaderMixin { } } cit.type.unbakedAssets = null; - }); + } profiler.pop(); } -- cgit From 6c8d3aed55abb448422287c1718f4f3d90ec68a9 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 20 Feb 2022 10:10:27 +0200 Subject: Cleaned some stuff up --- .../defaults/cit/conditions/ConditionHand.java | 33 ++++---- .../citresewn/defaults/cit/types/TypeItem.java | 4 + .../defaults/common/ResewnItemModelIdentifier.java | 2 +- .../defaults/mixin/common/ModelLoaderMixin.java | 87 ++++++++++++++++++++++ .../mixin/types/item/ItemRendererMixin.java | 20 +++-- .../mixin/types/item/ModelLoaderMixin.java | 64 ---------------- 6 files changed, 117 insertions(+), 93 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java index 5325e28..0405cbc 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionHand.java @@ -4,9 +4,6 @@ import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.cit.CITContext; import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.EnumCondition; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; public class ConditionHand extends EnumCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) @@ -34,23 +31,19 @@ public class ConditionHand extends EnumCondition { } protected enum Hand implements EnumCondition.Aliased { - MAINHAND { - @Override - public String[] getAliases() { - return new String[] { "main", "mainhand", "main_hand" }; - } - }, - OFFHAND { - @Override - public String[] getAliases() { - return new String[] { "off", "offhand", "off_hand" }; - } - }, - ANY { - @Override - public String[] getAliases() { - return new String[] { "any", "either", "*" }; - } + MAINHAND("main", "mainhand", "main_hand"), + OFFHAND("off", "offhand", "off_hand"), + ANY("any", "either", "*"); + + private final String[] aliases; + + Hand(String... aliases) { + this.aliases = aliases; + } + + @Override + public String[] getAliases() { + return this.aliases; } } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 2c431e1..f37fd6b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -39,6 +39,10 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; +/** + * Do not go through this class, it looks awful because it was ported from a "proof of concept".
+ * The whole type will be rewritten at some point. + */ public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java index c3f30f5..498f61f 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/common/ResewnItemModelIdentifier.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.defaults.common; import net.minecraft.util.Identifier; /** - * Marks models as cit item models. + * Marks models as cit models. */ public class ResewnItemModelIdentifier extends Identifier { public ResewnItemModelIdentifier(String id) { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java new file mode 100644 index 0000000..8ed0e20 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/common/ModelLoaderMixin.java @@ -0,0 +1,87 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.common; + +import com.mojang.datafixers.util.Either; +import net.minecraft.client.render.model.ModelLoader; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import org.apache.commons.io.IOUtils; +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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; +import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item.JsonUnbakedModelAccessor; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Will be rewritten at some point. + */ +@Mixin(ModelLoader.class) +public class ModelLoaderMixin { + @Shadow @Final private ResourceManager resourceManager; + + @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD")) + public void citresewn$forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable cir) { + if (id instanceof ResewnItemModelIdentifier) { + InputStream is = null; + Resource resource = null; + try { + JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8)); + json.id = id.toString(); + json.id = json.id.substring(0, json.id.length() - 5); + + ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { + Optional left = original.left(); + if (left.isPresent()) { + String originalPath = left.get().getTextureId().getPath(); + String[] split = originalPath.split("/"); + if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { + Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); + if (resolvedIdentifier != null) + return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); + } + } + return original; + }); + + Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); + if (parentId != null) { + String[] parentIdPathSplit = parentId.getPath().split("/"); + if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { + parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); + if (parentId != null) + ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); + } + } + + json.getOverrides().replaceAll(override -> { + String[] modelIdPathSplit = override.getModelId().getPath().split("/"); + if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { + Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); + if (resolvedOverridePath != null) + return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); + } + + return override; + }); + + cir.setReturnValue(json); + } catch (Exception ignored) { + } finally { + IOUtils.closeQuietly(is, resource); + } + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java index 9f6063b..49e26af 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java @@ -29,11 +29,15 @@ import java.lang.ref.WeakReference; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; +/** + * Do not go through this class, it looks awful because it was ported from a "proof of concept".
+ * The whole type will be rewritten at some point. + */ @Mixin(ItemRenderer.class) public class ItemRendererMixin { @Shadow @Final private ItemModels models; - private static WeakReference mojankCITModel = null; + private WeakReference citresewn$mojankCITModel = null; @Inject(method = "getModel", cancellable = true, at = @At("HEAD")) private void citresewn$getItemModel(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { @@ -55,19 +59,19 @@ public class ItemRendererMixin { if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) return; - mojankCITModel = null; + citresewn$mojankCITModel = null; if (((TypeItem.CITCacheItem) (Object) stack).citresewn$isMojankCITTypeItem()) { boolean bl = renderMode == ModelTransformation.Mode.GUI || renderMode == ModelTransformation.Mode.GROUND || renderMode == ModelTransformation.Mode.FIXED; if (bl) - mojankCITModel = new WeakReference<>(model); + citresewn$mojankCITModel = new WeakReference<>(model); else { // rendered in hand model of trident/spyglass if (stack.isOf(Items.TRIDENT)) - mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); + citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); else if (stack.isOf(Items.SPYGLASS)) - mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); + citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); } } else - mojankCITModel = null; + citresewn$mojankCITModel = null; } @ModifyVariable(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "LOAD", ordinal = 0, target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"), argsOnly = true) @@ -75,8 +79,8 @@ public class ItemRendererMixin { if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) return original; - if (mojankCITModel != null) - return mojankCITModel.get(); + if (citresewn$mojankCITModel != null) + return citresewn$mojankCITModel.get(); return original; } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 718cbca..c7fa70d 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -1,6 +1,5 @@ package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; -import com.mojang.datafixers.util.Either; import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.ModelLoader; @@ -9,12 +8,9 @@ import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.render.model.json.JsonUnbakedModel; import net.minecraft.client.render.model.json.ModelOverride; import net.minecraft.client.texture.TextureManager; -import net.minecraft.client.util.SpriteIdentifier; -import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; import net.minecraft.util.profiler.Profiler; -import org.apache.commons.io.IOUtils; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -26,22 +22,16 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITType; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; -import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnTextureIdentifier; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.stream.Collectors; import static shcm.shsupercm.fabric.citresewn.CITResewn.info; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem.CONTAINER; @Mixin(ModelLoader.class) public class ModelLoaderMixin { - @Shadow @Final private ResourceManager resourceManager; @Shadow @Final private Set modelsToLoad; @Shadow @Final private Map modelsToBake; @Shadow @Final private Map unbakedModels; @@ -99,60 +89,6 @@ public class ModelLoaderMixin { profiler.pop(); } - - @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD")) - public void citresewn$forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable cir) { - if (id instanceof ResewnItemModelIdentifier) { - InputStream is = null; - Resource resource = null; - try { - JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8)); - json.id = id.toString(); - json.id = json.id.substring(0, json.id.length() - 5); - - ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { - Optional left = original.left(); - if (left.isPresent()) { - String originalPath = left.get().getTextureId().getPath(); - String[] split = originalPath.split("/"); - if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { - Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); - if (resolvedIdentifier != null) - return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); - } - } - return original; - }); - - Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId(); - if (parentId != null) { - String[] parentIdPathSplit = parentId.getPath().split("/"); - if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); - if (parentId != null) - ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); - } - } - - json.getOverrides().replaceAll(override -> { - String[] modelIdPathSplit = override.getModelId().getPath().split("/"); - if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); - if (resolvedOverridePath != null) - return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); - } - - return override; - }); - - cir.setReturnValue(json); - } catch (Exception ignored) { - } finally { - IOUtils.closeQuietly(is, resource); - } - } - } - @ModifyArg(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;")) public Identifier citresewn$fixDuplicatePrefixSuffix(Identifier original) { -- cgit From 93efe2624458a66da06d7edc572924be1c8c7168 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 20 Feb 2022 10:52:50 +0200 Subject: Ported elytra type --- .../citresewn/defaults/cit/types/TypeElytra.java | 34 +++++++++++++++--- .../types/elytra/ElytraFeatureRendererMixin.java | 41 ++++++++++++++++++++++ .../mixin/types/elytra/ItemStackMixin.java | 16 +++++++++ .../shsupercm/fabric/citresewn/cit/CITType.java | 2 +- 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ElytraFeatureRendererMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ItemStackMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java index 1b5367b..d50d084 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java @@ -2,14 +2,15 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.*; +import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -17,6 +18,8 @@ public class TypeElytra extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); + public Identifier texture; + @Override public Set typeProperties() { return Set.of(PropertyKey.of("texture")); @@ -24,7 +27,12 @@ public class TypeElytra extends CITType { @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + if (conditions.removeIf(condition -> condition instanceof ConditionItems)) + warn("Type elytra does not support the items condition", null, properties); + texture = resolveAsset(properties.identifier, properties.getLastWithoutMetadata("citresewn", "texture"), "textures", ".png", resourceManager); + if (texture == null) + throw new CITParsingException("Texture not specified", properties, -1); } public static class Container extends CITTypeContainer { @@ -32,14 +40,32 @@ public class TypeElytra extends CITType { super(TypeElytra.class, TypeElytra::new, "elytra"); } + public Set> loaded = new HashSet<>(); + @Override public void load(List> parsedCITs) { - + loaded.addAll(parsedCITs); } @Override public void dispose() { + loaded.clear(); + } + public CIT getCIT(CITContext context) { + return ((CITCacheElytra) (Object) context.stack).citresewn$getCacheTypeElytra().get(context).get(); } + + public CIT getRealTimeCIT(CITContext context) { + for (CIT cit : loaded) + if (cit.test(context)) + return cit; + + return null; + } + } + + public interface CITCacheElytra { + CITCache.Single citresewn$getCacheTypeElytra(); } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ElytraFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ElytraFeatureRendererMixin.java new file mode 100644 index 0000000..ab4d420 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ElytraFeatureRendererMixin.java @@ -0,0 +1,41 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.elytra; + +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.entity.feature.ElytraFeatureRenderer; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeElytra; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeElytra.CONTAINER; + +@Mixin(ElytraFeatureRenderer.class) +public class ElytraFeatureRendererMixin { + @Shadow @Mutable @Final private static Identifier SKIN; + + private final static Identifier citresewn$ORIGINAL_SKIN = SKIN; + + @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/entity/LivingEntity;FFFFFF)V", at = @At("HEAD")) + public void citresewn$render(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { + if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + return; + + ItemStack equippedStack = livingEntity.getEquippedStack(EquipmentSlot.CHEST); + if (!equippedStack.isOf(Items.ELYTRA)) + return; + + CIT cit = CONTAINER.getCIT(new CITContext(equippedStack, livingEntity.getWorld(), livingEntity)); + SKIN = cit == null ? citresewn$ORIGINAL_SKIN : cit.type.texture; + } +} \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ItemStackMixin.java new file mode 100644 index 0000000..a751fb9 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/elytra/ItemStackMixin.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.elytra; + +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import shcm.shsupercm.fabric.citresewn.cit.CITCache; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeElytra; + +@Mixin(ItemStack.class) +public class ItemStackMixin implements TypeElytra.CITCacheElytra { + private final CITCache.Single citresewn$cacheTypeElytra = new CITCache.Single<>(TypeElytra.CONTAINER::getRealTimeCIT); + + @Override + public CITCache.Single citresewn$getCacheTypeElytra() { + return this.citresewn$cacheTypeElytra; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index 468c1cb..c3bb134 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -16,7 +16,7 @@ public abstract class CITType { public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { - CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); + CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value == null ? -1 : value.position())); } /** -- cgit From adf0277b743ed79a4bad6d3c6011f2cec3bea600 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 20 Feb 2022 16:31:39 +0200 Subject: Ported armor type --- .../citresewn/defaults/cit/types/TypeArmor.java | 78 ++++++++++++++++++++-- .../types/armor/ArmorFeatureRendererMixin.java | 53 +++++++++++++++ .../defaults/mixin/types/armor/ItemStackMixin.java | 16 +++++ 3 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ItemStackMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java index 9f0beed..d97bf67 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeArmor.java @@ -1,22 +1,27 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.item.ArmorItem; +import net.minecraft.item.Item; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.*; +import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionItems; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; -import java.util.List; -import java.util.Set; +import java.util.*; public class TypeArmor extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); + public final Map textures = new HashMap<>(); + @Override public Set typeProperties() { return Set.of(PropertyKey.of("texture")); @@ -24,7 +29,37 @@ public class TypeArmor extends CITType { @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + boolean itemsConditionPresent = false; + for (CITCondition condition : conditions) + if (condition instanceof ConditionItems conditionItems) + for (Item item : conditionItems.items) + if (item instanceof ArmorItem) + itemsConditionPresent = true; + else + throw new CITParsingException("This type only accepts armor items for the items condition", properties, -1); + + if (!itemsConditionPresent) + try { + Identifier propertiesName = new Identifier(properties.stripName()); + if (!Registry.ITEM.containsId(propertiesName)) + throw new Exception(); + Item item = Registry.ITEM.get(propertiesName); + if (!(item instanceof ArmorItem)) + throw new Exception(); + conditions.add(new ConditionItems(item)); + } catch (Exception ignored) { + throw new CITParsingException("Not targeting any item type", properties, -1); + } + for (PropertyValue propertyValue : properties.get("citresewn", "texture")) { + Identifier identifier = resolveAsset(properties.identifier, propertyValue, "textures", ".png", resourceManager); + if (identifier == null) + throw new CITParsingException("Could not resolve texture", properties, propertyValue.position()); + + textures.put(propertyValue.keyMetadata(), identifier); + } + if (textures.size() == 0) + throw new CITParsingException("Texture not specified", properties, -1); } public static class Container extends CITTypeContainer { @@ -32,14 +67,45 @@ public class TypeArmor extends CITType { super(TypeArmor.class, TypeArmor::new, "armor"); } + public Set> loaded = new HashSet<>(); + public Map>> loadedTyped = new IdentityHashMap<>(); + @Override public void load(List> parsedCITs) { - + loaded.addAll(parsedCITs); + for (CIT cit : parsedCITs) + for (CITCondition condition : cit.conditions) + if (condition instanceof ConditionItems items) + for (Item item : items.items) + if (item instanceof ArmorItem armorItem) + loadedTyped.computeIfAbsent(armorItem, i -> new LinkedHashSet<>()).add(cit); } @Override public void dispose() { + loaded.clear(); + loadedTyped.clear(); + } + public CIT getCIT(CITContext context) { + return ((CITCacheArmor) (Object) context.stack).citresewn$getCacheTypeArmor().get(context).get(); } + + public CIT getRealTimeCIT(CITContext context) { + if (!(context.stack.getItem() instanceof ArmorItem)) + return null; + + Set> loadedForItemType = loadedTyped.get(context.stack.getItem()); + if (loadedForItemType != null) + for (CIT cit : loadedForItemType) + if (cit.test(context)) + return cit; + + return null; + } + } + + public interface CITCacheArmor { + CITCache.Single citresewn$getCacheTypeArmor(); } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java new file mode 100644 index 0000000..fba7159 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java @@ -0,0 +1,53 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.armor; + +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.entity.feature.ArmorFeatureRenderer; +import net.minecraft.client.render.entity.model.BipedEntityModel; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ArmorItem; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeArmor; + +import java.util.Map; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeArmor.CONTAINER; + +@Mixin(ArmorFeatureRenderer.class) +public class ArmorFeatureRendererMixin, A extends BipedEntityModel> { + private Map citresewn$cachedTextures = null; + + @Inject(method = "renderArmor", at = @At("HEAD")) + public void citresewn$renderArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, T entity, EquipmentSlot armorSlot, int light, A model, CallbackInfo ci) { + citresewn$cachedTextures = null; + if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + return; + + ItemStack equippedStack = entity.getEquippedStack(armorSlot); + + CIT cit = CONTAINER.getCIT(new CITContext(equippedStack, entity.getWorld(), entity)); + if (cit != null) + citresewn$cachedTextures = cit.type.textures; + } + + @Inject(method = "getArmorTexture", cancellable = true, at = @At("HEAD")) + private void citresewn$getArmorTexture(ArmorItem item, boolean legs, String overlay, CallbackInfoReturnable cir) { + if (citresewn$cachedTextures == null) + return; + + Identifier identifier = citresewn$cachedTextures.get(item.getMaterial().getName() + "_layer_" + (legs ? "2" : "1") + (overlay == null ? "" : "_" + overlay)); + if (identifier != null) + cir.setReturnValue(identifier); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ItemStackMixin.java new file mode 100644 index 0000000..85b0d22 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ItemStackMixin.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.armor; + +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import shcm.shsupercm.fabric.citresewn.cit.CITCache; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeArmor; + +@Mixin(ItemStack.class) +public class ItemStackMixin implements TypeArmor.CITCacheArmor { + private final CITCache.Single citresewn$cacheTypeArmor = new CITCache.Single<>(TypeArmor.CONTAINER::getRealTimeCIT); + + @Override + public CITCache.Single citresewn$getCacheTypeArmor() { + return this.citresewn$cacheTypeArmor; + } +} -- cgit From 87baf18026aa53bba953888ac73ed656ceaf737c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Tue, 22 Feb 2022 19:54:53 +0200 Subject: Started porting enchantment type --- .../defaults/cit/types/TypeEnchantment.java | 152 ++++++++++++++++++++- .../mixin/types/enchantment/ItemStackMixin.java | 16 +++ 2 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index 566273a..760cc83 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -1,17 +1,21 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.client.render.RenderPhase; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import org.lwjgl.opengl.GL11; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.cit.CIT; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.cit.*; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; -import java.util.List; -import java.util.Set; +import java.lang.ref.WeakReference; +import java.util.*; + +import static com.mojang.blaze3d.systems.RenderSystem.*; +import static org.lwjgl.opengl.GL11.*; public class TypeEnchantment extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) @@ -34,6 +38,12 @@ public class TypeEnchantment extends CITType { PropertyKey.of("a")); } + public Identifier texture; + public float speed, rotation, duration, r, g, b, a; + public int layer; + public boolean useGlint, blur; + public Blend blend; + @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { @@ -44,14 +54,146 @@ public class TypeEnchantment extends CITType { super(TypeEnchantment.class, TypeEnchantment::new, "enchantment"); } + public List> loaded = new ArrayList<>(); + public List>> loadedLayered = new ArrayList<>(); + @Override public void load(List> parsedCITs) { + loaded.addAll(parsedCITs); + Map>> layers = new HashMap<>(); + for (CIT cit : loaded) + layers.computeIfAbsent(cit.type.layer, i -> new ArrayList<>()).add(cit); + loadedLayered.clear(); + layers.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .forEachOrdered(layer -> loadedLayered.add(layer.getValue())); } @Override public void dispose() { + loaded.clear(); + loadedLayered.clear(); + } + + public void apply(CITContext context) { + if (context == null) { + //todo clear + return; + } + + List>> cits = ((CITCacheEnchantment) (Object) context.stack).citresewn$getCacheTypeEnchantment().get(context); + + //todo apply + } + + public List> getRealTimeCIT(CITContext context) { + List> cits = new ArrayList<>(); + for (List> layer : loadedLayered) + for (CIT cit : layer) + if (cit.test(context)) { + cits.add(cit); + break; + } + return cits; + } + } + + public static class Blend extends RenderPhase.Transparency { + private final int src, dst, srcAlpha, dstAlpha; + + private Blend(String name, int src, int dst, int srcAlpha, int dstAlpha) { + super(name + "_glint_transparency", null, null); + this.src = src; + this.dst = dst; + this.srcAlpha = srcAlpha; + this.dstAlpha = dstAlpha; + } + + private Blend(String name, int src, int dst) { + this(name, src, dst, GL_ZERO, GL_ONE); + } + + @Override + public void startDrawing() { + enableBlend(); + blendFuncSeparate(src, dst, srcAlpha, dstAlpha); } + + @Override + public void endDrawing() { + defaultBlendFunc(); + disableBlend(); + } + + public static Blend getBlend(String blendString) throws BlendFormatException { + try { //check named blending function + return Named.valueOf(blendString.toUpperCase(Locale.ENGLISH)).blend; + } catch (IllegalArgumentException ignored) { // create custom blending function + try { + String[] split = blendString.split(" "); + int src, dst, srcAlpha, dstAlpha; + if (split.length == 2) { + src = parseGLConstant(split[0]); + dst = parseGLConstant(split[1]); + srcAlpha = GL_ZERO; + dstAlpha = GL_ONE; + } else if (split.length == 4) { + src = parseGLConstant(split[0]); + dst = parseGLConstant(split[1]); + srcAlpha = parseGLConstant(split[2]); + dstAlpha = parseGLConstant(split[3]); + } else + throw new Exception(); + + return new Blend("custom_" + src + "_" + dst + "_" + srcAlpha + "_" + dstAlpha, src, dst, srcAlpha, dstAlpha); + } catch (Exception e) { + throw new BlendFormatException(); + } + } + } + + private enum Named { + REPLACE(new Blend("replace", 0, 0) { + @Override + public void startDrawing() { + disableBlend(); + } + }), + GLINT(new Blend("glint", GL_SRC_COLOR, GL_ONE)), + ALPHA(new Blend("alpha", GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)), + ADD(new Blend("add", GL_SRC_ALPHA, GL_ONE)), + SUBTRACT(new Blend("subtract", GL_ONE_MINUS_DST_COLOR, GL_ZERO)), + MULTIPLY(new Blend("multiply", GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA)), + DODGE(new Blend("dodge", GL_ONE, GL_ONE)), + BURN(new Blend("burn", GL_ZERO, GL_ONE_MINUS_SRC_COLOR)), + SCREEN(new Blend("screen", GL_ONE, GL_ONE_MINUS_SRC_COLOR)), + OVERLAY(new Blend("overlay", GL_DST_COLOR, GL_SRC_COLOR)); + + public final Blend blend; + + Named(Blend blend) { + this.blend = blend; + } + } + + private static int parseGLConstant(String s) throws Exception { + try { + return GL11.class.getDeclaredField(s).getInt(null); + } catch (NoSuchFieldException ignored) { } + + return s.startsWith("0x") ? Integer.parseInt(s.substring(2), 16) : Integer.parseInt(s); + } + + public static class BlendFormatException extends Exception { + public BlendFormatException() { + super("Not a valid blending method"); + } + } + } + + public interface CITCacheEnchantment { + CITCache.MultiList citresewn$getCacheTypeEnchantment(); } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java new file mode 100644 index 0000000..bbd61d9 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import shcm.shsupercm.fabric.citresewn.cit.CITCache; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment; + +@Mixin(ItemStack.class) +public class ItemStackMixin implements TypeEnchantment.CITCacheEnchantment { + private final CITCache.MultiList citresewn$cacheTypeEnchantment = new CITCache.MultiList<>(TypeEnchantment.CONTAINER::getRealTimeCIT); + + @Override + public CITCache.MultiList citresewn$getCacheTypeEnchantment() { + return this.citresewn$cacheTypeEnchantment; + } +} -- cgit From 70b04c19b5c6da9d63a095fb7a2e17fd722c9cb1 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Wed, 23 Feb 2022 11:13:12 +0200 Subject: Ported more of the enchantment type --- .../defaults/cit/types/TypeEnchantment.java | 244 ++++++++++++++++++--- .../defaults/config/CITResewnDefaultsConfig.java | 2 +- .../enchantment/BufferBuilderStorageAccessor.java | 15 ++ .../types/enchantment/RenderPhaseAccessor.java | 21 ++ .../citresewn/pack/format/PropertyGroup.java | 8 + 5 files changed, 253 insertions(+), 37 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/BufferBuilderStorageAccessor.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/RenderPhaseAccessor.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index 760cc83..5fd6e3c 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -1,18 +1,28 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; +import com.mojang.blaze3d.systems.RenderSystem; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; -import net.minecraft.client.render.RenderPhase; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.*; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; +import net.minecraft.util.Util; +import net.minecraft.util.math.Matrix4f; +import net.minecraft.util.math.Vec3f; import org.lwjgl.opengl.GL11; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.cit.*; +import shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsConfig; +import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment.BufferBuilderStorageAccessor; +import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment.RenderPhaseAccessor; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.lang.ref.WeakReference; import java.util.*; +import java.util.function.Consumer; import static com.mojang.blaze3d.systems.RenderSystem.*; import static org.lwjgl.opengl.GL11.*; @@ -29,24 +39,68 @@ public class TypeEnchantment extends CITType { PropertyKey.of("speed"), PropertyKey.of("rotation"), PropertyKey.of("duration"), - PropertyKey.of("blend"), - PropertyKey.of("useGlint"), - PropertyKey.of("blur"), PropertyKey.of("r"), PropertyKey.of("g"), PropertyKey.of("b"), - PropertyKey.of("a")); + PropertyKey.of("a"), + PropertyKey.of("useGlint"), + PropertyKey.of("blur"), + PropertyKey.of("blend")); } public Identifier texture; - public float speed, rotation, duration, r, g, b, a; public int layer; + public float speed, rotation, duration, r, g, b, a; public boolean useGlint, blur; public Blend blend; + public final Map renderLayers = new EnumMap<>(GlintRenderLayer.class); + private final WrappedMethodIntensity methodIntensity = new WrappedMethodIntensity(); + @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { + PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture"); + if (textureProp == null) + throw new CITParsingException("No texture specified", properties, -1); + this.texture = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); + if (this.texture == null) + throw new CITParsingException("Could not resolve texture", properties, textureProp.position()); + + PropertyValue layerProp = properties.getLastWithoutMetadataOrDefault("0", "citresewn", "layer"); + try { + this.layer = Integer.parseInt(layerProp.value()); + } catch (Exception e) { + throw new CITParsingException("Could not parse integer", properties, layerProp.position(), e); + } + this.speed = parseFloatOrZero("speed", properties); + this.rotation = parseFloatOrZero("rotation", properties); + this.duration = parseFloatOrZero("duration", properties); + this.r = parseFloatOrZero("r", properties); + this.g = parseFloatOrZero("g", properties); + this.b = parseFloatOrZero("b", properties); + this.a = parseFloatOrZero("a", properties); + + this.useGlint = Boolean.parseBoolean(properties.getLastWithoutMetadataOrDefault("false", "citresewn", "useGlint").value()); + this.blur = Boolean.parseBoolean(properties.getLastWithoutMetadataOrDefault("true", "citresewn", "blur").value()); + + PropertyValue blendProp = properties.getLastWithoutMetadataOrDefault("add", "citresewn", "blend"); + try { + this.blend = Blend.getBlend(blendProp.value()); + } catch (Exception e) { + throw new CITParsingException("Could not parse blending method", properties, blendProp.position(), e); + } + } + + private float parseFloatOrZero(String propertyName, PropertyGroup properties) throws CITParsingException { + PropertyValue property = properties.getLastWithoutMetadata("citresewn", propertyName); + if (property == null) + return 0f; + try { + return Float.parseFloat(property.value()); + } catch (Exception e) { + throw new CITParsingException("Could not parse float", properties, property.position(), e); + } } public static class Container extends CITTypeContainer { @@ -57,6 +111,9 @@ public class TypeEnchantment extends CITType { public List> loaded = new ArrayList<>(); public List>> loadedLayered = new ArrayList<>(); + private List> appliedContext = null; + public boolean shouldApply = false; + @Override public void load(List> parsedCITs) { loaded.addAll(parsedCITs); @@ -68,23 +125,47 @@ public class TypeEnchantment extends CITType { layers.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(layer -> loadedLayered.add(layer.getValue())); + + for (CIT cit : loaded) + for (GlintRenderLayer glintLayer : GlintRenderLayer.values()) { + RenderLayer renderLayer = glintLayer.build(cit.type, cit.propertiesIdentifier); + + cit.type.renderLayers.put(glintLayer, renderLayer); + ((BufferBuilderStorageAccessor) MinecraftClient.getInstance().getBufferBuilders()).getEntityBuilders().put(renderLayer, new BufferBuilder(renderLayer.getExpectedBufferSize())); + } } @Override public void dispose() { + appliedContext = null; + + for (CIT cit : loaded) + for (RenderLayer renderLayer : cit.type.renderLayers.values()) + ((BufferBuilderStorageAccessor) MinecraftClient.getInstance().getBufferBuilders()).getEntityBuilders().remove(renderLayer); + loaded.clear(); loadedLayered.clear(); } - public void apply(CITContext context) { + public void setContext(CITContext context) { if (context == null) { - //todo clear + appliedContext = null; return; } List>> cits = ((CITCacheEnchantment) (Object) context.stack).citresewn$getCacheTypeEnchantment().get(context); - //todo apply + appliedContext = new ArrayList<>(); + if (cits != null) + for (WeakReference> citRef : cits) + if (citRef != null) { + CIT cit = citRef.get(); + if (cit != null) + appliedContext.add(cit); + } + + if (appliedContext.isEmpty()) + appliedContext = null; } public List> getRealTimeCIT(CITContext context) { @@ -99,6 +180,107 @@ public class TypeEnchantment extends CITType { return cits; } } + + public enum GlintRenderLayer { + ARMOR_GLINT("armor_glint", 8f, layer -> layer + .shader(RenderPhaseAccessor.ARMOR_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST()) + .layering(RenderPhaseAccessor.VIEW_OFFSET_Z_LAYERING())), + ARMOR_ENTITY_GLINT("armor_entity_glint", 0.16f, layer -> layer + .shader(RenderPhaseAccessor.ARMOR_ENTITY_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST()) + .layering(RenderPhaseAccessor.VIEW_OFFSET_Z_LAYERING())), + GLINT_TRANSLUCENT("glint_translucent", 8f, layer -> layer + .shader(RenderPhaseAccessor.TRANSLUCENT_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST()) + .target(RenderPhaseAccessor.ITEM_TARGET())), + GLINT("glint", 8f, layer -> layer + .shader(RenderPhaseAccessor.GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST())), + DIRECT_GLINT("glint_direct", 8f, layer -> layer + .shader(RenderPhaseAccessor.DIRECT_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST())), + ENTITY_GLINT("entity_glint", 0.16f, layer -> layer + .shader(RenderPhaseAccessor.ENTITY_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST()) + .target(RenderPhaseAccessor.ITEM_TARGET())), + DIRECT_ENTITY_GLINT("entity_glint_direct", 0.16f, layer -> layer + .shader(RenderPhaseAccessor.DIRECT_ENTITY_GLINT_SHADER()) + .writeMaskState(RenderPhaseAccessor.COLOR_MASK()) + .cull(RenderPhaseAccessor.DISABLE_CULLING()) + .depthTest(RenderPhaseAccessor.EQUAL_DEPTH_TEST())); + + public final String name; + private final Consumer setup; + private final float scale; + + GlintRenderLayer(String name, float scale, Consumer setup) { + this.name = name; + this.scale = scale; + this.setup = setup; + } + + public RenderLayer build(TypeEnchantment enchantment, Identifier propertiesIdentifier) { + final float speed = enchantment.speed, rotation = enchantment.rotation, r = enchantment.r, g = enchantment.g, b = enchantment.b, a = enchantment.a; + final WrappedMethodIntensity methodIntensity = enchantment.methodIntensity; + RenderLayer.MultiPhaseParameters.Builder layer = RenderLayer.MultiPhaseParameters.builder() + .texture(new RenderPhase.Texture(enchantment.texture, enchantment.blur, false)) + .texturing(new RenderPhase.Texturing("citresewn_glint_texturing", () -> { + float l = Util.getMeasuringTimeMs() * CITResewnDefaultsConfig.INSTANCE.type_enchantment_scroll_multiplier * speed; + float x = (l % 110000f) / 110000f; + float y = (l % 30000f) / 30000f; + Matrix4f matrix4f = Matrix4f.translate(-x, y, 0.0f); + matrix4f.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(rotation + 10f)); + matrix4f.multiply(Matrix4f.scale(scale, scale, scale)); + setTextureMatrix(matrix4f); + + setShaderColor(r, g, b, a * methodIntensity.intensity); + }, () -> { + RenderSystem.resetTextureMatrix(); + + setShaderColor(1f, 1f, 1f, 1f); + })) + .transparency(enchantment.blend); + + this.setup.accept(layer); + + return RenderLayer.of("citresewn:enchantment_" + this.name + ":" + propertiesIdentifier.toString(), + VertexFormats.POSITION_TEXTURE, + VertexFormat.DrawMode.QUADS, + 256, + layer.build(false)); + } + + public VertexConsumer tryApply(VertexConsumer base, RenderLayer baseLayer, VertexConsumerProvider provider) { + if (!CONTAINER.shouldApply || CONTAINER.appliedContext == null || CONTAINER.appliedContext.size() == 0) + return null; + + VertexConsumer[] layers = new VertexConsumer[Math.min(CONTAINER.appliedContext.size(), Integer.MAX_VALUE /*todo cap global property*/)]; + + for (int i = 0; i < layers.length; i++) + layers[i] = provider.getBuffer(CONTAINER.appliedContext.get(i).type.renderLayers.get(GlintRenderLayer.this)); + + provider.getBuffer(baseLayer); // refresh base layer for armor consumer + + return base == null ? VertexConsumers.union(layers) : VertexConsumers.union(VertexConsumers.union(layers), base); + } + } + + private static class WrappedMethodIntensity { + public float intensity = 1f; + } public static class Blend extends RenderPhase.Transparency { private final int src, dst, srcAlpha, dstAlpha; @@ -127,30 +309,26 @@ public class TypeEnchantment extends CITType { disableBlend(); } - public static Blend getBlend(String blendString) throws BlendFormatException { + public static Blend getBlend(String blendString) throws Exception { try { //check named blending function return Named.valueOf(blendString.toUpperCase(Locale.ENGLISH)).blend; } catch (IllegalArgumentException ignored) { // create custom blending function - try { - String[] split = blendString.split(" "); - int src, dst, srcAlpha, dstAlpha; - if (split.length == 2) { - src = parseGLConstant(split[0]); - dst = parseGLConstant(split[1]); - srcAlpha = GL_ZERO; - dstAlpha = GL_ONE; - } else if (split.length == 4) { - src = parseGLConstant(split[0]); - dst = parseGLConstant(split[1]); - srcAlpha = parseGLConstant(split[2]); - dstAlpha = parseGLConstant(split[3]); - } else - throw new Exception(); - - return new Blend("custom_" + src + "_" + dst + "_" + srcAlpha + "_" + dstAlpha, src, dst, srcAlpha, dstAlpha); - } catch (Exception e) { - throw new BlendFormatException(); - } + String[] split = blendString.split("\\p{Zs}+"); + int src, dst, srcAlpha, dstAlpha; + if (split.length == 2) { + src = parseGLConstant(split[0]); + dst = parseGLConstant(split[1]); + srcAlpha = GL_ZERO; + dstAlpha = GL_ONE; + } else if (split.length == 4) { + src = parseGLConstant(split[0]); + dst = parseGLConstant(split[1]); + srcAlpha = parseGLConstant(split[2]); + dstAlpha = parseGLConstant(split[3]); + } else + throw new Exception(); + + return new Blend("custom_" + src + "_" + dst + "_" + srcAlpha + "_" + dstAlpha, src, dst, srcAlpha, dstAlpha); } } @@ -185,12 +363,6 @@ public class TypeEnchantment extends CITType { return s.startsWith("0x") ? Integer.parseInt(s.substring(2), 16) : Integer.parseInt(s); } - - public static class BlendFormatException extends Exception { - public BlendFormatException() { - super("Not a valid blending method"); - } - } } public interface CITCacheEnchantment { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java index 0787fba..791205b 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java @@ -8,7 +8,7 @@ import shcm.shsupercm.fabric.citresewn.CITResewn; import java.io.*; public class CITResewnDefaultsConfig { - + public float type_enchantment_scroll_multiplier = 1f; private static final File FILE = new File("config/citresewn-defaults.json"); diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/BufferBuilderStorageAccessor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/BufferBuilderStorageAccessor.java new file mode 100644 index 0000000..e23e091 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/BufferBuilderStorageAccessor.java @@ -0,0 +1,15 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.render.BufferBuilder; +import net.minecraft.client.render.BufferBuilderStorage; +import net.minecraft.client.render.RenderLayer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import java.util.SortedMap; + +@Mixin(BufferBuilderStorage.class) +public interface BufferBuilderStorageAccessor { + @Accessor + SortedMap getEntityBuilders(); +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/RenderPhaseAccessor.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/RenderPhaseAccessor.java new file mode 100644 index 0000000..b2a59b4 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/RenderPhaseAccessor.java @@ -0,0 +1,21 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.render.RenderPhase; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(RenderPhase.class) +public interface RenderPhaseAccessor { + @Accessor("ARMOR_GLINT_SHADER") static RenderPhase.Shader ARMOR_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("ARMOR_ENTITY_GLINT_SHADER") static RenderPhase.Shader ARMOR_ENTITY_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("TRANSLUCENT_GLINT_SHADER") static RenderPhase.Shader TRANSLUCENT_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("GLINT_SHADER") static RenderPhase.Shader GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("DIRECT_GLINT_SHADER") static RenderPhase.Shader DIRECT_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("ENTITY_GLINT_SHADER") static RenderPhase.Shader ENTITY_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("DIRECT_ENTITY_GLINT_SHADER") static RenderPhase.Shader DIRECT_ENTITY_GLINT_SHADER() { throw new RuntimeException(); } + @Accessor("DISABLE_CULLING") static RenderPhase.Cull DISABLE_CULLING() { throw new RuntimeException(); } + @Accessor("EQUAL_DEPTH_TEST") static RenderPhase.DepthTest EQUAL_DEPTH_TEST() { throw new RuntimeException(); } + @Accessor("COLOR_MASK") static RenderPhase.WriteMaskState COLOR_MASK() { throw new RuntimeException(); } + @Accessor("VIEW_OFFSET_Z_LAYERING") static RenderPhase.Layering VIEW_OFFSET_Z_LAYERING() { throw new RuntimeException(); } + @Accessor("ITEM_TARGET") static RenderPhase.Target ITEM_TARGET() { throw new RuntimeException(); } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java index 5252330..75949db 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -49,6 +49,14 @@ public abstract class PropertyGroup { return value; } + public PropertyValue getLastWithoutMetadataOrDefault(String defaultValue, String namespace, String... pathAliases) { + PropertyValue property = getLastWithoutMetadata(namespace, pathAliases); + if (property == null) + property = new PropertyValue(null, defaultValue, "=", -1, this.identifier, this.packName); + + return property; + } + public String stripName() { return identifier.getPath().substring(identifier.getPath().lastIndexOf('/') + 1, identifier.getPath().length() - getExtension().length()); } -- cgit From 262d14882ec81115b7a73f0bae055e36488dd580 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 25 Feb 2022 10:36:15 +0200 Subject: Made the enchantment type work --- .../defaults/cit/types/TypeEnchantment.java | 99 ++++++++++++++-------- .../enchantment/ArmorFeatureRendererMixin.java | 32 +++++++ .../enchantment/ElytraFeatureRendererMixin.java | 31 +++++++ .../mixin/types/enchantment/ItemRendererMixin.java | 88 +++++++++++++++++++ .../mixin/types/enchantment/ItemStackMixin.java | 9 ++ 5 files changed, 226 insertions(+), 33 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ArmorFeatureRendererMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index 5fd6e3c..d28f441 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -60,11 +60,9 @@ public class TypeEnchantment extends CITType { @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture"); - if (textureProp == null) - throw new CITParsingException("No texture specified", properties, -1); this.texture = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (this.texture == null) - throw new CITParsingException("Could not resolve texture", properties, textureProp.position()); + throw textureProp == null ? new CITParsingException("No texture specified", properties, -1) : new CITParsingException("Could not resolve texture", properties, textureProp.position()); PropertyValue layerProp = properties.getLastWithoutMetadataOrDefault("0", "citresewn", "layer"); try { @@ -73,13 +71,13 @@ public class TypeEnchantment extends CITType { throw new CITParsingException("Could not parse integer", properties, layerProp.position(), e); } - this.speed = parseFloatOrZero("speed", properties); - this.rotation = parseFloatOrZero("rotation", properties); - this.duration = parseFloatOrZero("duration", properties); - this.r = parseFloatOrZero("r", properties); - this.g = parseFloatOrZero("g", properties); - this.b = parseFloatOrZero("b", properties); - this.a = parseFloatOrZero("a", properties); + this.speed = parseFloatOrDefault(1f, "speed", properties); + this.rotation = parseFloatOrDefault(10f, "rotation", properties); + this.duration = Math.max(0f, parseFloatOrDefault(0f, "duration", properties)); + this.r = Math.max(0f, parseFloatOrDefault(1f, "r", properties)); + this.g = Math.max(0f, parseFloatOrDefault(1f, "g", properties)); + this.b = Math.max(0f, parseFloatOrDefault(1f, "b", properties)); + this.a = Math.max(0f, parseFloatOrDefault(1f, "a", properties)); this.useGlint = Boolean.parseBoolean(properties.getLastWithoutMetadataOrDefault("false", "citresewn", "useGlint").value()); this.blur = Boolean.parseBoolean(properties.getLastWithoutMetadataOrDefault("true", "citresewn", "blur").value()); @@ -92,10 +90,10 @@ public class TypeEnchantment extends CITType { } } - private float parseFloatOrZero(String propertyName, PropertyGroup properties) throws CITParsingException { + private float parseFloatOrDefault(float defaultValue, String propertyName, PropertyGroup properties) throws CITParsingException { PropertyValue property = properties.getLastWithoutMetadata("citresewn", propertyName); if (property == null) - return 0f; + return defaultValue; try { return Float.parseFloat(property.value()); } catch (Exception e) { @@ -112,7 +110,7 @@ public class TypeEnchantment extends CITType { public List>> loadedLayered = new ArrayList<>(); private List> appliedContext = null; - public boolean shouldApply = false; + private boolean apply = false, defaultGlint = false; @Override public void load(List> parsedCITs) { @@ -147,11 +145,25 @@ public class TypeEnchantment extends CITType { loadedLayered.clear(); } - public void setContext(CITContext context) { - if (context == null) { - appliedContext = null; - return; - } + public void apply() { + if (appliedContext != null) + apply = true; + } + + public boolean shouldApply() { + return apply; + } + + public boolean shouldNotApplyDefaultGlint() { + return apply && !defaultGlint; + } + + public Container setContext(CITContext context) { + apply = false; + defaultGlint = false; + appliedContext = null; + if (context == null) + return this; List>> cits = ((CITCacheEnchantment) (Object) context.stack).citresewn$getCacheTypeEnchantment().get(context); @@ -160,12 +172,17 @@ public class TypeEnchantment extends CITType { for (WeakReference> citRef : cits) if (citRef != null) { CIT cit = citRef.get(); - if (cit != null) + if (cit != null) { appliedContext.add(cit); + if (cit.type.useGlint) + defaultGlint = true; + } } if (appliedContext.isEmpty()) appliedContext = null; + + return this; } public List> getRealTimeCIT(CITContext context) { @@ -233,21 +250,37 @@ public class TypeEnchantment extends CITType { } public RenderLayer build(TypeEnchantment enchantment, Identifier propertiesIdentifier) { - final float speed = enchantment.speed, rotation = enchantment.rotation, r = enchantment.r, g = enchantment.g, b = enchantment.b, a = enchantment.a; - final WrappedMethodIntensity methodIntensity = enchantment.methodIntensity; + class Texturing implements Runnable { + private final float speed, rotation, r, g, b, a; + private final WrappedMethodIntensity methodIntensity; + + Texturing(float speed, float rotation, float r, float g, float b, float a, WrappedMethodIntensity methodIntensity) { + this.speed = speed; + this.rotation = rotation; + this.r = r; + this.g = g; + this.b = b; + this.a = a; + this.methodIntensity = methodIntensity; + } + + @Override + public void run() { + float l = Util.getMeasuringTimeMs() * CITResewnDefaultsConfig.INSTANCE.type_enchantment_scroll_multiplier * speed; + float x = (l % 110000f) / 110000f; + float y = (l % 30000f) / 30000f; + Matrix4f matrix4f = Matrix4f.translate(-x, y, 0.0f); + matrix4f.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(rotation)); + matrix4f.multiply(Matrix4f.scale(scale, scale, scale)); + setTextureMatrix(matrix4f); + + setShaderColor(r, g, b, a * methodIntensity.intensity); + } + } + RenderLayer.MultiPhaseParameters.Builder layer = RenderLayer.MultiPhaseParameters.builder() .texture(new RenderPhase.Texture(enchantment.texture, enchantment.blur, false)) - .texturing(new RenderPhase.Texturing("citresewn_glint_texturing", () -> { - float l = Util.getMeasuringTimeMs() * CITResewnDefaultsConfig.INSTANCE.type_enchantment_scroll_multiplier * speed; - float x = (l % 110000f) / 110000f; - float y = (l % 30000f) / 30000f; - Matrix4f matrix4f = Matrix4f.translate(-x, y, 0.0f); - matrix4f.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(rotation + 10f)); - matrix4f.multiply(Matrix4f.scale(scale, scale, scale)); - setTextureMatrix(matrix4f); - - setShaderColor(r, g, b, a * methodIntensity.intensity); - }, () -> { + .texturing(new RenderPhase.Texturing("citresewn_glint_texturing", new Texturing(enchantment.speed, enchantment.rotation, enchantment.r, enchantment.g, enchantment.b, enchantment.a, enchantment.methodIntensity), () -> { RenderSystem.resetTextureMatrix(); setShaderColor(1f, 1f, 1f, 1f); @@ -264,7 +297,7 @@ public class TypeEnchantment extends CITType { } public VertexConsumer tryApply(VertexConsumer base, RenderLayer baseLayer, VertexConsumerProvider provider) { - if (!CONTAINER.shouldApply || CONTAINER.appliedContext == null || CONTAINER.appliedContext.size() == 0) + if (!CONTAINER.apply || CONTAINER.appliedContext == null || CONTAINER.appliedContext.size() == 0) return null; VertexConsumer[] layers = new VertexConsumer[Math.min(CONTAINER.appliedContext.size(), Integer.MAX_VALUE /*todo cap global property*/)]; diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ArmorFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ArmorFeatureRendererMixin.java new file mode 100644 index 0000000..272463b --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ArmorFeatureRendererMixin.java @@ -0,0 +1,32 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.entity.feature.ArmorFeatureRenderer; +import net.minecraft.client.render.entity.model.BipedEntityModel; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; +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 shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment.CONTAINER; + +@Mixin(ArmorFeatureRenderer.class) +public class ArmorFeatureRendererMixin, A extends BipedEntityModel> { + @Inject(method = "renderArmor", at = @At("HEAD")) + private void citresewn$enchantment$setAppliedContextAndStartApplyingArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, T livingEntity, EquipmentSlot armorSlot, int light, A model, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(new CITContext(livingEntity.getEquippedStack(armorSlot), livingEntity.world, livingEntity)).apply(); + } + + @Inject(method = "renderArmor", at = @At("RETURN")) + private void citresewn$enchantment$stopApplyingArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, T livingEntity, EquipmentSlot armorSlot, int light, A model, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(null); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java new file mode 100644 index 0000000..4966e6f --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java @@ -0,0 +1,31 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.entity.feature.ElytraFeatureRenderer; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; +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 shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment.CONTAINER; + +@Mixin(ElytraFeatureRenderer.class) +public class ElytraFeatureRendererMixin { + @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/entity/LivingEntity;FFFFFF)V", at = @At("HEAD")) + private void citresewn$enchantment$setAppliedContextAndStartApplyingElytra(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(new CITContext(livingEntity.getEquippedStack(EquipmentSlot.CHEST), livingEntity.world, livingEntity)).apply(); + } + + @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/entity/LivingEntity;FFFFFF)V", at = @At("RETURN")) + private void citresewn$enchantment$stopApplyingElytra(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(null); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java new file mode 100644 index 0000000..138e7f9 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java @@ -0,0 +1,88 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.*; +import net.minecraft.client.render.item.ItemRenderer; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.json.ModelTransformation; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment; + +import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment.CONTAINER; + +@Mixin(value = ItemRenderer.class, priority = 200) +public class ItemRendererMixin { + @Inject(method = "getModel", at = @At("HEAD")) + private void citresewn$enchantment$setAppliedContext(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(new CITContext(stack, world, entity)); + } + + @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD")) + private void citresewn$enchantment$startApplyingItem(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.apply(); + } + + @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("RETURN")) + private void citresewn$enchantment$stopApplyingItem(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { + if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + CONTAINER.setContext(null); + } + + @Inject(method = "getArmorGlintConsumer", cancellable = true, at = @At("RETURN")) + private static void citresewn$enchantment$getArmorGlintConsumer(VertexConsumerProvider provider, RenderLayer layer, boolean solid, boolean glint, CallbackInfoReturnable cir) { + if (!CONTAINER.shouldApply()) + return; + VertexConsumer vertexConsumer = solid ? TypeEnchantment.GlintRenderLayer.ARMOR_GLINT.tryApply(cir.getReturnValue(), layer, provider) : TypeEnchantment.GlintRenderLayer.ARMOR_ENTITY_GLINT.tryApply(cir.getReturnValue(), layer, provider); + if (vertexConsumer != null) + cir.setReturnValue(vertexConsumer); + } + + @Inject(method = "getCompassGlintConsumer", cancellable = true, at = @At("RETURN")) + private static void citresewn$enchantment$getCompassGlintConsumer(VertexConsumerProvider provider, RenderLayer layer, MatrixStack.Entry entry, CallbackInfoReturnable cir) { + if (!CONTAINER.shouldApply()) + return; + VertexConsumer vertexConsumer = TypeEnchantment.GlintRenderLayer.GLINT.tryApply(null, layer, provider); + if (vertexConsumer != null) + cir.setReturnValue(VertexConsumers.union(new OverlayVertexConsumer(vertexConsumer, entry.getPositionMatrix(), entry.getNormalMatrix()), cir.getReturnValue())); + } + + @Inject(method = "getDirectCompassGlintConsumer", cancellable = true, at = @At("RETURN")) + private static void citresewn$enchantment$getDirectCompassGlintConsumer(VertexConsumerProvider provider, RenderLayer layer, MatrixStack.Entry entry, CallbackInfoReturnable cir) { + if (!CONTAINER.shouldApply()) + return; + VertexConsumer vertexConsumer = TypeEnchantment.GlintRenderLayer.DIRECT_GLINT.tryApply(null, layer, provider); + if (vertexConsumer != null) + cir.setReturnValue(VertexConsumers.union(new OverlayVertexConsumer(vertexConsumer, entry.getPositionMatrix(), entry.getNormalMatrix()), cir.getReturnValue())); + } + + @Inject(method = "getItemGlintConsumer", cancellable = true, at = @At("RETURN")) + private static void citresewn$enchantment$getItemGlintConsumer(VertexConsumerProvider provider, RenderLayer layer, boolean solid, boolean glint, CallbackInfoReturnable cir) { + if (!CONTAINER.shouldApply()) + return; + VertexConsumer vertexConsumer = MinecraftClient.isFabulousGraphicsOrBetter() && layer == TexturedRenderLayers.getItemEntityTranslucentCull() ? TypeEnchantment.GlintRenderLayer.GLINT_TRANSLUCENT.tryApply(cir.getReturnValue(), layer, provider) : (solid ? TypeEnchantment.GlintRenderLayer.GLINT.tryApply(cir.getReturnValue(), layer, provider) : TypeEnchantment.GlintRenderLayer.ENTITY_GLINT.tryApply(cir.getReturnValue(), layer, provider)); + if (vertexConsumer != null) + cir.setReturnValue(vertexConsumer); + } + + @Inject(method = "getDirectItemGlintConsumer", cancellable = true, at = @At("RETURN")) + private static void citresewn$enchantment$getDirectItemGlintConsumer(VertexConsumerProvider provider, RenderLayer layer, boolean solid, boolean glint, CallbackInfoReturnable cir) { + if (!CONTAINER.shouldApply()) + return; + VertexConsumer vertexConsumer = solid ? TypeEnchantment.GlintRenderLayer.DIRECT_GLINT.tryApply(cir.getReturnValue(), layer, provider) : TypeEnchantment.GlintRenderLayer.DIRECT_ENTITY_GLINT.tryApply(cir.getReturnValue(), layer, provider); + if (vertexConsumer != null) + cir.setReturnValue(vertexConsumer); + } +} \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java index bbd61d9..d49e116 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemStackMixin.java @@ -2,6 +2,9 @@ package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; import net.minecraft.item.ItemStack; 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.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.cit.CITCache; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment; @@ -13,4 +16,10 @@ public class ItemStackMixin implements TypeEnchantment.CITCacheEnchantment { public CITCache.MultiList citresewn$getCacheTypeEnchantment() { return this.citresewn$cacheTypeEnchantment; } + + @Inject(method = "hasGlint", cancellable = true, at = @At("HEAD")) + private void citresewn$enchantment$disableDefaultGlint(CallbackInfoReturnable cir) { + if (TypeEnchantment.CONTAINER.shouldNotApplyDefaultGlint()) + cir.setReturnValue(false); + } } -- cgit From 5a08a70cf1d1c21992b21e484330fff54b744051 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 25 Feb 2022 13:21:43 +0200 Subject: Implemented the rest of the global properties --- .../defaults/cit/types/TypeEnchantment.java | 148 ++++++++++++++++++++- .../types/enchantment/MinecraftClientMixin.java | 16 +++ .../fabric/citresewn/pack/GlobalProperties.java | 2 +- 3 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/MinecraftClientMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index d28f441..6f9481e 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -4,14 +4,23 @@ import com.mojang.blaze3d.systems.RenderSystem; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.*; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.item.EnchantedBookItem; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import net.minecraft.resource.ResourceManager; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.Util; import net.minecraft.util.math.Matrix4f; import net.minecraft.util.math.Vec3f; import org.lwjgl.opengl.GL11; +import shcm.shsupercm.fabric.citresewn.api.CITGlobalProperties; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.cit.*; +import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionEnchantments; import shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsConfig; import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment.BufferBuilderStorageAccessor; import shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment.RenderPhaseAccessor; @@ -19,6 +28,7 @@ import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import shcm.shsupercm.util.logic.Loops; import java.lang.ref.WeakReference; import java.util.*; @@ -28,6 +38,7 @@ import static com.mojang.blaze3d.systems.RenderSystem.*; import static org.lwjgl.opengl.GL11.*; public class TypeEnchantment extends CITType { + @Entrypoint(CITGlobalProperties.ENTRYPOINT) @Entrypoint(CITTypeContainer.ENTRYPOINT) public static final Container CONTAINER = new Container(); @@ -55,7 +66,8 @@ public class TypeEnchantment extends CITType { public Blend blend; public final Map renderLayers = new EnumMap<>(GlintRenderLayer.class); - private final WrappedMethodIntensity methodIntensity = new WrappedMethodIntensity(); + private final MergeMethodIntensity methodIntensity = new MergeMethodIntensity(); + private Set enchantmentChecks = null; @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { @@ -88,6 +100,14 @@ public class TypeEnchantment extends CITType { } catch (Exception e) { throw new CITParsingException("Could not parse blending method", properties, blendProp.position(), e); } + + for (CITCondition condition : conditions) + if (condition instanceof ConditionEnchantments enchantments) { + if (enchantmentChecks == null && enchantments.getEnchantments().length > 0) + enchantmentChecks = new HashSet<>(); + + enchantmentChecks.addAll(Arrays.asList(enchantments.getEnchantments())); + } } private float parseFloatOrDefault(float defaultValue, String propertyName, PropertyGroup properties) throws CITParsingException { @@ -101,11 +121,16 @@ public class TypeEnchantment extends CITType { } } - public static class Container extends CITTypeContainer { + public static class Container extends CITTypeContainer implements CITGlobalProperties { public Container() { super(TypeEnchantment.class, TypeEnchantment::new, "enchantment"); } + public boolean globalUseGlint = true; + public int globalCap = Integer.MAX_VALUE; + public MergeMethodIntensity.MergeMethod globalMergeMethod = MergeMethodIntensity.MergeMethod.AVERAGE; + public float globalFade = 0.5f; + public List> loaded = new ArrayList<>(); public List>> loadedLayered = new ArrayList<>(); @@ -133,6 +158,26 @@ public class TypeEnchantment extends CITType { } } + @Override + public void globalProperty(String key, PropertyValue value) throws Exception { + switch (key) { + case "useGlint" -> { + globalUseGlint = Boolean.parseBoolean(value.value()); + if (!globalUseGlint && !"false".equalsIgnoreCase(value.value())) + throw new Exception("Could not parse boolean"); + } + case "cap" -> { + globalCap = Integer.parseInt(value.value()); + } + case "method" -> { + globalMergeMethod = MergeMethodIntensity.MergeMethod.parse(value.value()); + } + case "fade" -> { + globalFade = Float.parseFloat(value.value()); + } + } + } + @Override public void dispose() { appliedContext = null; @@ -143,6 +188,11 @@ public class TypeEnchantment extends CITType { loaded.clear(); loadedLayered.clear(); + + globalUseGlint = true; + globalCap = Integer.MAX_VALUE; + globalMergeMethod = MergeMethodIntensity.MergeMethod.AVERAGE; + globalFade = 0.5f; } public void apply() { @@ -155,7 +205,7 @@ public class TypeEnchantment extends CITType { } public boolean shouldNotApplyDefaultGlint() { - return apply && !defaultGlint; + return !globalUseGlint || (apply && !defaultGlint); } public Container setContext(CITContext context) { @@ -181,6 +231,8 @@ public class TypeEnchantment extends CITType { if (appliedContext.isEmpty()) appliedContext = null; + else + globalMergeMethod.applyMethod(appliedContext, context); return this; } @@ -252,9 +304,9 @@ public class TypeEnchantment extends CITType { public RenderLayer build(TypeEnchantment enchantment, Identifier propertiesIdentifier) { class Texturing implements Runnable { private final float speed, rotation, r, g, b, a; - private final WrappedMethodIntensity methodIntensity; + private final MergeMethodIntensity methodIntensity; - Texturing(float speed, float rotation, float r, float g, float b, float a, WrappedMethodIntensity methodIntensity) { + Texturing(float speed, float rotation, float r, float g, float b, float a, MergeMethodIntensity methodIntensity) { this.speed = speed; this.rotation = rotation; this.r = r; @@ -300,7 +352,7 @@ public class TypeEnchantment extends CITType { if (!CONTAINER.apply || CONTAINER.appliedContext == null || CONTAINER.appliedContext.size() == 0) return null; - VertexConsumer[] layers = new VertexConsumer[Math.min(CONTAINER.appliedContext.size(), Integer.MAX_VALUE /*todo cap global property*/)]; + VertexConsumer[] layers = new VertexConsumer[Math.min(CONTAINER.appliedContext.size(), CONTAINER.globalCap)]; for (int i = 0; i < layers.length; i++) layers[i] = provider.getBuffer(CONTAINER.appliedContext.get(i).type.renderLayers.get(GlintRenderLayer.this)); @@ -311,8 +363,90 @@ public class TypeEnchantment extends CITType { } } - private static class WrappedMethodIntensity { + public static class MergeMethodIntensity { public float intensity = 1f; + + public enum MergeMethod { + NONE, + AVERAGE { + @Override + public void applyIntensity(Map stackEnchantments, CIT cit) { + Identifier enchantment = null; + for (Identifier enchantmentMatch : cit.type.enchantmentChecks) + if (stackEnchantments.containsKey(enchantmentMatch)) { + enchantment = enchantmentMatch; + break; + } + + if (enchantment == null) { + cit.type.methodIntensity.intensity = 0f; + } else { + float sum = 0f; + for (Integer value : stackEnchantments.values()) + sum += value; + + cit.type.methodIntensity.intensity = (float) stackEnchantments.get(enchantment) / sum; + } + } + }, + LAYERED { + @Override + public void applyIntensity(Map stackEnchantments, CIT cit) { + Identifier enchantment = null; + for (Identifier enchantmentMatch : cit.type.enchantmentChecks) + if (stackEnchantments.containsKey(enchantmentMatch)) { + enchantment = enchantmentMatch; + break; + } + if (enchantment == null) { + cit.type.methodIntensity.intensity = 0f; + return; + } + + float max = 0f; + for (Integer value : stackEnchantments.values()) + if (value > max) + max = value; + + cit.type.methodIntensity.intensity = (float) stackEnchantments.get(enchantment) / max; + } + }, + CYCLE { + @Override + public void applyMethod(List> citEnchantments, CITContext context) { + List, Float>> durations = new ArrayList<>(); + for (CIT cit : citEnchantments) + durations.add(new HashMap.SimpleEntry<>(cit, cit.type.duration)); + + for (Map.Entry, Float> intensity : Loops.statelessFadingLoop(durations, CONTAINER.globalFade, ticks, 20).entrySet()) + intensity.getKey().type.methodIntensity.intensity = intensity.getValue(); + } + }; + + public static int ticks = 0; + + public void applyIntensity(Map stackEnchantments, CIT cit) { + cit.type.methodIntensity.intensity = 1f; + } + + public void applyMethod(List> citEnchantments, CITContext context) { + Map stackEnchantments = context.enchantments(); + + for (CIT cit : citEnchantments) + if (cit.type.enchantmentChecks != null) + applyIntensity(stackEnchantments, cit); + } + + public static MergeMethod parse(String value) { + return switch (value.toLowerCase(Locale.ROOT)) { + case "none" -> NONE; + case "average" -> AVERAGE; + case "layered" -> LAYERED; + case "cycle" -> CYCLE; + default -> throw new IllegalArgumentException("Unknown merge method"); + }; + } + } } public static class Blend extends RenderPhase.Transparency { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/MinecraftClientMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/MinecraftClientMixin.java new file mode 100644 index 0000000..e8b346b --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/MinecraftClientMixin.java @@ -0,0 +1,16 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.enchantment; + +import net.minecraft.client.MinecraftClient; +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 shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment; + +@Mixin(MinecraftClient.class) +public class MinecraftClientMixin { + @Inject(method = "tick", at = @At("HEAD")) + public void onTick(CallbackInfo ci) { + TypeEnchantment.MergeMethodIntensity.MergeMethod.ticks++; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java index c9e2cf8..456faab 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java @@ -37,7 +37,7 @@ public class GlobalProperties extends PropertyGroup { } public void callHandlers() { - for (EntrypointContainer container : FabricLoader.getInstance().getEntrypointContainers("citresewn:cit_global_properties", CITGlobalProperties.class)) { + for (EntrypointContainer container : FabricLoader.getInstance().getEntrypointContainers(CITGlobalProperties.ENTRYPOINT, CITGlobalProperties.class)) { String containerNamespace = container.getProvider().getMetadata().getId(); if (containerNamespace.equals("citresewn-defaults")) containerNamespace = "citresewn"; -- cgit From 6c602e022e2316a553bdbcad09e84e93fa6273fc Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 4 Mar 2022 08:22:51 +0200 Subject: Cleaned up type activity checks --- .../citresewn/defaults/cit/types/TypeEnchantment.java | 10 ++-------- .../mixin/types/armor/ArmorFeatureRendererMixin.java | 4 +--- .../mixin/types/elytra/ElytraFeatureRendererMixin.java | 9 +++++---- .../types/enchantment/ArmorFeatureRendererMixin.java | 6 ++---- .../types/enchantment/ElytraFeatureRendererMixin.java | 6 ++---- .../mixin/types/enchantment/ItemRendererMixin.java | 8 +++----- .../defaults/mixin/types/item/ItemRendererMixin.java | 11 +++-------- .../defaults/mixin/types/item/ModelLoaderMixin.java | 7 +++---- .../fabric/citresewn/api/CITTypeContainer.java | 17 ++++++++++++++++- .../shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java | 2 +- 10 files changed, 38 insertions(+), 42 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index 6f9481e..a59e96c 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -4,14 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.*; -import net.minecraft.enchantment.EnchantmentHelper; -import net.minecraft.item.EnchantedBookItem; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.resource.ResourceManager; -import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.Util; import net.minecraft.util.math.Matrix4f; @@ -181,6 +174,7 @@ public class TypeEnchantment extends CITType { @Override public void dispose() { appliedContext = null; + apply = false; for (CIT cit : loaded) for (RenderLayer renderLayer : cit.type.renderLayers.values()) @@ -201,7 +195,7 @@ public class TypeEnchantment extends CITType { } public boolean shouldApply() { - return apply; + return apply && active(); } public boolean shouldNotApplyDefaultGlint() { diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java index fba7159..d61af4c 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/armor/ArmorFeatureRendererMixin.java @@ -14,10 +14,8 @@ 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.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeArmor; import java.util.Map; @@ -31,7 +29,7 @@ public class ArmorFeatureRendererMixin, A extends BipedEntityModel> { @Inject(method = "renderArmor", at = @At("HEAD")) private void citresewn$enchantment$setAppliedContextAndStartApplyingArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, T livingEntity, EquipmentSlot armorSlot, int light, A model, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(new CITContext(livingEntity.getEquippedStack(armorSlot), livingEntity.world, livingEntity)).apply(); } @Inject(method = "renderArmor", at = @At("RETURN")) private void citresewn$enchantment$stopApplyingArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, T livingEntity, EquipmentSlot armorSlot, int light, A model, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(null); } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java index 4966e6f..c64fb77 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ElytraFeatureRendererMixin.java @@ -9,9 +9,7 @@ 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 shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment.CONTAINER; @@ -19,13 +17,13 @@ import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment public class ElytraFeatureRendererMixin { @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/entity/LivingEntity;FFFFFF)V", at = @At("HEAD")) private void citresewn$enchantment$setAppliedContextAndStartApplyingElytra(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(new CITContext(livingEntity.getEquippedStack(EquipmentSlot.CHEST), livingEntity.world, livingEntity)).apply(); } @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ILnet/minecraft/entity/LivingEntity;FFFFFF)V", at = @At("RETURN")) private void citresewn$enchantment$stopApplyingElytra(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, LivingEntity livingEntity, float f, float g, float h, float j, float k, float l, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(null); } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java index 138e7f9..b4bd37d 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/enchantment/ItemRendererMixin.java @@ -14,9 +14,7 @@ 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.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment; import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment.CONTAINER; @@ -25,19 +23,19 @@ import static shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeEnchantment public class ItemRendererMixin { @Inject(method = "getModel", at = @At("HEAD")) private void citresewn$enchantment$setAppliedContext(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(new CITContext(stack, world, entity)); } @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD")) private void citresewn$enchantment$startApplyingItem(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.apply(); } @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("RETURN")) private void citresewn$enchantment$stopApplyingItem(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { - if (CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive()) + if (CONTAINER.active()) CONTAINER.setContext(null); } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java index 49e26af..4a57445 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java @@ -19,10 +19,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; import java.lang.ref.WeakReference; @@ -41,7 +39,7 @@ public class ItemRendererMixin { @Inject(method = "getModel", cancellable = true, at = @At("HEAD")) private void citresewn$getItemModel(ItemStack stack, World world, LivingEntity entity, int seed, CallbackInfoReturnable cir) { - if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + if (!CONTAINER.active()) return; CITContext context = new CITContext(stack, world, entity); @@ -56,7 +54,7 @@ public class ItemRendererMixin { @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD")) private void citresewn$fixMojankCITsContext(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { - if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) + if (!CONTAINER.active()) return; citresewn$mojankCITModel = null; @@ -76,10 +74,7 @@ public class ItemRendererMixin { @ModifyVariable(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "LOAD", ordinal = 0, target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"), argsOnly = true) private BakedModel citresewn$fixMojankCITs(BakedModel original) { - if (!CITResewnConfig.INSTANCE.enabled || !ActiveCITs.isActive()) - return original; - - if (citresewn$mojankCITModel != null) + if (CONTAINER.active() && citresewn$mojankCITModel != null) return citresewn$mojankCITModel.get(); return original; diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index c7fa70d..93bab6a 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -20,7 +20,6 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; import shcm.shsupercm.fabric.citresewn.defaults.common.ResewnItemModelIdentifier; @@ -41,7 +40,7 @@ public class ModelLoaderMixin { @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/util/profiler/Profiler;swap(Ljava/lang/String;)V")) public void citresewn$addTypeItemModels(ResourceManager resourceManager, BlockColors blockColors, Profiler profiler, int i, CallbackInfo ci) { profiler.swap("citresewn:type_item_models"); - if (!ActiveCITs.isActive()) + if (!CONTAINER.active()) return; info("Loading item CIT models..."); @@ -65,7 +64,7 @@ public class ModelLoaderMixin { @Inject(method = "upload", at = @At("RETURN")) public void citresewn$linkTypeItemModels(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable cir) { - if (!ActiveCITs.isActive()) + if (!CONTAINER.active()) return; profiler.push("citresewn:type_item_linking"); @@ -92,7 +91,7 @@ public class ModelLoaderMixin { @ModifyArg(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;")) public Identifier citresewn$fixDuplicatePrefixSuffix(Identifier original) { - if (original.getPath().startsWith("models/models/") && original.getPath().endsWith(".json.json") && original.getPath().contains("cit")) + if (CONTAINER.active() && original.getPath().startsWith("models/models/") && original.getPath().endsWith(".json.json") && original.getPath().contains("cit")) return new Identifier(original.getNamespace(), original.getPath().substring(7, original.getPath().length() - 5)); return original; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java index a9666d3..5aa727d 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -1,7 +1,9 @@ package shcm.shsupercm.fabric.citresewn.api; +import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.CITType; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import java.util.List; import java.util.function.Supplier; @@ -12,16 +14,29 @@ public abstract class CITTypeContainer implements CITDisposab public final Supplier createType; public final String id; + protected boolean empty = true; + public CITTypeContainer(Class type, Supplier createType, String id) { this.type = type; this.createType = createType; this.id = id; } - public abstract void load(List> parsedCITs); + protected abstract void load(List> parsedCITs); @SuppressWarnings("unchecked") public final void loadUntyped(List parsedCITs) { + if (!parsedCITs.isEmpty()) + empty = false; load((List>) parsedCITs); } + + public final void unload() { + dispose(); + empty = true; + } + + public boolean active() { + return !empty && CITResewnConfig.INSTANCE.enabled && ActiveCITs.isActive(); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java index f54cc54..c7ca38c 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java @@ -61,6 +61,6 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} disposable.dispose(); for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) - typeContainer.dispose(); + typeContainer.unload(); } } -- cgit From d2a7a6b219ff2b229774dba336417822ec019b7b Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 4 Mar 2022 09:55:50 +0200 Subject: Added back enchantment scroll multiplier config --- .../CITResewnDefaultsConfigScreenFactory.java | 9 ++++-- .../assets/citresewn-defaults/lang/en_us.json | 7 +++-- .../config/CITResewnConfigScreenFactory.java | 33 ++++++++++++---------- 3 files changed, 29 insertions(+), 20 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java index 6d89bc6..95b0579 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java @@ -4,7 +4,6 @@ import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; @@ -17,13 +16,17 @@ public class CITResewnDefaultsConfigScreenFactory { ConfigBuilder builder = ConfigBuilder.create() .setParentScreen(parent) - .setTitle(new TranslatableText("config.citresewn.defaults.title")) + .setTitle(new TranslatableText("config.citresewn-defaults.title")) .setSavingRunnable(currentConfig::write); ConfigCategory category = builder.getOrCreateCategory(new LiteralText("")); ConfigEntryBuilder entryBuilder = builder.entryBuilder(); - + category.addEntry(entryBuilder.startFloatField(new TranslatableText("config.citresewn-defaults.type_enchantment_scroll_multiplier.title"), currentConfig.type_enchantment_scroll_multiplier) + .setTooltip(new TranslatableText("config.citresewn-defaults.type_enchantment_scroll_multiplier.tooltip")) + .setSaveConsumer(newConfig -> currentConfig.type_enchantment_scroll_multiplier = newConfig) + .setDefaultValue(defaultConfig.type_enchantment_scroll_multiplier) + .build()); return builder.build(); } diff --git a/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json index 72784ed..5f68e45 100644 --- a/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json +++ b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json @@ -1,4 +1,7 @@ { - "config.citresewn.defaults.title": "CIT Resewn: Defaults", - "config.citresewn.defaults.tooltip": "Go to the defaults' config menu" + "config.citresewn-defaults.title": "CIT Resewn: Defaults", + "config.citresewn-defaults.tooltip": "Go to the defaults' config menu", + + "config.citresewn-defaults.type_enchantment_scroll_multiplier.title": "Type Enchantment: Scroll multiplier", + "config.citresewn-defaults.type_enchantment_scroll_multiplier.tooltip": "A general multiplier for how fast cit enchantment glints scroll" } \ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java index 2d584aa..b05dc6e 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -37,20 +37,23 @@ public class CITResewnConfigScreenFactory { .setDefaultValue(defaultConfig.enabled) .build()); - class CurrentScreen { Screen screen; boolean prevToggle = false; } final CurrentScreen currentScreen = new CurrentScreen(); - category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.defaults.title"), false) - .setTooltip(new TranslatableText("config.citresewn.defaults.tooltip")) - .setYesNoTextSupplier((b) -> { - if (b != currentScreen.prevToggle) { - //noinspection unchecked - MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints(DEFAULTS_CONFIG_ENTRYPOINT, Function.class).stream().findAny().orElseThrow().apply(currentScreen.screen)); - - currentScreen.prevToggle = b; - } + if (FabricLoader.getInstance().isModLoaded("citresewn-defaults")) { + class CurrentScreen { boolean prevToggle = false; } final CurrentScreen currentScreen = new CurrentScreen(); + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn-defaults.title"), false) + .setTooltip(new TranslatableText("config.citresewn-defaults.tooltip")) - return new TranslatableText("config.citresewn.configure"); - }) - .build()); + .setYesNoTextSupplier((b) -> { + if (b != currentScreen.prevToggle) { + //noinspection unchecked + MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints(DEFAULTS_CONFIG_ENTRYPOINT, Function.class).stream().findAny().orElseThrow().apply(create(parent))); + + currentScreen.prevToggle = b; + } + + return new TranslatableText("config.citresewn.configure"); + }) + .build()); + } category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.mute_errors.title"), currentConfig.mute_errors) .setTooltip(new TranslatableText("config.citresewn.mute_errors.tooltip")) @@ -67,7 +70,7 @@ public class CITResewnConfigScreenFactory { category.addEntry(entryBuilder.startIntSlider(new TranslatableText("config.citresewn.cache_ms.title"), currentConfig.cache_ms / 50, 0, 5 * 20) .setTooltip(new TranslatableText("config.citresewn.cache_ms.tooltip")) .setSaveConsumer(newConfig -> currentConfig.cache_ms = newConfig * 50) - .setDefaultValue(currentConfig.cache_ms / 50) + .setDefaultValue(defaultConfig.cache_ms / 50) .setTextGetter(ticks -> { if (ticks <= 1) return new TranslatableText("config.citresewn.cache_ms.ticks." + ticks).formatted(Formatting.AQUA); @@ -90,6 +93,6 @@ public class CITResewnConfigScreenFactory { .requireRestart() .build()); - return currentScreen.screen = builder.build(); + return builder.build(); } } -- cgit From 1f82ebe89c0e98eaea141eea9a493547b9093dac Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 4 Mar 2022 11:05:49 +0200 Subject: Modified elytra type item condition warning --- .../fabric/citresewn/defaults/cit/types/TypeElytra.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java index d50d084..40e6fbe 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeElytra.java @@ -1,6 +1,8 @@ package shcm.shsupercm.fabric.citresewn.defaults.cit.types; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.item.ElytraItem; +import net.minecraft.item.Item; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; @@ -27,8 +29,11 @@ public class TypeElytra extends CITType { @Override public void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException { - if (conditions.removeIf(condition -> condition instanceof ConditionItems)) - warn("Type elytra does not support the items condition", null, properties); + for (CITCondition condition : conditions) + if (condition instanceof ConditionItems items) + for (Item item : items.items) + if (!(item instanceof ElytraItem)) + warn("Non elytra item type condition", null, properties); texture = resolveAsset(properties.identifier, properties.getLastWithoutMetadata("citresewn", "texture"), "textures", ".png", resourceManager); if (texture == null) -- cgit From 2aeadc2dd30c23aac6bb1d0a349720f28f2609ec Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 5 Mar 2022 08:25:26 +0200 Subject: Documentation (18/44, 0/35) --- .../citresewn/defaults/cit/types/TypeItem.java | 2 +- .../fabric/citresewn/cit/CITCondition.java | 2 +- .../fabric/citresewn/cit/CITRegistry.java | 2 +- .../shsupercm/fabric/citresewn/cit/CITType.java | 2 +- .../fabric/citresewn/ex/CITParsingException.java | 12 +-- .../citresewn/ex/UnknownCITTypeException.java | 3 + .../fabric/citresewn/mixin/ChatScreenMixin.java | 11 ++- .../citresewn/mixin/GroupResourcePackAccessor.java | 6 ++ .../fabric/citresewn/mixin/ModelLoaderMixin.java | 7 ++ .../AbstractFileResourcePackMixin.java | 16 +++- .../mixin/broken_paths/IdentifierMixin.java | 8 +- .../ReloadableResourceManagerImplMixin.java | 8 +- .../ResourcePackCompatibilityMixin.java | 8 +- .../pack/format/PropertiesGroupAdapter.java | 3 +- .../citresewn/pack/format/PropertyGroup.java | 100 +++++++++++++++++++-- .../fabric/citresewn/pack/format/PropertyKey.java | 12 +++ .../citresewn/pack/format/PropertySeparator.java | 12 ++- .../citresewn/pack/format/PropertyValue.java | 9 ++ 18 files changed, 197 insertions(+), 26 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index f37fd6b..21e1ab0 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -406,7 +406,7 @@ public class TypeItem extends CITType { }; if (replacement != null) { - CITResewn.logWarnLoading(CITParsingException.descriptionOf("Warning: Using deprecated sub item id \"" + subItem + "\" instead of \"" + replacement + "\"", properties, position)); + CITResewn.logWarnLoading(properties.messageWithDescriptorOf("Warning: Using deprecated sub item id \"" + subItem + "\" instead of \"" + replacement + "\"", position)); return new Identifier("minecraft", "item/" + replacement); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java index b5c1996..fd00ac6 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java @@ -22,6 +22,6 @@ public abstract class CITCondition { public abstract boolean test(CITContext context); protected void warn(String message, PropertyValue value, PropertyGroup properties) { - CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); + CITResewn.logWarnLoading("Warning: " + properties.messageWithDescriptorOf(message, value.position())); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java index 47f79fe..d8454a0 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java @@ -56,7 +56,7 @@ public class CITRegistry { public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { CITConditionContainer conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { - logWarnLoading(CITParsingException.descriptionOf("Unknown condition type \"" + key.toString() + "\"", properties, value.position())); + logWarnLoading(properties.messageWithDescriptorOf("Unknown condition type \"" + key.toString() + "\"", value.position())); return new ConstantCondition(false); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index c3bb134..b35d26c 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -16,7 +16,7 @@ public abstract class CITType { public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { - CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value == null ? -1 : value.position())); + CITResewn.logWarnLoading("Warning: " + properties.messageWithDescriptorOf(message, value == null ? -1 : value.position())); } /** diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java index 811d16b..f897ba7 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java @@ -2,16 +2,16 @@ package shcm.shsupercm.fabric.citresewn.ex; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +/** + * Thrown if errored while parsing the properties of a {@link PropertyGroup}. + * @see PropertyGroup#messageWithDescriptorOf(String, int) + */ public class CITParsingException extends Exception { public CITParsingException(String message, PropertyGroup propertyGroup, int position, Throwable throwable) { - super("Errored while parsing CIT: " + descriptionOf(message, propertyGroup, position), throwable); + super("Errored while parsing CIT: " + propertyGroup.messageWithDescriptorOf(message, position), throwable); } public CITParsingException(String message, PropertyGroup propertyGroup, int position) { - super("Errored while parsing CIT: " + descriptionOf(message, propertyGroup, position)); - } - - public static String descriptionOf(String message, PropertyGroup propertyGroup, int position) { - return message + (position != -1 ? " @L" + position : "") + " in " + propertyGroup.identifier.toString() + " from " + propertyGroup.packName; + super("Errored while parsing CIT: " + propertyGroup.messageWithDescriptorOf(message, position)); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java index eacbc69..23f9c82 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java @@ -2,6 +2,9 @@ package shcm.shsupercm.fabric.citresewn.ex; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +/** + * Thrown when a property group contains an unrecognized value associated with the "type" key. + */ public class UnknownCITTypeException extends CITParsingException { public UnknownCITTypeException(PropertyGroup propertyGroup, int position) { super("Unknown type", propertyGroup, position); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ChatScreenMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ChatScreenMixin.java index 5bacbde..c79143b 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ChatScreenMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ChatScreenMixin.java @@ -9,14 +9,23 @@ import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyArg; +import shcm.shsupercm.fabric.citresewn.CITResewnCommand; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfigScreenFactory; import static shcm.shsupercm.fabric.citresewn.CITResewnCommand.openConfig; +/** + * Opens the config screen when running the "/citresewn config" command. + * @see CITResewnCommand#openConfig + */ @Mixin(ChatScreen.class) public class ChatScreenMixin { + /** + * If {@link CITResewnCommand#openConfig} is true, changes the screen that's opened when the chat is closed to the config screen. + * @see CITResewnCommand#openConfig + */ @ModifyArg(method = "keyPressed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MinecraftClient;setScreen(Lnet/minecraft/client/gui/screen/Screen;)V")) - public Screen redirectConfigScreen(Screen original) { + public Screen citresewn$redirectConfigScreen(Screen original) { if (openConfig) { openConfig = false; return FabricLoader.getInstance().isModLoaded("cloth-config2") ? diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/GroupResourcePackAccessor.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/GroupResourcePackAccessor.java index 72af871..8263be8 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/GroupResourcePackAccessor.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/GroupResourcePackAccessor.java @@ -4,9 +4,15 @@ import net.fabricmc.fabric.impl.resource.loader.GroupResourcePack; import net.minecraft.resource.ResourcePack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; +import shcm.shsupercm.fabric.citresewn.pack.PackParser; import java.util.List; +/** + * Access for individual packs in a grouped resourcepack.
+ * Used by Fabric API to bundle all mods' resources into a single pack. + * @see PackParser#forEachPack + */ @Mixin(GroupResourcePack.class) public interface GroupResourcePackAccessor { @Accessor diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java index ea1d5ab..df11ac9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java @@ -10,8 +10,15 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs; +/** + * Initializes the (re)loading of active cits in the resource manager. + * @see ActiveCITs + */ @Mixin(ModelLoader.class) public class ModelLoaderMixin { + /** + * @see ActiveCITs#load(ResourceManager, Profiler) + */ @Inject(method = "", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/util/profiler/Profiler;push(Ljava/lang/String;)V")) private void citresewn$loadCITs(ResourceManager resourceManager, BlockColors blockColors, Profiler profiler, int i, CallbackInfo ci) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/AbstractFileResourcePackMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/AbstractFileResourcePackMixin.java index 9ad7992..b241fff 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/AbstractFileResourcePackMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/AbstractFileResourcePackMixin.java @@ -14,33 +14,43 @@ 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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.config.BrokenPaths; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.zip.ZipFile; +/** + * Adds a resourcepack compatibility error message when broken paths are enabled and are detected in a pack. + * @see BrokenPaths + * @see ResourcePackCompatibilityMixin + */ @Mixin(AbstractFileResourcePack.class) public abstract class AbstractFileResourcePackMixin implements ResourcePack { @Shadow @Final protected File base; @SuppressWarnings({"unchecked", "ConstantConditions", "EqualsBetweenInconvertibleTypes"}) @Inject(method = "parseMetadata(Lnet/minecraft/resource/metadata/ResourceMetadataReader;)Ljava/lang/Object;", cancellable = true, at = @At("RETURN")) - public void parseMetadata(ResourceMetadataReader metaReader, CallbackInfoReturnable cir) { + public void citresewn$brokenpaths$parseMetadata(ResourceMetadataReader metaReader, CallbackInfoReturnable cir) { if (cir.getReturnValue() != null) try { if (this.getClass().equals(ZipResourcePack.class)) { try (ZipFile zipFile = new ZipFile(base)) { zipFile.stream() .forEach(entry -> { - if (entry.getName().startsWith("assets")) + if (entry.getName().startsWith("assets") && !entry.getName().endsWith(".DS_Store")) new Identifier("minecraft", entry.getName()); }); } } else if (this.getClass().equals(DirectoryResourcePack.class)) { final Path assets = new File(base, "assets").toPath(); Files.walk(assets) - .forEach(path -> new Identifier("minecraft", assets.relativize(path).toString().replace('\\', '/'))); + .forEach(fullPath -> { + String path = assets.relativize(fullPath).toString().replace('\\', '/'); + if (!path.endsWith(".DS_Store")) + new Identifier("minecraft", path); + }); } } catch (InvalidIdentifierException e) { cir.setReturnValue((T) new PackResourceMetadata(cir.getReturnValue().getDescription(), Integer.MAX_VALUE - 53)); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/IdentifierMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/IdentifierMixin.java index 0be08f9..0bc36c3 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/IdentifierMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/IdentifierMixin.java @@ -6,13 +6,19 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.config.BrokenPaths; import static shcm.shsupercm.fabric.citresewn.config.BrokenPaths.processingBrokenPaths; +/** + * Applies broken paths logic when active. + * @see BrokenPaths + * @see ReloadableResourceManagerImplMixin + */ @Mixin(Identifier.class) public class IdentifierMixin { @Inject(method = "isPathValid", cancellable = true, at = @At("RETURN")) - private static void processBrokenPaths(String path, CallbackInfoReturnable cir) { + private static void citresewn$brokenpaths$processBrokenPaths(String path, CallbackInfoReturnable cir) { if (!processingBrokenPaths) return; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ReloadableResourceManagerImplMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ReloadableResourceManagerImplMixin.java index 3f54be3..1d94285 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ReloadableResourceManagerImplMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ReloadableResourceManagerImplMixin.java @@ -12,6 +12,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.config.BrokenPaths; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -19,12 +20,17 @@ import java.util.concurrent.Executor; import static shcm.shsupercm.fabric.citresewn.config.BrokenPaths.processingBrokenPaths; +/** + * Starts/Stops broken paths logic. + * @see BrokenPaths + * @see IdentifierMixin + */ @Mixin(ReloadableResourceManagerImpl.class) public class ReloadableResourceManagerImplMixin { @Shadow @Final private ResourceType type; @Inject(method = "reload", at = @At("RETURN")) - public void onReload(Executor prepareExecutor, Executor applyExecutor, CompletableFuture initialStage, List packs, CallbackInfoReturnable cir) { + public void citresewn$brokenpaths$onReload(Executor prepareExecutor, Executor applyExecutor, CompletableFuture initialStage, List packs, CallbackInfoReturnable cir) { if (processingBrokenPaths = this.type == ResourceType.CLIENT_RESOURCES) { CITResewn.LOG.error("[citresewn] Caution! Broken paths is enabled!"); cir.getReturnValue().whenComplete().thenRun(() -> processingBrokenPaths = false); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ResourcePackCompatibilityMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ResourcePackCompatibilityMixin.java index f187a56..813eb3d 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ResourcePackCompatibilityMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/broken_paths/ResourcePackCompatibilityMixin.java @@ -7,7 +7,13 @@ import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.config.BrokenPaths; +/** + * Adds a resourcepack compatibility error message when broken paths are enabled and are detected in a pack. + * @see BrokenPaths + * @see AbstractFileResourcePackMixin + */ @Mixin(ResourcePackCompatibility.class) public abstract class ResourcePackCompatibilityMixin { private static final ResourcePackCompatibility BROKEN_PATHS = ResourcePackCompatibility("BROKEN_PATHS", -1, "broken_paths"); @@ -19,7 +25,7 @@ public abstract class ResourcePackCompatibilityMixin { } @Inject(method = "from(ILnet/minecraft/resource/ResourceType;)Lnet/minecraft/resource/ResourcePackCompatibility;", cancellable = true, at = @At("HEAD")) - private static void redirectBrokenPathsCompatibility(int packVersion, ResourceType type, CallbackInfoReturnable cir) { + private static void citresewn$brokenpaths$redirectBrokenPathsCompatibility(int packVersion, ResourceType type, CallbackInfoReturnable cir) { if (packVersion == Integer.MAX_VALUE - 53) cir.setReturnValue(BROKEN_PATHS); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java index 4912a71..0375079 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java @@ -1,6 +1,7 @@ package shcm.shsupercm.fabric.citresewn.pack.format; import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; import java.io.*; import java.nio.charset.StandardCharsets; @@ -18,7 +19,7 @@ public class PropertiesGroupAdapter extends PropertyGroup { } @Override - public PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException { + public PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException, InvalidIdentifierException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { String line; int linePos = 0, multilineSkip = 0; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java index 1f21ed5..8d185fd 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -7,9 +7,29 @@ import java.io.IOException; import java.io.InputStream; import java.util.*; +/** + * Storage agnostic map of keys and values.
+ * Keys are stored as {@link PropertyKey}s holding the mod id of the property type.
+ * A key can have multiple values associated with it as they are stored in an ordered set. + * + * @see PropertyKey + * @see PropertyValue + * @see PropertiesGroupAdapter + */ public abstract class PropertyGroup { + /** + * The internal map that backs this property group. + */ public final Map> properties = new LinkedHashMap<>(); + + /** + * This group's location in its resourcepack. + */ public final Identifier identifier; + + /** + * The file name of the resourcepack that this property group is in. + */ public final String packName; protected PropertyGroup(String packName, Identifier identifier) { @@ -17,17 +37,63 @@ public abstract class PropertyGroup { this.identifier = identifier; } + /** + * Tries to parse a group out of a stream. + * @see #load(String, Identifier, InputStream) + * @see #getExtension() + * @see PropertiesGroupAdapter + * @param packName {@link #packName} + * @param identifier {@link #identifier}, needed for extension matching + * @param is a stream containing properties as specified by implementation + * @return the parsed group or null if could not match an adapter + * @throws IOException if errored while parsing the group + */ + public static PropertyGroup tryParseGroup(String packName, Identifier identifier, InputStream is) throws IOException { + PropertyGroup group = null; + if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) + group = new PropertiesGroupAdapter(packName, identifier); + + return group == null ? null : group.load(packName, identifier, is); + } + + /** + * @return file suffix for this property group's implementation + */ public abstract String getExtension(); + /** + * Reads the given input stream into the group. + * @param packName {@link #packName} + * @param identifier {@link #identifier} + * @param is a stream containing properties as specified by implementation + * @return this + * @throws IOException if errored while reading the stream + * @throws InvalidIdentifierException if encountered a malformed {@link Identifier} while reading + */ public abstract PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException, InvalidIdentifierException; - protected void put(int position, String packName, Identifier propertiesIdentifier, String key, String keyMetadata, PropertySeparator separator, String value) throws InvalidIdentifierException { + /** + * Adds the given value to the group. + * @param position implementation specific interpretation of the value's position in the group, has no effect on internal order + * @param packName the value's resourcepack file name + * @param propertiesIdentifier the value's property group location identifier + * @param key the value's key name + * @param keyMetadata nullable, implementation specific metadata for this value's key + * @param separator implementation specific connection between the key and the value + * @param value string representation of the value to be parsed by the group's user + */ + protected void put(int position, String packName, Identifier propertiesIdentifier, String key, String keyMetadata, PropertySeparator separator, String value) { Objects.requireNonNull(key); Objects.requireNonNull(value); this.properties.computeIfAbsent(PropertyKey.of(key), id -> new LinkedHashSet<>()).add(new PropertyValue(keyMetadata, value, separator, position, propertiesIdentifier, packName)); } + /** + * @param namespace the key's namespace(should be the value type's modid by convention) + * @param pathAliases all key name aliases to check for + * @return all values associated with the given key by alias>insertion order + */ public Set get(String namespace, String... pathAliases) { Set values = new LinkedHashSet<>(); @@ -40,6 +106,12 @@ public abstract class PropertyGroup { return values; } + /** + * @see #getLastWithoutMetadataOrDefault(String, String, String...) + * @param namespace the key's namespace(should be the value type's modid by convention) + * @param pathAliases all key name aliases to check for + * @return the last value associated with the key(by insertion order) that has a null key metadata or null if the key is not present in the group + */ public PropertyValue getLastWithoutMetadata(String namespace, String... pathAliases) { PropertyValue value = null; for (PropertyValue next : get(namespace, pathAliases)) @@ -49,6 +121,13 @@ public abstract class PropertyGroup { return value; } + /** + * @see #getLastWithoutMetadata(String, String...) + * @param defaultValue the dummy value to return if not present in the group + * @param namespace the key's namespace(should be the value type's modid by convention) + * @param pathAliases all key name aliases to check for + * @return the last value associated with the key(by insertion order) that has a null key metadata or the wrapped default value if the key is not present in the group + */ public PropertyValue getLastWithoutMetadataOrDefault(String defaultValue, String namespace, String... pathAliases) { PropertyValue property = getLastWithoutMetadata(namespace, pathAliases); if (property == null) @@ -57,15 +136,22 @@ public abstract class PropertyGroup { return property; } + /** + * @see #getExtension() + * @see #identifier + * @return the name of this group without its path or extension + */ public String stripName() { return identifier.getPath().substring(identifier.getPath().lastIndexOf('/') + 1, identifier.getPath().length() - getExtension().length()); } - public static PropertyGroup tryParseGroup(String packName, Identifier identifier, InputStream is) throws IOException { - PropertyGroup group = null; - if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) - group = new PropertiesGroupAdapter(packName, identifier); - - return group == null ? null : group.load(packName, identifier, is); + /** + * Compiles a message with attached info on a value's origin. + * @param message message to add descriptor to + * @param position implementation specific position of + * @return the formatted message + */ + public String messageWithDescriptorOf(String message, int position) { + return message + (position != -1 ? " @L" + position : "") + " in " + this.identifier.toString() + " from " + this.packName; } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java index 37a698d..7bc2cdc 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java @@ -1,6 +1,18 @@ package shcm.shsupercm.fabric.citresewn.pack.format; +import net.minecraft.util.Identifier; + +/** + * Namespace/path pair of strings. Similar to {@link Identifier} but without validity restrictions. + * @see Identifier + */ public record PropertyKey(String namespace, String path) { + /** + * Attempts to split a given string into a namespace and path by the first occurrence of a colon.
+ * If a namespace cannot be extracted from the given string, "citresewn" is set instead. + * @param key key to parse + * @return parsed property key + */ public static PropertyKey of(String key) { String[] split = new String[] {"citresewn", key}; int i = key.indexOf(':'); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertySeparator.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertySeparator.java index 612d83f..ce4ebab 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertySeparator.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertySeparator.java @@ -1,8 +1,18 @@ package shcm.shsupercm.fabric.citresewn.pack.format; +/** + * Marker for the connection between a {@link PropertyKey} and its {@link PropertyValue}. + */ public enum PropertySeparator { - EQUALS("="); + /** + * Marks either a check for equality or an action to set a value. + */ + EQUALS("=") + ; + /** + * String representation of the separator. + */ public final String separator; PropertySeparator(String separator) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java index fe126d7..f41119f 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java @@ -2,6 +2,15 @@ package shcm.shsupercm.fabric.citresewn.pack.format; import net.minecraft.util.Identifier; +/** + * Wrapped representation of a property group's value with additional attached metadata. + * @param keyMetadata nullable, implementation specific metadata for this value's key + * @param value string representation of the value to be parsed by the group's user + * @param separator implementation specific connection between the key and the value + * @param position implementation specific interpretation of the value's position in the group, has no effect on internal order + * @param propertiesIdentifier the value's property group location identifier + * @param packName the value's resourcepack file name + */ public record PropertyValue(String keyMetadata, String value, PropertySeparator separator, -- cgit From bb9dcef4b53d201cf4bcac3bf88d2042a1ce226c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 5 Mar 2022 18:44:04 +0200 Subject: Documentation (29/44, 0/35) --- .../citresewn/defaults/cit/types/TypeItem.java | 7 ++- .../shcm/shsupercm/fabric/citresewn/cit/CIT.java | 30 ++++++++++++ .../shsupercm/fabric/citresewn/cit/CITCache.java | 51 ++++++++++++++++++-- .../fabric/citresewn/cit/CITCondition.java | 36 +++++++++++++++ .../shsupercm/fabric/citresewn/cit/CITContext.java | 26 ++++++++++- .../fabric/citresewn/cit/CITRegistry.java | 54 +++++++++++++++++++++- .../shsupercm/fabric/citresewn/cit/CITType.java | 23 +++++++++ 7 files changed, 218 insertions(+), 9 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index 21e1ab0..aa4df72 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -40,8 +40,11 @@ import java.util.*; import java.util.stream.Collectors; /** - * Do not go through this class, it looks awful because it was ported from a "proof of concept".
- * The whole type will be rewritten at some point. + * ///// PORTED FROM BETA \\\\\ + * This shit was ported from the + * beta and will be rewritten at + * some point! + * \\\\\ ///// */ public class TypeItem extends CITType { @Entrypoint(CITTypeContainer.ENTRYPOINT) diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java index 3bc708f..7c7ba5b 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java @@ -2,11 +2,34 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.util.Identifier; +/** + * Runtime representation of a CIT, holding its type and conditions as well as additional metadata. + */ public class CIT { + /** + * The full location of this CIT in its resourcepack. + */ public final Identifier propertiesIdentifier; + + /** + * Name of the resourcepack that contains this CIT. + */ public final String packName; + + /** + * The CIT's type. + * @see CITType + */ public final T type; + + /** + * Conditions that must be met for this CIT to work. + */ public final CITCondition[] conditions; + + /** + * The weight of this CIT to be used when resolving multiple CIT matching conflicts. + */ public final int weight; public CIT(Identifier propertiesIdentifier, String packName, T type, CITCondition[] conditions, int weight) { @@ -17,6 +40,13 @@ public class CIT { this.weight = weight; } + /** + * Tests the given context against all of this CIT's conditions. + * + * @see #conditions + * @param context context to check + * @return true if none of this CIT's {@link #conditions} tested false + */ public boolean test(CITContext context) { try { for (CITCondition condition : conditions) diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java index 5fc6f5a..31ccf04 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java @@ -1,5 +1,6 @@ package shcm.shsupercm.fabric.citresewn.cit; +import net.minecraft.item.ItemStack; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import java.lang.ref.WeakReference; @@ -7,17 +8,42 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Function; +/** + * Runtime cache for a CIT type to be stored in type implementation specific locations. (usually ducked onto {@link ItemStack}s) + * @see Single + * @see MultiList + */ public abstract class CITCache { + /** + * The last time in epoch milliseconds that this cache invalidated its stored CIT. + */ public long lastCachedStamp = 0; + /** + * Common implementation of a single CIT per holder({@link ItemStack}) caching. + */ public static class Single extends CITCache { - public WeakReference> cit = null; - public final Function> realtime; + /** + * A reload-safe reference to the CIT that was last selected for this holder. + */ + protected WeakReference> cit = null; + + /** + * Real time Context -> CIT supplier for this cache for invalidated holders. + */ + protected final Function> realtime; public Single(Function> realtime) { this.realtime = realtime; } + /** + * Retrieves the CIT reference associated with this cache and invalidates it every config-defined interval. + * + * @see CITResewnConfig#cache_ms + * @param context context to check + * @return reference to the CIT or reference to null if no CIT applied + */ public WeakReference> get(CITContext context) { if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { this.cit = new WeakReference<>(this.realtime.apply(context)); @@ -28,14 +54,31 @@ public abstract class CITCache { } } + /** + * Common implementation of multiple CITs per holder({@link ItemStack}) caching. + */ public static class MultiList extends CITCache { - public List>> cit = null; - public final Function>> realtime; + /** + * List of reload-safe references to CITs that were last selected for this holder. + */ + protected List>> cit = null; + + /** + * Real time Context -> CIT list supplier for this cache for invalidated holders. + */ + protected final Function>> realtime; public MultiList(Function>> realtime) { this.realtime = realtime; } + /** + * Retrieves the CIT references associated with this cache and invalidates them every config-defined interval. + * + * @see CITResewnConfig#cache_ms + * @param context context to check + * @return list of references to CITs or empty list no CIT applied + */ public List>> get(CITContext context) { if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { this.cit = new ArrayList<>(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java index fd00ac6..cd4cb9d 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java @@ -1,6 +1,8 @@ package shcm.shsupercm.fabric.citresewn.cit; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -8,19 +10,53 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.Collections; import java.util.Set; +/** + * Instanced parent for CIT Types that are applied to items when conditions pass.
+ * Common condition types are available under {@link shcm.shsupercm.fabric.citresewn.cit.builtin.conditions}. + * @see CITConditionContainer + * @see CIT + */ public abstract class CITCondition { + /** + * Parses the given property value into the condition. + * @param value value to read + * @param properties the group containing value + * @throws CITParsingException if errored while parsing the condition + */ public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException; + /** + * @return a set of classes of conditions that have integration with this condition + */ public Set> siblingConditions() { return Collections.emptySet(); } + /** + * Modifies the given sibling if present in the CIT and declared in {@link #siblingConditions()}. + * @param sibling sibling to modify or be modified by + * @return the sibling or null to remove it from the CIT. + */ public T modifySibling(T sibling) { return sibling; } + /** + * Tests the given context against this condition. + * @param context context to check + * @return true if the given context passes + */ public abstract boolean test(CITContext context); + /** + * Logs a warning with the given value's descriptor if enabled in config. + * + * @see CITResewn#logWarnLoading(String) + * @see CITResewnConfig#mute_warns + * @param message warning message + * @param value value associated with the warning + * @param properties property group associated with the warning + */ protected void warn(String message, PropertyValue value, PropertyGroup properties) { CITResewn.logWarnLoading("Warning: " + properties.messageWithDescriptorOf(message, value.position())); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java index c5f1922..cfc10f8 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java @@ -11,23 +11,47 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.util.Identifier; import net.minecraft.world.World; +import javax.annotation.Nullable; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; +/** + * Holds momentary information to be used for CITs' condition matching and type effects. + */ public class CITContext { + /** + * The main item stack to check for the CIT. + */ public final ItemStack stack; + + /** + * The item's containing world(defaults to {@link MinecraftClient#world} if null) + */ public final World world; + + /** + * The item's associated living entity if present. (null if not relevant) + */ + @Nullable public final LivingEntity entity; + /** + * Cached enchantment map from {@link #stack}. + * @see #enchantments() + */ private Map enchantments = null; - public CITContext(ItemStack stack, World world, LivingEntity entity) { + public CITContext(ItemStack stack, @Nullable World world, @Nullable LivingEntity entity) { this.stack = stack; this.world = world == null ? MinecraftClient.getInstance().world : world; this.entity = entity; } + /** + * @see #enchantments + * @return a map of this context item's enchantments + */ public Map enchantments() { if (this.enchantments == null) { this.enchantments = new LinkedHashMap<>(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java index d8454a0..bf7c2ca 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java @@ -7,6 +7,7 @@ import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.ConstantCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; +import shcm.shsupercm.fabric.citresewn.pack.PackParser; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -16,13 +17,38 @@ import java.util.*; import static shcm.shsupercm.fabric.citresewn.CITResewn.info; import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading; -public class CITRegistry { +/** + * Holds a static registry runtime for all types and conditions. + * @see PackParser + * @see CITTypeContainer + * @see CITConditionContainer + */ +public final class CITRegistry { private CITRegistry(){} + /** + * Currently registered CIT types. + */ public static final Map> TYPES = new HashMap<>(); + /** + * Currently registered condition types. + */ public static final Map> CONDITIONS = new HashMap<>(); + /** + * Fast id lookup map for types. + * @see #idOfType(Class) + */ private static final Map, Identifier> TYPE_TO_ID = new IdentityHashMap<>(); + /** + * Fast id lookup map for conditions. + * @see #idOfCondition(Class) + */ private static final Map, PropertyKey> CONDITION_TO_ID = new IdentityHashMap<>(); + /** + * Loads all available CIT and condition types to registry. (internal use only) + * @see CITTypeContainer + * @see CITConditionContainer + */ public static void registerAll() { info("Registering CIT Conditions"); for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) { @@ -53,6 +79,15 @@ public class CITRegistry { } } + /** + * Parses a condition from the given property.
+ * + * @param key the condition's key in the group + * @param value the condition's value + * @param properties the containing property group + * @return the parsed condition or an always-failing {@link ConstantCondition} if unrecognized + * @throws CITParsingException if errored while parsing hte condition + */ public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { CITConditionContainer conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { @@ -65,7 +100,14 @@ public class CITRegistry { return condition; } - public static CITType parseType(PropertyGroup properties) throws CITParsingException { + /** + * Parses a CIT type from the given property group.
+ * If the group does not contain a "citresewn:type" property, defaults to "citresewn:item". + * @param properties group of properties to parse the CIT type from + * @return a new instance of the group's CIT type + * @throws UnknownCITTypeException if the given type is unrecognized in the registry + */ + public static CITType parseType(PropertyGroup properties) throws UnknownCITTypeException { Identifier type = new Identifier("citresewn", "item"); PropertyValue propertiesType = properties.getLastWithoutMetadata("citresewn", "type"); @@ -84,10 +126,18 @@ public class CITRegistry { return typeContainer.createType.get(); } + /** + * @see #TYPE_TO_ID + * @return the id of the given CIT type's class. + */ public static Identifier idOfType(Class clazz) { return TYPE_TO_ID.get(clazz); } + /** + * @see #CONDITION_TO_ID + * @return the first key of the given condition's class. + */ public static PropertyKey idOfCondition(Class clazz) { return CONDITION_TO_ID.get(clazz); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index b35d26c..65b3097 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -3,6 +3,7 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; @@ -10,9 +11,25 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.*; +/** + * Instanced parent for CIT Types that are applied to items when conditions pass. + * @see CITTypeContainer + * @see CIT + */ public abstract class CITType { + /** + * Used to determine which property keys are not conditions. + * @return a set of property keys used by this type + */ public abstract Set typeProperties(); + /** + * Loads the given property group into the type. + * @param conditions conditions that were parsed out of the property group + * @param properties group of properties to be read into this type + * @param resourceManager the CIT's containing resource manager + * @throws CITParsingException if errored while parsing the type + */ public abstract void load(List conditions, PropertyGroup properties, ResourceManager resourceManager) throws CITParsingException; protected void warn(String message, PropertyValue value, PropertyGroup properties) { @@ -20,6 +37,12 @@ public abstract class CITType { } /** + * ///// PORTED FROM BETA \\\\\ + * This shit was ported from the + * beta and will be rewritten at + * some point! + * \\\\\ ///// + * * Takes a defined path and resolves it to an identifier pointing to the resourcepack's path of the specified extension(returns null if no path can be resolved).
* If definedPath is null, will try to resolve a relative file with the same name as the rootIdentifier with the extension, otherwise:
* definedPath will be formatted to replace "\\" with "/" the extension will be appended if not there already.
-- cgit From 2356c6bb055079ef03cfd26e9d505ab9cea3960d Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 6 Mar 2022 17:42:04 +0200 Subject: Modified global properties api a bit --- .../defaults/cit/types/TypeEnchantment.java | 16 ++++++--------- .../fabric/citresewn/api/CITGlobalProperties.java | 9 ++++---- .../shsupercm/fabric/citresewn/cit/ActiveCITs.java | 24 ++++++++-------------- .../fabric/citresewn/pack/GlobalProperties.java | 14 ++++++------- 4 files changed, 27 insertions(+), 36 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java index a59e96c..0bd29e7 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeEnchantment.java @@ -23,6 +23,7 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import shcm.shsupercm.util.logic.Loops; +import javax.annotation.Nullable; import java.lang.ref.WeakReference; import java.util.*; import java.util.function.Consumer; @@ -152,21 +153,21 @@ public class TypeEnchantment extends CITType { } @Override - public void globalProperty(String key, PropertyValue value) throws Exception { + public void globalProperty(String key, @Nullable PropertyValue value) throws Exception { switch (key) { case "useGlint" -> { - globalUseGlint = Boolean.parseBoolean(value.value()); + globalUseGlint = value == null ? true : Boolean.parseBoolean(value.value()); if (!globalUseGlint && !"false".equalsIgnoreCase(value.value())) throw new Exception("Could not parse boolean"); } case "cap" -> { - globalCap = Integer.parseInt(value.value()); + globalCap = value == null ? Integer.MAX_VALUE : Integer.parseInt(value.value()); } case "method" -> { - globalMergeMethod = MergeMethodIntensity.MergeMethod.parse(value.value()); + globalMergeMethod = value == null ? MergeMethodIntensity.MergeMethod.AVERAGE : MergeMethodIntensity.MergeMethod.parse(value.value()); } case "fade" -> { - globalFade = Float.parseFloat(value.value()); + globalFade = value == null ? 0.5f : Float.parseFloat(value.value()); } } } @@ -182,11 +183,6 @@ public class TypeEnchantment extends CITType { loaded.clear(); loadedLayered.clear(); - - globalUseGlint = true; - globalCap = Integer.MAX_VALUE; - globalMergeMethod = MergeMethodIntensity.MergeMethod.AVERAGE; - globalFade = 0.5f; } public void apply() { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java index a3d78ae..05495af 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java @@ -2,6 +2,8 @@ package shcm.shsupercm.fabric.citresewn.api; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import javax.annotation.Nullable; + /** * @see #globalProperty(String, PropertyValue) */ @@ -16,10 +18,9 @@ public interface CITGlobalProperties { /** * Invoked before CIT parsing for any global property name associated with the handler's modid.
* May be called multiple times for a key to overwrite its global property with higher-priority resourcepacks.
- * Handlers should take care to reset back any changes global properties make by listening to CIT disposal. - * @see CITDisposable#dispose() + * When unloading resourcepacks(usually before reloading), all keys that were invoked in the previous load will get called again with a null value to allow for disposal. * @param key name of the property key stripped of its modid - * @param value the value it's been set to + * @param value the value it's been set to or null if resetting */ - void globalProperty(String key, PropertyValue value) throws Exception; + void globalProperty(String key, @Nullable PropertyValue value) throws Exception; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java index 5270d20..82f341f 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java @@ -62,9 +62,16 @@ public class ActiveCITs { private ActiveCITs() {} */ public static void load(ResourceManager resourceManager, Profiler profiler) { profiler.push("citresewn:disposing"); - disposeAll(); + for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class)) + disposable.dispose(); + + for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) + typeContainer.unload(); + if (active != null) { - //todo send reset calls to global properties with null value + active.globalProperties.properties.replaceAll((key, value) -> Set.of()); + active.globalProperties.callHandlers(); + active = null; } @@ -96,17 +103,4 @@ public class ActiveCITs { private ActiveCITs() {} if (!cits.isEmpty()) ActiveCITs.active = active; } - - /** - * Cleans up any registered disposable element. - * @see CITDisposable - * @see CITTypeContainer - */ - public static void disposeAll() { - for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class)) - disposable.dispose(); - - for (CITTypeContainer typeContainer : CITRegistry.TYPES.values()) - typeContainer.unload(); - } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java index c16242f..3e93b07 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java @@ -19,6 +19,7 @@ import java.util.Set; /** * Property group representation of the global cit.properties file. + * @see CITGlobalProperties * @see PackParser#loadGlobalProperties(ResourceManager, GlobalProperties) */ public class GlobalProperties extends PropertyGroup { @@ -59,13 +60,12 @@ public class GlobalProperties extends PropertyGroup { for (PropertyValue value : entry.getValue()) lastValue = value; - if (lastValue != null) - try { - container.getEntrypoint().globalProperty(entry.getKey().path(), lastValue); - } catch (Exception e) { - CITResewn.logErrorLoading("Errored while parsing global properties: Line " + lastValue.position() + " of " + lastValue.propertiesIdentifier() + " in " + lastValue.packName()); - e.printStackTrace(); - } + try { + container.getEntrypoint().globalProperty(entry.getKey().path(), lastValue); + } catch (Exception e) { + CITResewn.logErrorLoading(lastValue == null ? "Errored while disposing global properties" : "Errored while parsing global properties: Line " + lastValue.position() + " of " + lastValue.propertiesIdentifier() + " in " + lastValue.packName()); + e.printStackTrace(); + } } } } -- cgit From ea77714908943f4b7deed5b1a235e9bf17c80d7a Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Tue, 8 Mar 2022 15:59:52 +0200 Subject: Fixed context world not being a ClientWorld crash Fixes #108 --- .../shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index aa4df72..bd490d3 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -438,12 +438,13 @@ public class TypeItem extends CITType { } public BakedModel getItemModel(CITContext context, int seed) { + ClientWorld world = context.world instanceof ClientWorld clientWorld ? clientWorld : null; // get sub items or bakedModel if no sub item matches @Nullable - BakedModel bakedModel = bakedSubModels.apply(this.bakedModel, context.stack, (ClientWorld) context.world, context.entity, seed); + BakedModel bakedModel = bakedSubModels.apply(this.bakedModel, context.stack, world, context.entity, seed); // apply model overrides if (bakedModel != null && bakedModel.getOverrides() != null) - bakedModel = bakedModel.getOverrides().apply(bakedModel, context.stack, (ClientWorld) context.world, context.entity, seed); + bakedModel = bakedModel.getOverrides().apply(bakedModel, context.stack, world, context.entity, seed); return bakedModel; } -- cgit From 08168af5926129733b8f766b6a6eb24b6167fe01 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Mar 2022 16:07:46 +0200 Subject: Fixed crash from non-existent config folder Fixes #114 --- .../fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java | 1 + 1 file changed, 1 insertion(+) (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java index 791205b..41126d2 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java @@ -33,6 +33,7 @@ public class CITResewnDefaultsConfig { Gson gson = new Gson(); JsonWriter writer = null; try { + FILE.getParentFile().mkdirs(); writer = gson.newJsonWriter(new FileWriter(FILE)); writer.setIndent(" "); -- cgit From d93d0cf00dc07d3e8b4534d4f6247afcc85232ef Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 18 Mar 2022 11:01:54 +0200 Subject: Rewrote mojank model handling Fixes #111 --- .../citresewn/defaults/cit/types/TypeItem.java | 13 +++----- .../mixin/types/item/BakedModelManagerMixin.java | 29 +++++++++++++++++ .../mixin/types/item/ItemRendererMixin.java | 36 +++++++--------------- .../defaults/mixin/types/item/ItemStackMixin.java | 12 -------- 4 files changed, 44 insertions(+), 46 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/BakedModelManagerMixin.java (limited to 'defaults/src/main') diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index bd490d3..fe0bafb 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -500,17 +500,11 @@ public class TypeItem extends CITType { } public CIT getRealTimeCIT(CITContext context) { - ((CITCacheItem) (Object) context.stack).citresewn$setMojankCITTypeItem(false); - Set> loadedForItemType = loadedTyped.get(context.stack.getItem()); if (loadedForItemType != null) for (CIT cit : loadedForItemType) - if (cit.test(context)) { - if (context.stack.isOf(Items.TRIDENT) || context.stack.isOf(Items.SPYGLASS)) - ((CITCacheItem) (Object) context.stack).citresewn$setMojankCITTypeItem(true); - + if (cit.test(context)) return cit; - } return null; } @@ -518,8 +512,9 @@ public class TypeItem extends CITType { public interface CITCacheItem { CITCache.Single citresewn$getCacheTypeItem(); + } - boolean citresewn$isMojankCITTypeItem(); - void citresewn$setMojankCITTypeItem(boolean mojankCIT); + public interface BakedModelManagerMixinAccess { + void citresewn$forceMojankModel(BakedModel model); } } \ No newline at end of file diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/BakedModelManagerMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/BakedModelManagerMixin.java new file mode 100644 index 0000000..f7f6b48 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/BakedModelManagerMixin.java @@ -0,0 +1,29 @@ +package shcm.shsupercm.fabric.citresewn.defaults.mixin.types.item; + +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.BakedModelManager; +import net.minecraft.client.util.ModelIdentifier; +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.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; + +@Mixin(BakedModelManager.class) +public class BakedModelManagerMixin implements TypeItem.BakedModelManagerMixinAccess { + private BakedModel citresewn$forcedMojankModel = null; + + @Inject(method = "getModel", cancellable = true, at = + @At("HEAD")) + private void citresewn$getCITMojankModel(ModelIdentifier id, CallbackInfoReturnable cir) { + if (citresewn$forcedMojankModel != null) { + cir.setReturnValue(citresewn$forcedMojankModel); + citresewn$forcedMojankModel = null; + } + } + + @Override + public void citresewn$forceMojankModel(BakedModel model) { + this.citresewn$forcedMojankModel = model; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java index 4a57445..5f59329 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemRendererMixin.java @@ -5,7 +5,6 @@ import net.minecraft.client.render.item.ItemModels; import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.json.ModelTransformation; -import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; @@ -16,7 +15,6 @@ 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.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import shcm.shsupercm.fabric.citresewn.cit.CIT; @@ -44,39 +42,27 @@ public class ItemRendererMixin { CITContext context = new CITContext(stack, world, entity); CIT cit = CONTAINER.getCIT(context, seed); + citresewn$mojankCITModel = null; if (cit != null) { BakedModel citModel = cit.type.getItemModel(context, seed); - if (citModel != null) - cir.setReturnValue(citModel); + if (citModel != null) { + if (stack.isOf(Items.TRIDENT) || stack.isOf(Items.SPYGLASS)) { + citresewn$mojankCITModel = new WeakReference<>(citModel); + } else + cir.setReturnValue(citModel); + } } } @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD")) private void citresewn$fixMojankCITsContext(ItemStack stack, ModelTransformation.Mode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci) { - if (!CONTAINER.active()) + if (!CONTAINER.active() || citresewn$mojankCITModel == null) return; - citresewn$mojankCITModel = null; - if (((TypeItem.CITCacheItem) (Object) stack).citresewn$isMojankCITTypeItem()) { - boolean bl = renderMode == ModelTransformation.Mode.GUI || renderMode == ModelTransformation.Mode.GROUND || renderMode == ModelTransformation.Mode.FIXED; - if (bl) - citresewn$mojankCITModel = new WeakReference<>(model); - else { // rendered in hand model of trident/spyglass - if (stack.isOf(Items.TRIDENT)) - citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:trident_in_hand#inventory"))); - else if (stack.isOf(Items.SPYGLASS)) - citresewn$mojankCITModel = new WeakReference<>(this.models.getModelManager().getModel(new ModelIdentifier("minecraft:spyglass_in_hand#inventory"))); - } - } else - citresewn$mojankCITModel = null; - } - - @ModifyVariable(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformation$Mode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "LOAD", ordinal = 0, target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"), argsOnly = true) - private BakedModel citresewn$fixMojankCITs(BakedModel original) { - if (CONTAINER.active() && citresewn$mojankCITModel != null) - return citresewn$mojankCITModel.get(); + if (renderMode == ModelTransformation.Mode.GUI || renderMode == ModelTransformation.Mode.GROUND || renderMode == ModelTransformation.Mode.FIXED) + ((TypeItem.BakedModelManagerMixinAccess) this.models.getModelManager()).citresewn$forceMojankModel(citresewn$mojankCITModel.get()); - return original; + citresewn$mojankCITModel = null; } } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java index 2fb5396..450d8a3 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ItemStackMixin.java @@ -9,20 +9,8 @@ import shcm.shsupercm.fabric.citresewn.defaults.cit.types.TypeItem; public class ItemStackMixin implements TypeItem.CITCacheItem { private final CITCache.Single citresewn$cacheTypeItem = new CITCache.Single<>(TypeItem.CONTAINER::getRealTimeCIT); - private boolean citresewn$mojankCITTypeItem = false; - @Override public CITCache.Single citresewn$getCacheTypeItem() { return this.citresewn$cacheTypeItem; } - - @Override - public boolean citresewn$isMojankCITTypeItem() { - return this.citresewn$mojankCITTypeItem; - } - - @Override - public void citresewn$setMojankCITTypeItem(boolean mojankCITTypeItem) { - this.citresewn$mojankCITTypeItem = mojankCITTypeItem; - } } -- cgit