From 27faec4fe9ee6274e436d3ac0550956b2303a39e Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 3 Jun 2023 20:30:41 -0500 Subject: [PATCH] Split action listeners based on conversion data --- .../pack/converter/PackConverter.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/converter/src/main/java/org/geysermc/pack/converter/PackConverter.java b/converter/src/main/java/org/geysermc/pack/converter/PackConverter.java index c1a2138..330b993 100644 --- a/converter/src/main/java/org/geysermc/pack/converter/PackConverter.java +++ b/converter/src/main/java/org/geysermc/pack/converter/PackConverter.java @@ -47,6 +47,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -54,7 +55,7 @@ public class PackConverter { private Path input; private Path output; - private final List> actionListeners = new ArrayList<>(); + private final Map, List>> actionListeners = new IdentityHashMap<>(); @Getter private final Map> customModelData = new HashMap<>(); @@ -96,13 +97,16 @@ public class PackConverter { return this; } - public PackConverter actionListeners(@NotNull ActionListener... actionListeners) { - this.actionListeners.addAll(List.of(actionListeners)); + public PackConverter actionListeners(@NotNull Class clazz, @NotNull ActionListener... actionListeners) { + this.actionListeners.put(clazz, List.of(actionListeners)); return this; } - public PackConverter actionListeners(@NotNull List> actionListeners) { - this.actionListeners.addAll(actionListeners); + public PackConverter actionListeners(@NotNull Map, List>> actionListeners) { + for (Map.Entry, List>> entry : actionListeners.entrySet()) { + this.actionListeners.put(entry.getKey(), (List) entry.getValue()); + } + return this; } @@ -138,10 +142,11 @@ public class PackConverter { ConversionData data = converter.createConversionData(this, input, this.tmpDir); PackConversionContext context = new PackConversionContext<>(data, this, javaResourcePack, bedrockResourcePack, this.logListener); + List> actionListeners = this.actionListeners.getOrDefault(data.getClass(), List.of()); try { - this.actionListeners.forEach(actionListener -> actionListener.preConvert((PackConversionContext) context)); + actionListeners.forEach(actionListener -> actionListener.preConvert((PackConversionContext) context)); converter.convert(context); - this.actionListeners.forEach(actionListener -> actionListener.postConvert((PackConversionContext) context)); + actionListeners.forEach(actionListener -> actionListener.postConvert((PackConversionContext) context)); } catch (Throwable t) { this.logListener.error("Error converting pack!", t); errors++;