aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/pack
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2021-09-03 11:43:33 +0300
committerSHsuperCM <shsupercm@gmail.com>2021-09-03 11:43:33 +0300
commite0c23cb732a8837871f4e7f6d4125d0fa92404f5 (patch)
tree7a794d1929ea1cee6905277e3070c58c8c8788b5 /src/main/java/shcm/shsupercm/fabric/citresewn/pack
parent8fc272f9596b37e71bf4ff70f5d784a76bdef143 (diff)
downloadCITResewn-e0c23cb732a8837871f4e7f6d4125d0fa92404f5.tar.gz
CITResewn-e0c23cb732a8837871f4e7f6d4125d0fa92404f5.tar.bz2
CITResewn-e0c23cb732a8837871f4e7f6d4125d0fa92404f5.zip
Made it easier to integrate cit types
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/pack')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java57
1 files changed, 40 insertions, 17 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
index 5204c9c..ee7c938 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
@@ -4,6 +4,7 @@ import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.resource.ZipResourcePack;
import net.minecraft.util.Identifier;
+import org.apache.commons.io.IOUtils;
import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.ex.CITParseException;
import shcm.shsupercm.fabric.citresewn.pack.cits.*;
@@ -13,6 +14,14 @@ import java.util.*;
import java.util.function.Predicate;
public class CITParser { private CITParser() {}
+ public static final Map<String, CITConstructor> REGISTRY = new HashMap<>();
+
+ static {
+ REGISTRY.put("item", CITItem::new);
+ REGISTRY.put("armor", CITArmor::new);
+ REGISTRY.put("elytra", CITElytra::new);
+ REGISTRY.put("enchantment", CITEnchantment::new);
+ }
/**
* Parses cit entries from an ordered collection of resourcepacks.
@@ -47,20 +56,40 @@ public class CITParser { private CITParser() {}
for (Map.Entry<ResourcePack, Set<Identifier>> citPackEntry : citPacks.entrySet()) {
CITPack citPack = new CITPack(citPackEntry.getKey());
- for (Identifier citIdentifier : citPackEntry.getValue()) {
- try {
- InputStream is = citPackEntry.getKey().open(ResourceType.CLIENT_RESOURCES, citIdentifier);
- Properties citProperties = new Properties();
+ InputStream is = null;
+ Properties citProperties = new Properties();
+ try {
+ if (citPackEntry.getValue().remove(mcpatcherCITSettingsIdentifier))
+ is = citPackEntry.getKey().open(ResourceType.CLIENT_RESOURCES, mcpatcherCITSettingsIdentifier);
+ else if (citPackEntry.getValue().remove(mcpatcherCITSettingsIdentifier))
+ is = citPackEntry.getKey().open(ResourceType.CLIENT_RESOURCES, mcpatcherCITSettingsIdentifier);
+ else if (citPackEntry.getValue().remove(mcpatcherCITSettingsIdentifier))
+ is = citPackEntry.getKey().open(ResourceType.CLIENT_RESOURCES, mcpatcherCITSettingsIdentifier);
+
+ if (is != null) {
citProperties.load(is);
- is.close();
+ citPack.loadProperties(citProperties);
+ }
+ } catch (Exception e) {
+ CITResewn.logErrorLoading(e.getMessage());
+ } finally {
+ IOUtils.closeQuietly(is);
+ }
+
- if (citIdentifier == citresewnCITSettingsIdentifier || citIdentifier == mcpatcherCITSettingsIdentifier || citIdentifier == optifineCITSettingsIdentifier)
- citPack.loadProperties(citProperties);
- else
- citPack.cits.add(parseCIT(citPack, citIdentifier, citProperties));
+ for (Identifier citIdentifier : citPackEntry.getValue()) {
+ try {
+ citProperties = new Properties();
+ citProperties.load(is = citPackEntry.getKey().open(ResourceType.CLIENT_RESOURCES, citIdentifier));
+ CITConstructor type = REGISTRY.get(citProperties.getProperty("type", "item"));
+ if (type == null)
+ throw new CITParseException(citPack.resourcePack, citIdentifier, "Unknown cit type \"" + citProperties.getProperty("type") + "\"");
+ citPack.cits.add(type.cit(citPack, citIdentifier, citProperties));
} catch (Exception e) {
CITResewn.logErrorLoading(e.getMessage());
+ } finally {
+ IOUtils.closeQuietly(is);
}
}
cits.addAll(citPack.cits);
@@ -69,13 +98,7 @@ public class CITParser { private CITParser() {}
return cits;
}
- public static CIT parseCIT(CITPack pack, Identifier identifier, Properties properties) throws CITParseException {
- return switch (properties.getProperty("type", "item")) {
- case "item" -> new CITItem(pack, identifier, properties);
- case "armor" -> new CITArmor(pack, identifier, properties);
- case "elytra" -> new CITElytra(pack, identifier, properties);
- case "enchantment" -> new CITEnchantment(pack, identifier, properties);
- default -> throw new CITParseException(pack.resourcePack, identifier, "Unknown cit type");
- };
+ public interface CITConstructor {
+ CIT cit(CITPack pack, Identifier identifier, Properties properties) throws CITParseException;
}
}