diff --git a/Applications/Console/pom.xml b/Applications/Console/pom.xml index 43239265..659243c6 100644 --- a/Applications/Console/pom.xml +++ b/Applications/Console/pom.xml @@ -5,7 +5,7 @@ Applications com.agentdid127.resourcepack - 2.2.4 + 2.2.5 4.0.0 diff --git a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Main.java b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Main.java index 5155d27f..3d4e7736 100644 --- a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Main.java +++ b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Main.java @@ -5,15 +5,17 @@ import com.agentdid127.resourcepack.library.utilities.Util; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.Strictness; import joptsimple.OptionSet; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Path; public class Main { /** * Main class. Runs program - * + * * @param args * @throws IOException */ @@ -24,20 +26,24 @@ public static void main(String[] args) throws IOException { return; } - String from = optionSet.valueOf(Options.FROM); - String to = optionSet.valueOf(Options.TO); + Path inputPath = optionSet.valueOf(Options.INPUT_DIR); String light = optionSet.valueOf(Options.LIGHT); boolean minify = optionSet.has(Options.MINIFY); boolean debug = optionSet.valueOf(Options.DEBUG); PrintStream out = System.out; - GsonBuilder gsonBuilder = new GsonBuilder(); + + GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping().setStrictness(Strictness.LENIENT); + if (!minify) { + gsonBuilder.setPrettyPrinting(); + } Gson gson = gsonBuilder.disableHtmlEscaping().create(); - if (Util.getVersionProtocol(gson, from) > Util.getVersionProtocol(gson, to)) - new BackwardsPackConverter(from, to, light, minify, optionSet.valueOf(Options.INPUT_DIR), debug, out) - .runDir(); - else - new ForwardsPackConverter(from, to, light, minify, optionSet.valueOf(Options.INPUT_DIR), debug, out) - .runDir(); + int from = Util.getVersionProtocol(gson, optionSet.valueOf(Options.FROM)); + int to = Util.getVersionProtocol(gson, optionSet.valueOf(Options.TO)); + if (from < to) { + new ForwardsPackConverter(gson, from, to, light, inputPath, debug, out).runDir(); + } else { + new BackwardsPackConverter(gson, from, to, inputPath, debug, out).runDir(); + } } } \ No newline at end of file diff --git a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java index 3362bfff..15f81b4f 100644 --- a/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java +++ b/Applications/Console/src/main/java/com/agentdid127/resourcepack/Options.java @@ -16,7 +16,7 @@ public class Options { public static final OptionSpec INPUT_DIR = PARSER.acceptsAll(Arrays.asList("i", "input", "input-dir"), "Input directory for the packs").withRequiredArg().withValuesConvertedBy(new PathConverter()).defaultsTo(Paths.get("./")); - public static final OptionSpec DEBUG = PARSER.accepts("debug", "Displays other output").withRequiredArg().ofType(Boolean.class).defaultsTo(true); + public static final OptionSpec DEBUG = PARSER.accepts("debug", "Displays other output").withRequiredArg().ofType(Boolean.class).defaultsTo(false); public static final OptionSpec TO = PARSER.accepts("to", "Updates to version").withRequiredArg().ofType(String.class).defaultsTo("1.13"); diff --git a/Applications/Gui/pom.xml b/Applications/Gui/pom.xml index e7d77534..6f44e016 100644 --- a/Applications/Gui/pom.xml +++ b/Applications/Gui/pom.xml @@ -5,7 +5,7 @@ Applications com.agentdid127.resourcepack - 2.2.4 + 2.2.5 4.0.0 diff --git a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.form b/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.form deleted file mode 100644 index 2f9d6f4e..00000000 --- a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.form +++ /dev/null @@ -1,86 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java b/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java index b8cf1a77..a66d52fa 100644 --- a/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java +++ b/Applications/Gui/src/main/java/com/agentdid127/resourcepack/GUI.java @@ -5,155 +5,165 @@ import com.agentdid127.resourcepack.library.utilities.Util; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.Strictness; import javax.swing.*; +import javax.swing.text.DefaultCaret; import java.awt.*; -import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Objects; -public class GUI { - static JFrame frame; - private JPanel panel1; - private JComboBox initialVersionBox; - private JComboBox finalVersionBox; - private JTextArea outputArea; - private JCheckBox minifyCheckBox; - private JLabel finalVersionLabel; - private JLabel initialVersionLabel; - private JLabel outputLabel; - private JButton convertButton; +public class GUI extends JPanel { + private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().setStrictness(Strictness.LENIENT).disableHtmlEscaping().create(); private PrintStream out; - private GsonBuilder gsonBuilder = new GsonBuilder(); - private Gson gson = gsonBuilder.disableHtmlEscaping().create(); + private final JTextArea outputLogPane; + private final JComboBox baseVersions; + private final JComboBox targetVersions; + private final JCheckBox minifyCheckBox; + private final JComboBox lightOptions; + private final JButton convertButton; public GUI() { - String[] versions = Util.getSupportedVersions(gson); + // Log Output Panel + final JPanel logPanel = new JPanel(); + final JScrollPane scrollPane = new JScrollPane(); + this.outputLogPane = new JTextArea(); + this.outputLogPane.setColumns(60); + this.outputLogPane.setRows(20); + this.outputLogPane.setText(""); + this.outputLogPane.setEditable(false); + ((DefaultCaret) this.outputLogPane.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); + scrollPane.setViewportView(this.outputLogPane); + logPanel.add(scrollPane); + this.add(logPanel); + + // Menubar Panel + final JPanel menuBarPanel = new JPanel(); + // Base Version + JLabel baseVersionLabel = new JLabel(); + baseVersionLabel.setText("Base Version"); + menuBarPanel.add(baseVersionLabel); + this.baseVersions = new JComboBox<>(); + baseVersionLabel.setLabelFor(this.baseVersions); + menuBarPanel.add(this.baseVersions); + + // Target Version + JLabel targetVersionsLabel = new JLabel(); + targetVersionsLabel.setText("Target Version"); + menuBarPanel.add(targetVersionsLabel); + this.targetVersions = new JComboBox<>(); + targetVersionsLabel.setLabelFor(targetVersions); + menuBarPanel.add(this.targetVersions); + + // Add items to both ^ + String[] versions = Util.getSupportedVersions(GSON); + if (versions == null) { + throw new RuntimeException("Failed to get supported version, application possibly corrupt!"); + } else if (versions.length > 0) { + for (String version : versions) { + baseVersions.addItem(version); + targetVersions.addItem(version); + } - for (String item : versions) { - initialVersionBox.addItem(item); - finalVersionBox.addItem(item); + targetVersions.setSelectedIndex(targetVersions.getItemCount() - 1); } - convertButton.addActionListener(e -> { - out = redirectSystemStreams(); + // Minify Checkbox + JLabel minifyCheckboxLabel = new JLabel(); + minifyCheckboxLabel.setText("Minify"); + menuBarPanel.add(minifyCheckboxLabel); + + this.minifyCheckBox = new JCheckBox(); + minifyCheckboxLabel.setLabelFor(this.minifyCheckBox); + menuBarPanel.add(this.minifyCheckBox); + + // Item Lighting Options + JLabel lightOptionsLabel = new JLabel(); + lightOptionsLabel.setText("Item Lighting"); + menuBarPanel.add(lightOptionsLabel); + + this.lightOptions = new JComboBox<>(); + lightOptionsLabel.setLabelFor(this.lightOptions); + menuBarPanel.add(this.lightOptions); - String from = initialVersionBox.getSelectedItem().toString(); - String to = finalVersionBox.getSelectedItem().toString(); - String light = "none"; + Arrays.stream((new String[]{"none", "front", "side"})).forEach(lightOptions::addItem); + // Convert Button + this.convertButton = new JButton(); + this.convertButton.setText("Convert"); + this.convertButton.addActionListener(e -> { + out = redirectSystemStreams(); + String light = Objects.requireNonNull(lightOptions.getSelectedItem()).toString(); + int from = Util.getVersionProtocol(GSON, Objects.requireNonNull(baseVersions.getSelectedItem()).toString()); + int to = Util.getVersionProtocol(GSON, Objects.requireNonNull(targetVersions.getSelectedItem()).toString()); boolean minify = minifyCheckBox.isSelected(); new Thread(() -> { - convertButton.setVisible(false); + convertButton.setEnabled(false); try { - if (Util.getVersionProtocol(gson, from) > Util.getVersionProtocol(gson, to)) - new BackwardsPackConverter(from, to, light, minify, Paths.get("./"), true, out).runDir(); - else - new ForwardsPackConverter(from, to, light, minify, Paths.get("./"), true, out).runDir(); - } catch (IOException ioException) { - out.println(Arrays.toString(ioException.getStackTrace())); + Gson packGson = GSON; + if (!minify) { + packGson = packGson.newBuilder().setPrettyPrinting().create(); + } + + Path dotPath = Paths.get("./"); + if (from < to) { + new ForwardsPackConverter(packGson, from, to, light, dotPath, true, out).runDir(); + } else { + new BackwardsPackConverter(packGson, from, to, dotPath, true, out).runDir(); + } + } catch (Exception exception) { + out.println(Arrays.toString(exception.getStackTrace())); } - convertButton.setVisible(true); - return; + convertButton.setEnabled(true); }).start(); }); + menuBarPanel.add(this.convertButton); + + this.add(menuBarPanel); } public static void main(String[] args) { - frame = new JFrame("Resource Pack Converter"); - frame.setContentPane(new GUI().panel1); + JFrame frame = new JFrame("RPC - Resource Pack Converter"); + frame.setContentPane(new GUI()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + try { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); + } catch (Exception ignored) { + System.err.println("GTK look not supported, ignoring."); + } + + Dimension dimensions = new Dimension(854, 480); + frame.setMinimumSize(dimensions); + frame.setPreferredSize(dimensions); + frame.setFocusable(true); + frame.setResizable(false); + frame.setLocationRelativeTo(null); // Centers window on screen frame.pack(); frame.setVisible(true); } - private void updateTextArea(final String text) { - outputArea.append(text); - } - - // Followings are The Methods that do the Redirect, you can simply Ignore them. private PrintStream redirectSystemStreams() { - OutputStream out2 = new OutputStream() { + return new PrintStream(new OutputStream() { @Override - public void write(int b) throws IOException { - updateTextArea(String.valueOf((char) b)); + public void write(int b) { + outputLogPane.append(String.valueOf((char) b)); } @Override - public void write(byte[] b, int off, int len) throws IOException { - updateTextArea(new String(b, off, len)); + public void write(byte[] b, int off, int len) { + outputLogPane.append(new String(b, off, len)); } @Override - public void write(byte[] b) throws IOException { + public void write(byte[] b) { write(b, 0, b.length); } - }; - - return new PrintStream(out2); - } - - { - // GUI initializer generated by IntelliJ IDEA GUI Designer - // >>> IMPORTANT!! <<< - // DO NOT EDIT OR ADD ANY CODE HERE! - $$$setupUI$$$(); - } - - /** - * Method generated by IntelliJ IDEA GUI Designer - * >>> IMPORTANT!! <<< - * DO NOT edit this method OR call it in your code! - * - * @noinspection ALL - */ - private void $$$setupUI$$$() { - panel1 = new JPanel(); - panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); - panel1.setFocusable(true); - panel1.setMaximumSize(new Dimension(700, 375)); - panel1.setMinimumSize(new Dimension(700, 375)); - panel1.setPreferredSize(new Dimension(700, 375)); - final JPanel panel2 = new JPanel(); - panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); - panel1.add(panel2); - initialVersionBox = new JComboBox(); - panel2.add(initialVersionBox); - initialVersionLabel = new JLabel(); - initialVersionLabel.setText("Initial Version"); - panel2.add(initialVersionLabel); - finalVersionBox = new JComboBox(); - panel2.add(finalVersionBox); - finalVersionLabel = new JLabel(); - finalVersionLabel.setText("Final Version"); - panel2.add(finalVersionLabel); - minifyCheckBox = new JCheckBox(); - minifyCheckBox.setText("Minify"); - panel2.add(minifyCheckBox); - final JPanel panel3 = new JPanel(); - panel3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); - panel1.add(panel3); - outputLabel = new JLabel(); - outputLabel.setText("Output"); - panel3.add(outputLabel); - final JScrollPane scrollPane1 = new JScrollPane(); - panel3.add(scrollPane1); - outputArea = new JTextArea(); - outputArea.setColumns(60); - outputArea.setRows(15); - outputArea.setText(""); - scrollPane1.setViewportView(outputArea); - convertButton = new JButton(); - convertButton.setActionCommand("Convert"); - convertButton.setLabel("Convert"); - convertButton.setText("Convert"); - panel1.add(convertButton); - } - - public JComponent $$$getRootComponent$$$() { - return panel1; + }); } } diff --git a/Applications/pom.xml b/Applications/pom.xml index 1360e1a8..9ed8e767 100644 --- a/Applications/pom.xml +++ b/Applications/pom.xml @@ -5,7 +5,7 @@ ResourcePackConverter com.agentdid127 - 2.2.4 + 2.2.5 4.0.0 @@ -33,12 +33,12 @@ com.agentdid127.resourcepack Forwards - 2.2.4 + 2.2.5 com.agentdid127.resourcepack Backwards - 2.2.4 + 2.2.5 diff --git a/LICENSE b/LICENSE index a0f0e2ac..577621d8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Hypixel Inc. +Copyright (c) 2024 agentdid127. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.MD b/README.MD index 86ee49e5..1d46af50 100644 --- a/README.MD +++ b/README.MD @@ -1,12 +1,9 @@ # Resource Pack Converter [![](https://jitpack.io/v/agentdid127/ResourcePackConverter.svg)](https://jitpack.io/#agentdid127/ResourcePackConverter) +This is a program that converts most issues with resource packs between any version from 1.7.2 all the way to 1.21.4. -This is my fork of Hypixel's Resource Pack Converter. It updates to any newer version from 1.8.x - 1.20.x - -We know that many use resource packs in nonstandard and quirky ways - but giving this a shot *may* reduce quite a bit of your pain and workload for the 1.13 conversion. - -This should convert most things, but if it doesn't please let me know what didn't work so I can fix it. +This should convert most things, but if you notice anything that does not work correctly, or should be added, create an issue on Github. Also if any other developers would like to open any PRs with fixes and additions please feel free. @@ -31,9 +28,12 @@ To update to any other version than 1.12-1.13, you can use these parameters. We hope this helps out! -# Not On the list to add -- Horse and Zombified Piglin Conversion (the models were changed, which may make that much more difficult to convert) +## Not On the list to add +- Horse, Zombified Piglin, and Bat Conversion (the models were changed, which may make that much more difficult to convert) ## In-Development Versions Before a version of the converter is ready for release, it will be automatically built on a Jenkins server, where you can get non-production builds. Note: that there is no guaruntee that these versions are stable, however, feel free to post issue reports on the issues page. [Jenkins Server](https://jenkins.dc1.agentdid127.com/job/agentdid127/job/ResourcePackConverter/job/master/) + +### Upstream +This program was originally derived from Hypixel's Resource Pack Converter, though has been essentially reworked over the span of 5 years. Very few, if any bits of the original software remain, but in case that does exist, their original license is included in the licenses' directory. diff --git a/conversions/Backwards/pom.xml b/conversions/Backwards/pom.xml index f9628855..5d49c732 100644 --- a/conversions/Backwards/pom.xml +++ b/conversions/Backwards/pom.xml @@ -5,7 +5,7 @@ conversions com.agentdid127.resourcepack - 2.2.4 + 2.2.5 4.0.0 diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java index 565909fe..45821116 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/BackwardsPackConverter.java @@ -7,107 +7,85 @@ import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.GsonBuilder; +import com.google.gson.Gson; -import java.io.IOException; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; +import java.util.stream.Stream; public class BackwardsPackConverter extends PackConverter { Path INPUT_DIR; + private final int from; + private final int to; - public BackwardsPackConverter(String from, String to, String light, boolean minify, Path input, boolean debug, - PrintStream out) { - GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping().setLenient(); - if (!minify) - gsonBuilder.setPrettyPrinting(); - gson = gsonBuilder.create(); - DEBUG = debug; + public BackwardsPackConverter(Gson gson, int from, int to, Path input, boolean debug, PrintStream out) { + this.gson = gson; + Logger.setDebug(debug); Logger.setStream(out); - Logger.log(from); - Logger.log(to); + Logger.log("Converting packs from: " + from + " to " + to); + this.from = from; + this.to = to; this.INPUT_DIR = input; - converterRunner(from, to, light); + this.setupConverters(); } - private void converterRunner(String from, String to, String light) { - int protocolFrom = Util.getVersionProtocol(gson, from); - int protocolTo = Util.getVersionProtocol(gson, to); - - // This needs to be run first, other converters might reference - // new directory names. - this.registerConverter(new NameConverter(this, protocolFrom, protocolTo)); - this.registerConverter(new PackMetaConverter(this, protocolTo)); - - this.registerConverter(new DeleteFileConverter(this, protocolFrom, protocolTo)); - + private void setupConverters() { + this.registerConverter(new NameConverter(this, from, to)); // This needs to be run first, other converters might reference new directory names. + this.registerConverter(new PackMetaConverter(this, to)); + this.registerConverter(new DeleteFileConverter(this, from, to)); // TODO: backwards TitleConverter for going from 1.20 to below - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.19.4") - && protocolTo < Util.getVersionProtocol(gson, "1.19.4")) - this.registerConverter(new EnchantPathConverter(this)); - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.18") - && protocolTo < Util.getVersionProtocol(gson, "1.18")) { - this.registerConverter(new ParticleConverter(this)); - this.registerConverter(new InventoryConverter(this)); - } - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.15") - && protocolTo < Util.getVersionProtocol(gson, "1.15")) { - this.registerConverter(new EnchantConverter(this)); - this.registerConverter(new ChestConverter(this)); - } - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.14") - && protocolTo < Util.getVersionProtocol(gson, "1.14")) - this.registerConverter(new PaintingConverter(this)); - - this.registerConverter(new ParticleTextureConverter(this, protocolFrom, protocolTo)); - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.13") - && protocolTo < Util.getVersionProtocol(gson, "1.13")) { - this.registerConverter(new LangConverter(this, from, to)); - // this.registerConverter(new SoundsConverter(this)); - // this.registerConverter(new AnimationConverter(this)); - this.registerConverter(new MapIconConverter(this)); - this.registerConverter(new MCPatcherConverter(this)); - this.registerConverter(new WaterConverter(this)); - } - - this.registerConverter(new BlockStateConverter(this, protocolFrom, protocolTo)); - this.registerConverter(new ModelConverter(this, light, protocolFrom, protocolTo)); - - if (protocolFrom >= Util.getVersionProtocol(gson, "1.9") - && protocolTo < Util.getVersionProtocol(gson, "1.9")) - this.registerConverter(new CompassConverter(this, protocolTo)); + this.registerConverter(new EnchantPathConverter(this)); + this.registerConverter(new ParticleConverter(this)); + this.registerConverter(new InventoryConverter(this)); + this.registerConverter(new EnchantConverter(this)); + this.registerConverter(new ChestConverter(this)); + this.registerConverter(new PaintingConverter(this)); + this.registerConverter(new ParticleTextureConverter(this, from, to)); + this.registerConverter(new LangConverter(this, from, to)); +// this.registerConverter(new SoundsConverter(this)); // return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); +// this.registerConverter(new AnimationConverter(this)); // return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); + this.registerConverter(new MapIconConverter(this)); + this.registerConverter(new MCPatcherConverter(this)); + this.registerConverter(new WaterConverter(this)); + this.registerConverter(new BlockStateConverter(this, from, to)); + this.registerConverter(new ModelConverter(this, from, to)); + this.registerConverter(new CompassConverter(this, to)); } public void runPack(Pack pack) { try { Logger.log("Converting " + pack); + Logger.addTab(); pack.getHandler().setup(); - Logger.log(" Running Converters"); + Logger.log("Running Converters"); for (Converter converter : converters.values()) { - if (DEBUG) - Logger.log(" Running " + converter.getClass().getSimpleName()); - converter.convert(pack); + if (converter.shouldConvert(gson, from, to)) { + Logger.addTab(); + Logger.log("Running " + converter.getClass().getSimpleName()); + converter.convert(pack); + Logger.subTab(); + } } pack.getHandler().finish(); + Logger.subTab(); } catch (Throwable t) { + Logger.resetTab(); Logger.log("Failed to convert!"); Util.propagate(t); } } - public void runDir() throws IOException { - Files.list(INPUT_DIR) - .map(Pack::parse) - .filter(Objects::nonNull) - .forEach(pack -> { - runPack(pack); - }); + public void runDir() { + try (Stream pathStream = Files.list(INPUT_DIR)) { + try (Stream packStream = pathStream.map(Pack::parse).filter(Objects::nonNull)) { + packStream.forEach(this::runPack); + } catch (Exception exception) { + Util.propagate(exception); + } + } catch (Exception exception) { + Util.propagate(exception); + } } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java index 02b5f407..9bd7dff2 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/BlockStateConverter.java @@ -6,6 +6,7 @@ import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -15,9 +16,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; +import java.util.stream.Stream; public class BlockStateConverter extends Converter { - private int from, to; + private final int from; + private final int to; private boolean anyChanges; public BlockStateConverter(PackConverter packConverter, int from, int to) { @@ -26,81 +29,86 @@ public BlockStateConverter(PackConverter packConverter, int from, int to) { this.to = to; } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + /** * Updates blockstates in blockstates folder - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path blockstatesPath = pack.getWorkingPath() - .resolve("assets/minecraft/blockstates".replace("/", File.separator)); - if (!blockstatesPath.toFile().exists()) + Path blockstatesPath = pack.getWorkingPath().resolve("assets/minecraft/blockstates".replace("/", File.separator)); + if (!blockstatesPath.toFile().exists()) { return; - Files.list(blockstatesPath) - .filter(file -> file.toString().endsWith(".json")) - .forEach(file -> { - try { - JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); - anyChanges = false; - - // process multipart - JsonArray multipartArray = json.getAsJsonArray("multipart"); - - if (multipartArray != null) { - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.9")) { - // TODO: Convert Multipart to variants - Files.delete(file); - return; - } else { - for (int i = 0; i < multipartArray.size(); i++) { - JsonObject multipartObject = multipartArray.get(i) - .getAsJsonObject(); - for (Map.Entry entry : multipartObject.entrySet()) - updateModelPath(entry); - } + } + + try (Stream pathStream = Files.list(blockstatesPath).filter(file -> file.toString().endsWith(".json"))) { + pathStream.forEach(file -> { + try { + JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + anyChanges = false; + + // process multipart + JsonArray multipartArray = json.getAsJsonArray("multipart"); + + if (multipartArray != null) { + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.9")) { + // TODO: Convert Multipart to variants + Files.delete(file); + return; + } else { + for (int i = 0; i < multipartArray.size(); i++) { + JsonObject multipartObject = multipartArray.get(i) + .getAsJsonObject(); + for (Map.Entry entry : multipartObject.entrySet()) + updateModelPath(entry); } } + } - // process variants - JsonObject variantsObject = json.getAsJsonObject("variants"); - if (variantsObject != null) { - // change "normal" key to "" - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - - if (!variantsObject.has("")) { - // TODO: find a better way to deal with this. - Files.delete(file); - return; - } - JsonElement normal = variantsObject.get(""); - if (normal instanceof JsonObject || normal instanceof JsonArray) { - variantsObject.add("normal", normal); - variantsObject.remove(""); - anyChanges = true; - } - } + // process variants + JsonObject variantsObject = json.getAsJsonObject("variants"); + if (variantsObject != null) { + // change "normal" key to "" + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - // update model paths to prepend block - for (Map.Entry entry : variantsObject.entrySet()) - updateModelPath(entry); - } - if (anyChanges) { - JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + if (!variantsObject.has("")) { + // TODO: find a better way to deal with this. + Files.delete(file); + return; + } + JsonElement normal = variantsObject.get(""); + if (normal instanceof JsonObject || normal instanceof JsonArray) { + variantsObject.add("normal", normal); + variantsObject.remove(""); + anyChanges = true; + } } - } catch (IOException e) { - Util.propagate(e); + + // update model paths to prepend block + for (Map.Entry entry : variantsObject.entrySet()) + updateModelPath(entry); + } + if (anyChanges) { + JsonUtil.writeJson(packConverter.getGson(), file, json); + Logger.debug("Converted " + file.getFileName()); } - }); + } catch (IOException e) { + Util.propagate(e); + } + }); + } } /** * Updates Model paths - * + * * @param entry */ private void updateModelPath(Map.Entry entry) { @@ -110,9 +118,7 @@ private void updateModelPath(Map.Entry entry) { if (value.has("model")) { String[] split = value.get("model").getAsString().split("/"); String val = split[split.length - 1]; - String prefix = value.get("model").getAsString().substring(0, - value.get("model").getAsString().length() - val.length()); - + String prefix = value.get("model").getAsString().substring(0, value.get("model").getAsString().length() - val.length()); if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") && to < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { prefix = prefix.replaceAll("minecraft:", ""); @@ -144,8 +150,9 @@ private void updateModelPath(Map.Entry entry) { anyChanges = true; } - if (anyChanges) + if (anyChanges) { value.addProperty("model", prefix + val); + } } } else if (entry.getValue() instanceof JsonArray) { // some states have arrays for (JsonElement jsonElement : ((JsonArray) entry.getValue())) { @@ -188,8 +195,9 @@ private void updateModelPath(Map.Entry entry) { anyChanges = true; } - if (anyChanges) + if (anyChanges) { value.addProperty("model", prefix + val); + } } } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/DeleteFileConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/DeleteFileConverter.java index 11a54e53..19263206 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/DeleteFileConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/DeleteFileConverter.java @@ -6,6 +6,7 @@ import com.agentdid127.resourcepack.library.utilities.FileUtil; import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -16,7 +17,8 @@ import java.nio.file.Path; public class DeleteFileConverter extends Converter { - int from, to; + private final int from; + private final int to; public DeleteFileConverter(PackConverter packConverter, int from, int to) { super(packConverter); @@ -25,12 +27,14 @@ public DeleteFileConverter(PackConverter packConverter, int from, int to) { } @Override - public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath() - .resolve("assets/minecraft/models".replace("/", File.separator)); - Path textures = pack.getWorkingPath() - .resolve("assets/minecraft/textures".replace("/", File.separator)); + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + @Override + public void convert(Pack pack) throws IOException { + Path models = pack.getWorkingPath().resolve("assets/minecraft/models".replace("/", File.separator)); + Path textures = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); for (int i = from; i > to; i--) { deleteBlocks(models, textures, i); deleteItems(models, textures, i); @@ -39,33 +43,39 @@ public void convert(Pack pack) throws IOException { findFiles(models); findFiles(textures); - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { - Path atlases = pack.getWorkingPath() - .resolve("assets/minecraft/atlases".replace("/", File.separator)); - if (atlases.toFile().exists()) + && to <= Util.getVersionProtocol(packConverter.getGson(), "1.19.2")) { + Path atlases = pack.getWorkingPath().resolve("assets/minecraft/atlases".replace("/", File.separator)); + if (atlases.toFile().exists()) { FileUtil.deleteDirectoryAndContents(atlases); + } } } protected void findFiles(Path path) throws IOException { if (!path.toFile().exists()) return; + File directory = path.toFile(); File[] filesList = directory.listFiles(); - for (File file : filesList) - if (file.isDirectory()) + + assert filesList != null; + for (File file : filesList) { + if (file.isDirectory()) { findFiles(file.toPath()); + } + } + filesList = directory.listFiles(); - if (filesList.length == 0) + assert filesList != null; + if (filesList.length == 0) { Files.deleteIfExists(directory.toPath()); + } } public void deleteBlocks(Path models, Path textures, int version) throws IOException { String protocol = Util.getVersionFromProtocol(packConverter.getGson(), version); JsonObject blocks = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/delete/blocks.json"); - if (blocks.has(protocol)) { Path blockMPath, blockTPath; if (version < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { @@ -89,7 +99,6 @@ public void deleteBlocks(Path models, Path textures, int version) throws IOExcep public void deleteItems(Path models, Path textures, int version) throws IOException { String protocol = Util.getVersionFromProtocol(packConverter.getGson(), version); JsonObject items = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/delete/items.json"); - if (items.has(protocol)) { Path itemMPath, itemTPath; if (version < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java index 43f2ee9f..37d65854 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java @@ -3,6 +3,8 @@ import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -14,21 +16,47 @@ public EnchantPathConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.19.4") && to <= Util.getVersionProtocol(gson, "1.19.3"); + } + @Override public void convert(Pack pack) throws IOException { Path miscPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc".replace("/", File.separator)); + if (miscPath.toFile().exists()) { + return; + } + + Path enchantGlintItemPath = miscPath.resolve("enchanted_glint_item.png"); + Path enchantGlintEntityPath = miscPath.resolve("enchanted_glint_entity.png"); + Path newEnchantGlintItemPath = miscPath.resolve("enchanted_item_glint.png"); + if (enchantGlintItemPath.toFile().exists()) { + if (newEnchantGlintItemPath.toFile().exists()) { + newEnchantGlintItemPath.toFile().delete(); + } + Files.move(enchantGlintItemPath, newEnchantGlintItemPath); + } else if (enchantGlintEntityPath.toFile().exists()) { + if (newEnchantGlintItemPath.toFile().exists()) { + newEnchantGlintItemPath.toFile().delete(); + } + Files.move(enchantGlintEntityPath, newEnchantGlintItemPath); + } - if (miscPath.resolve("enchanted_glint_item.png").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_item.png"), miscPath.resolve("enchanted_item_glint.png")); - else if (miscPath.resolve("enchanted_glint_entity.png").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_entity.png"), miscPath.resolve("enchanted_item_glint.png")); - - if (miscPath.resolve("enchanted_glint_item.png.mcmeta").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_item.png.mcmeta"), - miscPath.resolve("enchanted_item_glint.png.mcmeta")); - else if (miscPath.resolve("enchanted_glint_entity.png.mcmeta").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_entity.png.mcmeta"), - miscPath.resolve("enchanted_item_glint.png.mcmeta")); + Path enchantGlintItemMetaPath = miscPath.resolve("enchanted_glint_item.png.mcmeta"); + Path enchantGlintEntityMetaPath = miscPath.resolve("enchanted_glint_entity.png.mcmeta"); + Path newEnchantGlintItemMetaPath = miscPath.resolve("enchanted_item_glint.png.mcmeta"); + if (enchantGlintItemMetaPath.toFile().exists()) { + if (newEnchantGlintItemMetaPath.toFile().exists()) { + newEnchantGlintItemMetaPath.toFile().delete(); + } + Files.move(enchantGlintItemMetaPath, newEnchantGlintItemMetaPath); + } else if (enchantGlintEntityMetaPath.toFile().exists()) { + if (newEnchantGlintItemMetaPath.toFile().exists()) { + newEnchantGlintItemMetaPath.toFile().delete(); + } + Files.move(enchantGlintEntityMetaPath, newEnchantGlintItemMetaPath); + } Files.deleteIfExists(miscPath.resolve("enchanted_glint_entity.png.mcmeta")); Files.deleteIfExists(miscPath.resolve("enchanted_glint_item.png.mcmeta")); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java index 88427e5a..a20a0f4d 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/LangConverter.java @@ -7,53 +7,58 @@ import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.PropertiesEx; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Map; +import java.util.stream.Stream; public class LangConverter extends Converter { - private String version; - private String from; + private final int from; + private final int to; - public LangConverter(PackConverter packConverter, String fromIn, String versionIn) { + public LangConverter(PackConverter packConverter, int from, int to) { super(packConverter); - version = versionIn; - from = fromIn; + this.from = from; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); } /** * Moves Lang (properties) to JSON - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path langPath = pack.getWorkingPath() - .resolve("assets/minecraft/lang".replace("/", File.separator)); - if (!langPath.toFile().exists()) + Path langPath = pack.getWorkingPath().resolve("assets/minecraft/lang".replace("/", File.separator)); + if (!langPath.toFile().exists()) { return; - ArrayList models = new ArrayList(); - Files.list(langPath) - .filter(path1 -> path1.toString().endsWith(".json")) - .forEach(model -> { - PropertiesEx out = new PropertiesEx(); - try (InputStream input = new FileInputStream(model.toString())) { - JsonObject object = JsonUtil.readJson(packConverter.getGson(), model, JsonObject.class); + } - if (Util.getVersionProtocol(packConverter.getGson(), from) > Util - .getVersionProtocol(packConverter.getGson(), "1.12") - && ((Util.getVersionProtocol(packConverter.getGson(), version) < Util - .getVersionProtocol(packConverter.getGson(), "1.13")) - && (Util.getVersionProtocol(packConverter.getGson(), version) > Util - .getVersionProtocol(packConverter.getGson(), "1.13.2")))) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/lang.json") - .getAsJsonObject("1_13"); + ArrayList models = new ArrayList<>(); + try (Stream pathStream = Files.list(langPath).filter(path1 -> path1.toString().endsWith(".json"))) { + pathStream.forEach(model -> { + PropertiesEx out = new PropertiesEx(); + try { + JsonObject object = JsonUtil.readJson(packConverter.getGson(), model, JsonObject.class); + if (from > Util.getVersionProtocol(packConverter.getGson(), "1.12") + && ((to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) + && (to > Util.getVersionProtocol(packConverter.getGson(), "1.13.2")))) { + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/lang.json").getAsJsonObject("1_13"); + if (object != null) { object.keySet().forEach(key -> { String value = object.get(key).getAsString(); for (Map.Entry id2 : id.entrySet()) { @@ -63,11 +68,11 @@ public void convert(Pack pack) throws IOException { } }); } + } - if (Util.getVersionProtocol(packConverter.getGson(), version) <= Util - .getVersionProtocol(packConverter.getGson(), "1.14")) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/lang.json") - .getAsJsonObject("1_14"); + if (to <= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/lang.json").getAsJsonObject("1_14"); + if (object != null) { object.keySet().forEach(key -> { String value = object.get(key).getAsString(); for (Map.Entry id2 : id.entrySet()) @@ -75,35 +80,29 @@ public void convert(Pack pack) throws IOException { out.setProperty(id2.getValue().getAsString(), value); }); } - - input.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } - try { - int modelNoJson = model.getFileName().toString().indexOf(".json"); - String file2 = model.getFileName().toString().substring(0, modelNoJson); - Logger.log("Saving: " + file2 + ".lang"); - out.store( - new FileOutputStream( - pack.getWorkingPath() - .resolve(("assets/minecraft/lang/" + file2 + ".lang").replace("/", - File.separator)) - .toFile()), - ""); - } catch (IOException e) { - e.printStackTrace(); - } + try { + int modelNoJson = model.getFileName().toString().indexOf(".json"); + String file2 = model.getFileName().toString().substring(0, modelNoJson); + Logger.debug("Saving: " + file2 + ".lang"); + out.store(Files.newOutputStream(pack.getWorkingPath().resolve(("assets/minecraft/lang/" + file2 + ".lang").replace("/", File.separator)).toFile().toPath()), ""); + } catch (IOException e) { + e.printStackTrace(); + } + + models.add(model.getFileName().toString()); + }); + } - models.add(model.getFileName().toString()); - }); - for (int i = 0; i < models.size(); i++) { - Path langFilePath = pack.getWorkingPath() - .resolve(("assets/minecraft/lang/" + models.get(i)).replace("/", File.separator)); - Logger.log("Deleting: " + langFilePath); + for (String model : models) { + Path langFilePath = pack.getWorkingPath().resolve(("assets/minecraft/lang/" + model).replace("/", File.separator)); + Logger.debug("Deleting: " + langFilePath); Files.delete(langFilePath); } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java index a9af3ca2..0fa8da23 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/MCPatcherConverter.java @@ -7,11 +7,19 @@ import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.PropertiesEx; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonObject; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Stream; @Deprecated // will be removed when extensions are made public class MCPatcherConverter extends Converter { @@ -19,76 +27,79 @@ public MCPatcherConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); + } + /** * Parent conversion for MCPatcher - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath() - .resolve("assets/minecraft/mcpatcher".replace("/", File.separator)); - if (models.toFile().exists()) + Path models = pack.getWorkingPath().resolve("assets/minecraft/mcpatcher".replace("/", File.separator)); + if (models.toFile().exists()) { findFiles(models); + } // remapModelJson(models.resolve("item")); // remapModelJson(models.resolve("block")); } /** * Finds all files in Path path - * + * * @param path * @throws IOException */ protected void findFiles(Path path) throws IOException { File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - continue; - remapProperties(file.toPath()); - findFiles(file.toPath()); + for (File file : Objects.requireNonNull(directory.listFiles())) { + if (!file.isDirectory()) { + remapProperties(file.toPath()); + findFiles(file.toPath()); + } } } /** * Remaps properties to work in newer versions - * + * * @param path * @throws IOException */ protected void remapProperties(Path path) throws IOException { - if (!path.toFile().exists()) - return; - Files.list(path) - .filter(path1 -> path1.toString().endsWith(".properties")) - .forEach(model -> { - try (InputStream input = new FileInputStream(model.toString())) { - if (PackConverter.DEBUG) - Logger.log("Updating:" + model.getFileName()); + if (path.toFile().exists()) { + try (Stream pathStream = Files.list(path).filter(path1 -> path1.toString().endsWith(".properties"))) { + pathStream.forEach(model -> { + Path inputStreamPath = Paths.get(model.toString()); + try (InputStream input = Files.newInputStream(inputStreamPath)) { + Logger.debug("Updating:" + model.getFileName()); PropertiesEx prop = new PropertiesEx(); prop.load(input); - - try (OutputStream output = new FileOutputStream(model.toString())) { + try (OutputStream output = Files.newOutputStream(inputStreamPath)) { // updates textures - if (prop.containsKey("texture")) + if (prop.containsKey("texture")) { prop.setProperty("texture", replaceTextures(prop)); + } // Updates Item IDs - if (prop.containsKey("matchItems")) - prop.setProperty("matchItems", - updateID("matchItems", prop, "regular").replaceAll("\"", "")); + if (prop.containsKey("matchItems")) { + prop.setProperty("matchItems", updateID("matchItems", prop, "regular").replaceAll("\"", "")); + } - if (prop.containsKey("items")) + if (prop.containsKey("items")) { prop.setProperty("items", updateID("items", prop, "regular").replaceAll("\"", "")); + } - if (prop.containsKey("matchBlocks")) - prop.setProperty("matchBlocks", - updateID("matchBlocks", prop, "regular").replaceAll("\"", "")); + if (prop.containsKey("matchBlocks")) { + prop.setProperty("matchBlocks", updateID("matchBlocks", prop, "regular").replaceAll("\"", "")); + } // Saves File prop.store(output, ""); - output.close(); } catch (IOException io) { io.printStackTrace(); } @@ -96,44 +107,47 @@ protected void remapProperties(Path path) throws IOException { throw Util.propagate(e); } }); + } + } } /** * Replaces texture paths with blocks and items - * + * * @param prop * @return */ protected String replaceTextures(PropertiesEx prop) { NameConverter nameConverter = packConverter.getConverter(NameConverter.class); String properties = prop.getProperty("texture"); - if (properties.startsWith("textures/block/")) + if (properties.startsWith("textures/block/")) { properties = "textures/blocks/" + nameConverter.getBlockMapping(); - else if (properties.startsWith("textures/item/")) + } else if (properties.startsWith("textures/item/")) { properties = "textures/items/" + nameConverter.getItemMapping(); + } return properties; } /** * Fixes item IDs and switches them from a numerical id to minecraft: something - * + * * @param type * @param prop * @return */ protected String updateID(String type, PropertiesEx prop, String selection) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/ids.json").get(selection) - .getAsJsonObject(); + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/backwards/ids.json").get(selection).getAsJsonObject(); String[] split = prop.getProperty(type).split(" "); - String properties2 = " "; - for (int i = 0; i < split.length; i++) - if (id.get(split[i]) != null) + StringBuilder properties2 = new StringBuilder(" "); + for (int i = 0; i < split.length; i++) { + if (id.get(split[i]) != null) { split[i] = id.get(split[i]).getAsString(); - for (String item : split) - properties2 += item + " "; - properties2.substring(0, properties2.length() - 1); - if (prop.containsKey("metadata")) - prop.remove("metadata"); - return properties2; + } + } + Arrays.stream(split).forEach(item -> properties2.append(item).append(" ")); + // TODO/NOTE: Might of broken this? Originally returned properties2.toString() & ignored the line below + String output = properties2.substring(0, properties2.length() - 1); + prop.remove("metadata"); + return output; } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java index 9465902b..0f6fbdc5 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ModelConverter.java @@ -18,473 +18,476 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; public class ModelConverter extends Converter { - private int version; - private int from; - protected String light = "none"; - - public ModelConverter(PackConverter packConverter, String lightIn, int fromIn, int versionIn) { - super(packConverter); - light = lightIn; - from = fromIn; - version = versionIn; - } - - /** - * Runs findfiles with the directory Models - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath() - .resolve("assets/minecraft/models".replace("/", File.separator)); - if (!models.toFile().exists()) - return; - findFiles(models); - } - - /** - * Recursively finds files with Path path and runs remapModelJson - * - * @param path - * @throws IOException - */ - protected void findFiles(Path path) throws IOException { - File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - continue; - remapModelJson(file.toPath()); - findFiles(file.toPath()); - } - } - - /** - * Updates model Json to newer versions - * - * @param models Model path - * @throws IOException - */ - protected void remapModelJson(Path models) throws IOException { - if (!models.toFile().exists()) - return; - Files.list(models) - .filter(path1 -> path1.toString().endsWith(".json")) - .forEach(model -> { - try { - JsonObject jsonObject; - if (JsonUtil.readJson(packConverter.getGson(), model) != null - && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) - jsonObject = JsonUtil.readJson(packConverter.getGson(), model); - else { - if (PackConverter.DEBUG) { - Logger.log("Could not convert model: " + model.getFileName()); - if (JsonUtil.readJson(packConverter.getGson(), model) == null) - Logger.log("Check for Syntax Errors in file."); - else - Logger.log("File is not JSON Object."); - } - return; - } - - // Parent Stuff - if (jsonObject.has("parent")) { - // Change parent to lowercase - for (Map.Entry entry : jsonObject.entrySet()) { - if (entry.getKey().equals("parent")) { - String parent = entry.getValue().getAsString().toLowerCase(); - jsonObject.addProperty(entry.getKey(), getParent(parent)); - } - } - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.9") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.9")) { - jsonObject = mergeParent(models, jsonObject, jsonObject.get("parent").getAsString()); - } - } - - // GUI light system for 1.15.2 - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.15.2")) { - jsonObject.remove("gui_light"); - } - // minify the json so we can replace spaces in paths easily - // TODO Improvement: handle this in a cleaner way? - - // handle the remapping of textures, for models that use default texture names - if (jsonObject.has("textures") && jsonObject.get("textures").isJsonObject()) { - NameConverter nameConverter = packConverter.getConverter(NameConverter.class); - JsonObject initialTextureObject = jsonObject.getAsJsonObject("textures"); - JsonObject textureObject = initialTextureObject.deepCopy(); - for (Map.Entry entry : initialTextureObject.entrySet()) { - String value = entry.getValue().getAsString(); - textureObject.remove(entry.getKey()); - - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { - value = value.replaceAll("minecraft:", ""); - } - // 1.19 Mappings - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.19")) - if (value.startsWith("block/")) - value = "block/" + nameConverter.getBlockMapping19() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - value = value.toLowerCase().replaceAll("[()]", ""); - - // 1.17 Mappings - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - if (value.startsWith("block/")) - value = "block/" + nameConverter.getBlockMapping17() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - else if (value.startsWith("item/")) - value = "item/" + nameConverter.getItemMapping17() - .remap(value.substring("item/".length())).toLowerCase() - .replaceAll("[()]", ""); - value = value.toLowerCase().replaceAll("[()]", ""); - } - - // 1.14 Mappings - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.14")) - if (value.startsWith("block/")) - value = "block/" - + nameConverter.getNewBlockMapping() - .remap(value.substring("block/".length())); - - // Dyes - if (value.startsWith("item/") && value.contains("dye")) - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.14")) - value = "item/" - + nameConverter.getNewItemMapping() - .remap(value.substring("item/".length())); - - // 1.13 Mappings - if (version < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (value.startsWith("block/")) { - List same = Arrays.asList("snow"); - String remap = nameConverter.getBlockMapping() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - for (String s : same) { - if (s.equals(value.substring("block/".length()))) { - remap = s; - } - } - value = "blocks/" + remap; - } else if (value.startsWith("item/")) - value = "items/" + nameConverter.getItemMapping() - .remap(value.substring("item/".length())).toLowerCase() - .replaceAll("[()]", ""); - else - value = value.toLowerCase().replaceAll("[()]", ""); - } - - if (!textureObject.has(entry.getKey())) - textureObject.addProperty(entry.getKey(), value); - } - - jsonObject.remove("textures"); - jsonObject.add("textures", textureObject); - } - - // fix display settings for packs for 1.8 - if (jsonObject.has("display") && from > Util.getVersionProtocol(packConverter.getGson(), "1.8") - && version == Util.getVersionProtocol(packConverter.getGson(), "1.8")) { - JsonElement display = jsonObject.remove("display"); - - if (display.isJsonObject()) { - jsonObject.add("display", - updateDisplay(packConverter.getGson(), display.getAsJsonObject())); - } - } - - if (jsonObject.has("overrides")) { - for (Map.Entry entry : jsonObject.entrySet()) { - if (entry.getKey().equals("overrides")) { - JsonArray overrides = jsonObject.get("overrides").getAsJsonArray(); - JsonArray overrides2 = new JsonArray(); - for (int i = 0; i < overrides.size(); i++) { - JsonObject object = overrides.get(i).getAsJsonObject(); - for (Map.Entry json : object.entrySet()) { - if (json.getKey().equals("model")) - object.addProperty(json.getKey(), - json.getValue().getAsString().replaceAll("[()]", "")); - else - object.add(json.getKey(), json.getValue()); - } - overrides2.add(object); - } - jsonObject.add(entry.getKey(), overrides2); - } - } - } - - if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { - if (PackConverter.DEBUG) - Logger.log("Updating Model: " + model.getFileName()); - JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); - } - } catch (IOException e) { - throw Util.propagate(e); - } - }); - } - - private String getParent(String parent) { - parent = parent.replace(" ", "_"); - - // Get block/item parents renamed - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) - parent = parent.replaceAll("minecraft:", ""); - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.19")) - if (parent.startsWith("block/")) - parent = setParent("block/", "/backwards/blocks.json", parent, "1_19"); - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.17") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/backwards/blocks.json", parent, "1_17"); - if (parent.startsWith("item/")) - parent = setParent("item/", "/backwards/items.json", parent, "1_17"); - } - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.14") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/backwards/blocks.json", parent, "1_14"); - if (parent.startsWith("item/")) - parent = setParent("item/", "/backwards/items.json", parent, "1_14"); - } - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - - if (parent.startsWith("block/")) - parent = setParent("block/", "/backwards/blocks.json", parent, "1_13"); - if (parent.startsWith("item/")) - parent = setParent("item/", "/backwards/items.json", parent, "1_13"); - - } - return parent; - } - - protected JsonObject mergeParent(Path models, JsonObject current, String parent) { - JsonObject jsonObject = current.deepCopy(); - if (parent == null || !models.resolve(parent + ".json").toFile().exists()) - return jsonObject; - - if (!current.has("parent")) - return jsonObject; - - Path parentPath = models.resolve(parent + ".json"); - try { - JsonObject parentObj = JsonUtil.readJson(packConverter.getGson(), parentPath); - if (parentObj != null) { - jsonObject.remove("parent"); - if (parentObj.has("parent")) { - String parentStr = parentObj.get("parent").getAsString(); - String parentVal = parentStr == null ? null : getParent(parentStr); - jsonObject = mergeParent(models, parentObj, parentVal); - } - - if (!jsonObject.has("elements") && current.has("elements")) - jsonObject.add("elements", current.get("elements")); - - if (!jsonObject.has("elements") && parentObj.has("elements")) - jsonObject.add("elements", parentObj.get("elements")); - - if (!jsonObject.has("display") && parentObj.has("display")) - jsonObject.add("display", parentObj.get("display")); - - JsonObject textures = new JsonObject(); - if (jsonObject.has("textures")) - textures = jsonObject.remove("textures").getAsJsonObject(); - - if (current.has("textures")) { - for (String s : current.get("textures").getAsJsonObject().keySet()) - if (!textures.has(s)) - textures.add(s, current.get("textures").getAsJsonObject().get(s)); - } - - if (parentObj.has("textures")) { - JsonObject parentTextures = parentObj.get("textures").getAsJsonObject(); - for (String s : parentTextures.keySet()) - if (!textures.has(s)) - textures.add(s, parentTextures.get(s)); - } - - for (int i = 0; i < 5; i++) { - JsonObject textures2 = new JsonObject(); - for (String s : textures.keySet()) { - if (textures.get(s).getAsString().startsWith("#") && - textures.has(textures.get(s).getAsString().substring(1))) { - textures2.add(s, - textures.get(textures.get(s).getAsString().substring(1))); - } else { - textures2.add(s, textures.get(s)); - } - } - textures = textures2; - } - - if (textures.keySet().size() > 0) - jsonObject.add("textures", textures); - } - } catch (IOException e) { - } - - return jsonObject; - } - - /** - * Gets parent object and sets a new one - * - * @param prefix prefix of file path - * @param path File path of json control - * @param parent Parent String - * @return New string with changed parent. - */ - protected String setParent(String prefix, String path, String parent, String item) { - String parent2 = parent.replace(prefix, ""); - JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); - if (file == null) { - Logger.log("Prefix Failed on: " + parent); - return ""; - } - return file.has(parent2) ? prefix + file.get(parent2).getAsString() : parent; - } - - protected static JsonObject updateDisplay(Gson gson, JsonObject display) { - JsonObject defaults = JsonUtil.readJsonResource(gson, "/backwards/display.json"); - if (display == null) - return defaults.deepCopy(); - - // First Person - boolean found = false; - JsonObject firstPerson = defaults.get("firstperson").getAsJsonObject().deepCopy(); - if (display.has("firstperson_righthand")) { - firstPerson = updateDisplayFirstPerson(gson, display.remove("firstperson_righthand").getAsJsonObject()); - found = true; - } - if (display.has("firstperson_lefthand")) { - JsonObject firstPersonLeft = display.remove("firstperson_lefthand").getAsJsonObject(); - if (!found) { - firstPerson = updateDisplayFirstPerson(gson, getLeftHand(gson, firstPersonLeft)); - } - } - display.remove("firstperson"); - display.add("firstperson", firstPerson); - - // Third Person - found = false; - JsonObject thirdPerson = defaults.get("thirdperson").getAsJsonObject().deepCopy(); - if (display.has("thirdperson_righthand")) { - thirdPerson = updateDisplayThirdPerson(gson, display.remove("thirdperson_righthand").getAsJsonObject()); - found = true; - } - if (display.has("thirdperson_lefthand")) { - JsonObject thirdPersonLeft = display.remove("thirdperson_lefthand").getAsJsonObject(); - if (!found) { - thirdPerson = updateDisplayThirdPerson(gson, getLeftHand(gson, thirdPersonLeft)); - } - } - display.remove("thirdperson"); - display.add("thirdperson", thirdPerson); - - if (display.has("ground")) { - display.remove("ground"); - } - - if (display.has("head")) { - display.remove("head"); - } - - return display; - } - - private static JsonObject getLeftHand(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray oldRotation = newObject.remove("rotation").getAsJsonArray(); - JsonArray rotation = new JsonArray(); - rotation.add(oldRotation.get(0).getAsNumber()); - rotation.add(0 - oldRotation.get(1).getAsDouble()); - rotation.add(0 - oldRotation.get(2).getAsDouble()); - newObject.add("rotation", - rotation); - } - - return newObject; - } - - private static JsonObject updateDisplayFirstPerson(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); - newObject.add("rotation", - JsonUtil.subtract( - rotation, - JsonUtil.asArray(gson, "[0, 45, 0]"))); - } - - if (old.has("translation")) { - JsonArray translation = newObject.remove("translation").getAsJsonArray(); - newObject.add("translation", - JsonUtil.add( - JsonUtil.divide( - JsonUtil.subtract( - translation, - JsonUtil.asArray(gson, "[1.13, 3.2, 1.13]")), - JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]")), - JsonUtil.asArray(gson, "[0, 4, 2]"))); - - } - - if (old.has("scale")) { - JsonArray scale = newObject.remove("scale").getAsJsonArray(); - newObject.add("scale", - JsonUtil.divide( - scale, - JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]"))); - } - - return newObject; - } - - private static JsonObject updateDisplayThirdPerson(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); - newObject.add("rotation", - JsonUtil.divide( - JsonUtil.subtract( - rotation, - JsonUtil.asArray(gson, "[0, 0, 20]")), - JsonUtil.asArray(gson, "[1, -1, -1]"))); - } - - if (old.has("translation")) { - JsonArray translation = newObject.remove("translation").getAsJsonArray(); - newObject.add("translation", - JsonUtil.divide( - JsonUtil.subtract( - translation, - JsonUtil.asArray(gson, "[0, 2.75, -3]")), - JsonUtil.asArray(gson, "[1, 1, -1]"))); - } - - // For keeping order - if (old.has("scale")) { - JsonArray scale = newObject.remove("scale").getAsJsonArray(); - newObject.add("scale", scale); - } - - return newObject; - } + private final int to; + private final int from; + + public ModelConverter(PackConverter packConverter, int from, int to) { + super(packConverter); + this.from = from; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + + /** + * Runs findfiles with the directory Models + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path models = pack.getWorkingPath().resolve("assets/minecraft/models".replace("/", File.separator)); + if (models.toFile().exists()) { + findFiles(models); + } + } + + /** + * Recursively finds files with Path path and runs remapModelJson + * + * @param path + * @throws IOException + */ + protected void findFiles(Path path) throws IOException { + File directory = path.toFile(); + for (File file : Objects.requireNonNull(directory.listFiles())) { + if (file.isDirectory()) + continue; + remapModelJson(file.toPath()); + findFiles(file.toPath()); + } + } + + /** + * Updates model Json to newer versions + * + * @param models Model path + * @throws IOException + */ + protected void remapModelJson(Path models) throws IOException { + if (!models.toFile().exists()) + return; + Files.list(models) + .filter(path1 -> path1.toString().endsWith(".json")) + .forEach(model -> { + try { + JsonObject jsonObject; + if (JsonUtil.readJson(packConverter.getGson(), model) != null + && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) + jsonObject = JsonUtil.readJson(packConverter.getGson(), model); + else { + Logger.debug("Could not convert model: " + model.getFileName()); + Logger.addTab(); + if (JsonUtil.readJson(packConverter.getGson(), model) == null) + Logger.debug("Check for Syntax Errors in file."); + else + Logger.debug("File is not JSON Object."); + Logger.subTab(); + return; + } + + // Parent Stuff + if (jsonObject.has("parent")) { + // Change parent to lowercase + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getKey().equals("parent")) { + String parent = entry.getValue().getAsString().toLowerCase(); + jsonObject.addProperty(entry.getKey(), getParent(parent)); + } + } + + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.9") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.9")) { + jsonObject = mergeParent(models, jsonObject, jsonObject.get("parent").getAsString()); + } + } + + // GUI light system for 1.15.2 + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.15.2")) { + jsonObject.remove("gui_light"); + } + + // minify the json so we can replace spaces in paths easily + // TODO Improvement: handle this in a cleaner way? + + // handle the remapping of textures, for models that use default texture names + if (jsonObject.has("textures") && jsonObject.get("textures").isJsonObject()) { + NameConverter nameConverter = packConverter.getConverter(NameConverter.class); + JsonObject initialTextureObject = jsonObject.getAsJsonObject("textures"); + JsonObject textureObject = initialTextureObject.deepCopy(); + for (Map.Entry entry : initialTextureObject.entrySet()) { + String value = entry.getValue().getAsString(); + textureObject.remove(entry.getKey()); + + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19.3") + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { + value = value.replaceAll("minecraft:", ""); + } + // 1.19 Mappings + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) + if (value.startsWith("block/")) + value = "block/" + nameConverter.getBlockMapping19() + .remap(value.substring("block/".length())).toLowerCase() + .replaceAll("[()]", ""); + value = value.toLowerCase().replaceAll("[()]", ""); + + // 1.17 Mappings + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + if (value.startsWith("block/")) + value = "block/" + nameConverter.getBlockMapping17() + .remap(value.substring("block/".length())).toLowerCase() + .replaceAll("[()]", ""); + else if (value.startsWith("item/")) + value = "item/" + nameConverter.getItemMapping17() + .remap(value.substring("item/".length())).toLowerCase() + .replaceAll("[()]", ""); + value = value.toLowerCase().replaceAll("[()]", ""); + } + + // 1.14 Mappings + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) + if (value.startsWith("block/")) + value = "block/" + + nameConverter.getNewBlockMapping() + .remap(value.substring("block/".length())); + + // Dyes + if (value.startsWith("item/") && value.contains("dye")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) + value = "item/" + + nameConverter.getNewItemMapping() + .remap(value.substring("item/".length())); + + // 1.13 Mappings + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + if (value.startsWith("block/")) { + List same = Arrays.asList("snow"); + String remap = nameConverter.getBlockMapping() + .remap(value.substring("block/".length())).toLowerCase() + .replaceAll("[()]", ""); + for (String s : same) { + if (s.equals(value.substring("block/".length()))) { + remap = s; + } + } + value = "blocks/" + remap; + } else if (value.startsWith("item/")) + value = "items/" + nameConverter.getItemMapping() + .remap(value.substring("item/".length())).toLowerCase() + .replaceAll("[()]", ""); + else + value = value.toLowerCase().replaceAll("[()]", ""); + } + + if (!textureObject.has(entry.getKey())) + textureObject.addProperty(entry.getKey(), value); + } + + jsonObject.remove("textures"); + jsonObject.add("textures", textureObject); + } + + // fix display settings for packs for 1.8 + if (jsonObject.has("display") && from > Util.getVersionProtocol(packConverter.getGson(), "1.8") + && to == Util.getVersionProtocol(packConverter.getGson(), "1.8")) { + JsonElement display = jsonObject.remove("display"); + + if (display.isJsonObject()) { + jsonObject.add("display", + updateDisplay(packConverter.getGson(), display.getAsJsonObject())); + } + } + + if (jsonObject.has("overrides")) { + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getKey().equals("overrides")) { + JsonArray overrides = jsonObject.get("overrides").getAsJsonArray(); + JsonArray overrides2 = new JsonArray(); + for (int i = 0; i < overrides.size(); i++) { + JsonObject object = overrides.get(i).getAsJsonObject(); + for (Map.Entry json : object.entrySet()) { + if (json.getKey().equals("model")) + object.addProperty(json.getKey(), + json.getValue().getAsString().replaceAll("[()]", "")); + else + object.add(json.getKey(), json.getValue()); + } + overrides2.add(object); + } + jsonObject.add(entry.getKey(), overrides2); + } + } + } + + if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { + Logger.debug("Updating Model: " + model.getFileName()); + JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); + } + } catch (IOException e) { + throw Util.propagate(e); + } + }); + } + + private String getParent(String parent) { + parent = parent.replace(" ", "_"); + + // Get block/item parents renamed + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) + parent = parent.replaceAll("minecraft:", ""); + + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.19") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) + if (parent.startsWith("block/")) + parent = setParent("block/", "/backwards/blocks.json", parent, "1_19"); + + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.17") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + if (parent.startsWith("block/")) + parent = setParent("block/", "/backwards/blocks.json", parent, "1_17"); + if (parent.startsWith("item/")) + parent = setParent("item/", "/backwards/items.json", parent, "1_17"); + } + + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.14") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + if (parent.startsWith("block/")) + parent = setParent("block/", "/backwards/blocks.json", parent, "1_14"); + if (parent.startsWith("item/")) + parent = setParent("item/", "/backwards/items.json", parent, "1_14"); + } + + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + + if (parent.startsWith("block/")) + parent = setParent("block/", "/backwards/blocks.json", parent, "1_13"); + if (parent.startsWith("item/")) + parent = setParent("item/", "/backwards/items.json", parent, "1_13"); + + } + return parent; + } + + protected JsonObject mergeParent(Path models, JsonObject current, String parent) { + JsonObject jsonObject = current.deepCopy(); + if (parent == null || !models.resolve(parent + ".json").toFile().exists()) + return jsonObject; + + if (!current.has("parent")) + return jsonObject; + + Path parentPath = models.resolve(parent + ".json"); + try { + JsonObject parentObj = JsonUtil.readJson(packConverter.getGson(), parentPath); + if (parentObj != null) { + jsonObject.remove("parent"); + if (parentObj.has("parent")) { + String parentStr = parentObj.get("parent").getAsString(); + String parentVal = parentStr == null ? null : getParent(parentStr); + jsonObject = mergeParent(models, parentObj, parentVal); + } + + if (!jsonObject.has("elements") && current.has("elements")) + jsonObject.add("elements", current.get("elements")); + + if (!jsonObject.has("elements") && parentObj.has("elements")) + jsonObject.add("elements", parentObj.get("elements")); + + if (!jsonObject.has("display") && parentObj.has("display")) + jsonObject.add("display", parentObj.get("display")); + + JsonObject textures = new JsonObject(); + if (jsonObject.has("textures")) + textures = jsonObject.remove("textures").getAsJsonObject(); + + if (current.has("textures")) { + for (String s : current.get("textures").getAsJsonObject().keySet()) + if (!textures.has(s)) + textures.add(s, current.get("textures").getAsJsonObject().get(s)); + } + + if (parentObj.has("textures")) { + JsonObject parentTextures = parentObj.get("textures").getAsJsonObject(); + for (String s : parentTextures.keySet()) + if (!textures.has(s)) + textures.add(s, parentTextures.get(s)); + } + + for (int i = 0; i < 5; i++) { + JsonObject textures2 = new JsonObject(); + for (String s : textures.keySet()) { + if (textures.get(s).getAsString().startsWith("#") && + textures.has(textures.get(s).getAsString().substring(1))) { + textures2.add(s, + textures.get(textures.get(s).getAsString().substring(1))); + } else { + textures2.add(s, textures.get(s)); + } + } + textures = textures2; + } + + if (textures.keySet().size() > 0) + jsonObject.add("textures", textures); + } + } catch (IOException e) { + } + + return jsonObject; + } + + /** + * Gets parent object and sets a new one + * + * @param prefix prefix of file path + * @param path File path of json control + * @param parent Parent String + * @return New string with changed parent. + */ + protected String setParent(String prefix, String path, String parent, String item) { + String parent2 = parent.replace(prefix, ""); + JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); + if (file == null) { + Logger.debug("Prefix Failed on: " + parent); + return ""; + } + return file.has(parent2) ? prefix + file.get(parent2).getAsString() : parent; + } + + protected static JsonObject updateDisplay(Gson gson, JsonObject display) { + JsonObject defaults = JsonUtil.readJsonResource(gson, "/backwards/display.json"); + if (display == null) + return defaults.deepCopy(); + + // First Person + boolean found = false; + JsonObject firstPerson = defaults.get("firstperson").getAsJsonObject().deepCopy(); + if (display.has("firstperson_righthand")) { + firstPerson = updateDisplayFirstPerson(gson, display.remove("firstperson_righthand").getAsJsonObject()); + found = true; + } + if (display.has("firstperson_lefthand")) { + JsonObject firstPersonLeft = display.remove("firstperson_lefthand").getAsJsonObject(); + if (!found) { + firstPerson = updateDisplayFirstPerson(gson, getLeftHand(gson, firstPersonLeft)); + } + } + display.remove("firstperson"); + display.add("firstperson", firstPerson); + + // Third Person + found = false; + JsonObject thirdPerson = defaults.get("thirdperson").getAsJsonObject().deepCopy(); + if (display.has("thirdperson_righthand")) { + thirdPerson = updateDisplayThirdPerson(gson, display.remove("thirdperson_righthand").getAsJsonObject()); + found = true; + } + if (display.has("thirdperson_lefthand")) { + JsonObject thirdPersonLeft = display.remove("thirdperson_lefthand").getAsJsonObject(); + if (!found) { + thirdPerson = updateDisplayThirdPerson(gson, getLeftHand(gson, thirdPersonLeft)); + } + } + display.remove("thirdperson"); + display.add("thirdperson", thirdPerson); + + if (display.has("ground")) { + display.remove("ground"); + } + + if (display.has("head")) { + display.remove("head"); + } + + return display; + } + + private static JsonObject getLeftHand(Gson gson, JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray oldRotation = newObject.remove("rotation").getAsJsonArray(); + JsonArray rotation = new JsonArray(); + rotation.add(oldRotation.get(0).getAsNumber()); + rotation.add(0 - oldRotation.get(1).getAsDouble()); + rotation.add(0 - oldRotation.get(2).getAsDouble()); + newObject.add("rotation", + rotation); + } + + return newObject; + } + + private static JsonObject updateDisplayFirstPerson(Gson gson, JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); + newObject.add("rotation", + JsonUtil.subtract( + rotation, + JsonUtil.asArray(gson, "[0, 45, 0]"))); + } + + if (old.has("translation")) { + JsonArray translation = newObject.remove("translation").getAsJsonArray(); + newObject.add("translation", + JsonUtil.add( + JsonUtil.divide( + JsonUtil.subtract( + translation, + JsonUtil.asArray(gson, "[1.13, 3.2, 1.13]")), + JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]")), + JsonUtil.asArray(gson, "[0, 4, 2]"))); + + } + + if (old.has("scale")) { + JsonArray scale = newObject.remove("scale").getAsJsonArray(); + newObject.add("scale", + JsonUtil.divide( + scale, + JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]"))); + } + + return newObject; + } + + private static JsonObject updateDisplayThirdPerson(Gson gson, JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); + newObject.add("rotation", + JsonUtil.divide( + JsonUtil.subtract( + rotation, + JsonUtil.asArray(gson, "[0, 0, 20]")), + JsonUtil.asArray(gson, "[1, -1, -1]"))); + } + + if (old.has("translation")) { + JsonArray translation = newObject.remove("translation").getAsJsonArray(); + newObject.add("translation", + JsonUtil.divide( + JsonUtil.subtract( + translation, + JsonUtil.asArray(gson, "[0, 2.75, -3]")), + JsonUtil.asArray(gson, "[1, 1, -1]"))); + } + + // For keeping order + if (old.has("scale")) { + JsonArray scale = newObject.remove("scale").getAsJsonArray(); + newObject.add("scale", scale); + } + + return newObject; + } } \ No newline at end of file diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java index 46525417..8eb228b0 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java @@ -13,13 +13,14 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; public class NameConverter extends Converter { - private int to; - private int from; + private final int from; + private final int to; private final Mapping blockMapping; private final Mapping newBlockMapping; @@ -32,7 +33,7 @@ public class NameConverter extends Converter { private final Mapping langMapping; private final Mapping langMapping14; private final Mapping blockMapping203; - private final Mapping itemMapping203; +// private final Mapping itemMapping203; public NameConverter(PackConverter packConverter, int from, int to) { super(packConverter); @@ -50,7 +51,12 @@ public NameConverter(PackConverter packConverter, int from, int to) { langMapping = new Mapping(gson, "lang", "1_13", true); langMapping14 = new Mapping(gson, "lang", "1_14", true); blockMapping203 = new Mapping(gson, "blocks", "1_20_3", true); - itemMapping203 = new Mapping(gson, "items", "1_20_3", true); +// TODO/(not used?): itemMapping203 = new Mapping(gson, "items", "1_20_3", true); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; } /** @@ -68,53 +74,55 @@ public void convert(Pack pack) throws IOException { && from > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { // OptiFine conversion if (minecraftPath.resolve("mcpatcher").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("MCPatcher exists, switching to optifine"); + Logger.debug("MCPatcher exists, switching to optifine"); if (minecraftPath.resolve("optifine").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("OptiFine exists, merging directories"); - FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), - minecraftPath.resolve("mcpatcher").toFile()); - } else + Logger.debug("OptiFine exists, merging directories"); + FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), minecraftPath.resolve("mcpatcher").toFile()); + } else { Files.move(minecraftPath.resolve("mcpatcher"), minecraftPath.resolve("optifine")); - if (minecraftPath.resolve("mcpatcher").toFile().exists()) + } + + if (minecraftPath.resolve("mcpatcher").toFile().exists()) { FileUtil.deleteDirectoryAndContents(minecraftPath.resolve("mcpatcher")); + } } } Path modelsPath = minecraftPath.resolve("models"); - Path itemModelsPath = modelsPath.resolve("item"); Path blockModelsPath = modelsPath.resolve("block"); - if (modelsPath.toFile().exists()) { - // 1.20.3 Models - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) { renameAll(blockMapping203, ".json", blockModelsPath); + } // 1.19 Models - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) { renameAll(blockMapping19, ".json", blockModelsPath); + } // Update 1.14 items - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { renameAll(newItemMapping, ".json", itemModelsPath); + } // 1.13 Models if (to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { renameAll(itemMapping, ".json", itemModelsPath); renameAll(blockMapping, ".json", blockModelsPath); Path bannerModelPath = itemModelsPath.resolve("banner.json"); - if (bannerModelPath.toFile().exists()) + if (bannerModelPath.toFile().exists()) { bannerModelPath.toFile().delete(); + } } } // Update BlockStates Path blockStates = minecraftPath.resolve("blockstates"); - if (blockStates.toFile().exists()) + if (blockStates.toFile().exists()) { renameAll(blockMapping, ".json", blockStates); + } // Update textures Path texturesPath = minecraftPath.resolve("textures"); @@ -124,92 +132,110 @@ public void convert(Pack pack) throws IOException { // 1.20.3 if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) { renameAll(blockMapping203, ".png", texturesBlockPath); + } // 1.19 if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { renameAll(blockMapping19, ".png", texturesBlockPath); + } // Entities Path entityPath = texturesPath.resolve("entity"); + if (entityPath.toFile().exists()) { + // 1.17 Squid + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.17") + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + renameAll(blockMapping17, ".png", texturesBlockPath); + renameAll(itemMapping17, ".png", texturesItemPath); + renameAll(blockMapping17, ".png", blockModelsPath); + renameAll(itemMapping17, ".png", itemModelsPath); + Path squidPath = entityPath.resolve("squid/squid.png"); + if (squidPath.toFile().exists()) { + Path newSquidPath = entityPath.resolve("squid.png"); + if (newSquidPath.toFile().exists()) { + newSquidPath.toFile().delete(); + } + Files.move(squidPath, newSquidPath); + } + } - // 1.17 Squid - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.17") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - renameAll(blockMapping17, ".png", texturesBlockPath); - renameAll(itemMapping17, ".png", texturesItemPath); - renameAll(blockMapping17, ".png", blockModelsPath); - renameAll(itemMapping17, ".png", itemModelsPath); - Path newSquidPath = entityPath.resolve("squid/squid.png"); - if (newSquidPath.toFile().exists()) - Files.move(newSquidPath, entityPath.resolve("squid.png")); - } - - // 1.16 Iron golems - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.16") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.16")) { - Path newIronGolemPath = entityPath - .resolve("iron_golem/iron_golem.png".replace("/", File.separator)); - if (newIronGolemPath.toFile().exists()) - Files.move(newIronGolemPath, entityPath.resolve("iron_golem.png")); - } - - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - renameAll(newBlockMapping, ".png", texturesBlockPath); - renameAll(newBlockMapping, ".png.mcmeta", texturesBlockPath); - renameAll(newItemMapping, ".png", texturesItemPath); - renameAll(newItemMapping, ".png.mcmeta", texturesItemPath); - } - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - renameAll(blockMapping, ".png", texturesBlockPath); - renameAll(blockMapping, ".png.mcmeta", texturesBlockPath); + // 1.16 Iron golems + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.16") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.16")) { + Path ironGolemPath = entityPath.resolve("iron_golem/iron_golem.png".replace("/", File.separator)); + if (ironGolemPath.toFile().exists()) { + Path newIronGolemPath = entityPath.resolve("iron_golem.png"); + if (newIronGolemPath.toFile().exists()) { + newIronGolemPath.toFile().delete(); + } + Files.move(ironGolemPath, newIronGolemPath); + } + } - renameAll(itemMapping, ".png", texturesItemPath); - renameAll(itemMapping, ".png.mcmeta", texturesItemPath); + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + renameAll(newBlockMapping, ".png", texturesBlockPath); + renameAll(newBlockMapping, ".png.mcmeta", texturesBlockPath); + renameAll(newItemMapping, ".png", texturesItemPath); + renameAll(newItemMapping, ".png.mcmeta", texturesItemPath); + } - // 1.13 End Crystals - if (entityPath.resolve("end_crystal").toFile().exists()) - Files.move(entityPath.resolve("end_crystal"), entityPath.resolve("endercrystal")); + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + renameAll(blockMapping, ".png", texturesBlockPath); + renameAll(blockMapping, ".png.mcmeta", texturesBlockPath); + renameAll(itemMapping, ".png", texturesItemPath); + renameAll(itemMapping, ".png.mcmeta", texturesItemPath); + + // 1.13 End Crystals + if (entityPath.resolve("end_crystal").toFile().exists()) { + Path newEnderCrystalFolderPath = entityPath.resolve("endcrystal"); + if (newEnderCrystalFolderPath.toFile().exists()) { + newEnderCrystalFolderPath.toFile().delete(); + } + Files.move(entityPath.resolve("end_crystal"), newEnderCrystalFolderPath); + } - findEntityFiles(texturesPath.resolve("entity")); + findEntityFiles(texturesPath.resolve("entity")); + } } } // Less than 1.12 if (from > Util.getVersionProtocol(packConverter.getGson(), "1.13") && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (PackConverter.DEBUG) - Logger.log("Finding files that are greater than 1.12"); + Logger.debug("Finding files that are greater than 1.12"); findFiles(texturesPath); } } /** * Finds files in entity folder - * + * * @param path * @throws IOException */ protected void findEntityFiles(Path path) throws IOException { - if (!path.toFile().exists()) - return; File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - continue; - renameAll(entityMapping, ".png", file.toPath()); - renameAll(entityMapping, ".png.mcmeta", file.toPath()); - findEntityFiles(file.toPath()); + if (!directory.exists()) + return; + File[] files = directory.listFiles(); + if (files == null) + return; // why is this happening?? + for (File file : files) { + if (file.isDirectory()) { + renameAll(entityMapping, ".png", file.toPath()); + renameAll(entityMapping, ".png.mcmeta", file.toPath()); + findEntityFiles(file.toPath()); + } } } /** * Finds files in folders called - * + * * @param path * @throws IOException */ @@ -217,29 +243,28 @@ protected void findFiles(Path path) throws IOException { if (!path.toFile().exists()) return; File directory = path.toFile(); - for (File file : directory.listFiles()) { + for (File file : Objects.requireNonNull(directory.listFiles())) { if (file.isDirectory()) { if (file.getName().equals("item")) { - if (PackConverter.DEBUG) - Logger.log("Found Items folder, renaming"); + Logger.debug("Found Items folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("item", "items")); } if (file.getName().equals("block")) { - if (PackConverter.DEBUG) - Logger.log("Found blocks folder, renaming"); + Logger.debug("Found blocks folder, renaming"); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("block", "blocks")); } findFiles(file.toPath()); } - if (file.getName().contains("(")) + if (file.getName().contains("(")) { FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("[()]", "")); + } - if (!file.getName().equals(file.getName().toLowerCase())) - if (PackConverter.DEBUG) - Logger.log("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + if (!file.getName().equals(file.getName().toLowerCase())) { + Logger.debug("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + } FileUtil.renameFile(path.resolve(file.getName()), file.getName().toLowerCase()); } @@ -247,75 +272,101 @@ protected void findFiles(Path path) throws IOException { /** * Renames folder - * + * * @param mapping * @param extension * @param path * @throws IOException */ protected void renameAll(Mapping mapping, String extension, Path path) throws IOException { - if (path.toFile().exists()) { - // remap grass blocks in order due to the cyclical way their names have changed, - // i.e grass -> grass_block, tall_grass -> grass, double_grass -> tall_grass - List grasses = Arrays.asList("tall_grass", "grass", "grass_block"); - if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") - && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { - grasses.stream().forEach(name -> { - String newName = mapping.remap(name); - Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), - newName + extension); - if (ret == null) - return; - if (ret && PackConverter.DEBUG) - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); - else if (!ret) - System.err.println(" Failed to rename: " + name + extension + "->" + newName + extension); - }); - } + if (!path.toFile().exists()) { + return; + } - if (from > Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && - to <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2")) { - Arrays.asList("snow", "snow_block").stream().forEach(name -> { - String newName = mapping.remap(name); - if (!extension.equals(".png")) { - Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), - newName + extension); - if (ret == null) - return; - if (ret && PackConverter.DEBUG) - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); - else if (!ret) - System.err.println( - " Failed to rename: " + name + extension + "->" + newName + extension); - } - }); - } + // remap grass blocks in order due to the cyclical way their names have changed, + // i.e grass -> grass_block, tall_grass -> grass, double_grass -> tall_grass + List grasses = Arrays.asList("tall_grass", "grass", "grass_block"); + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") + && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { + grasses.forEach(name -> { + Path basePath = path.resolve(name + extension); + String remappedName = mapping.remap(name); + + Path newPath = path.resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } - Files.list(path).forEach(path1 -> { - if (!path1.toString().endsWith(extension)) + Boolean renameSuccess = FileUtil.renameFile(basePath, newPath); + if (renameSuccess == null) { return; + } - String baseName = path1.getFileName().toString().substring(0, - path1.getFileName().toString().length() - extension.length()); - // skip the already renamed grass blocks - if (grasses.contains(baseName) - && (path.endsWith("blockstates") || path.endsWith("textures/block"))) - return; + if (renameSuccess) { + Logger.debug("Renamed: " + name + extension + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.error(" Failed to rename: " + name + extension + "->" + newPath.getFileName()); + } + }); + } + + if (from > Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && + to <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2")) { + Stream.of("snow", "snow_block").forEach(name -> { + Path basePath = path.resolve(name + extension); + String remappedName = mapping.remap(name); + + if (!extension.equals(".png")) { + Path newPath = path.resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } - String newName = mapping.remap(baseName); - if (newName != null && !newName.equals(baseName)) { - Boolean ret = FileUtil.renameFile(path1, newName + extension); - if (ret == null) + Boolean renameSuccess = FileUtil.renameFile(basePath, newPath); + if (renameSuccess == null) { return; - if (ret && PackConverter.DEBUG) - Logger.log( - " Renamed: " + path1.getFileName().toString() + "->" + newName + extension); - else if (!ret) - System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName - + extension); + } + + if (renameSuccess) { + Logger.debug("Renamed: " + name + extension + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.log("Failed to rename: " + name + extension + "->" + newPath.getFileName()); + } } }); } + + Files.list(path) + .filter(path1 -> path1.toString().endsWith(extension)) + .forEach(path1 -> { + String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); + // NOTE: skip the already renamed grass blocks + if (grasses.contains(baseName) && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { + return; + } + + String remappedName = mapping.remap(baseName); + if (remappedName != null && !remappedName.equals(baseName)) { + Path newPath = path1.getParent().resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } + + Boolean renameSuccess = FileUtil.renameFile(path1, newPath); + if (renameSuccess == null) { + return; + } + + if (renameSuccess) { + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.error(" Failed to rename: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } + } + }); } public Mapping getBlockMapping() { diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/PackMetaConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/PackMetaConverter.java index eeda5567..28a0c2ee 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/PackMetaConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/PackMetaConverter.java @@ -5,89 +5,61 @@ import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonObject; import java.io.IOException; import java.nio.file.Path; public class PackMetaConverter extends Converter { - private int version; + private final int to; private int versionInt = 4; - public PackMetaConverter(PackConverter packConverter, int versionIn) { + public PackMetaConverter(PackConverter packConverter, int to) { super(packConverter); - version = versionIn; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; } /** * Converts MCMeta to newer version - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { Path file = pack.getWorkingPath().resolve("pack.mcmeta"); - if (!file.toFile().exists()) + if (!file.toFile().exists()) { return; - // Possible TODO: Make this JSON? Possibly use protocol.json, but update it. - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) - versionInt = 22; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.20.2") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) - versionInt = 18; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.20") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.20.2")) - versionInt = 15; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19.4") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.20")) - versionInt = 13; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.19.4")) - versionInt = 12; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) - versionInt = 9; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.18") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.19")) - versionInt = 8; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.17") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.18")) - versionInt = 7; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.16.2") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.17")) - versionInt = 6; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.15") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.16.2")) - versionInt = 5; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.15")) - versionInt = 4; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.11") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.13")) - versionInt = 3; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.9") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.11")) - versionInt = 2; - else if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.7.2") - && version < Util.getVersionProtocol(packConverter.getGson(), "1.9")) - versionInt = 1; - else - versionInt = 0; + } + + JsonObject versionObj = Util.getVersionObjectByProtocol(packConverter.getGson(), to); + if (versionObj != null) { + versionInt = versionObj.get("pack_format").getAsInt(); + } JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); { JsonObject meta = json.getAsJsonObject("meta"); - if (meta == null) + if (meta == null) { meta = new JsonObject(); - meta.addProperty("game_version", Util.getVersionFromProtocol(packConverter.getGson(), version)); + } + + meta.addProperty("game_version", Util.getVersionFromProtocol(packConverter.getGson(), to)); json.add("meta", meta); } { JsonObject packObject = json.getAsJsonObject("pack"); - if (packObject == null) + if (packObject == null) { packObject = new JsonObject(); + } + packObject.addProperty("pack_format", versionInt); json.add("pack", packObject); } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ParticleConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ParticleConverter.java index 446667e1..a99bb076 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ParticleConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/ParticleConverter.java @@ -3,6 +3,8 @@ import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -14,13 +16,19 @@ public ParticleConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.18") && to <= Util.getVersionProtocol(gson, "1.17.1"); + } + @Override public void convert(Pack pack) throws IOException { Path particles = pack.getWorkingPath().resolve("assets/minecraft/particles".replace("/", File.separator)); - if (!particles.toFile().exists()) - return; - Path blockMarkerPath = particles.resolve("block_marker.json"); - if (blockMarkerPath.toFile().exists()) - Files.move(blockMarkerPath, particles.resolve("light.json")); + if (particles.toFile().exists()) { + Path blockMarkerPath = particles.resolve("block_marker.json"); + if (blockMarkerPath.toFile().exists()) { + Files.move(blockMarkerPath, particles.resolve("light.json")); + } + } } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ChestConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ChestConverter.java index fd1a2b63..0596b6aa 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ChestConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ChestConverter.java @@ -4,6 +4,8 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -14,18 +16,23 @@ public ChestConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.15") && to <= Util.getVersionProtocol(gson, "1.14.4"); + } + /** * Fixes Chest Textures in 1.15 Remaps textures, and updates images - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/entity/chest".replace("/", File.separator)); - if (!imagePath.toFile().exists()) + Path imagePath = pack.getWorkingPath().resolve("assets/minecraft/textures/entity/chest".replace("/", File.separator)); + if (!imagePath.toFile().exists()) { return; + } // Double chest doubleChest(imagePath, "normal"); @@ -41,19 +48,21 @@ public void convert(Pack pack) throws IOException { /** * Fixes Normal chests - * + * * @param imagePath * @param name * @throws IOException */ private void chest(Path imagePath, String name) throws IOException { int defaultW = 64, defaultH = 64; - if (!imagePath.resolve(name + ".png").toFile().exists()) + if (!imagePath.resolve(name + ".png").toFile().exists()) { return; + } ImageConverter normal = new ImageConverter(defaultW, defaultH, imagePath.resolve(name + ".png")); - if (!normal.fileIsPowerOfTwo()) + if (!normal.fileIsPowerOfTwo()) { return; + } // Create a new Image normal.newImage(defaultW, defaultH); @@ -88,7 +97,7 @@ private void chest(Path imagePath, String name) throws IOException { /** * Splits Double Chests into 2 images - * + * * @param imagePath * @param name * @throws IOException diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/CompassConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/CompassConverter.java index 29090e56..b9a8903c 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/CompassConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/CompassConverter.java @@ -5,28 +5,34 @@ import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; import java.nio.file.Path; public class CompassConverter extends Converter { - private int to; - private Path items; + private final int to; public CompassConverter(PackConverter packConverter, int to) { super(packConverter); this.to = to; } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.9") && to <= Util.getVersionProtocol(gson, "1.8.9"); + } + @Override public void convert(Pack pack) throws IOException { String itemsT = "items"; - if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) + if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { itemsT = "item"; - Path compassPath = pack.getWorkingPath() - .resolve(("assets/minecraft/textures/" + itemsT + "/compass.png").replace("/", File.separator)); - items = compassPath.getParent(); + } + + Path compassPath = pack.getWorkingPath().resolve(("assets/minecraft/textures/" + itemsT + "/compass.png").replace("/", File.separator)); + Path items = compassPath.getParent(); if (compassPath.toFile().exists()) { ImageConverter imageConverter = new ImageConverter(16, 16 * 32, compassPath); if (!imageConverter.fileIsPowerOfTwo()) diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/EnchantConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/EnchantConverter.java index 0f43ed4e..3b0ffa5f 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/EnchantConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/EnchantConverter.java @@ -4,6 +4,8 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -15,18 +17,20 @@ public EnchantConverter(PackConverter packConverter) { } @Override - public void convert(Pack pack) throws IOException { - Path paintingPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/misc/enchanted_item_glint.png".replace("/", File.separator)); - if (!paintingPath.toFile().exists()) - return; - - ImageConverter imageConverter = new ImageConverter(64, 64, paintingPath); - if (!imageConverter.fileIsPowerOfTwo()) - return; + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.15") && to <= Util.getVersionProtocol(gson, "1.14.4"); + } - imageConverter.newImage(64, 64); - imageConverter.grayscale(); - imageConverter.store(); + @Override + public void convert(Pack pack) throws IOException { + Path paintingPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc/enchanted_item_glint.png".replace("/", File.separator)); + if (paintingPath.toFile().exists()) { + ImageConverter imageConverter = new ImageConverter(64, 64, paintingPath); + if (imageConverter.fileIsPowerOfTwo()) { + imageConverter.newImage(64, 64); + imageConverter.grayscale(); + imageConverter.store(); + } + } } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/InventoryConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/InventoryConverter.java index c595c400..86303dad 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/InventoryConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/InventoryConverter.java @@ -4,6 +4,8 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -14,12 +16,17 @@ public InventoryConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.18") && to <= Util.getVersionProtocol(gson, "1.17.1"); + } + @Override public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/gui/container/inventory.png".replace("/", File.separator)); - if (!imagePath.toFile().exists()) + Path imagePath = pack.getWorkingPath().resolve("assets/minecraft/textures/gui/container/inventory.png".replace("/", File.separator)); + if (!imagePath.toFile().exists()) { return; + } int defaultW = 256, defaultH = 256; ImageConverter image = new ImageConverter(defaultW, defaultH, imagePath); @@ -27,7 +34,6 @@ public void convert(Pack pack) throws IOException { image.subImage(0, 0, 256, 256, 0, 0); image.subImage(0, 198, 16, 230, 0, 166); image.subImage(16, 198, 32, 230, 104, 166); - image.store(); } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java index 3cc23ba6..13f3401e 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/MapIconConverter.java @@ -5,6 +5,7 @@ import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import javax.imageio.ImageIO; import java.awt.*; @@ -32,9 +33,14 @@ public MapIconConverter(PackConverter packConverter) { mapping.put(pack(72, 0), pack(8, 16)); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); + } + /** * Converts maps - * + * * @param pack * @throws IOException */ @@ -61,7 +67,7 @@ public void convert(Pack pack) throws IOException { int newX = (int) (mapped >> 32); int newY = (int) (long) mapped; - Logger.log(" Mapping " + x + "," + y + " to " + newX + "," + newY); + Logger.log("Mapping " + x + "," + y + " to " + newX + "," + newY); g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/PaintingConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/PaintingConverter.java index 7f460500..fccfbe36 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/PaintingConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/PaintingConverter.java @@ -4,6 +4,8 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; @@ -12,27 +14,33 @@ import java.util.ArrayList; public class PaintingConverter extends Converter { - private ArrayList paintings = new ArrayList<>(); + private final ArrayList paintings = new ArrayList<>(); public PaintingConverter(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.14") && to <= Util.getVersionProtocol(gson, "1.13.2"); + } + /** * Remaps painting image to multiple images. - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path paintingPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/painting".replace("/", File.separator)); - if (!paintingPath.toFile().exists()) + Path paintingPath = pack.getWorkingPath().resolve("assets/minecraft/textures/painting".replace("/", File.separator)); + if (!paintingPath.toFile().exists()) { return; + } File[] paintingFiles = paintingPath.toFile().listFiles(); String filename = ""; + assert paintingFiles != null; for (File file : paintingFiles) { if (file.getName().endsWith(".png")) { filename = file.getName(); @@ -44,53 +52,53 @@ public void convert(Pack pack) throws IOException { normal.newImage(256, 256); // 16x16 - painting(normal, paintingPath, "kebab.png", 0, 0, 1, 1); - painting(normal, paintingPath, "aztec.png", 16, 0, 1, 1); - painting(normal, paintingPath, "alban.png", 32, 0, 1, 1); - painting(normal, paintingPath, "aztec2.png", 48, 0, 1, 1); - painting(normal, paintingPath, "bomb.png", 64, 0, 1, 1); - painting(normal, paintingPath, "plant.png", 80, 0, 1, 1); - painting(normal, paintingPath, "wasteland.png", 96, 0, 1, 1); - painting(normal, paintingPath, "back.png", 192, 0, 1, 1); + painting(normal, paintingPath, "kebab.png", 0, 0); + painting(normal, paintingPath, "aztec.png", 16, 0); + painting(normal, paintingPath, "alban.png", 32, 0); + painting(normal, paintingPath, "aztec2.png", 48, 0); + painting(normal, paintingPath, "bomb.png", 64, 0); + painting(normal, paintingPath, "plant.png", 80, 0); + painting(normal, paintingPath, "wasteland.png", 96, 0); + painting(normal, paintingPath, "back.png", 192, 0); // 32x16 - painting(normal, paintingPath, "pool.png", 0, 32, 2, 1); - painting(normal, paintingPath, "courbet.png", 32, 32, 2, 1); - painting(normal, paintingPath, "sea.png", 64, 32, 2, 1); - painting(normal, paintingPath, "sunset.png", 96, 32, 2, 1); - painting(normal, paintingPath, "creebet.png", 128, 32, 2, 1); + painting(normal, paintingPath, "pool.png", 0, 32); + painting(normal, paintingPath, "courbet.png", 32, 32); + painting(normal, paintingPath, "sea.png", 64, 32); + painting(normal, paintingPath, "sunset.png", 96, 32); + painting(normal, paintingPath, "creebet.png", 128, 32); // 16x3 - painting(normal, paintingPath, "wanderer.png", 0, 64, 1, 2); - painting(normal, paintingPath, "graham.png", 16, 64, 1, 2); + painting(normal, paintingPath, "wanderer.png", 0, 64); + painting(normal, paintingPath, "graham.png", 16, 64); // 64x48 - painting(normal, paintingPath, "skeleton.png", 192, 64, 4, 3); - painting(normal, paintingPath, "donkey_kong.png", 192, 112, 4, 3); + painting(normal, paintingPath, "skeleton.png", 192, 64); + painting(normal, paintingPath, "donkey_kong.png", 192, 112); // 64x32 - painting(normal, paintingPath, "fighters.png", 0, 96, 4, 2); + painting(normal, paintingPath, "fighters.png", 0, 96); // 32x32 - painting(normal, paintingPath, "match.png", 0, 128, 2, 2); - painting(normal, paintingPath, "bust.png", 32, 128, 2, 2); - painting(normal, paintingPath, "stage.png", 64, 128, 2, 2); - painting(normal, paintingPath, "void.png", 96, 128, 2, 2); - painting(normal, paintingPath, "skull_and_roses.png", 128, 128, 2, 2); - painting(normal, paintingPath, "wither.png", 160, 128, 2, 2); + painting(normal, paintingPath, "match.png", 0, 128); + painting(normal, paintingPath, "bust.png", 32, 128); + painting(normal, paintingPath, "stage.png", 64, 128); + painting(normal, paintingPath, "void.png", 96, 128); + painting(normal, paintingPath, "skull_and_roses.png", 128, 128); + painting(normal, paintingPath, "wither.png", 160, 128); // 64x64 - painting(normal, paintingPath, "pointer.png", 0, 192, 4, 4); - painting(normal, paintingPath, "pigscene.png", 64, 192, 4, 4); - painting(normal, paintingPath, "burning_skull.png", 128, 192, 4, 4); + painting(normal, paintingPath, "pointer.png", 0, 192); + painting(normal, paintingPath, "pigscene.png", 64, 192); + painting(normal, paintingPath, "burning_skull.png", 128, 192); normal.store(paintingPath.resolve("paintings_kristoffer_zetterstrand.png")); - for (String item : paintings) + for (String item : paintings) { Files.deleteIfExists(paintingPath.resolve(item)); + } } - private void painting(ImageConverter normal, Path paintingPath, String name, int x, int y, int scaleX, - int scaleY) + private void painting(ImageConverter normal, Path paintingPath, String name, int x, int y) throws IOException { if (paintingPath.resolve(name).toFile().exists()) { normal.addImage(paintingPath.resolve(name), x, y); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ParticleTextureConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ParticleTextureConverter.java index 860d94b3..1d4b8406 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ParticleTextureConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/ParticleTextureConverter.java @@ -5,16 +5,17 @@ import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; +import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Path; -import javax.imageio.ImageIO; - public class ParticleTextureConverter extends Converter { - int from, to; + private final int from; + private final int to; public ParticleTextureConverter(PackConverter packConverter, int from, int to) { super(packConverter); @@ -22,27 +23,36 @@ public ParticleTextureConverter(PackConverter packConverter, int from, int to) { this.to = to; } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + /** * Updates Particles - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) + if (!texturesPath.toFile().exists()) { return; + } Path particleFolderPath = texturesPath.resolve("particle"); - if (!particleFolderPath.toFile().exists()) + if (!particleFolderPath.toFile().exists()) { return; + } Path particlesImagePath = particleFolderPath.resolve("particles.png"); - if (!particlesImagePath.toFile().exists()) + if (!particlesImagePath.toFile().exists()) { return; + } - int defaultW = 128, defaultH = 128; + // Default W/H is 128 + int defaultW, defaultH; // Particles if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.14") @@ -148,8 +158,9 @@ public void convert(Pack pack) throws IOException { Path entityPath = texturesPath.resolve("entity"); Path fishingHookPath = entityPath.resolve("fishing_hook.png"); - if (fishingHookPath.toFile().exists()) + if (fishingHookPath.toFile().exists()) { converter.addImage(fishingHookPath, 8, 16); + } converter.store(); } @@ -159,11 +170,11 @@ public void convert(Pack pack) throws IOException { defaultW = 128; defaultH = 128; ImageConverter converter = new ImageConverter(defaultW, defaultH, particlesImagePath); - if (!converter.fileIsPowerOfTwo()) - return; - converter.newImage(128, 128); - converter.subImage(0, 0, 128, 128, 0, 0); - converter.store(); + if (converter.fileIsPowerOfTwo()) { + converter.newImage(128, 128); + converter.subImage(0, 0, 128, 128, 0, 0); + converter.store(); + } } } } diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/WaterConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/WaterConverter.java index d153ffe8..32ffee4c 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/WaterConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/textures/WaterConverter.java @@ -4,35 +4,41 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; -import java.awt.Color; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.awt.*; import java.io.File; import java.io.IOException; import java.nio.file.Path; public class WaterConverter extends Converter { - public WaterConverter(PackConverter packConverter) { - super(packConverter); - } + public WaterConverter(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from >= Util.getVersionProtocol(gson, "1.13") && to <= Util.getVersionProtocol(gson, "1.12.2"); + } - @Override - public void convert(Pack pack) throws IOException { - Path blocksFolder = pack.getWorkingPath() - .resolve("assets/minecraft/textures/blocks".replace("/", File.separator)); - if (!blocksFolder.toFile().exists()) - return; - colorize(blocksFolder.resolve("water_flow.png"), 32, 1024); - colorize(blocksFolder.resolve("water_still.png"), 32, 1024); - colorize(blocksFolder.resolve("water_overlay.png"), 32, 32); - } + @Override + public void convert(Pack pack) throws IOException { + Path blocksFolder = pack.getWorkingPath().resolve("assets/minecraft/textures/blocks".replace("/", File.separator)); + if (blocksFolder.toFile().exists()) { + colorize(blocksFolder.resolve("water_flow.png"), 32, 1024); + colorize(blocksFolder.resolve("water_still.png"), 16, 512); + colorize(blocksFolder.resolve("water_overlay.png"), 16, 16); + } + } - private void colorize(Path path, int w, int h) throws IOException { - if (!path.toFile().exists()) - return; - ImageConverter imageConverter = new ImageConverter(w, h, path); - if (!imageConverter.fileIsPowerOfTwo()) - return; - imageConverter.newImage(w, h); - imageConverter.colorize(new Color(45, 63, 244, 170)); - imageConverter.store(); - } + private void colorize(Path path, int w, int h) throws IOException { + if (path.toFile().exists()) { + ImageConverter imageConverter = new ImageConverter(w, h, path); + if (imageConverter.fileIsPowerOfTwo()) { + imageConverter.colorizeGrayscale(new Color(45, 63, 244)); + imageConverter.store(); + } + } + } } diff --git a/conversions/Forwards/pom.xml b/conversions/Forwards/pom.xml index 7f3f1ce9..61f01879 100644 --- a/conversions/Forwards/pom.xml +++ b/conversions/Forwards/pom.xml @@ -5,7 +5,7 @@ conversions com.agentdid127.resourcepack - 2.2.4 + 2.2.5 4.0.0 diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java index 5ff1e8d4..cc62f30a 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/ForwardsPackConverter.java @@ -1,140 +1,122 @@ package com.agentdid127.resourcepack.forwards; -import com.agentdid127.resourcepack.forwards.impl.textures.*; +import com.agentdid127.resourcepack.forwards.impl.other.*; +import com.agentdid127.resourcepack.forwards.impl.other.textures.InventoryConverter; +import com.agentdid127.resourcepack.forwards.impl.other.textures.ParticlesSlicerConverter; +import com.agentdid127.resourcepack.forwards.impl.v1_14.textures.MobEffectAtlasSlicerConverter1_14; +import com.agentdid127.resourcepack.forwards.impl.v1_14.textures.PaintingSlicerConverter1_14_4; +import com.agentdid127.resourcepack.forwards.impl.v1_9.textures.OffHandCreator1_9; +import com.agentdid127.resourcepack.forwards.impl.v1_19.textures.WidgetSlidersCreator1_19_4; +import com.agentdid127.resourcepack.forwards.impl.v1_13.AnimationConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_13.LangConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_13.MCPatcherConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_13.SoundsConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_13.textures.MapIconConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_13.textures.WaterConverter1_13; +import com.agentdid127.resourcepack.forwards.impl.v1_15.textures.ChestConverter1_15; +import com.agentdid127.resourcepack.forwards.impl.v1_15.textures.EnchantConverter1_15; +import com.agentdid127.resourcepack.forwards.impl.v1_19.AtlasConverter1_19_3; +import com.agentdid127.resourcepack.forwards.impl.v1_19.EnchantPathConverter1_19_4; +import com.agentdid127.resourcepack.forwards.impl.v1_19.textures.CreativeTabsConverter1_19_3; +import com.agentdid127.resourcepack.forwards.impl.v1_20.ImageFormatConverter1_20_3; +import com.agentdid127.resourcepack.forwards.impl.v1_20.textures.GuiSlicerConverter1_20_2; +import com.agentdid127.resourcepack.forwards.impl.v1_20.textures.MapIconSlicerConverter1_20_5; +import com.agentdid127.resourcepack.forwards.impl.v1_20.textures.TitleConverter1_20; +import com.agentdid127.resourcepack.forwards.impl.v1_21.textures.ArmorMoverConverter1_21_2; +import com.agentdid127.resourcepack.forwards.impl.v1_21.textures.PostEffectShadersConverter1_21_2; +import com.agentdid127.resourcepack.forwards.impl.v1_9.textures.CompassConverter1_9; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; -import com.agentdid127.resourcepack.forwards.impl.*; -import com.google.gson.GsonBuilder; +import com.google.gson.Gson; import java.io.IOException; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; +import java.util.stream.Stream; public class ForwardsPackConverter extends PackConverter { Path INPUT_DIR; + private final int from; + private final int to; - public ForwardsPackConverter(String from, String to, String light, boolean minify, Path input, boolean debug, - PrintStream out) { - GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping().setLenient(); - if (!minify) - gsonBuilder.setPrettyPrinting(); - gson = gsonBuilder.create(); - DEBUG = debug; + public ForwardsPackConverter(Gson gson, int from, int to, String light, Path input, boolean debug, + PrintStream out) { + this.gson = gson; + Logger.setDebug(debug); Logger.setStream(out); - Logger.log(from); - Logger.log(to); + Logger.log("Converting packs from: " + from + " to " + to); + this.from = from; + this.to = to; this.INPUT_DIR = input; - converterRunner(from, to, light); + this.setupConverters(light); } - private void converterRunner(String from, String to, String light) { - int protocolFrom = Util.getVersionProtocol(gson, from); - int protocolTo = Util.getVersionProtocol(gson, to); - - // This needs to be run first, other converters might reference - // new directory names. - this.registerConverter(new NameConverter(this, protocolFrom, protocolTo)); - this.registerConverter(new PackMetaConverter(this, protocolFrom, protocolTo)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.9") - && protocolTo >= Util.getVersionProtocol(gson, "1.9")) { - this.registerConverter(new CompassConverter(this, protocolTo)); - this.registerConverter(new OffHandCreator(this)); - } - - if (protocolFrom < Util.getVersionProtocol(gson, "1.11") - && protocolTo >= Util.getVersionProtocol(gson, "1.11")) - this.registerConverter(new SpacesConverter(this)); - - this.registerConverter(new ModelConverter(this, light, protocolTo, protocolFrom)); - - if (protocolFrom <= Util.getVersionProtocol(gson, "1.12.2") - && protocolTo >= Util.getVersionProtocol(gson, "1.13")) { - this.registerConverter(new SoundsConverter(this)); - this.registerConverter(new AnimationConverter(this)); - this.registerConverter(new MapIconConverter(this)); - this.registerConverter(new MCPatcherConverter(this)); - this.registerConverter(new WaterConverter(this)); - } - - this.registerConverter(new BlockStateConverter(this, protocolFrom, protocolTo)); - - if (protocolTo >= Util.getVersionProtocol(gson, "1.13")) - this.registerConverter(new LangConverter(this, from, to)); - - this.registerConverter(new ParticleTextureConverter(this, protocolFrom, protocolTo)); - if (protocolFrom < Util.getVersionProtocol(gson, "1.15") && protocolTo >= Util.getVersionProtocol(gson, "1.15")) - this.registerConverter(new ChestConverter(this)); - - if (protocolFrom <= Util.getVersionProtocol(gson, "1.13") - && protocolTo >= Util.getVersionProtocol(gson, "1.14.4")) - this.registerConverter(new PaintingConverter(this)); - - if (protocolFrom <= Util.getVersionProtocol(gson, "1.13.2") - && protocolTo >= Util.getVersionProtocol(gson, "1.14")) - this.registerConverter(new MobEffectAtlasConverter(this, protocolFrom)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.15") - && protocolTo >= Util.getVersionProtocol(gson, "1.15")) - this.registerConverter(new EnchantConverter(this)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.18") - && protocolTo >= Util.getVersionProtocol(gson, "1.18")) - this.registerConverter(new ParticleConverter(this)); - + private void setupConverters(String light) { + this.registerConverter(new NameConverter(this, from, to)); // This needs to be run first, other converters might reference new directory names. + this.registerConverter(new PackMetaConverter(this, from, to)); + this.registerConverter(new CompassConverter1_9(this, to)); + this.registerConverter(new OffHandCreator1_9(this)); + this.registerConverter(new RemoveSpacesConverter1_11(this)); + this.registerConverter(new ModelConverter(this, light, from, to)); + this.registerConverter(new SoundsConverter1_13(this)); + this.registerConverter(new AnimationConverter1_13(this)); + this.registerConverter(new MapIconConverter1_13(this)); + this.registerConverter(new MCPatcherConverter1_13(this)); + this.registerConverter(new WaterConverter1_13(this)); + this.registerConverter(new BlockStateConverter(this, from, to)); + this.registerConverter(new LangConverter1_13(this, from, to)); + this.registerConverter(new ParticlesSlicerConverter(this, from, to)); + this.registerConverter(new PaintingSlicerConverter1_14_4(this)); + this.registerConverter(new MobEffectAtlasSlicerConverter1_14(this, from)); + this.registerConverter(new ChestConverter1_15(this)); + this.registerConverter(new EnchantConverter1_15(this)); + this.registerConverter(new ParticleConverter1_18(this)); this.registerConverter(new InventoryConverter(this)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.19.3") - && protocolTo >= Util.getVersionProtocol(gson, "1.19.3")) { - this.registerConverter(new AtlasConverter(this)); - this.registerConverter(new CreativeTabsConverter(this)); - } - - if (protocolFrom < Util.getVersionProtocol(gson, "1.19.4") - && protocolTo >= Util.getVersionProtocol(gson, "1.19.4")) { - this.registerConverter(new EnchantPathConverter(this)); - this.registerConverter(new SlidersCreator(this)); - } - - if (protocolFrom < Util.getVersionProtocol(gson, "1.20") - && protocolTo >= Util.getVersionProtocol(gson, "1.20")) - this.registerConverter(new TitleConverter(this)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.20.2") - && protocolTo >= Util.getVersionProtocol(gson, "1.20.2")) - this.registerConverter(new SlicerConverter(this, protocolFrom)); - - if (protocolFrom < Util.getVersionProtocol(gson, "1.20.3") - && protocolTo >= Util.getVersionProtocol(gson, "1.20.3")) - this.registerConverter(new ImageFormatConverter(this)); + this.registerConverter(new AtlasConverter1_19_3(this)); + this.registerConverter(new CreativeTabsConverter1_19_3(this)); + this.registerConverter(new EnchantPathConverter1_19_4(this)); + this.registerConverter(new WidgetSlidersCreator1_19_4(this)); + this.registerConverter(new TitleConverter1_20(this)); + this.registerConverter(new GuiSlicerConverter1_20_2(this, from)); + this.registerConverter(new ImageFormatConverter1_20_3(this)); + this.registerConverter(new MapIconSlicerConverter1_20_5(this, from)); + this.registerConverter(new ArmorMoverConverter1_21_2(this)); + this.registerConverter(new PostEffectShadersConverter1_21_2(this)); } public void runPack(Pack pack) { try { Logger.log("Converting " + pack); pack.getHandler().setup(); - Logger.log(" Running Converters"); + Logger.addTab(); + Logger.log("Running Converters"); for (Converter converter : converters.values()) { - if (DEBUG) - Logger.log(" Running " + converter.getClass().getSimpleName()); - converter.convert(pack); + if (converter.shouldConvert(gson, from, to)) { + Logger.addTab(); + Logger.log("Running " + converter.getClass().getSimpleName()); + converter.convert(pack); + Logger.subTab(); + } } + Logger.subTab(); pack.getHandler().finish(); } catch (Throwable t) { + Logger.resetTab(); Logger.log("Failed to convert!"); Util.propagate(t); } } public void runDir() throws IOException { - Files.list(INPUT_DIR) - .map(Pack::parse) - .filter(Objects::nonNull) - .forEach(pack -> runPack(pack)); + try (Stream pathStream = Files.list(INPUT_DIR)) { + try (Stream packStream = pathStream.map(Pack::parse).filter(Objects::nonNull)) { + packStream.forEach(this::runPack); + } + } } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.java deleted file mode 100644 index ae7feef1..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class EnchantPathConverter extends Converter { - public EnchantPathConverter(PackConverter packConverter) { - super(packConverter); - } - - @Override - public void convert(Pack pack) throws IOException { - Path miscPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc".replace("/", File.separator)); - - if (miscPath.resolve("enchanted_item_glint.png").toFile().exists()) { - Files.copy(miscPath.resolve("enchanted_item_glint.png"), miscPath.resolve("enchanted_glint_entity.png")); - Files.copy(miscPath.resolve("enchanted_item_glint.png"), miscPath.resolve("enchanted_glint_item.png")); - Files.delete(miscPath.resolve("enchanted_item_glint.png")); - } - - if (miscPath.resolve("enchanted_item_glint.png.mcmeta").toFile().exists()) { - Files.copy(miscPath.resolve("enchanted_item_glint.png.mcmeta"), - miscPath.resolve("enchanted_glint_entity.png.mcmeta")); - Files.copy(miscPath.resolve("enchanted_item_glint.png.mcmeta"), - miscPath.resolve("enchanted_glint_item.png.mcmeta")); - Files.delete(miscPath.resolve("enchanted_item_glint.png.mcmeta")); - } - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java deleted file mode 100644 index 76fd7de7..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/LangConverter.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Logger; -import com.agentdid127.resourcepack.library.utilities.PropertiesEx; -import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Map; - -public class LangConverter extends Converter { - private String version; - private String from; - - public LangConverter(PackConverter packConverter, String fromIn, String versionIn) { - super(packConverter); - version = versionIn; - from = fromIn; - } - - /** - * Moves Lang (properties) to JSON - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path langPath = pack.getWorkingPath().resolve("assets/minecraft/lang".replace("/", File.separator)); - if (!langPath.toFile().exists()) - return; - - ArrayList models = new ArrayList(); - Files.list(langPath) - .filter(path1 -> path1.toString().endsWith(".lang")) - .forEach(model -> { - JsonObject out = new JsonObject(); - try (InputStream input = new FileInputStream(model.toString())) { - PropertiesEx prop = new PropertiesEx(); - prop.load(input); - if (Util.getVersionProtocol(packConverter.getGson(), from) <= Util - .getVersionProtocol(packConverter.getGson(), "1.12") - && ((Util.getVersionProtocol(packConverter.getGson(), version) >= Util - .getVersionProtocol(packConverter.getGson(), "1.13")) - && (Util.getVersionProtocol(packConverter.getGson(), version) <= Util - .getVersionProtocol(packConverter.getGson(), "1.13.2")))) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/lang.json") - .getAsJsonObject("1_13"); - - Enumeration enums = (Enumeration) prop.propertyNames(); - while (enums.hasMoreElements()) { - String key = enums.nextElement(); - String value = prop.getProperty(key); - for (Map.Entry id2 : id.entrySet()) { - if (key.equals(id2.getKey())) - out.addProperty(id2.getValue().getAsString(), value); - else - out.addProperty(key, value); - } - } - // Saves File - } - if (Util.getVersionProtocol(packConverter.getGson(), version) > Util - .getVersionProtocol(packConverter.getGson(), "1.14")) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/lang.json") - .getAsJsonObject("1_14"); - - Enumeration enums = (Enumeration) prop.propertyNames(); - while (enums.hasMoreElements()) { - String key = enums.nextElement(); - String value = prop.getProperty(key); - for (Map.Entry id2 : id.entrySet()) { - if (key.equals(id2.getKey())) - out.addProperty(id2.getValue().getAsString(), value); - else - out.addProperty(key, value); - } - } - // Saves File - } - input.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - try { - int modelNoLang = model.getFileName().toString().indexOf(".lang"); - String file2 = model.getFileName().toString().substring(0, modelNoLang); - Logger.log("Saving: " + file2 + ".json"); - Path outLangPath = pack.getWorkingPath() - .resolve(("assets/minecraft/lang/" + file2 + ".json").replace("/", File.separator)); - JsonUtil.writeJson(packConverter.getGson(), outLangPath, out); - - } catch (IOException e) { - e.printStackTrace(); - } - - models.add(model.getFileName().toString()); - }); - - for (int i = 0; i < models.size(); i++) { - Path langFilePath = pack.getWorkingPath() - .resolve(("assets/minecraft/lang/" + models.get(i)).replace("/", File.separator)); - Logger.log("Deleting: " + langFilePath); - Files.delete(langFilePath); - } - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java deleted file mode 100644 index e91a1e88..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Logger; -import com.agentdid127.resourcepack.library.utilities.PropertiesEx; -import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.JsonObject; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; - -@Deprecated // will be removed when extensions are made -public class MCPatcherConverter extends Converter { - public MCPatcherConverter(PackConverter packConverter) { - super(packConverter); - } - - /** - * Parent conversion for MCPatcher - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath() - .resolve("assets/minecraft/optifine".replace("/", File.separator)); - if (models.toFile().exists()) - findFiles(models); - // remapModelJson(models.resolve("item")); - // remapModelJson(models.resolve("block")); - } - - /** - * Finds all files in the specified path - * - * @param path - * @throws IOException - */ - protected void findFiles(Path path) throws IOException { - File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (!file.isDirectory()) - continue; - remapProperties(file.toPath()); - findFiles(file.toPath()); - } - } - - /** - * Remaps properties to work in newer versions - * - * @param path - * @throws IOException - */ - protected void remapProperties(Path path) throws IOException { - if (!path.toFile().exists()) - return; - - Files.list(path).filter(path1 -> path1.toString().endsWith(".properties")).forEach(model -> { - try (InputStream input = new FileInputStream(model.toString())) { - if (PackConverter.DEBUG) - Logger.log("Updating:" + model.getFileName()); - - PropertiesEx prop = new PropertiesEx(); - prop.load(input); - - try (OutputStream output = new FileOutputStream(model.toString())) { - // updates textures - if (prop.containsKey("texture")) - prop.setProperty("texture", replaceTextures(prop)); - - // Updates Item IDs - if (prop.containsKey("matchItems")) - prop.setProperty("matchItems", updateID("matchItems", prop, "regular").replaceAll("\"", "")); - - if (prop.containsKey("items")) - prop.setProperty("items", updateID("items", prop, "regular").replaceAll("\"", "")); - - if (prop.containsKey("matchBlocks")) - prop.setProperty("matchBlocks", updateID("matchBlocks", prop, "regular").replaceAll("\"", "")); - - // Saves File - prop.store(output, ""); - output.close(); - } catch (IOException io) { - io.printStackTrace(); - } - } catch (IOException e) { - throw Util.propagate(e); - } - }); - } - - /** - * Replaces texture paths with blocks and items - * - * @param prop - * @return - */ - protected String replaceTextures(PropertiesEx prop) { - NameConverter nameConverter = packConverter.getConverter(NameConverter.class); - String properties = prop.getProperty("texture"); - if (properties.startsWith("textures/blocks/")) - properties = "textures/block/" + nameConverter.getBlockMapping(); - else if (properties.startsWith("textures/items/")) - properties = "textures/item/" + nameConverter.getItemMapping(); - return properties; - } - - /** - * Fixes item IDs and switches them from a numerical id to minecraft: something - * - * @param type - * @param prop - * @return - */ - protected String updateID(String type, PropertiesEx prop, String selection) { - JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/ids.json").get(selection) - .getAsJsonObject(); - String[] split = prop.getProperty(type).split(" "); - String properties2 = " "; - for (int i = 0; i < split.length; i++) - if (id.get(split[i]) != null) - split[i] = "minecraft:" + id.get(split[i]).getAsString(); - for (String item : split) - properties2 += item + " "; - properties2.substring(0, properties2.length() - 1); - if (prop.containsKey("metadata")) - prop.remove("metadata"); - return properties2; - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java deleted file mode 100644 index 85df5b7e..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ModelConverter.java +++ /dev/null @@ -1,441 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Logger; -import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Map; - -public class ModelConverter extends Converter { - private int version; - private int from; - protected String light = "none"; - - public ModelConverter(PackConverter packConverter, String lightIn, int versionIn, int fromIn) { - super(packConverter); - light = lightIn; - version = versionIn; - from = fromIn; - } - - /** - * Runs findfiles with the directory Models - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path models = pack.getWorkingPath() - .resolve("assets/minecraft/models".replace("/", File.separator)); - if (!models.toFile().exists()) - return; - findFiles(models); - } - - /** - * Recursively finds files with Path path and runs remapModelJson - * - * @param path - * @throws IOException - */ - protected void findFiles(Path path) throws IOException { - File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - continue; - remapModelJson(file.toPath()); - findFiles(file.toPath()); - } - } - - /** - * Updates model Json to newer versions - * - * @param path - * @throws IOException - */ - protected void remapModelJson(Path path) throws IOException { - if (!path.toFile().exists()) - return; - Files.list(path) - .filter(path1 -> path1.toString().endsWith(".json")) - .forEach(model -> { - try { - JsonObject jsonObject; - if (JsonUtil.readJson(packConverter.getGson(), model) != null - && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) - jsonObject = JsonUtil.readJson(packConverter.getGson(), model); - else { - if (PackConverter.DEBUG) { - Logger.log("Could not convert model: " + model.getFileName()); - if (JsonUtil.readJson(packConverter.getGson(), model) == null) - Logger.log("Check for Syntax Errors in file."); - else - Logger.log("File is not JSON Object."); - } - return; - } - - // GUI light system for 1.15.2 - if (!light.equals("none") && (light.equals("front") || light.equals("side"))) - jsonObject.addProperty("gui_light", light); - - // minify the json so we can replace spaces in paths easily - // TODO Improvement: handle this in a cleaner way? - String content = jsonObject.toString(); - content = content.replaceAll("items/", "item/"); - content = content.replaceAll("blocks/", "block/"); - content = content.replaceAll(" ", "_"); - - // handle the remapping of textures, for models that use default texture names - jsonObject = packConverter.getGson().fromJson(content, JsonObject.class); - if (jsonObject.keySet().isEmpty() || jsonObject.entrySet().isEmpty()) { - Logger.log("Model '" + model.getFileName() + "' was empty, skipping..."); - return; - } - - if (jsonObject.has("textures") && jsonObject.get("textures").isJsonObject()) { - NameConverter nameConverter = packConverter.getConverter(NameConverter.class); - - JsonObject initialTextureObject = jsonObject.getAsJsonObject("textures"); - JsonObject textureObject = initialTextureObject.deepCopy(); - for (Map.Entry entry : initialTextureObject.entrySet()) { - String value = entry.getValue().getAsString(); - textureObject.remove(entry.getKey()); - - // 1.8 mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.9")) { - if (entry.getKey().equals("layer0") - && (!value.startsWith(path.getFileName().toString()) - && !value.startsWith("minecraft:" + path.getFileName().toString()))) - value = path.getFileName() + "/" + value; - } - - // 1.13 Mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (value.startsWith("block/")) { - value = "block/" + nameConverter.getBlockMapping() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - if (PackConverter.DEBUG) { - Logger.log(value.substring("block/".length()).toLowerCase() - .replaceAll("[()]", "")); - Logger.log(nameConverter.getBlockMapping() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", "")); - } - } else if (value.startsWith("item/")) { - value = "item/" + nameConverter.getItemMapping() - .remap(value.substring("item/".length())).toLowerCase() - .replaceAll("[()]", ""); - } else - value = value.toLowerCase().replaceAll("[()]", ""); - } - - // 1.14 Mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - if (value.startsWith("block/")) - value = "block/" + nameConverter.getNewBlockMapping() - .remap(value.substring("block/".length())); - } - - // 1.17 Mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - if (value.startsWith("block/")) { - value = "block/" + nameConverter.getBlockMapping17() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - } else if (value.startsWith("item/")) { - value = "item/" + nameConverter.getItemMapping17() - .remap(value.substring("item/".length())).toLowerCase() - .replaceAll("[()]", ""); - } - value = value.toLowerCase().replaceAll("[()]", ""); - } - - // 1.19 Mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { - if (value.startsWith("block/")) { - value = "block/" + nameConverter.getBlockMapping19() - .remap(value.substring("block/".length())).toLowerCase() - .replaceAll("[()]", ""); - } - value = value.toLowerCase().replaceAll("[()]", ""); - } - - // Dyes - if (value.startsWith("item/") && value.contains("dye")) { - if (version > Util.getVersionProtocol(packConverter.getGson(), "1.13")) - value = "item/" + nameConverter.getNewItemMapping() - .remap(value.substring("item/".length())); - } - - // 1.19.3 Mappings - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && from < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { - if (!value.startsWith("minecraft:") && !value.startsWith("#")) - value = "minecraft:" + value; - } - - if (!textureObject.has(entry.getKey())) - textureObject.addProperty(entry.getKey(), value); - } - - jsonObject.remove("textures"); - jsonObject.add("textures", textureObject); - } - - // Fix Display Model For Packs (<= 1.8.9) - if (jsonObject.has("display") - && from <= Util.getVersionProtocol(packConverter.getGson(), "1.8")) { - JsonObject display = updateDisplay(packConverter.getGson(), - jsonObject.remove("display").getAsJsonObject()); - jsonObject.add("display", display); - } - - if (jsonObject.has("overrides")) { - for (Map.Entry entry : jsonObject.entrySet()) { - if (entry.getKey().equals("overrides")) { - JsonArray overrides = jsonObject.get("overrides").getAsJsonArray(); - JsonArray overrides2 = new JsonArray(); - for (int i = 0; i < overrides.size(); i++) { - JsonObject object = overrides.get(i).getAsJsonObject(); - for (Map.Entry json : object.entrySet()) { - if (json.getKey().equals("model")) - object.addProperty(json.getKey(), - json.getValue().getAsString().replaceAll("[()]", "")); - else - object.add(json.getKey(), json.getValue()); - } - overrides2.add(object); - } - jsonObject.add(entry.getKey(), overrides2); - } - } - } - - // Parent Stuff - if (jsonObject.has("parent")) { - // Change parent to lowercase - for (Map.Entry entry : jsonObject.entrySet()) { - if (entry.getKey().equals("parent")) { - String parent = entry.getValue().getAsString().toLowerCase(); - - parent = parent.replace(" ", "_"); - - // Get block/item parents renamed - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13") - && version >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/forwards/blocks.json", parent, "1_13"); - else if (parent.startsWith("item/")) - parent = setParent("item/", "/forwards/items.json", parent, "1_13"); - } - - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.14") - && version >= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/forwards/blocks.json", parent, "1_14"); - else if (parent.startsWith("item/")) - parent = setParent("item/", "/forwards/items.json", parent, "1_14"); - } - - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.17") - && version >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/forwards/blocks.json", parent, "1_17"); - else if (parent.startsWith("item/")) - parent = setParent("item/", "/forwards/items.json", parent, "1_17"); - } - - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.19") - && version >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { - if (parent.startsWith("block/")) - parent = setParent("block/", "/forwards/blocks.json", parent, "1_19"); - } - - if (version >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && from < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { - if (!parent.startsWith("minecraft:")) - parent = "minecraft:" + parent; - } - - jsonObject.addProperty(entry.getKey(), parent); - } - } - } - - if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { - if (PackConverter.DEBUG) - Logger.log("Updating Model: " + model.getFileName()); - JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); - } - } catch (IOException e) { - throw Util.propagate(e); - } - }); - } - - /** - * Gets parent object and sets a new one - * - * @param prefix prefix of file path - * @param path File path of json control - * @param parent Parent String - * @return New string with changed parent. - */ - protected String setParent(String prefix, String path, String parent, String item) { - String parent2 = parent.replace(prefix, ""); - JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); - if (file == null) { - Logger.log("Prefix Failed on: " + parent); - return ""; - } - - return file.has(parent2) ? prefix + file.get(parent2).getAsString() : parent; - } - - protected static JsonObject updateDisplay(Gson gson, JsonObject display) { - JsonObject defaults = JsonUtil.readJsonResource(gson, "/forwards/display.json"); - if (display == null) { - return defaults.deepCopy(); - } - - // First Person - if (display.has("firstperson")) { - JsonObject firstPerson = display.remove("firstperson").getAsJsonObject(); - display.add("firstperson_righthand", - updateDisplayFirstPerson(gson, firstPerson)); - } else if (!display.has("firstperson_righthand")) { - JsonObject rightHand = defaults.get("firstperson_righthand") - .getAsJsonObject().deepCopy(); - display.add("firstperson_righthand", rightHand); - } - - if (!display.has("firstperson_lefthand")) { - display.add("firstperson_lefthand", - getLeftHand(gson, - display.get("firstperson_righthand").getAsJsonObject().deepCopy())); - } - - // Third Person - if (display.has("thirdperson")) { - JsonObject thirdPerson = display.remove("thirdperson").getAsJsonObject(); - display.add("thirdperson_righthand", - updateDisplayThirdPerson(gson, thirdPerson)); - } else if (!display.has("thirdperson_righthand")) { - JsonObject rightHand = defaults.get("thirdperson_righthand") - .getAsJsonObject().deepCopy(); - display.add("thirdperson_righthand", rightHand); - } - - if (!display.has("thirdperson_lefthand")) { - display.add("thirdperson_lefthand", - getLeftHand(gson, - display.get("thirdperson_righthand").getAsJsonObject().deepCopy())); - } - - if (!display.has("ground")) { - display.add("ground", - defaults.get("ground").getAsJsonObject().deepCopy()); - } - - if (!display.has("head")) { - display.add("head", - defaults.get("head").getAsJsonObject().deepCopy()); - } - - return display; - } - - private static JsonObject getLeftHand(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray oldRotation = newObject.remove("rotation").getAsJsonArray(); - JsonArray rotation = new JsonArray(); - rotation.add(oldRotation.get(0).getAsNumber()); - rotation.add(0 - oldRotation.get(1).getAsDouble()); - rotation.add(0 - oldRotation.get(2).getAsDouble()); - newObject.add("rotation", - rotation); - } - - return newObject; - } - - private static JsonObject updateDisplayFirstPerson(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); - newObject.add("rotation", - JsonUtil.add( - rotation, - JsonUtil.asArray(gson, "[0, 45, 0]"))); - } - - if (old.has("translation")) { - JsonArray translation = newObject.remove("translation").getAsJsonArray(); - newObject.add("translation", - JsonUtil.add( - JsonUtil.multiply( - JsonUtil.subtract( - translation, - JsonUtil.asArray(gson, "[0, 4, 2]")), - JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]")), - JsonUtil.asArray(gson, "[1.13, 3.2, 1.13]"))); - } - - if (old.has("scale")) { - JsonArray scale = newObject.remove("scale").getAsJsonArray(); - newObject.add("scale", - JsonUtil.multiply( - scale, - JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]"))); - } - - return newObject; - } - - private static JsonObject updateDisplayThirdPerson(Gson gson, JsonObject old) { - JsonObject newObject = old.deepCopy(); - if (old.has("rotation")) { - JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); - newObject.add("rotation", - JsonUtil.add( - JsonUtil.multiply( - rotation, - JsonUtil.asArray(gson, "[1, -1, -1]")), - JsonUtil.asArray(gson, "[0, 0, 20]"))); - } - - if (old.has("translation")) { - JsonArray translation = newObject.remove("translation").getAsJsonArray(); - newObject.add("translation", - JsonUtil.add( - JsonUtil.multiply( - translation, - JsonUtil.asArray(gson, "[1, 1, -1]")), - JsonUtil.asArray(gson, "[0, 2.75, -3]"))); - } - - // For keeping order - if (old.has("scale")) { - JsonArray scale = newObject.remove("scale").getAsJsonArray(); - newObject.add("scale", scale); - } - - return newObject; - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.java deleted file mode 100644 index 9d32d198..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.JsonObject; - -import java.io.IOException; -import java.nio.file.Path; - -// Reference: https://minecraft.wiki/w/Pack_format -public class PackMetaConverter extends Converter { - private int to; - private int from; - - public PackMetaConverter(PackConverter packConverter, int from, int to) { - super(packConverter); - this.from = from; - this.to = to; - } - - /** - * Converts MCMeta to newer version - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path file = pack.getWorkingPath().resolve("pack.mcmeta"); - if (!file.toFile().exists()) - return; - - int versionInt = 4; - - // Possible TODO: Make this JSON? Possibly use protocol.json, but update it. - if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) - versionInt = 22; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.20.2") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) - versionInt = 18; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.20") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.20.2")) - versionInt = 15; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.19.4") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.20")) - versionInt = 13; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.19.4")) - versionInt = 12; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.19") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) - versionInt = 9; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.18") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) - versionInt = 8; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.17") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.18")) - versionInt = 7; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.16.2") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.17")) - versionInt = 6; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.15") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.16.2")) - versionInt = 5; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.15")) - versionInt = 4; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.11") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) - versionInt = 3; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.9") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.11")) - versionInt = 2; - else if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.7.2") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.9")) - versionInt = 1; - else - versionInt = 0; - - JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); - { - JsonObject meta = json.getAsJsonObject("meta"); - if (meta == null) - meta = new JsonObject(); - meta.addProperty("game_version", Util.getVersionFromProtocol(packConverter.getGson(), to)); - json.add("meta", meta); - } - - { - JsonObject packObject = json.getAsJsonObject("pack"); - if (packObject == null) - packObject = new JsonObject(); - packObject.addProperty("pack_format", versionInt); - - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.20.2") - && to >= Util.getVersionProtocol(packConverter.getGson(), "1.20.2")) { - JsonObject supportedFormats = new JsonObject(); - supportedFormats.addProperty("min_inclusive", versionInt); - supportedFormats.addProperty("max_inclusive", versionInt); // TODO: A better way to do this - packObject.add("supported_formats", supportedFormats); - } - - json.add("pack", packObject); - } - - JsonUtil.writeJson(packConverter.getGson(), file, json); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java deleted file mode 100644 index 1a36301a..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.FileUtil; -import com.agentdid127.resourcepack.library.utilities.Logger; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class SpacesConverter extends Converter { - public SpacesConverter(PackConverter packConverter) { - super(packConverter); - } - - /** - * Runs findFiles - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path assets = pack.getWorkingPath().resolve("assets"); - if (!assets.toFile().exists()) - return; - findFiles(assets); - } - - /** - * Recursively finds files to fix Spaces - * - * @param path - * @throws IOException - */ - protected void findFiles(Path path) throws IOException { - if (!path.toFile().exists()) - return; - File directory = path.toFile(); - for (File file : directory.listFiles()) { - String dir = fixSpaces(file.toPath()); - if (file.isDirectory()) - findFiles(Paths.get(dir)); - } - } - - /** - * Replaces spaces in files with underscores - * - * @param path - * @return - * @throws IOException - */ - protected String fixSpaces(Path path) throws IOException { - if (!path.getFileName().toString().contains(" ")) - return path.toString(); - - String noSpaces = path.getFileName().toString().replaceAll(" ", "_"); - - Boolean ret = FileUtil.renameFile(path, noSpaces); - if (ret == null) - return "null"; - - if (ret && PackConverter.DEBUG) { - Logger.log(" Renamed: " + path.getFileName().toString() + "->" + noSpaces); - return path.getParent() + File.separator + noSpaces; - } else if (!ret) { - System.err.println(" Failed to rename: " + path.getFileName().toString() + "->" + noSpaces); - return path.getParent() + File.separator + noSpaces; - } - - return null; - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/BlockStateConverter.java similarity index 68% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/BlockStateConverter.java index 8fe1965c..916be0d7 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/BlockStateConverter.java @@ -1,4 +1,4 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.other; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; @@ -6,6 +6,7 @@ import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -15,35 +16,44 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; +import java.util.stream.Stream; public class BlockStateConverter extends Converter { + private final int from; + private final int to; private boolean anyChanges; - private int from, to; public BlockStateConverter(PackConverter packConverter, int from, int to) { super(packConverter); - this.anyChanges = false; this.from = from; this.to = to; + this.anyChanges = false; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; } /** * Updates blockstates in blockstates folder - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { Path states = pack.getWorkingPath().resolve("assets/minecraft/blockstates".replace("/", File.separator)); - if (!states.toFile().exists()) + if (!states.toFile().exists()) { return; - Files.list(states) - .filter(file -> file.toString().endsWith(".json")) - .forEach(file -> { - try { - JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + } + Logger.addTab(); + try (Stream pathStream = Files.list(states).filter(file -> file.toString().endsWith(".json"))) { + pathStream.forEach(file -> { + try { + JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + if (json != null) { // process multipart if (json.has("multipart")) { JsonElement multipart = json.get("multipart"); @@ -51,8 +61,9 @@ public void convert(Pack pack) throws IOException { JsonArray multipartArray = multipart.getAsJsonArray(); for (JsonElement element : multipartArray) { JsonObject multipartObject = element.getAsJsonObject(); - for (Map.Entry entry : multipartObject.entrySet()) + for (Map.Entry entry : multipartObject.entrySet()) { updateModelPath(entry); + } } } } @@ -75,38 +86,45 @@ public void convert(Pack pack) throws IOException { } // update model paths to prepend block - for (Map.Entry entry : variantsObject.entrySet()) + for (Map.Entry entry : variantsObject.entrySet()) { updateModelPath(entry); + } } } if (anyChanges) { JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + Logger.debug("Converted " + file.getFileName()); } - } catch (IOException e) { - Util.propagate(e); + } else { + Logger.log("Failed to convert: " + file.getFileName() + " as JSON is invalid."); } - }); + } catch (IOException e) { + Util.propagate(e); + } + }); + } + Logger.subTab(); } /** * Updates Model paths - * + * * @param entry */ private void updateModelPath(Map.Entry entry) { if (entry.getValue() instanceof JsonObject) { - JsonObject value = (JsonObject) entry.getValue(); - if (value.has("model")) + JsonObject value = entry.getValue().getAsJsonObject(); + if (value.has("model")) { updateModelObject(value); + } } else if (entry.getValue() instanceof JsonArray) { for (JsonElement jsonElement : ((JsonArray) entry.getValue())) { if (jsonElement instanceof JsonObject) { - JsonObject value = (JsonObject) jsonElement; - if (value.has("model")) + JsonObject value = jsonElement.getAsJsonObject(); + if (value.has("model")) { updateModelObject(value); + } } } } @@ -117,47 +135,48 @@ private void updateModelObject(JsonObject value) { String[] split = value.get("model").getAsString().split("/"); String val = split[split.length - 1]; - String prefix = value.get("model").getAsString().substring(0, - value.get("model").getAsString().length() - val.length()); + String prefix = value.get("model").getAsString().substring(0, value.get("model").getAsString().length() - val.length()); - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.9") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.8.9") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.9")) { prefix = "block/" + prefix; anyChanges = true; } - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - val = nameConverter.getBlockMapping().remap(val); + val = nameConverter.getBlockMapping_1_13().remap(val); prefix = prefix.replaceAll("blocks", "block"); anyChanges = true; } - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.14") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.13.2") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - val = nameConverter.getNewBlockMapping().remap(val); + val = nameConverter.getBlockMapping_1_14().remap(val); anyChanges = true; } - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.17") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.16.5") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - val = nameConverter.getBlockMapping17().remap(val); + val = nameConverter.getBlockMapping_1_17().remap(val); anyChanges = true; } - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.19") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.18.2") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { - val = nameConverter.getBlockMapping19().remap(val); + val = nameConverter.getBlockMapping_1_19().remap(val); anyChanges = true; } - if (from < Util.getVersionProtocol(packConverter.getGson(), "1.19.3") + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.19.2") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { prefix = "minecraft:" + prefix; anyChanges = true; } - if (anyChanges) - value.addProperty("model", prefix + val); + String result = prefix + val; + if (anyChanges) { + value.addProperty("model", result); + } } } \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ModelConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ModelConverter.java new file mode 100644 index 00000000..f930faf5 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ModelConverter.java @@ -0,0 +1,584 @@ +package com.agentdid127.resourcepack.forwards.impl.other; + +import com.agentdid127.resourcepack.forwards.ForwardsPackConverter; +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; + +public class ModelConverter extends Converter { + private final int from; + private final int to; + protected String light = "none"; + + private Pack pack; + + public ModelConverter(PackConverter packConverter, String light, int from, int to) { + super(packConverter); + this.light = light; + this.from = from; + this.to = to; + pack = null; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + + /** + * Runs findfiles with the directory Models + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + this.pack = pack; + Path models = pack.getWorkingPath().resolve("assets/minecraft/models".replace("/", File.separator)); + if (models.toFile().exists()) { + Logger.addTab(); + findFiles(models); + Logger.subTab(); + } + } + + /** + * Recursively finds files with Path rootPath and runs remapModelJson + * + * @param rootPath + * @throws IOException + */ + protected void findFiles(Path rootPath) throws IOException { + File directory = rootPath.toFile(); + for (File file : Objects.requireNonNull(directory.listFiles())) { + Path filePath = file.toPath(); + if (!file.isDirectory()) { + remapModelJson(filePath.getParent(), filePath); + } else { + remapModelJsons(file.toPath()); + } + } + } + + /** + * Updates model Json to newer versions + * + * @param rootPath + * @throws IOException + */ + protected void remapModelJsons(Path rootPath) throws IOException { + if (rootPath.toFile().exists()) { + try (Stream pathStream = Files.list(rootPath).filter(path1 -> path1.toString().endsWith(".json"))) { + pathStream.forEach(modelPath -> { + try { + this.remapModelJson(rootPath, modelPath); + } catch (IOException e) { + Util.propagate(e); + } + }); + } + } + } + + protected void remapModelJson(Path rootPath, Path model) throws IOException { + if (!model.toFile().exists()) { + return; + } + + try { + JsonObject jsonObject; + if (JsonUtil.readJson(packConverter.getGson(), model) != null && JsonUtil.readJson(packConverter.getGson(), model).isJsonObject()) { + jsonObject = JsonUtil.readJson(packConverter.getGson(), model); + } else { + Logger.debug("Could not convert model: " + model.getFileName()); + Logger.addTab(); + if (JsonUtil.readJson(packConverter.getGson(), model) == null) { + Logger.debug("Check for Syntax Errors in file."); + } else { + Logger.debug("File is not JSON Object."); + } + Logger.subTab(); + return; + } + + // GUI light system for 1.15.2 + if (!light.equals("none") && (light.equals("front") || light.equals("side"))) { + Logger.log("Model did not have light, adding light '" + light + "'"); + jsonObject.addProperty("gui_light", light); + } + + if (jsonObject.has("textures") && jsonObject.get("textures").isJsonObject()) { + NameConverter nameConverter = packConverter.getConverter(NameConverter.class); + + JsonObject initialTextureObject = jsonObject.getAsJsonObject("textures"); + JsonObject textureObject = initialTextureObject.deepCopy(); + for (Map.Entry entry : initialTextureObject.entrySet()) { + String value = entry.getValue().getAsString(); + textureObject.remove(entry.getKey()); + + // 1.8 mappings + if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.9")) { + if (entry.getKey().equals("layer0") && (!value.startsWith(rootPath.getFileName().toString()) && !value.startsWith("minecraft:" + rootPath.getFileName()))) { + value = rootPath.getFileName() + "/" + value; + } + } + + // 1.13 Mappings + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + if (value.startsWith("blocks/")) { + value = "block/" + nameConverter.getBlockMapping_1_13().remap(value.substring("blocks/".length())).toLowerCase().replaceAll("[()]", ""); + } else if (value.startsWith("items/")) { + value = "item/" + nameConverter.getItemMapping_1_13().remap(value.substring("items/".length())).toLowerCase().replaceAll("[()]", ""); + } else { + value = value.toLowerCase().replaceAll("[()]", ""); + } + } + + // 1.14 Mappings + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.13.2") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + if (value.startsWith("block/")) { + value = "block/" + nameConverter.getBlockMapping_1_14().remap(value.substring("block/".length())); + } + + // Dyes + if (value.startsWith("item/") && value.contains("dye")) { + value = "item/" + nameConverter.getItemMapping_1_14().remap(value.substring("item/".length())); + } + } + + // 1.17 Mappings + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.16.5") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + if (value.startsWith("block/")) { + value = "block/" + nameConverter.getBlockMapping_1_17().remap(value.substring("block/".length())).toLowerCase().replaceAll("[()]", ""); + } else if (value.startsWith("item/")) { + value = "item/" + nameConverter.getItemMapping_1_17().remap(value.substring("item/".length())).toLowerCase().replaceAll("[()]", ""); + } + + value = value.toLowerCase().replaceAll("[()]", ""); + } + + // 1.19 Mappings + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.18.2") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { + if (value.startsWith("block/")) { + value = "block/" + nameConverter.getBlockMapping_1_19().remap(value.substring("block/".length())).toLowerCase().replaceAll("[()]", ""); + } + + value = value.toLowerCase().replaceAll("[()]", ""); + } + + // 1.19.3 Mappings + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.19.2") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { + if (!value.startsWith("minecraft:") && !value.startsWith("#")) { + value = "minecraft:" + value; + } + } + + if (!textureObject.has(entry.getKey())) { + textureObject.addProperty(entry.getKey(), value); + } + } + + jsonObject.remove("textures"); + jsonObject.add("textures", textureObject); + } + + // Fix Display Model For Packs (<= 1.8.9) + if (jsonObject.has("display") && + from <= Util.getVersionProtocol(packConverter.getGson(), "1.8") && + to >= Util.getVersionProtocol(packConverter.getGson(), "1.9")) { + JsonObject display = updateDisplay(packConverter.getGson(), jsonObject.remove("display").getAsJsonObject()); + jsonObject.add("display", display); + } + + if (jsonObject.has("overrides")) { + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getKey().equals("overrides")) { + JsonArray overrides = jsonObject.get("overrides").getAsJsonArray(); + JsonArray overridesArray = new JsonArray(); + for (int i = 0; i < overrides.size(); i++) { + JsonObject object = overrides.get(i).getAsJsonObject(); + for (Map.Entry json : object.entrySet()) { + if (json.getKey().equals("model")) { + object.addProperty(json.getKey(), json.getValue().getAsString().replaceAll("[()]", "")); + } else { + object.add(json.getKey(), json.getValue()); + } + } + overridesArray.add(object); + } + jsonObject.add(entry.getKey(), overridesArray); + } + } + } + + // Parent Stuff + if (jsonObject.has("parent")) { + // Change parent to lowercase + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getKey().equals("parent")) { + String parent = entry.getValue().getAsString().toLowerCase().replace(" ", "_"); + + // Get block/item parents renamed + if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + if (parent.startsWith("block/")) { + parent = setParent("block/", "/forwards/blocks.json", parent, "1_13"); + } else if (parent.startsWith("item/")) { + parent = setParent("item/", "/forwards/items.json", parent, "1_13"); + } + } + + if (from < Util.getVersionProtocol(packConverter.getGson(), "1.14") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + if (parent.startsWith("block/")) { + parent = setParent("block/", "/forwards/blocks.json", parent, "1_14"); + } else if (parent.startsWith("item/")) { + parent = setParent("item/", "/forwards/items.json", parent, "1_14"); + } + } + + if (from < Util.getVersionProtocol(packConverter.getGson(), "1.17") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + if (parent.startsWith("block/")) { + parent = setParent("block/", "/forwards/blocks.json", parent, "1_17"); + } else if (parent.startsWith("item/")) { + parent = setParent("item/", "/forwards/items.json", parent, "1_17"); + } + } + + if (from < Util.getVersionProtocol(packConverter.getGson(), "1.19") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { + if (parent.startsWith("block/")) { + parent = setParent("block/", "/forwards/blocks.json", parent, "1_19"); + } + } + + if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.19.3") + && from < Util.getVersionProtocol(packConverter.getGson(), "1.19.3")) { + if (!parent.startsWith("minecraft:")) { + parent = "minecraft:" + parent; + } + } + + jsonObject.addProperty(entry.getKey(), parent); + } + } + } + + + // New Override System for 1.21.4+ + if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.21.4") + && from < Util.getVersionProtocol(packConverter.getGson(), "1.21.4")) { + + // Check for new overrides + if (jsonObject.has("overrides")) { + JsonArray overridesArray = jsonObject.get("overrides").getAsJsonArray(); + + // Convert damages, and model data + if (isDamageModel(overridesArray)) { + createDamageModel(overridesArray, model); + } else if (isCustomModelData(overridesArray)) { + createCustomModelData(overridesArray, model); + } + + // remove old damages + jsonObject.remove("overrides"); + } + } + + + if (!JsonUtil.readJson(packConverter.getGson(), model).equals(jsonObject)) { + Logger.debug("Updating Model: " + model.getFileName()); + JsonUtil.writeJson(packConverter.getGson(), model, jsonObject); + } else { + Logger.debug("Skipping Model: " + model.getFileName()); + } + } catch (IOException e) { + throw Util.propagate(e); + } + } + + /** + * Creates a Damage-based item in the new directory for 1.21.4+ + * + * @param overrides overrides system from old model + * @param original original model file + * @throws IOException if we can't save the new file. + */ + private void createDamageModel(JsonArray overrides, Path original) throws IOException { + if (pack == null) return; + Path itemsPath = pack.getWorkingPath().resolve("assets/minecraft/items"); + if (!itemsPath.toFile().exists()) { + itemsPath.toFile().mkdirs(); + } + + JsonObject out = new JsonObject(); + JsonObject model = new JsonObject(); + model.addProperty("type", "range_dispatch"); + model.addProperty("property", "damage"); + + JsonObject fallback = new JsonObject(); + fallback.addProperty("type", "model"); + + fallback.addProperty("model", original.toFile().getAbsolutePath().replace(pack.getWorkingPath().resolve("assets/minecraft/models").toFile().getAbsolutePath() + "/", "").replace(".json", "")); + model.add("fallback", fallback); + + + // iterate through old override entries, and migrate to new file. + JsonArray entries = new JsonArray(); + for (JsonElement override : overrides) { + JsonObject entry = new JsonObject(); + if (override.isJsonObject()) { + double damage = override.getAsJsonObject().get("predicate").getAsJsonObject().get("damage").getAsDouble(); + String entryModel = override.getAsJsonObject().get("model").getAsString(); + + entry.addProperty("threshold", damage); + JsonObject modelEntry = new JsonObject(); + modelEntry.addProperty("model", entryModel); + modelEntry.addProperty("type", "model"); + entry.add("model", modelEntry); + entries.add(entry); + } + } + + model.add("entries", entries); + out.add("model", model); + + JsonUtil.writeJson(packConverter.getGson(), itemsPath.resolve(original.toFile().getName()), out); + } + + + /** + * Creates a Custom Model Data based item in the 1.21.4+ format. + * + * @param overrides old overrides + * @param original original model file path + * @throws IOException if we can't save the new file. + */ + private void createCustomModelData(JsonArray overrides, Path original) throws IOException { + if (pack == null) return; + Path itemsPath = pack.getWorkingPath().resolve("assets/minecraft/items"); + if (!itemsPath.toFile().exists()) { + itemsPath.toFile().mkdirs(); + } + + JsonObject out = new JsonObject(); + JsonObject model = new JsonObject(); + model.addProperty("type", "range_dispatch"); + model.addProperty("property", "custom_model_data"); + + JsonObject fallback = new JsonObject(); + fallback.addProperty("type", "model"); + fallback.addProperty("model", original.toFile().getAbsolutePath().replace(pack.getWorkingPath().resolve("assets/minecraft/models").toFile().getAbsolutePath() + "/", "").replace(".json", "")); + model.add("fallback", fallback); + + JsonArray entries = new JsonArray(); + for (JsonElement override : overrides) { + JsonObject entry = new JsonObject(); + if (override.isJsonObject()) { + long customModelData = override.getAsJsonObject().get("predicate").getAsJsonObject().get("custom_model_data").getAsLong(); + String entryModel = override.getAsJsonObject().get("model").getAsString(); + + entry.addProperty("threshold", customModelData); + JsonObject modelEntry = new JsonObject(); + modelEntry.addProperty("model", entryModel); + modelEntry.addProperty("type", "model"); + entry.add("model", modelEntry); + entries.add(entry); + } + } + model.add("entries", entries); + out.add("model", model); + + JsonUtil.writeJson(packConverter.getGson(), itemsPath.resolve(original.toFile().getName()), out); + } + + /** + * Checks if overrides is damage-based + * + * @param overrides old overrides model + * @return {@code true} if it is a damage-based model + */ + private boolean isDamageModel(JsonArray overrides) { + for (JsonElement override : overrides) { + if (override instanceof JsonObject && override.getAsJsonObject().has("predicate")) { + JsonObject predicate = override.getAsJsonObject().get("predicate").getAsJsonObject(); + if (predicate.has("damage") && !predicate.has("custom_model_data")) { + return true; + } + } + } + return false; + } + + /** + * Checks if overrides is CustomModelData-based + * + * @param overrides old overrides model + * @return {@code true} if it is a CustomModelData-based model + */ + private boolean isCustomModelData(JsonArray overrides) { + for (JsonElement override : overrides) { + if (override instanceof JsonObject && override.getAsJsonObject().has("predicate")) { + JsonObject predicate = override.getAsJsonObject().get("predicate").getAsJsonObject(); + if (!predicate.has("damage") && predicate.has("custom_model_data")) { + return true; + } + } + } + return false; + } + /** + * Gets parent object and sets a new one + * + * @param prefix prefix of file path + * @param path File path of json control + * @param parent Parent String + * @return New string with changed parent. + */ + protected String setParent(String prefix, String path, String parent, String item) { + String parentWithoutPrefix = parent.replace(prefix, ""); + JsonObject file = JsonUtil.readJsonResource(packConverter.getGson(), path).getAsJsonObject(item); + if (file == null) { + Logger.debug("Prefix Failed on: " + parent); + return parent; + } else { + return file.has(parentWithoutPrefix) ? prefix + file.get(parentWithoutPrefix).getAsString() : parent; + } + } + + protected static JsonObject updateDisplay(Gson gson, JsonObject display) { + JsonObject defaults = JsonUtil.readJsonResource(gson, "/forwards/display.json"); + if (display == null) { + return defaults.deepCopy(); + } + + // First Person + if (display.has("firstperson")) { + JsonObject firstPerson = display.remove("firstperson").getAsJsonObject(); + display.add("firstperson_righthand", updateDisplayFirstPerson(gson, firstPerson)); + } else if (!display.has("firstperson_righthand")) { + JsonObject rightHand = defaults.get("firstperson_righthand").getAsJsonObject().deepCopy(); + display.add("firstperson_righthand", rightHand); + } + + if (!display.has("firstperson_lefthand")) { + display.add("firstperson_lefthand", getLeftHand(display.get("firstperson_righthand").getAsJsonObject().deepCopy())); + } + + // Third Person + if (display.has("thirdperson")) { + JsonObject thirdPerson = display.remove("thirdperson").getAsJsonObject(); + display.add("thirdperson_righthand", updateDisplayThirdPerson(gson, thirdPerson)); + } else if (!display.has("thirdperson_righthand")) { + JsonObject rightHand = defaults.get("thirdperson_righthand").getAsJsonObject().deepCopy(); + display.add("thirdperson_righthand", rightHand); + } + + if (!display.has("thirdperson_lefthand")) { + display.add("thirdperson_lefthand", getLeftHand(display.get("thirdperson_righthand").getAsJsonObject().deepCopy())); + } + + if (!display.has("ground")) { + display.add("ground", defaults.get("ground").getAsJsonObject().deepCopy()); + } + + if (!display.has("head")) { + display.add("head", defaults.get("head").getAsJsonObject().deepCopy()); + } + + return display; + } + + private static JsonObject getLeftHand(JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray oldRotation = newObject.remove("rotation").getAsJsonArray(); + JsonArray rotation = new JsonArray(); + rotation.add(oldRotation.get(0).getAsNumber()); + rotation.add(0 - oldRotation.get(1).getAsDouble()); + rotation.add(0 - oldRotation.get(2).getAsDouble()); + newObject.add("rotation", rotation); + } + + return newObject; + } + + private static JsonObject updateDisplayFirstPerson(Gson gson, JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); + newObject.add("rotation", JsonUtil.add(rotation, JsonUtil.asArray(gson, "[0, 45, 0]"))); + } + + if (old.has("translation")) { + JsonArray translation = newObject.remove("translation").getAsJsonArray(); + newObject.add("translation", + JsonUtil.add( + JsonUtil.multiply( + JsonUtil.subtract( + translation, + JsonUtil.asArray(gson, "[0, 4, 2]")), + JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]")), + JsonUtil.asArray(gson, "[1.13, 3.2, 1.13]"))); + } + + if (old.has("scale")) { + JsonArray scale = newObject.remove("scale").getAsJsonArray(); + newObject.add("scale", JsonUtil.multiply(scale, JsonUtil.asArray(gson, "[0.4, 0.4, 0.4]"))); + } + + return newObject; + } + + private static JsonObject updateDisplayThirdPerson(Gson gson, JsonObject old) { + JsonObject newObject = old.deepCopy(); + if (old.has("rotation")) { + JsonArray rotation = newObject.remove("rotation").getAsJsonArray(); + newObject.add("rotation", + JsonUtil.add( + JsonUtil.multiply( + rotation, + JsonUtil.asArray(gson, "[1, -1, -1]")), + JsonUtil.asArray(gson, "[0, 0, 20]"))); + } + + if (old.has("translation")) { + JsonArray translation = newObject.remove("translation").getAsJsonArray(); + newObject.add("translation", + JsonUtil.add( + JsonUtil.multiply( + translation, + JsonUtil.asArray(gson, "[1, 1, -1]")), + JsonUtil.asArray(gson, "[0, 2.75, -3]"))); + } + + // For keeping order + if (old.has("scale")) { + JsonArray scale = newObject.remove("scale").getAsJsonArray(); + newObject.add("scale", scale); + } + + return newObject; + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/NameConverter.java similarity index 52% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/NameConverter.java index ea75a0f7..1e5c1d15 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/NameConverter.java @@ -1,4 +1,4 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.other; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; @@ -16,57 +16,74 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; public class NameConverter extends Converter { - private int to; - private int from; - - private final Mapping blockMapping; - private final Mapping newBlockMapping; - private final Mapping blockMapping17; - private final Mapping blockMapping19; - private final Mapping itemMapping; - private final Mapping newItemMapping; - private final Mapping itemMapping17; + private final int from; + private final int to; + private final Mapping entityMapping; - private final Mapping langMapping; - private final Mapping langMapping14; - private final Mapping blockMapping203; - private final Mapping itemMapping203; + + private final Mapping blockMapping_1_13; + private final Mapping itemMapping_1_13; + + private final Mapping blockMapping_1_14; + private final Mapping itemMapping_1_14; + + private final Mapping blockMapping_1_17; + private final Mapping itemMapping_1_17; + + private final Mapping blockMapping_1_19; + + private final Mapping blockMapping_1_20_3; + private final Mapping itemMapping1_20_3; public NameConverter(PackConverter packConverter, int from, int to) { super(packConverter); + this.from = from; this.to = to; + Gson gson = packConverter.getGson(); - blockMapping = new Mapping(gson, "blocks", "1_13", false); - newBlockMapping = new Mapping(gson, "blocks", "1_14", false); - blockMapping17 = new Mapping(gson, "blocks", "1_17", false); - blockMapping19 = new Mapping(gson, "blocks", "1_19", false); - itemMapping = new Mapping(gson, "items", "1_13", false); - newItemMapping = new Mapping(gson, "items", "1_14", false); - itemMapping17 = new Mapping(gson, "items", "1_17", false); entityMapping = new Mapping(gson, "entities", "*", false); - langMapping = new Mapping(gson, "lang", "1_13", false); - langMapping14 = new Mapping(gson, "lang", "1_14", false); - blockMapping203 = new Mapping(gson, "blocks", "1_20_3", false); - itemMapping203 = new Mapping(gson, "items", "1_20_3", false); + +// TODO/(Not used): langMapping_1_13 = new Mapping(gson, "lang", "1_13", false); + blockMapping_1_13 = new Mapping(gson, "blocks", "1_13", false); + itemMapping_1_13 = new Mapping(gson, "items", "1_13", false); + +// TODO/(Not used): langMapping_1_14 = new Mapping(gson, "lang", "1_14", false); + blockMapping_1_14 = new Mapping(gson, "blocks", "1_14", false); + itemMapping_1_14 = new Mapping(gson, "items", "1_14", false); + + blockMapping_1_17 = new Mapping(gson, "blocks", "1_17", false); + itemMapping_1_17 = new Mapping(gson, "items", "1_17", false); + + blockMapping_1_19 = new Mapping(gson, "blocks", "1_19", false); + + blockMapping_1_20_3 = new Mapping(gson, "blocks", "1_20_3", false); + itemMapping1_20_3 = new Mapping(gson, "items", "1_20_3", false); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; } /** * Fixes folder names and file names - * + * * @param pack Resource Pack * @throws IOException Error handler */ @Override public void convert(Pack pack) throws IOException { Path minecraftPath = pack.getWorkingPath().resolve("assets/minecraft".replace("/", File.separator)); + Logger.addTab(); // Less than 1.12 if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (PackConverter.DEBUG) - Logger.log("Finding files that are less than 1.12"); + Logger.debug("Finding files that are less than 1.12"); findFiles(minecraftPath); } @@ -74,17 +91,17 @@ public void convert(Pack pack) throws IOException { if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { // OptiFine conversion if (minecraftPath.resolve("mcpatcher").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("MCPatcher exists, switching to optifine"); + Logger.debug("MCPatcher exists, switching to optifine"); if (minecraftPath.resolve("optifine").toFile().exists()) { - if (PackConverter.DEBUG) - Logger.log("OptiFine exists, merging directories"); - FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), - minecraftPath.resolve("mcpatcher").toFile()); - } else + Logger.debug("OptiFine exists, merging directories"); + FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), minecraftPath.resolve("mcpatcher").toFile()); + } else { Files.move(minecraftPath.resolve("mcpatcher"), minecraftPath.resolve("optifine")); - if (minecraftPath.resolve("mcpatcher").toFile().exists()) + } + + if (minecraftPath.resolve("mcpatcher").toFile().exists()) { FileUtil.deleteDirectoryAndContents(minecraftPath.resolve("mcpatcher")); + } } // 1.13 Models @@ -92,102 +109,116 @@ public void convert(Pack pack) throws IOException { if (modelsPath.toFile().exists()) { // 1.13 block/item name change if (modelsPath.resolve("blocks").toFile().exists()) { - if (modelsPath.resolve("block").toFile().exists()) + if (modelsPath.resolve("block").toFile().exists()) { FileUtil.deleteDirectoryAndContents(modelsPath.resolve("block")); + } + Files.move(modelsPath.resolve("blocks"), modelsPath.resolve("block")); } // Update all blocks for 1.13 - renameAll(blockMapping, ".json", modelsPath.resolve("block")); + renameAll(blockMapping_1_13, ".json", modelsPath.resolve("block")); if (modelsPath.resolve("items").toFile().exists()) { - if (modelsPath.resolve("item").toFile().exists()) + if (modelsPath.resolve("item").toFile().exists()) { FileUtil.deleteDirectoryAndContents(modelsPath.resolve("item")); + } + Files.move(modelsPath.resolve("items"), modelsPath.resolve("item")); } // Update all items for 1.13 - renameAll(itemMapping, ".json", modelsPath.resolve("item")); + renameAll(itemMapping_1_13, ".json", modelsPath.resolve("item")); // Update 1.14 items if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { Path itemModelPath = modelsPath.resolve("iem"); - renameAll(newItemMapping, ".json", itemModelPath); + renameAll(itemMapping_1_14, ".json", itemModelPath); + if (!itemModelPath.resolve("ink_sac.json").toFile().exists() - && itemModelPath.resolve("black_dye.json").toFile().exists()) - Files.copy(itemModelPath.resolve("black_dye.json"), - itemModelPath.resolve("ink_sac.json")); + && itemModelPath.resolve("black_dye.json").toFile().exists()) { + Files.copy(itemModelPath.resolve("black_dye.json"), itemModelPath.resolve("ink_sac.json")); + } + if (!itemModelPath.resolve("cocoa_beans.json").toFile().exists() - && itemModelPath.resolve("brown_dye.json").toFile().exists()) - Files.copy(itemModelPath.resolve("brown_dye.json"), - itemModelPath.resolve("cocoa_beans.json")); + && itemModelPath.resolve("brown_dye.json").toFile().exists()) { + Files.copy(itemModelPath.resolve("brown_dye.json"), itemModelPath.resolve("cocoa_beans.json")); + } + if (!itemModelPath.resolve("bone_meal.json").toFile().exists() - && itemModelPath.resolve("white_dye.json").toFile().exists()) - Files.copy(itemModelPath.resolve("white_dye.json"), - itemModelPath.resolve("bone_meal.json")); + && itemModelPath.resolve("white_dye.json").toFile().exists()) { + Files.copy(itemModelPath.resolve("white_dye.json"), itemModelPath.resolve("bone_meal.json")); + } + if (!itemModelPath.resolve("lapis_lazuli.json").toFile().exists() - && itemModelPath.resolve("blue_dye.json").toFile().exists()) - Files.copy(itemModelPath.resolve("blue_dye.json"), - itemModelPath.resolve("lapis_lazuli.json")); + && itemModelPath.resolve("blue_dye.json").toFile().exists()) { + Files.copy(itemModelPath.resolve("blue_dye.json"), itemModelPath.resolve("lapis_lazuli.json")); + } } - if (to > Util.getVersionProtocol(packConverter.getGson(), "1.19")) - renameAll(blockMapping19, ".json", modelsPath.resolve("block")); + if (to > Util.getVersionProtocol(packConverter.getGson(), "1.19")) { + renameAll(blockMapping_1_19, ".json", modelsPath.resolve("block")); + } if (to > Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) { - renameAll(itemMapping203, ".json", modelsPath.resolve("item")); - renameAll(blockMapping203, ".json", modelsPath.resolve("block")); + renameAll(itemMapping1_20_3, ".json", modelsPath.resolve("item")); + renameAll(blockMapping_1_20_3, ".json", modelsPath.resolve("block")); } } // Update BlockStates - Path blockStates = pack.getWorkingPath() - .resolve("assets/minecraft/blockstates".replace("/", File.separator)); - if (blockStates.toFile().exists()) - renameAll(blockMapping, ".json", blockStates); + Path blockStates = pack.getWorkingPath().resolve("assets/minecraft/blockstates".replace("/", File.separator)); + if (blockStates.toFile().exists()) { + renameAll(blockMapping_1_13, ".json", blockStates); + } // Update textures Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); if (texturesPath.toFile().exists()) { Path blockPath = texturesPath.resolve("block"); - if (texturesPath.resolve("blocks").toFile().exists()) + if (texturesPath.resolve("blocks").toFile().exists()) { Files.move(texturesPath.resolve("blocks"), blockPath); + } if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - renameAll(blockMapping, ".png", blockPath); - renameAll(blockMapping, ".png.mcmeta", blockPath); + renameAll(blockMapping_1_13, ".png", blockPath); + renameAll(blockMapping_1_13, ".png.mcmeta", blockPath); } if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - renameAll(newBlockMapping, ".png", blockPath); - renameAll(newBlockMapping, ".png.mcmeta", blockPath); + renameAll(blockMapping_1_14, ".png", blockPath); + renameAll(blockMapping_1_14, ".png.mcmeta", blockPath); } Path itemPath = texturesPath.resolve("item"); if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - renameAll(itemMapping, ".png", itemPath); - renameAll(itemMapping, ".png.mcmeta", itemPath); + renameAll(itemMapping_1_13, ".png", itemPath); + renameAll(itemMapping_1_13, ".png.mcmeta", itemPath); } if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13.2")) { - renameAll(newItemMapping, ".png", itemPath); - renameAll(newItemMapping, ".png.mcmeta", itemPath); + renameAll(itemMapping_1_14, ".png", itemPath); + renameAll(itemMapping_1_14, ".png.mcmeta", itemPath); + Path itemModelPath = modelsPath.resolve("item"); if (!itemModelPath.resolve("ink_sac.png").toFile().exists() - && itemModelPath.resolve("black_dye.png").toFile().exists()) - Files.copy(itemModelPath.resolve("black_dye.png"), - itemModelPath.resolve("ink_sac.png")); + && itemModelPath.resolve("black_dye.png").toFile().exists()) { + Files.copy(itemModelPath.resolve("black_dye.png"), itemModelPath.resolve("ink_sac.png")); + } + if (!itemModelPath.resolve("cocoa_beans.png").toFile().exists() - && itemModelPath.resolve("brown_dye.png").toFile().exists()) - Files.copy(itemModelPath.resolve("brown_dye.png"), - itemModelPath.resolve("cocoa_beans.png")); + && itemModelPath.resolve("brown_dye.png").toFile().exists()) { + Files.copy(itemModelPath.resolve("brown_dye.png"), itemModelPath.resolve("cocoa_beans.png")); + } + if (!itemModelPath.resolve("bone_meal.png").toFile().exists() - && itemModelPath.resolve("white_dye.png").toFile().exists()) - Files.copy(itemModelPath.resolve("white_dye.png"), - itemModelPath.resolve("bone_meal.png")); + && itemModelPath.resolve("white_dye.png").toFile().exists()) { + Files.copy(itemModelPath.resolve("white_dye.png"), itemModelPath.resolve("bone_meal.png")); + } + if (!itemModelPath.resolve("lapis_lazuli.png").toFile().exists() - && itemModelPath.resolve("blue_dye.png").toFile().exists()) - Files.copy(itemModelPath.resolve("blue_dye.png"), - itemModelPath.resolve("lapis_lazuli.png")); + && itemModelPath.resolve("blue_dye.png").toFile().exists()) { + Files.copy(itemModelPath.resolve("blue_dye.png"), itemModelPath.resolve("lapis_lazuli.png")); + } } // Entities @@ -196,91 +227,113 @@ public void convert(Pack pack) throws IOException { // 1.16 Iron golems if (from < Util.getVersionProtocol(packConverter.getGson(), "1.15") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.15")) { - if (!entityPath.resolve("iron_golem").toFile().exists()) + if (!entityPath.resolve("iron_golem").toFile().exists()) { entityPath.resolve("iron_golem").toFile().mkdir(); - if (entityPath.resolve("iron_golem.png").toFile().exists()) - Files.move(entityPath.resolve("iron_golem.png"), entityPath - .resolve("iron_golem/iron_golem.png")); + } + + if (entityPath.resolve("iron_golem.png").toFile().exists()) { + Path newIronGolemPath = entityPath.resolve("iron_golem/iron_golem.png"); + if (newIronGolemPath.toFile().exists()) { + newIronGolemPath.toFile().delete(); + } + + Files.move(entityPath.resolve("iron_golem.png"), newIronGolemPath); + } } // 1.17 Squid if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.17") && from < Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - renameAll(blockMapping17, ".png", blockPath); - renameAll(itemMapping17, ".png", texturesPath.resolve("item")); - renameAll(blockMapping17, ".png", modelsPath.resolve("block")); - renameAll(itemMapping17, ".png", modelsPath.resolve("item")); - if (!entityPath.resolve("squid").toFile().exists()) - entityPath.resolve("squid").toFile().mkdir(); - if (entityPath.resolve("squid.png").toFile().exists()) - Files.move(entityPath.resolve("squid.png"), - entityPath.resolve("squid/squid.png")); + renameAll(blockMapping_1_17, ".png", blockPath); + renameAll(itemMapping_1_17, ".png", itemPath); + renameAll(blockMapping_1_17, ".png", modelsPath.resolve("block")); + renameAll(itemMapping_1_17, ".png", modelsPath.resolve("item")); + + Path squidFolderPath = entityPath.resolve("squid"); + if (!squidFolderPath.toFile().exists()) { + squidFolderPath.toFile().mkdir(); + } + + if (entityPath.resolve("squid.png").toFile().exists()) { + Path newSquidPath = squidFolderPath.resolve("squid.png"); + if (newSquidPath.toFile().exists()) { + newSquidPath.toFile().delete(); + } + + Files.move(entityPath.resolve("squid.png"), newSquidPath); + } } if (to >= Util.getVersionProtocol(packConverter.getGson(), "1.19") - && from < Util.getVersionProtocol(packConverter.getGson(), "1.19")) - renameAll(blockMapping19, ".png", blockPath); + && from < Util.getVersionProtocol(packConverter.getGson(), "1.19")) { + renameAll(blockMapping_1_19, ".png", blockPath); + } // 1.13 End Crystals if (entityPath.resolve("endercrystal").toFile().exists() - && !entityPath.resolve("end_crystal").toFile().exists()) - Files.move(entityPath.resolve("endercrystal"), entityPath.resolve("end_crystal")); + && !entityPath.resolve("end_crystal").toFile().exists()) { + Path newEnderCrystalPath = entityPath.resolve("end_crystal"); + if (newEnderCrystalPath.toFile().exists()) { + newEnderCrystalPath.toFile().delete(); + } + + Files.move(entityPath.resolve("endercrystal"), newEnderCrystalPath); + } + findEntityFiles(entityPath); } } + Logger.subTab(); } /** * Finds files in entity folder - * - * @param path - * @throws IOException + * + * @param path The input path + * @throws IOException The IO exception */ protected void findEntityFiles(Path path) throws IOException { - if (!path.toFile().exists()) - return; - File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (!file.isDirectory()) - continue; - renameAll(entityMapping, ".png", file.toPath()); - renameAll(entityMapping, ".png.mcmeta", file.toPath()); - findEntityFiles(file.toPath()); + if (path.toFile().exists()) { + File directory = path.toFile(); + for (File file : Objects.requireNonNull(directory.listFiles())) { + if (file.isDirectory()) { + renameAll(entityMapping, ".png", file.toPath()); + renameAll(entityMapping, ".png.mcmeta", file.toPath()); + findEntityFiles(file.toPath()); + } + } } } /** * Finds files in folders called - * - * @param path - * @throws IOException + * @param path The input path */ - protected void findFiles(Path path) throws IOException { + protected void findFiles(Path path) { if (path.toFile().exists()) { File directory = path.toFile(); - for (File file : directory.listFiles()) { + for (File file : Objects.requireNonNull(directory.listFiles())) { if (file.isDirectory()) { if (file.getName().equals("items")) { - if (PackConverter.DEBUG) - Logger.log("Found Items folder, renaming"); + Logger.debug("Found Items folder, renaming.."); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("items", "item")); } if (file.getName().equals("blocks")) { - if (PackConverter.DEBUG) - Logger.log("Found blocks folder, renaming"); + Logger.debug("Found blocks folder, renaming.."); FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("blocks", "block")); } findFiles(file.toPath()); } - if (file.getName().contains("(")) + if (file.getName().contains("(")) { FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("[()]", "")); + } - if (!file.getName().equals(file.getName().toLowerCase())) - if (PackConverter.DEBUG) - Logger.log("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + if (!file.getName().equals(file.getName().toLowerCase())) { + Logger.debug("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + } FileUtil.renameFile(path.resolve(file.getName()), file.getName().toLowerCase()); } @@ -289,11 +342,11 @@ protected void findFiles(Path path) throws IOException { /** * Renames folder - * - * @param mapping - * @param extension - * @param path - * @throws IOException + * + * @param mapping The input mapping + * @param extension The file extension + * @param path The input path + * @throws IOException The IO exception */ protected void renameAll(Mapping mapping, String extension, Path path) throws IOException { if (path.toFile().exists()) { @@ -302,17 +355,17 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO List grasses = Arrays.asList("grass", "tall_grass", "double_grass"); if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2")) { if ((path.endsWith("blockstates") || path.endsWith("textures/block"))) { - grasses.stream().forEach(name -> { + grasses.forEach(name -> { String newName = mapping.remap(name); - Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), - newName + extension); - if (ret == null) + Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), newName + extension); + if (ret == null) { return; - if (ret && PackConverter.DEBUG) { - Logger.log(" Renamed: " + name + extension + "->" + newName + extension); + } + + if (ret) { + Logger.debug("Renamed: " + name + extension + "->" + newName + extension); } else if (!ret) { - System.err.println( - " Failed to rename: " + name + extension + "->" + newName + extension); + Logger.log("Failed to rename: " + name + extension + "->" + newName + extension); } }); } @@ -321,72 +374,69 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO // remap snow jsons, but not images. if (from < Util.getVersionProtocol(packConverter.getGson(), "1.13") && to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - if (path.resolve("snow_layer.json").toFile().exists()) + if (path.resolve("snow_layer.json").toFile().exists()) { FileUtil.renameFile(path.resolve("snow_layer" + extension), "snow" + extension); + } } - Files.list(path).forEach(path1 -> { - if (!path1.toString().endsWith(extension)) - return; - - String baseName = path1.getFileName().toString().substring(0, - path1.getFileName().toString().length() - extension.length()); - - // skip the already renamed grass blocks - if (grasses.contains(baseName) - && (path.endsWith("blockstates") || path.endsWith("textures/block"))) - return; + try (Stream pathStream = Files.list(path).filter(path1 -> path1.toString().endsWith(extension))) { + pathStream.forEach(path1 -> { + String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); - String newName = mapping.remap(baseName); - if (newName != null && !newName.equals(baseName)) { - Boolean ret = FileUtil.renameFile(path1, newName + extension); - if (ret == null) + // skip the already renamed grass blocks + if (grasses.contains(baseName) && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { return; - if (ret && PackConverter.DEBUG) { - Logger.log( - " Renamed: " + path1.getFileName().toString() + "->" + newName + extension); - } else if (!ret) { - System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName - + extension); } - } - }); - } - } - public Mapping getBlockMapping() { - return blockMapping; - } + String remappedName = mapping.remap(baseName); + if (remappedName != null && !remappedName.equals(baseName)) { + Path newPath = path1.getParent().resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } + + Boolean renameSuccess = FileUtil.renameFile(path1, newPath); + if (renameSuccess == null) { + return; + } - public Mapping getItemMapping() { - return itemMapping; + if (renameSuccess) { + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.log("Failed to rename: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } + } + }); + } + } } - public Mapping getNewBlockMapping() { - return newBlockMapping; + public Mapping getBlockMapping_1_13() { + return blockMapping_1_13; } - public Mapping getNewItemMapping() { - return newItemMapping; + public Mapping getItemMapping_1_13() { + return itemMapping_1_13; } - public Mapping getItemMapping17() { - return itemMapping17; + public Mapping getBlockMapping_1_14() { + return blockMapping_1_14; } - public Mapping getBlockMapping17() { - return blockMapping17; + public Mapping getItemMapping_1_14() { + return itemMapping_1_14; } - public Mapping getBlockMapping19() { - return blockMapping19; + public Mapping getItemMapping_1_17() { + return itemMapping_1_17; } - public Mapping getLangMapping() { - return langMapping; + public Mapping getBlockMapping_1_17() { + return blockMapping_1_17; } - public Mapping getLangMapping14() { - return langMapping14; + public Mapping getBlockMapping_1_19() { + return blockMapping_1_19; } } \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/PackMetaConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/PackMetaConverter.java new file mode 100644 index 00000000..364a2dd3 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/PackMetaConverter.java @@ -0,0 +1,87 @@ +package com.agentdid127.resourcepack.forwards.impl.other; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.IOException; +import java.nio.file.Path; + +// Reference: https://minecraft.wiki/w/Pack_format +public class PackMetaConverter extends Converter { + private final int from; + private final int to; + + public PackMetaConverter(PackConverter packConverter, int from, int to) { + super(packConverter); + this.from = from; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + + /** + * Converts MCMeta to newer version + * + * @param pack The input pack + * @throws IOException The IO Exception + */ + @Override + public void convert(Pack pack) throws IOException { + Path file = pack.getWorkingPath().resolve("pack.mcmeta"); + if (!file.toFile().exists()) { + return; + } + + int versionInt = 4; + JsonObject versionObj = Util.getVersionObjectByProtocol(packConverter.getGson(), to); + if (versionObj != null) { + versionInt = versionObj.get("pack_format").getAsInt(); + } + + JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + if (json == null) { + json = new JsonObject(); + } + + { + JsonObject meta = json.getAsJsonObject("meta"); + if (meta == null) { + meta = new JsonObject(); + } + + meta.addProperty("game_version", Util.getVersionFromProtocol(packConverter.getGson(), to)); + json.add("meta", meta); + } + + { + JsonObject packObject = json.getAsJsonObject("pack"); + if (packObject == null) { + packObject = new JsonObject(); + } + + packObject.addProperty("pack_format", versionInt); + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.20.1") + && to >= Util.getVersionProtocol(packConverter.getGson(), "1.20.2")) { + JsonObject fromVersion = Util.getVersionObjectByProtocol(packConverter.getGson(), from); + JsonObject toVersion = Util.getVersionObjectByProtocol(packConverter.getGson(), to); + + JsonObject supportedFormats = new JsonObject(); + supportedFormats.addProperty("min_inclusive", fromVersion != null ? fromVersion.get("pack_format").getAsInt() : versionInt); + supportedFormats.addProperty("max_inclusive", toVersion != null ? toVersion.get("pack_format").getAsInt() : versionInt); + packObject.add("supported_formats", supportedFormats); + } + + json.add("pack", packObject); + } + + JsonUtil.writeJson(packConverter.getGson(), file, json); + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ParticleConverter1_18.java similarity index 57% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ParticleConverter1_18.java index 7f059231..a4514138 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/ParticleConverter1_18.java @@ -1,41 +1,48 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.other; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -public class ParticleConverter extends Converter { - // Set it up. - public ParticleConverter(PackConverter packConverter) { +public class ParticleConverter1_18 extends Converter { + public ParticleConverter1_18(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.17.1") && to >= Util.getVersionProtocol(gson, "1.18"); + } + @Override public void convert(Pack pack) throws IOException { - Path particlesFolderPath = pack.getWorkingPath() - .resolve("assets/minecraft/particles".replace("/", File.separator)); - if (!particlesFolderPath.toFile().exists()) + Path particlesFolderPath = pack.getWorkingPath().resolve("assets/minecraft/particles".replace("/", File.separator)); + if (!particlesFolderPath.toFile().exists()) { return; + } // Check if the two merged files exist. boolean barrier = false; boolean light = false; - - if (particlesFolderPath.resolve("barrier.json").toFile().exists()) + if (particlesFolderPath.resolve("barrier.json").toFile().exists()) { barrier = true; - else if (particlesFolderPath.resolve("light.json").toFile().exists()) + } else if (particlesFolderPath.resolve("light.json").toFile().exists()) { light = true; + } // Move around files depending on what exists or what doesn't exist. if (barrier) { Files.move(particlesFolderPath.resolve("barrier.json"), particlesFolderPath.resolve("block_marker.json")); - if (light) + if (light) { Files.delete(particlesFolderPath.resolve("light.json")); + } } else if (light) { Files.move(particlesFolderPath.resolve("light.json"), particlesFolderPath.resolve("block_marker.json")); } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/RemoveSpacesConverter1_11.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/RemoveSpacesConverter1_11.java new file mode 100644 index 00000000..0bc86095 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/RemoveSpacesConverter1_11.java @@ -0,0 +1,87 @@ +package com.agentdid127.resourcepack.forwards.impl.other; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.FileUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; + +public class RemoveSpacesConverter1_11 extends Converter { + public RemoveSpacesConverter1_11(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.10.2") && to >= Util.getVersionProtocol(gson, "1.11"); + } + + /** + * Runs findFiles + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path assets = pack.getWorkingPath().resolve("assets"); + if (assets.toFile().exists()) { + Logger.addTab(); + findFiles(assets); + Logger.subTab(); + } + } + + /** + * Recursively finds files to fix Spaces + * + * @param path + */ + protected void findFiles(Path path) { + if (path.toFile().exists()) { + File directory = path.toFile(); + for (File file : Objects.requireNonNull(directory.listFiles())) { + String dir = fixSpaces(file.toPath()); + if (file.isDirectory()) { + findFiles(Paths.get(dir)); + } + } + } + } + + /** + * Replaces spaces in files with underscores + * + * @param path + * @return + */ + protected String fixSpaces(Path path) { + if (!path.getFileName().toString().contains(" ")) { + return path.toString(); + } + + String noSpaces = path.getFileName().toString().replaceAll(" ", "_"); + Boolean ret = FileUtil.renameFile(path, noSpaces); + if (ret == null) { + return "null"; + } + + if (ret) { + Logger.debug("Renamed: " + path.getFileName().toString() + "->" + noSpaces); + return path.getParent() + File.separator + noSpaces; + } else if (!ret) { + Logger.log("Failed to rename: " + path.getFileName().toString() + "->" + noSpaces); + return path.getParent() + File.separator + noSpaces; + } + + return null; + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/InventoryConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/InventoryConverter.java new file mode 100644 index 00000000..6b8c717f --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/InventoryConverter.java @@ -0,0 +1,37 @@ +package com.agentdid127.resourcepack.forwards.impl.other.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class InventoryConverter extends Converter { + public InventoryConverter(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + + // TODO: Figure out what this is for/make ^ correct range + @Override + public void convert(Pack pack) throws IOException { + Path imagePath = pack.getWorkingPath().resolve("assets/minecraft/textures/gui/container/inventory.png".replace("/", File.separator)); + if (imagePath.toFile().exists()) { + int defaultW = 256, defaultH = 256; + ImageConverter image = new ImageConverter(defaultW, defaultH, imagePath); + image.newImage(defaultH, defaultW); + image.subImage(0, 0, 256, 256, 0, 0); + image.subImage(0, 166, 16, 198, 0, 198); + image.subImage(104, 166, 120, 198, 16, 198); + image.store(); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/ParticlesSlicerConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/ParticlesSlicerConverter.java new file mode 100644 index 00000000..1aae5750 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/other/textures/ParticlesSlicerConverter.java @@ -0,0 +1,86 @@ +package com.agentdid127.resourcepack.forwards.impl.other.textures; + +import com.agentdid127.resourcepack.forwards.impl.v1_20.textures.GuiSlicerConverter1_20_2; +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.*; +import com.agentdid127.resourcepack.library.utilities.slicing.Slice; +import com.agentdid127.resourcepack.library.utilities.slicing.Slicer; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class ParticlesSlicerConverter extends Converter { + private final int from; + private final int to; + + public ParticlesSlicerConverter(PackConverter packConverter, int from, int to) { + super(packConverter); + this.from = from; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return true; + } + + /** + * Slice particles.png into multiple for 1.14+ + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); + if (!texturesPath.toFile().exists()) { + return; + } + + Path particlePath = texturesPath.resolve("particle"); + if (!particlePath.toFile().exists()) { + return; + } + + Logger.addTab(); + + Gson gson = packConverter.getGson(); + JsonObject particlesJson = JsonUtil.readJsonResource(gson, "/forwards/particles.json", JsonObject.class); + assert particlesJson != null; + + Slice slice = Slice.parse(particlesJson); + if (to >= Util.getVersionProtocol(gson, "1.13")) { + if (from <= Util.getVersionProtocol(gson, "1.12.2")) { + Path particlesPath = particlePath.resolve(slice.getPath()); + if (particlesPath.toFile().exists()) { + Logger.debug("Detected 'particles.png' from versions before 1.13, converting to newer size.."); + ImageConverter converter = new ImageConverter(slice.getWidth(), slice.getHeight(), particlesPath); + converter.newImage(256, 256); + converter.subImage(0, 0, 128, 128, 0, 0); + converter.store(); + } + } + + slice.setWidth(256); + slice.setHeight(256); + } + + if (from <= Util.getVersionProtocol(gson, "1.13.2") && + to >= Util.getVersionProtocol(gson, "1.14")) { + Slicer.run(gson, slice, particlePath, new GuiSlicerConverter1_20_2.GuiPredicateRunnable(gson), from, false); + Path entityPath = texturesPath.resolve("entity"); + Path newFishingHookPath = entityPath.resolve("fishing_hook.png"); + if (newFishingHookPath.toFile().exists()) { + newFishingHookPath.toFile().delete(); + } + FileUtil.moveIfExists(particlePath.resolve("fishing_hook.png"), newFishingHookPath); + } + + Logger.subTab(); + } +} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.java deleted file mode 100644 index 6e8e4d09..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; -import com.agentdid127.resourcepack.library.utilities.Util; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class CompassConverter extends Converter { - private int to; - private Path items; - - public CompassConverter(PackConverter packConverter, int to) { - super(packConverter); - this.to = to; - } - - @Override - public void convert(Pack pack) throws IOException { - String itemsT = "items"; - if (to > Util.getVersionProtocol(packConverter.getGson(), "1.13")) - itemsT = "item"; - Path compassPath = pack.getWorkingPath() - .resolve(("assets/minecraft/textures/" + itemsT + "/compass.png").replace("/", File.separator)); - items = compassPath.getParent(); - if (compassPath.toFile().exists()) { - ImageConverter imageConverter = new ImageConverter(16, 512, compassPath); - if (!imageConverter.fileIsPowerOfTwo()) - return; - - for (int i = 0; i < 32; i++) { - int h = i * 16; - String it = String.valueOf(i); - if (i < 10) - it = "0" + it; - compass(0, h, 16, h + 16, "compass_" + it, imageConverter); - } - - if (items.resolve("compass.png.mcmeta").toFile().exists()) - Files.delete(items.resolve("compass.png.mcmeta")); - } - } - - private void compass(int x, int y, int x2, int y2, String name, ImageConverter imageConverter) throws IOException { - imageConverter.newImage(16, 16); - imageConverter.subImage(x, y, x2, y2, 0, 0); - imageConverter.store(items.resolve(name + ".png")); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.java deleted file mode 100644 index 5e1ae765..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; - -import java.awt.*; -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class EnchantConverter extends Converter { - public EnchantConverter(PackConverter packConverter) { - super(packConverter); - } - - @Override - public void convert(Pack pack) throws IOException { - Path itemGlintPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/misc/enchanted_item_glint.png".replace("/", File.separator)); - if (!itemGlintPath.toFile().exists()) - return; - ImageConverter imageConverter = new ImageConverter(64, 64, itemGlintPath); - if (!imageConverter.fileIsPowerOfTwo()) - return; - imageConverter.newImage(64, 64); - imageConverter.colorize(new Color(94, 9, 178, 120)); - imageConverter.store(); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/InventoryConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/InventoryConverter.java deleted file mode 100644 index 17b745ef..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/InventoryConverter.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class InventoryConverter extends Converter { - public InventoryConverter(PackConverter packConverter) { - super(packConverter); - } - - @Override - public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/gui/container/inventory.png".replace("/", File.separator)); - if (!imagePath.toFile().exists()) - return; - - int defaultW = 256, defaultH = 256; - ImageConverter image = new ImageConverter(defaultW, defaultH, imagePath); - - image.newImage(defaultH, defaultW); - image.subImage(0, 0, 256, 256, 0, 0); - image.subImage(0, 166, 16, 198, 0, 198); - image.subImage(104, 166, 120, 198, 16, 198); - - image.store(); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java deleted file mode 100644 index e35aba8f..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.Logger; -import com.agentdid127.resourcepack.library.utilities.Util; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -public class MapIconConverter extends Converter { - protected Map mapping = new HashMap<>(); - - public MapIconConverter(PackConverter packConverter) { - super(packConverter); - mapping.put(pack(0, 0), pack(0, 0)); - mapping.put(pack(8, 0), pack(8, 0)); - mapping.put(pack(16, 0), pack(16, 0)); - mapping.put(pack(24, 0), pack(24, 0)); - mapping.put(pack(0, 8), pack(32, 0)); - mapping.put(pack(8, 8), pack(40, 0)); - mapping.put(pack(16, 8), pack(48, 0)); - mapping.put(pack(24, 8), pack(56, 0)); - mapping.put(pack(0, 16), pack(64, 0)); - mapping.put(pack(8, 16), pack(72, 0)); - } - - /** - * Converts maps - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path mapIconsPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/map/map_icons.png".replace("/", File.separator)); - if (!mapIconsPath.toFile().exists()) - return; - - BufferedImage newImage = Util.readImageResource("/forwards/map_icons.png"); - if (newImage == null) - throw new NullPointerException(); - - Graphics2D g2d = (Graphics2D) newImage.getGraphics(); - - BufferedImage image = ImageIO.read(mapIconsPath.toFile()); - int scale = image.getWidth() / 32; - - for (int x = 0; x <= 32 - 8; x += 8) { - for (int y = 0; y <= 32 - 8; y += 8) { - Long mapped = mapping.get(pack(x, y)); - if (mapped == null) - continue; - - int newX = (int) (mapped >> 32); - int newY = (int) (long) mapped; - Logger.log(" Mapping " + x + "," + y + " to " + newX + "," + newY); - - g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, - null); - } - } - - ImageIO.write(newImage, "png", mapIconsPath.toFile()); - } - - protected long pack(int x, int y) { - return (((long) x) << 32) | (y & 0xffffffffL); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.java deleted file mode 100644 index e6aefc72..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slice; -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slicer; -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -public class MobEffectAtlasConverter extends Converter { - private int from; - - public MobEffectAtlasConverter(PackConverter packConverter, int from) { - super(packConverter); - this.from = from; - } - - /** - * Slices the mob effect images from inventory.png for 1.14+ - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) - return; - Path containerPath = texturesPath.resolve("gui/container".replace("/", File.separator)); - if (!containerPath.toFile().exists()) - return; - Path inventoryPath = containerPath.resolve("inventory.png"); - if (!inventoryPath.toFile().exists()) - return; - Gson gson = packConverter.getGson(); - JsonObject effectJson = JsonUtil.readJsonResource(gson, "/forwards/mob_effect.json", JsonObject.class); - Slice slice = Slice.parse(effectJson); - Slicer.runSlicer(gson, slice, texturesPath, SlicerConverter.PredicateRunnable.class, from, false); - } -} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.java deleted file mode 100644 index 955b6895..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; - -public class OffHandCreator extends Converter { - public OffHandCreator(PackConverter packConverter) { - super(packConverter); - } - - @Override - public void convert(Pack pack) throws IOException { - Path widgetsPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/gui/widgets.png".replace("/", File.separator)); - if (!widgetsPath.toFile().exists()) - return; - - ImageConverter converter = new ImageConverter(256, 256, widgetsPath); - - int hotbar_end_x = 182; - - int hotbar_offhand_width = 29; - int hotbar_offhand_height = 24; - - int hotbar_offhand_l_x = 24; - int hotbar_offhand_r_x = 60; - int hotbar_offhand_y = 22; - - int slice_w = 11; - int slice_h = 22; - - converter.fillEmpty(hotbar_offhand_l_x, hotbar_offhand_y, hotbar_offhand_width, hotbar_offhand_height); - converter.fillEmpty(hotbar_offhand_r_x, hotbar_offhand_y, hotbar_offhand_width, hotbar_offhand_height); - - // OffHand (Left) - converter.subImage( - 0, - 0, - slice_w, - slice_h, - hotbar_offhand_l_x, - hotbar_offhand_y + 1); - converter.subImage( - hotbar_end_x - slice_w, - 0, - hotbar_end_x, - slice_h, - hotbar_offhand_l_x + slice_w, - hotbar_offhand_y + 1); - - // OffHand (Right) - converter.subImage( - 0, - 0, - slice_w, - slice_h, - hotbar_offhand_r_x, - hotbar_offhand_y + 1); - converter.subImage( - hotbar_end_x - slice_w, - 0, - hotbar_end_x, - slice_h, - hotbar_offhand_r_x + slice_w, - hotbar_offhand_y + 1); - - converter.store(); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java deleted file mode 100644 index 65e2db17..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slice; -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slicer; -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.FileUtil; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class ParticleTextureConverter extends Converter { - int from, to; - - public ParticleTextureConverter(PackConverter packConverter, int from, int to) { - super(packConverter); - this.from = from; - this.to = to; - } - - /** - * Slice particles.png into multiple for 1.14+ - * - * @param pack - * @throws IOException - */ - @Override - public void convert(Pack pack) throws IOException { - Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) - return; - Path particlePath = texturesPath.resolve("particle"); - if (!particlePath.toFile().exists()) - return; - Gson gson = packConverter.getGson(); - JsonObject particlesJson = JsonUtil.readJsonResource(gson, "/forwards/particles.json", - JsonObject.class); - Slice slice = Slice.parse(particlesJson); - Slicer.runSlicer(gson, slice, particlePath, SlicerConverter.PredicateRunnable.class, from, false); - Path entityPath = texturesPath.resolve("entity"); - FileUtil.moveIfExists(particlePath.resolve("fishing_hook.png"), entityPath.resolve("fishing_hook.png")); - } -} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.java deleted file mode 100644 index b7e65a7a..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slice; -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slicer; -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Util; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -public class SlicerConverter extends Converter { - private int from; - - public SlicerConverter(PackConverter converter, int from) { - super(converter); - this.from = from; - } - - @Override - public void convert(Pack pack) throws IOException { - Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) - return; - Path guiPath = texturesPath.resolve("gui"); - if (!guiPath.toFile().exists()) - return; // No need to do any slicing. - Path spritesPath = guiPath.resolve("sprites"); - if (!spritesPath.toFile().exists()) - spritesPath.toFile().mkdirs(); - Gson gson = packConverter.getGson(); - JsonArray array = JsonUtil.readJsonResource(gson, "/forwards/gui.json", JsonArray.class); - Slice[] slices = Slice.parseArray(array); - for (Slice slice : slices) - Slicer.runSlicer(gson, slice, guiPath, PredicateRunnable.class, from, true); - } - - public static class PredicateRunnable { - public static boolean run(Gson gson, int from, JsonObject predicate) { - if (predicate.has("protocol")) { - JsonObject protocol = predicate.getAsJsonObject("protocol"); - - Integer min_inclusive = null; - Integer max_inclusive = null; - - JsonElement min_inclusive_prim = protocol.get("min_inclusive"); - if (min_inclusive_prim.isJsonPrimitive() && min_inclusive_prim.getAsJsonPrimitive().isNumber()) - min_inclusive = min_inclusive_prim.getAsInt(); - - if (min_inclusive == null) - min_inclusive = 4; // hardcoded bruh - - JsonElement max_inclusive_prim = protocol.get("max_inclusive"); - if (max_inclusive_prim.isJsonPrimitive() && max_inclusive_prim.getAsJsonPrimitive().isNumber()) - max_inclusive = max_inclusive_prim.getAsInt(); - - if (max_inclusive == null) - max_inclusive = Util.getLatestProtocol(gson); - - if (from < min_inclusive || from > max_inclusive) - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.java deleted file mode 100644 index 3fdb4f2d..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class WaterConverter extends Converter { - public WaterConverter(PackConverter packConverter) { - super(packConverter); - } - - @Override - public void convert(Pack pack) throws IOException { - Path blockFolder = pack.getWorkingPath() - .resolve("assets/minecraft/textures/block".replace("/", File.separator)); - if (!blockFolder.toFile().exists()) - return; - grayscale(blockFolder.resolve("water_flow.png"), 16, 1024); - grayscale(blockFolder.resolve("water_still.png"), 16, 512); - grayscale(blockFolder.resolve("water_overlay.png"), 16, 16); - } - - private void grayscale(Path path, int w, int h) throws IOException { - if (!path.toFile().exists()) - return; - ImageConverter imageConverter = new ImageConverter(w, h, path); - if (!imageConverter.fileIsPowerOfTwo()) - return; - imageConverter.newImage(w, h); - imageConverter.grayscale(); - imageConverter.store(); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java deleted file mode 100644 index a9505de0..00000000 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slicer.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.agentdid127.resourcepack.forwards.impl.textures.slicing; - -import java.io.IOException; -import java.lang.reflect.Method; -import java.nio.file.Path; - -import com.agentdid127.resourcepack.forwards.impl.textures.SlicerConverter; -import com.agentdid127.resourcepack.library.utilities.FileUtil; -import com.agentdid127.resourcepack.library.utilities.ImageConverter; -import com.agentdid127.resourcepack.library.utilities.JsonUtil; -import com.agentdid127.resourcepack.library.utilities.Logger; -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -public class Slicer { - public static void runSlicer(Gson gson, Slice slice, Path root, Class predicateClass, int from, - boolean texture_has_metadata) - throws IOException { - if (!SlicerConverter.PredicateRunnable.run(gson, from, slice.getPredicate())) - return; - - Path path = root.resolve(slice.getPath()); - if (!path.toFile().exists()) { - Logger.log("Texture '" + slice.getPath() + "' doesn't exist, ignoring..."); - return; - } - - ImageConverter converter = new ImageConverter(slice.getWidth(), slice.getHeight(), path); - - Path imagePath = path; - if (slice.getPathName() != null) - imagePath = path.resolveSibling(slice.getPathName()); - - for (Texture texture : slice.getTextures()) { - Path texturePath = root.resolve(texture.getPath()); - FileUtil.ensureParentExists(texturePath); - - if (predicateClass != null) { - // Hacky - try { - Method predicateTest = predicateClass.getMethod("run", Gson.class, int.class, JsonObject.class); - if (!((boolean) predicateTest.invoke(null, gson, from, texture.getPredicate()))) - continue; - } catch (Exception exception) { - Logger.log("Failed to get test predicate from predicate class"); - } - } - - try { - Box box = texture.getBox(); - - converter.saveSlice( - box.getX(), - box.getY(), - box.getWidth(), - box.getHeight(), - texturePath); - - if (texture.shouldRemove()) - converter.fillEmpty( - box.getX(), - box.getY(), - box.getWidth(), - box.getHeight()); - - if (texture_has_metadata) { - JsonObject metadata = texture.getMetadata(); - if (metadata.keySet().isEmpty() || metadata.entrySet().isEmpty()) - continue; - Path metadataPath = texturePath.resolveSibling(texturePath.getFileName() + ".mcmeta"); - JsonUtil.writeJson(gson, metadataPath, metadata); - } - } catch (Exception exception) { - Logger.log("Failed to slice texture '" + texture.getPath() + "' (error='" - + exception.getLocalizedMessage() + "')"); - Logger.log(" - box: (x=" + texture.getBox().getX() + ", y=" + texture.getBox().getY() + ", width=" - + texture.getBox().getWidth() + ", height=" + texture.getBox().getHeight() + ")"); - } - } - - if (!path.toFile().delete()) - Logger.log("Failed to remove '" + path.getFileName() + "' whilst slicing."); - - if (!slice.shouldDelete()) - converter.store(imagePath); - } -} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/AnimationConverter1_13.java similarity index 50% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/AnimationConverter1_13.java index eecc5e59..487276c9 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/AnimationConverter1_13.java @@ -1,4 +1,4 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.v1_13; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; @@ -6,6 +6,7 @@ import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -13,55 +14,63 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.stream.Stream; -public class AnimationConverter extends Converter { - public AnimationConverter(PackConverter packConverter) { +public class AnimationConverter1_13 extends Converter { + public AnimationConverter1_13(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.12.2") && to >= Util.getVersionProtocol(gson, "1.13"); + } + @Override public void convert(Pack pack) throws IOException { - fixAnimations(pack.getWorkingPath().resolve( - "assets/minecraft/textures/block".replace("/", File.separator))); - fixAnimations(pack.getWorkingPath().resolve( - "assets/minecraft/textures/item".replace("/", File.separator))); + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); + fixAnimations(texturesPath.resolve("block")); + fixAnimations(texturesPath.resolve("item")); } /** * Updates animated images to newer versions - * + * * @param animations * @throws IOException */ protected void fixAnimations(Path animations) throws IOException { if (!animations.toFile().exists()) return; - Files.list(animations) - .filter(file -> file.toString().endsWith(".png.mcmeta")) - .forEach(file -> { - try { - JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); - + Logger.addTab(); + try (Stream pathStream = Files.list(animations).filter(file -> file.toString().endsWith(".png.mcmeta"))) { + pathStream.forEach(file -> { + try { + JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + if (json != null) { boolean anyChanges = false; JsonElement animationElement = json.get("animation"); if (animationElement instanceof JsonObject) { - JsonObject animationObject = (JsonObject) animationElement; - + JsonObject animationObject = animationElement.getAsJsonObject(); // TODO: Confirm this doesn't break any packs animationObject.remove("width"); animationObject.remove("height"); - anyChanges = true; } if (anyChanges) { JsonUtil.writeJson(packConverter.getGson(), file, json); - if (PackConverter.DEBUG) - Logger.log(" Converted " + file.getFileName()); + Logger.debug("Converted " + file.getFileName()); } - } catch (IOException e) { - Util.propagate(e); + } else { + Logger.log("File: " + file.getFileName() + " is not a valid JSON file."); } - }); + } catch (IOException e) { + Logger.log("Failed to convert file: " + file.getFileName()); + Util.propagate(e); + } + }); + } + Logger.subTab(); } } \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/LangConverter1_13.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/LangConverter1_13.java new file mode 100644 index 00000000..e15bcbb6 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/LangConverter1_13.java @@ -0,0 +1,119 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_13; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.PropertiesEx; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Map; +import java.util.stream.Stream; + +public class LangConverter1_13 extends Converter { + private final int from; + private final int to; + + public LangConverter1_13(PackConverter packConverter, int from, int to) { + super(packConverter); + this.from = from; + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return to >= Util.getVersionProtocol(gson, "1.13"); + } + + /** + * Moves Lang (properties) to JSON + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path langPath = pack.getWorkingPath().resolve("assets/minecraft/lang".replace("/", File.separator)); + if (!langPath.toFile().exists()) { + return; + } + + Logger.addTab(); + ArrayList models = new ArrayList(); + try (Stream pathStream = Files.list(langPath).filter(path1 -> path1.toString().endsWith(".lang"))) { + pathStream.forEach(lang -> { + JsonObject out = new JsonObject(); + try (InputStream input = Files.newInputStream(Paths.get(lang.toString()))) { + PropertiesEx prop = new PropertiesEx(); + prop.load(input); + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12") + && ((to >= Util.getVersionProtocol(packConverter.getGson(), "1.13")) + && (to <= Util.getVersionProtocol(packConverter.getGson(), "1.13.2")))) { + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/lang.json").getAsJsonObject("1_13"); + Enumeration enums = (Enumeration) prop.propertyNames(); + while (enums.hasMoreElements()) { + String key = enums.nextElement(); + String value = prop.getProperty(key); + for (Map.Entry id2 : id.entrySet()) { + if (key.equals(id2.getKey())) + out.addProperty(id2.getValue().getAsString(), value); + else + out.addProperty(key, value); + } + } + } + + if (to > Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/lang.json").getAsJsonObject("1_14"); + Enumeration enums = (Enumeration) prop.propertyNames(); + while (enums.hasMoreElements()) { + String key = enums.nextElement(); + String value = prop.getProperty(key); + for (Map.Entry id2 : id.entrySet()) { + if (key.equals(id2.getKey())) + out.addProperty(id2.getValue().getAsString(), value); + else + out.addProperty(key, value); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + try { + int modelNoLang = lang.getFileName().toString().indexOf(".lang"); + String file2 = lang.getFileName().toString().substring(0, modelNoLang); + Logger.debug("Saving: " + file2 + ".json"); + Path outLangPath = pack.getWorkingPath().resolve(("assets/minecraft/lang/" + file2 + ".json").replace("/", File.separator)); + JsonUtil.writeJson(packConverter.getGson(), outLangPath, out); + + } catch (IOException e) { + e.printStackTrace(); + } + + models.add(lang.getFileName().toString()); + }); + } + + for (String model : models) { + Path langFilePath = pack.getWorkingPath().resolve(("assets/minecraft/lang/" + model).replace("/", File.separator)); + Logger.debug("Deleting: " + langFilePath); + Files.delete(langFilePath); + } + + Logger.subTab(); + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/MCPatcherConverter1_13.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/MCPatcherConverter1_13.java new file mode 100644 index 00000000..5d335793 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/MCPatcherConverter1_13.java @@ -0,0 +1,173 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_13; + +import com.agentdid127.resourcepack.forwards.impl.other.NameConverter; +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.PropertiesEx; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Stream; + +@Deprecated // will be removed when extensions are made +public class MCPatcherConverter1_13 extends Converter { + public MCPatcherConverter1_13(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.12.2") && to >= Util.getVersionProtocol(gson, "1.13"); + } + + /** + * Parent conversion for MCPatcher + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path optifinePath = pack.getWorkingPath().resolve("assets/minecraft/optifine".replace("/", File.separator)); + if (optifinePath.toFile().exists()) { + Logger.addTab(); + findFiles(optifinePath); + Logger.subTab(); + } + // remapModelJson(models.resolve("item")); + // remapModelJson(models.resolve("block")); + } + + /** + * Finds all files in the specified rootPath + * + * @param rootPath + * @throws IOException + */ + protected void findFiles(Path rootPath) throws IOException { + File directory = rootPath.toFile(); + for (File file : Objects.requireNonNull(directory.listFiles())) { + Path filePath = file.toPath(); + if (!file.isDirectory()) { + remapProperties(filePath); + } else { + remapPropertiesFolder(filePath); + } + } + } + + protected void remapPropertiesFolder(Path rootPath) throws IOException { + if (rootPath.toFile().exists()) { + try (Stream pathStream = Files.list(rootPath).filter(path1 -> path1.toString().endsWith(".properties"))) { + pathStream.forEach(propertyPath -> { + try { + this.remapProperties(propertyPath); + } catch (IOException e) { + Util.propagate(e); + } + }); + } + } + } + + /** + * Remaps properties to work in newer versions + * + * @param rootPath + * @param propertyPath + * @throws IOException + */ + protected void remapProperties(Path propertyPath) throws IOException { + if (!propertyPath.toFile().exists()) { + return; + } + + Path inputStreamPath = Paths.get(propertyPath.toString()); + try (InputStream input = Files.newInputStream(inputStreamPath)) { + Logger.debug("Updating:" + propertyPath.getFileName()); + + PropertiesEx prop = new PropertiesEx(); + prop.load(input); + + try (OutputStream output = Files.newOutputStream(inputStreamPath)) { + // updates textures + if (prop.containsKey("texture")) { + prop.setProperty("texture", replaceTextures(prop)); + } + + // Updates Item IDs + if (prop.containsKey("matchItems")) { + prop.setProperty("matchItems", updateID("matchItems", prop, "regular").replaceAll("\"", "")); + } + + if (prop.containsKey("items")) { + prop.setProperty("items", updateID("items", prop, "regular").replaceAll("\"", "")); + } + + if (prop.containsKey("matchBlocks")) { + prop.setProperty("matchBlocks", updateID("matchBlocks", prop, "regular").replaceAll("\"", "")); + } + + // Saves File + prop.store(output, ""); + } catch (IOException io) { + io.printStackTrace(); + } + } catch (IOException e) { + throw Util.propagate(e); + } + } + + /** + * Replaces texture paths with blocks and items + * + * @param prop + * @return + */ + protected String replaceTextures(PropertiesEx prop) { + NameConverter nameConverter = packConverter.getConverter(NameConverter.class); + String properties = prop.getProperty("texture"); + if (properties.startsWith("textures/blocks/")) { + properties = "textures/block/" + nameConverter.getBlockMapping_1_13(); + } else if (properties.startsWith("textures/items/")) { + properties = "textures/item/" + nameConverter.getItemMapping_1_13(); + } + return properties; + } + + /** + * Fixes item IDs and switches them from a numerical id to minecraft: something + * + * @param type + * @param prop + * @return + */ + protected String updateID(String type, PropertiesEx prop, String selection) { + JsonObject id = JsonUtil.readJsonResource(packConverter.getGson(), "/forwards/ids.json").get(selection).getAsJsonObject(); + String[] split = prop.getProperty(type).split(" "); + StringBuilder properties2 = new StringBuilder(" "); + for (int i = 0; i < split.length; i++) { + if (id.get(split[i]) != null) { + split[i] = "minecraft:" + id.get(split[i]).getAsString(); + } + } + Arrays.stream(split).forEach(item -> properties2.append(item).append(" ")); + // TODO/NOTE: Might of broken this? Originally returned properties2.toString() & ignored the line below + String output = properties2.substring(0, properties2.length() - 1); + prop.remove("metadata"); + return output; + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/SoundsConverter1_13.java similarity index 71% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/SoundsConverter1_13.java index 855d9d14..bbdd4584 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/SoundsConverter1_13.java @@ -1,4 +1,4 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.v1_13; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; @@ -6,37 +6,39 @@ import com.agentdid127.resourcepack.library.utilities.FileUtil; import com.agentdid127.resourcepack.library.utilities.JsonUtil; import com.agentdid127.resourcepack.library.utilities.Logger; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.*; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.Map; -public class SoundsConverter extends Converter { - public SoundsConverter(PackConverter packConverter) { +public class SoundsConverter1_13 extends Converter { + public SoundsConverter1_13(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.12.2") && to >= Util.getVersionProtocol(gson, "1.13"); + } + /** * Updates Sounds - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path soundsJsonPath = pack.getWorkingPath() - .resolve("assets/minecraft/sounds.json".replace("/", File.separator)); - if (!soundsJsonPath.toFile().exists()) + Path soundsJsonPath = pack.getWorkingPath().resolve("assets/minecraft/sounds.json".replace("/", File.separator)); + if (!soundsJsonPath.toFile().exists()) { return; + } JsonObject sounds = JsonUtil.readJson(packConverter.getGson(), soundsJsonPath); JsonObject newSoundsObject = new JsonObject(); - for (Map.Entry entry : sounds.entrySet()) { if (entry.getValue().isJsonObject()) { JsonObject soundObject = entry.getValue().getAsJsonObject(); @@ -46,24 +48,21 @@ public void convert(Pack pack) throws IOException { JsonArray newSoundsArray = new JsonArray(); for (JsonElement jsonElement : soundsArray) { String sound; - - if (jsonElement instanceof JsonObject) + if (jsonElement instanceof JsonObject) { sound = ((JsonObject) jsonElement).get("name").getAsString(); - else if (jsonElement instanceof JsonPrimitive) + } else if (jsonElement instanceof JsonPrimitive) { sound = jsonElement.getAsString(); - else - throw new IllegalArgumentException( - "Unknown element type: " + jsonElement.getClass().getSimpleName()); + } else { + throw new IllegalArgumentException("Unknown element type: " + jsonElement.getClass().getSimpleName()); + } - Path baseSoundsPath = pack.getWorkingPath() - .resolve("assets/minecraft/sounds".replace("/", File.separator)); + Path baseSoundsPath = pack.getWorkingPath().resolve("assets/minecraft/sounds".replace("/", File.separator)); Path path = baseSoundsPath.resolve(sound + ".ogg"); if (!FileUtil.fileExistsCorrectCasing(path)) { String rewrite = path.toFile().getCanonicalPath().substring( baseSoundsPath.toString().length() + 1, path.toFile().getCanonicalPath().length() - 4); - if (PackConverter.DEBUG) - Logger.log(" Rewriting Sound: '" + sound + "' -> '" + rewrite + "'"); + Logger.debug("Rewriting Sound: '" + sound + "' -> '" + rewrite + "'"); sound = rewrite; } else { sound = jsonElement.getAsString(); @@ -76,8 +75,9 @@ else if (jsonElement instanceof JsonPrimitive) if (jsonElement instanceof JsonObject) { ((JsonObject) jsonElement).addProperty("name", sound); newSound = jsonElement; - } else if (jsonElement instanceof JsonPrimitive) + } else if (jsonElement instanceof JsonPrimitive) { newSound = new JsonPrimitive(jsonElement.getAsString()); + } newSoundsArray.add(newSound); } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/MapIconConverter1_13.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/MapIconConverter1_13.java new file mode 100644 index 00000000..709ee387 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/MapIconConverter1_13.java @@ -0,0 +1,77 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_13.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +public class MapIconConverter1_13 extends Converter { + private static final Map MAPPING = new HashMap<>(); + + static { + MAPPING.put(pack(0, 0), pack(0, 0)); + MAPPING.put(pack(8, 0), pack(8, 0)); + MAPPING.put(pack(16, 0), pack(16, 0)); + MAPPING.put(pack(24, 0), pack(24, 0)); + MAPPING.put(pack(0, 8), pack(32, 0)); + MAPPING.put(pack(8, 8), pack(40, 0)); + MAPPING.put(pack(16, 8), pack(48, 0)); + MAPPING.put(pack(24, 8), pack(56, 0)); + MAPPING.put(pack(0, 16), pack(64, 0)); + MAPPING.put(pack(8, 16), pack(72, 0)); + } + + public MapIconConverter1_13(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.12.2") && to >= Util.getVersionProtocol(gson, "1.13"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path mapIconsPath = pack.getWorkingPath().resolve("assets/minecraft/textures/map/map_icons.png".replace("/", File.separator)); + if (!mapIconsPath.toFile().exists()) { + return; + } + + BufferedImage newImage = Util.readImageResource("/forwards/map_icons.png"); + if (newImage == null) { + throw new NullPointerException(); + } + + Graphics2D g2d = (Graphics2D) newImage.getGraphics(); + BufferedImage image = ImageIO.read(mapIconsPath.toFile()); + int scale = image.getWidth() / 32; + for (int x = 0; x <= 32 - 8; x += 8) { + for (int y = 0; y <= 32 - 8; y += 8) { + Long mapped = MAPPING.get(pack(x, y)); + if (mapped != null) { + int newX = (int) (mapped >> 32); + int newY = (int) (long) mapped; + Logger.debug("Mapping " + x + "," + y + " to " + newX + "," + newY); + g2d.drawImage(image.getSubimage(x * scale, y * scale, 8 * scale, 8 * scale), newX * scale, newY * scale, null); + } + } + } + + ImageIO.write(newImage, "png", mapIconsPath.toFile()); + } + + private static long pack(int x, int y) { + return (((long) x) << 32) | (y & 0xffffffffL); + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/WaterConverter1_13.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/WaterConverter1_13.java new file mode 100644 index 00000000..1d4917f2 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_13/textures/WaterConverter1_13.java @@ -0,0 +1,44 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_13.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class WaterConverter1_13 extends Converter { + public WaterConverter1_13(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.12.2") && to >= Util.getVersionProtocol(gson, "1.13"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path blockFolder = pack.getWorkingPath().resolve("assets/minecraft/textures/block".replace("/", File.separator)); + if (blockFolder.toFile().exists()) { + grayscale(blockFolder.resolve("water_flow.png"), 16, 1024); + grayscale(blockFolder.resolve("water_still.png"), 16, 512); + grayscale(blockFolder.resolve("water_overlay.png"), 16, 16); + } + } + + private void grayscale(Path path, int w, int h) throws IOException { + if (path.toFile().exists()) { + ImageConverter imageConverter = new ImageConverter(w, h, path); + if (imageConverter.fileIsPowerOfTwo()) { + imageConverter.newImage(w, h); + imageConverter.grayscale(); + imageConverter.store(); + } + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/MobEffectAtlasSlicerConverter1_14.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/MobEffectAtlasSlicerConverter1_14.java new file mode 100644 index 00000000..2de236f6 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/MobEffectAtlasSlicerConverter1_14.java @@ -0,0 +1,51 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_14.textures; + +import com.agentdid127.resourcepack.forwards.impl.v1_20.textures.GuiSlicerConverter1_20_2; +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.agentdid127.resourcepack.library.utilities.slicing.Slice; +import com.agentdid127.resourcepack.library.utilities.slicing.Slicer; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class MobEffectAtlasSlicerConverter1_14 extends Converter { + private final int from; + + public MobEffectAtlasSlicerConverter1_14(PackConverter packConverter, int from) { + super(packConverter); + this.from = from; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.13.2") && to >= Util.getVersionProtocol(gson, "1.14"); + } + + /** + * Slices the mob effect images from inventory.png for 1.14+ + * + * @param pack + * @throws IOException + */ + @Override + public void convert(Pack pack) throws IOException { + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); + Path inventoryPath = texturesPath.resolve("gui/container/inventory.png".replace("/", File.separator)); + if (!inventoryPath.toFile().exists()) { + return; + } + + Gson gson = packConverter.getGson(); + JsonObject effectJson = JsonUtil.readJsonResource(gson, "/forwards/mob_effect.json", JsonObject.class); + if (effectJson != null) { + Slicer.run(gson, Slice.parse(effectJson), texturesPath, new GuiSlicerConverter1_20_2.GuiPredicateRunnable(gson), from, false); + } + } +} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/PaintingSlicerConverter1_14_4.java similarity index 52% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/PaintingSlicerConverter1_14_4.java index dcb5d670..ceeb600f 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_14/textures/PaintingSlicerConverter1_14_4.java @@ -1,11 +1,12 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; +package com.agentdid127.resourcepack.forwards.impl.v1_14.textures; -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slice; -import com.agentdid127.resourcepack.forwards.impl.textures.slicing.Slicer; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.agentdid127.resourcepack.library.utilities.slicing.Slice; +import com.agentdid127.resourcepack.library.utilities.slicing.Slicer; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -13,30 +14,35 @@ import java.io.IOException; import java.nio.file.Path; -public class PaintingConverter extends Converter { - public PaintingConverter(PackConverter packConverter) { +public class PaintingSlicerConverter1_14_4 extends Converter { + public PaintingSlicerConverter1_14_4(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.13") && to >= Util.getVersionProtocol(gson, "1.14.4"); + } + /** * Slices the paintings_kristoffer_zetterstrand painting image into multiple for * 1.14+ - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) - return; Path paintingPath = texturesPath.resolve("painting"); - if (!paintingPath.toFile().exists()) + if (!paintingPath.toFile().exists()) { return; + } + Gson gson = packConverter.getGson(); - JsonObject effectJson = JsonUtil.readJsonResource(gson, "/forwards/paintings_kristoffer_zetterstrand.json", - JsonObject.class); - Slice slice = Slice.parse(effectJson); - Slicer.runSlicer(gson, slice, paintingPath, null, -1, false); + JsonObject effectJson = JsonUtil.readJsonResource(gson, "/forwards/paintings_kristoffer_zetterstrand.json", JsonObject.class); + if (effectJson != null) { + Slicer.run(gson, Slice.parse(effectJson), paintingPath, (from, predicate) -> true, -1, false); + } } } \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/ChestConverter1_15.java similarity index 81% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/ChestConverter1_15.java index 467bd605..4fa1d055 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/ChestConverter1_15.java @@ -1,31 +1,39 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; +package com.agentdid127.resourcepack.forwards.impl.v1_15.textures; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import java.io.File; import java.io.IOException; import java.nio.file.Path; -public class ChestConverter extends Converter { - public ChestConverter(PackConverter packConverter) { +public class ChestConverter1_15 extends Converter { + public ChestConverter1_15(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.14.4") && to >= Util.getVersionProtocol(gson, "1.15"); + } + /** * Fixes Chest Textures in 1.15 Remaps textures, and updates images - * + * * @param pack * @throws IOException */ @Override public void convert(Pack pack) throws IOException { - Path imagePath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/entity/chest".replace("/", File.separator)); - if (!imagePath.toFile().exists()) + Path imagePath = pack.getWorkingPath().resolve("assets/minecraft/textures/entity/chest".replace("/", File.separator)); + if (!imagePath.toFile().exists()) { return; + } // Double chest doubleChest(imagePath, "normal"); @@ -41,19 +49,21 @@ public void convert(Pack pack) throws IOException { /** * Fixes Normal chests - * + * * @param imagePath * @param name * @throws IOException */ private void chest(Path imagePath, String name) throws IOException { - if (!imagePath.resolve(name + ".png").toFile().exists()) + if (!imagePath.resolve(name + ".png").toFile().exists()) { return; + } int defaultW = 64, defaultH = 64; ImageConverter normal = new ImageConverter(defaultW, defaultH, imagePath.resolve(name + ".png")); - if (!normal.fileIsPowerOfTwo()) + if (!normal.fileIsPowerOfTwo()) { return; + } // Create a new Image normal.newImage(defaultW, defaultH); @@ -85,18 +95,21 @@ private void chest(Path imagePath, String name) throws IOException { /** * Splits Double Chests into 2 images - * + * * @param imagePath * @param name * @throws IOException */ private void doubleChest(Path imagePath, String name) throws IOException { int defaultW = 128, defaultH = 64; - if (!imagePath.resolve(name + "_double.png").toFile().exists()) + if (!imagePath.resolve(name + "_double.png").toFile().exists()) { return; + } + ImageConverter normal = new ImageConverter(defaultW, defaultH, imagePath.resolve(name + "_double.png")); - if (!normal.fileIsPowerOfTwo()) + if (!normal.fileIsPowerOfTwo()) { return; + } // Left Side // Body @@ -142,6 +155,8 @@ private void doubleChest(Path imagePath, String name) throws IOException { normal.subImage(1, 1, 2, 5, 3, 1, true); normal.store(imagePath.resolve(name + "_right.png")); - imagePath.resolve(name + "_double.png").toFile().delete(); + if (!imagePath.resolve(name + "_double.png").toFile().delete()) { + Logger.log("Failed to remove chest \"_double.png\"."); + } } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/EnchantConverter1_15.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/EnchantConverter1_15.java new file mode 100644 index 00000000..d2e42892 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_15/textures/EnchantConverter1_15.java @@ -0,0 +1,38 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_15.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.awt.*; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class EnchantConverter1_15 extends Converter { + public EnchantConverter1_15(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.14.4") && to >= Util.getVersionProtocol(gson, "1.15"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path itemGlintPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc/enchanted_item_glint.png".replace("/", File.separator)); + if (!itemGlintPath.toFile().exists()) { + return; + } + + ImageConverter imageConverter = new ImageConverter(64, 64, itemGlintPath); + if (imageConverter.fileIsPowerOfTwo()) { + imageConverter.colorizeGrayscale(new Color(128, 64, 204)); + imageConverter.store(); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/AtlasConverter1_19_3.java similarity index 57% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/AtlasConverter1_19_3.java index 44d1ecb0..940294ed 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/AtlasConverter1_19_3.java @@ -1,35 +1,45 @@ -package com.agentdid127.resourcepack.forwards.impl; +package com.agentdid127.resourcepack.forwards.impl.v1_19; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; -public class AtlasConverter extends Converter { +public class AtlasConverter1_19_3 extends Converter { JsonObject out = new JsonObject(); JsonArray sources = new JsonArray(); - public AtlasConverter(PackConverter packConverter) { + public AtlasConverter1_19_3(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.19.2") && to >= Util.getVersionProtocol(gson, "1.19.3"); + } + @Override public void convert(Pack pack) throws IOException { Path atlasesPath = pack.getWorkingPath().resolve("assets/minecraft/atlases".replace("/", File.separator)); - if (!atlasesPath.toFile().exists()) + if (!atlasesPath.toFile().exists()) { atlasesPath.toFile().mkdirs(); + } Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (texturesPath.toFile().exists()) { - for (File file : texturesPath.toFile().listFiles()) { + File texturesFile = texturesPath.toFile(); + if (texturesFile.exists()) { + for (File file : Objects.requireNonNull(texturesFile.listFiles())) { JsonObject source = new JsonObject(); if (file.isDirectory()) { source.addProperty("type", "directory"); @@ -48,7 +58,7 @@ public void convert(Pack pack) throws IOException { File output = atlasesPath.resolve("blocks.json").toFile(); if (!output.exists()) { - OutputStream os = new FileOutputStream(output); + OutputStream os = Files.newOutputStream(output.toPath()); os.write(packConverter.getGson().toJson(out).getBytes(StandardCharsets.UTF_8)); os.close(); } @@ -57,18 +67,18 @@ public void convert(Pack pack) throws IOException { public void findFiles(Path directory, String prefix) { File directoryFile = directory.toFile(); - if (!directoryFile.isDirectory()) - return; - for (File file : directoryFile.listFiles()) { - JsonObject source = new JsonObject(); - if (!file.isDirectory()) - continue; - source.addProperty("type", "directory"); - source.addProperty("source", prefix + "/" + file.getName()); - source.addProperty("prefix", prefix + "/" + file.getName() + "/"); - sources.add(source); - String nextPrefix = prefix + "/" + file.getName(); - findFiles(directory.resolve(file.getName()), nextPrefix); + if (directoryFile.isDirectory()) { + for (File file : Objects.requireNonNull(directoryFile.listFiles())) { + JsonObject source = new JsonObject(); + if (file.isDirectory()) { + source.addProperty("type", "directory"); + source.addProperty("source", prefix + "/" + file.getName()); + source.addProperty("prefix", prefix + "/" + file.getName() + "/"); + sources.add(source); + String nextPrefix = prefix + "/" + file.getName(); + findFiles(directory.resolve(file.getName()), nextPrefix); + } + } } } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/EnchantPathConverter1_19_4.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/EnchantPathConverter1_19_4.java new file mode 100644 index 00000000..479bcca5 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/EnchantPathConverter1_19_4.java @@ -0,0 +1,41 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_19; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class EnchantPathConverter1_19_4 extends Converter { + public EnchantPathConverter1_19_4(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.19.3") && to >= Util.getVersionProtocol(gson, "1.19.4"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path miscPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc".replace("/", File.separator)); + Path enchantedItemGlintPath = miscPath.resolve("enchanted_item_glint.png"); + if (miscPath.resolve("enchanted_item_glint.png").toFile().exists()) { + Files.copy(enchantedItemGlintPath, miscPath.resolve("enchanted_glint_entity.png")); + Files.copy(enchantedItemGlintPath, miscPath.resolve("enchanted_glint_item.png")); + Files.delete(enchantedItemGlintPath); + } + + Path enchantedItemGlintMetaPath = miscPath.resolve("enchanted_item_glint.png.mcmeta"); + if (enchantedItemGlintMetaPath.toFile().exists()) { + Files.copy(enchantedItemGlintMetaPath, miscPath.resolve("enchanted_glint_entity.png.mcmeta")); + Files.copy(enchantedItemGlintMetaPath, miscPath.resolve("enchanted_glint_item.png.mcmeta")); + Files.delete(enchantedItemGlintMetaPath); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/CreativeTabsConverter1_19_3.java similarity index 54% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/CreativeTabsConverter1_19_3.java index 58156f0e..455f04a7 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/CreativeTabsConverter1_19_3.java @@ -1,34 +1,41 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; +package com.agentdid127.resourcepack.forwards.impl.v1_19.textures; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; -public class CreativeTabsConverter extends Converter { - private static int old_tab_width = 28; - private static int new_tab_width = 26; - private static int old_half = old_tab_width / 2; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class CreativeTabsConverter1_19_3 extends Converter { + private static final int OLD_TAB_WIDTH = 28; + private static final int NEW_TAB_WIDTH = 26; + private static final int OLD_TAB_HALF = OLD_TAB_WIDTH / 2; - public CreativeTabsConverter(PackConverter converter) { + public CreativeTabsConverter1_19_3(PackConverter converter) { super(converter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.19.2") && to >= Util.getVersionProtocol(gson, "1.19.3"); + } + @Override public void convert(Pack pack) throws IOException { - Path guiPath = pack.getWorkingPath().resolve( - "assets/minecraft/textures/gui".replace("/", File.separator)); - if (!guiPath.toFile().exists()) + Path guiPath = pack.getWorkingPath().resolve("assets/minecraft/textures/gui".replace("/", File.separator)); + if (!guiPath.toFile().exists()) { return; + } - Path tabsImage = guiPath - .resolve("container/creative_inventory/tabs.png".replace("/", File.separator)); - if (!tabsImage.toFile().exists()) + Path tabsImage = guiPath.resolve("container/creative_inventory/tabs.png".replace("/", File.separator)); + if (!tabsImage.toFile().exists()) { return; + } int originalWidth = 256; int originalHeight = 256; @@ -60,25 +67,24 @@ public void convert(Pack pack) throws IOException { converter.store(); } - private static void copy_tab(ImageConverter converter, int index, int original_index, int left_padding, - int right_padding) { - int first_tab_start_x = original_index * old_tab_width; - int first_tab_start_end_x = (original_index * old_tab_width) + old_tab_width; + private static void copy_tab(ImageConverter converter, int index, int original_index, int left_padding, int right_padding) { + int first_tab_start_x = original_index * OLD_TAB_WIDTH; + int first_tab_start_end_x = (original_index * OLD_TAB_WIDTH) + OLD_TAB_WIDTH; converter.subImage( first_tab_start_x, 0, - first_tab_start_end_x - old_half, + first_tab_start_end_x - OLD_TAB_HALF, 160, - (index * new_tab_width) + left_padding, + (index * NEW_TAB_WIDTH) + left_padding, 0); converter.subImage( - first_tab_start_x + old_half, + first_tab_start_x + OLD_TAB_HALF, 0, first_tab_start_end_x, 160, - ((index * new_tab_width) + (old_half - right_padding)), + ((index * NEW_TAB_WIDTH) + (OLD_TAB_HALF - right_padding)), 0); } } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/WidgetSlidersCreator1_19_4.java similarity index 62% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/WidgetSlidersCreator1_19_4.java index 3c5f2d0a..e84e7979 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_19/textures/WidgetSlidersCreator1_19_4.java @@ -1,33 +1,39 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; +package com.agentdid127.resourcepack.forwards.impl.v1_19.textures; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; -public class SlidersCreator extends Converter { - public SlidersCreator(PackConverter packConverter) { +public class WidgetSlidersCreator1_19_4 extends Converter { + public WidgetSlidersCreator1_19_4(PackConverter packConverter) { super(packConverter); } + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.19.3") && to >= Util.getVersionProtocol(gson, "1.19.4"); + } + @Override public void convert(Pack pack) throws IOException { - Path widgetsPath = pack.getWorkingPath() - .resolve("assets/minecraft/textures/gui/widgets.png".replace("/", File.separator)); - if (!widgetsPath.toFile().exists()) + Path widgetsPath = pack.getWorkingPath().resolve("assets/minecraft/textures/gui/widgets.png".replace("/", File.separator)); + if (!widgetsPath.toFile().exists()) { return; + } ImageConverter converter = new ImageConverter(256, 256, widgetsPath); - int button_width = 200; - int button_height = 20; - converter.newImage(256, 256); + int button_width = 200; + int button_height = 20; int y = 46; converter.subImageSized(0, y, button_width, button_height, 0, 0); converter.subImageSized(0, y, button_width, button_height, 0, button_height); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ImageFormatConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/ImageFormatConverter1_20_3.java similarity index 67% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ImageFormatConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/ImageFormatConverter1_20_3.java index b28b4912..ee8347ea 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/ImageFormatConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/ImageFormatConverter1_20_3.java @@ -1,84 +1,92 @@ -package com.agentdid127.resourcepack.forwards.impl; - -import com.agentdid127.resourcepack.library.Converter; -import com.agentdid127.resourcepack.library.PackConverter; -import com.agentdid127.resourcepack.library.pack.Pack; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -/** - * Converts Images to only use PNG formats. - */ -public class ImageFormatConverter extends Converter { - - private static final String[] types = new String[]{"jpg", "jpeg", "raw", "ico", "bmp"}; - - /** - * constructs a new ImageFormatConverter. - * - * @param packConverter Base PackConverter. - */ - public ImageFormatConverter(PackConverter packConverter) { - super(packConverter); - } - - /** - * Converts other types of images to PNG images. - * @param pack Pack to convert - * @throws IOException if the images are of an invalid type, or do not exist. - */ - @Override - public void convert(Pack pack) throws IOException { - // All textures in the game - Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - - // check for invalid images for all of these types. - for (String type : types) { - findImage(texturesPath, type); - } - - } - - /** - * Recursively finds files with {@link Path} path and remaps the files. - * - * @param path Path of directory to find files in. - * @throws IOException If the images are not found, or the remapping fails. - */ - protected void findImage(Path path, String type) throws IOException { - File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - findImage(file.toPath(), type); - else { - if (file.getName().endsWith(type)) { - remapFile(file, type); - } - } - } - } - - /** - * Remaps a single image to a PNG format. - * - * @param file File to remap. - * @param oldFormat Original format of the file. - * @throws IOException If the formatting fails. - */ - protected void remapFile(File file, String oldFormat) throws IOException { - // Get the image - BufferedImage image = ImageIO.read(file); - - // Output the correct format - ImageIO.write(image, "png", new File(file.getAbsolutePath().replaceAll(oldFormat, "png"))); - - // Delete the old image. - if (file.exists()) { - file.delete(); - } - } -} +package com.agentdid127.resourcepack.forwards.impl.v1_20; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; + +/** + * Converts Images to only use PNG formats. + */ +public class ImageFormatConverter1_20_3 extends Converter { + private static final String[] types = new String[]{"jpg", "jpeg", "raw", "ico", "bmp"}; + + /** + * constructs a new ImageFormatConverter. + * + * @param packConverter Base PackConverter. + */ + public ImageFormatConverter1_20_3(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.20.2") && to >= Util.getVersionProtocol(gson, "1.20.3"); + } + + /** + * Converts other types of images to PNG images. + * @param pack Pack to convert + * @throws IOException if the images are of an invalid type, or do not exist. + */ + @Override + public void convert(Pack pack) throws IOException { + // All textures in the game + Path texturesPath = pack.getWorkingPath().resolve(Paths.get("assets", "minecraft", "textures")); + // check for invalid images for all of these types. + for (String type : types) { + findImage(texturesPath, type); + } + } + + /** + * Recursively finds files with {@link Path} path and remaps the files. + * + * @param path Path of directory to find files in. + * @throws IOException If the images are not found, or the remapping fails. + */ + protected void findImage(Path path, String type) throws IOException { + // Find all files in the directory + File directory = path.toFile(); + if (!directory.exists() || !directory.isDirectory()) { + return; + } + + // Check each file + for (File file : Objects.requireNonNull(directory.listFiles())) { + if (file.isDirectory()) { + findImage(file.toPath(), type); + } else if (file.getName().endsWith(type)) { + remapFile(file, type); + } + } + } + + /** + * Remaps a single image to a PNG format. + * + * @param file File to remap. + * @param oldFormat Original format of the file. + * @throws IOException If the formatting fails. + */ + protected void remapFile(File file, String oldFormat) throws IOException { + // Get the image + BufferedImage image = ImageIO.read(file); + // Output the correct format + ImageIO.write(image, "png", new File(file.getAbsolutePath().replaceAll(oldFormat, "png"))); + // Delete the old image. + if (file.exists()) { + file.delete(); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/GuiSlicerConverter1_20_2.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/GuiSlicerConverter1_20_2.java new file mode 100644 index 00000000..4fc687ef --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/GuiSlicerConverter1_20_2.java @@ -0,0 +1,97 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_20.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.agentdid127.resourcepack.library.utilities.slicing.PredicateRunnable; +import com.agentdid127.resourcepack.library.utilities.slicing.Slice; +import com.agentdid127.resourcepack.library.utilities.slicing.Slicer; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class GuiSlicerConverter1_20_2 extends Converter { + private final int from; + + public GuiSlicerConverter1_20_2(PackConverter converter, int from) { + super(converter); + this.from = from; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.20.1") && to >= Util.getVersionProtocol(gson, "1.20.2"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); + Path guiPath = texturesPath.resolve("gui"); + if (!guiPath.toFile().exists()) { + return; // No need to do any slicing. + } + + Path spritesPath = guiPath.resolve("sprites"); + if (!spritesPath.toFile().exists()) { + spritesPath.toFile().mkdirs(); + } + + Gson gson = packConverter.getGson(); + JsonArray array = JsonUtil.readJsonResource(gson, "/forwards/gui.json", JsonArray.class); + if (array != null) { + Logger.addTab(); + for (Slice slice : Slice.parseArray(array)) { + Slicer.run(gson, slice, guiPath, new GuiPredicateRunnable(gson), from, true); + } + Logger.subTab(); + } + } + + public static class GuiPredicateRunnable implements PredicateRunnable { + private final Gson gson; + + public GuiPredicateRunnable(Gson gson) { + this.gson = gson; + } + + @Override + public boolean run(int from, JsonObject predicate) { + if (predicate.has("protocol")) { + JsonObject protocol = predicate.getAsJsonObject("protocol"); + + Integer min_inclusive = null; + Integer max_inclusive = null; + + JsonElement min_inclusive_prim = protocol.get("min_inclusive"); + if (min_inclusive_prim.isJsonPrimitive() && min_inclusive_prim.getAsJsonPrimitive().isNumber()) { + min_inclusive = min_inclusive_prim.getAsInt(); + } + + if (min_inclusive == null) { + min_inclusive = 4; // hardcoded bruh + } + + JsonElement max_inclusive_prim = protocol.get("max_inclusive"); + if (max_inclusive_prim.isJsonPrimitive() && max_inclusive_prim.getAsJsonPrimitive().isNumber()) { + max_inclusive = max_inclusive_prim.getAsInt(); + } + + if (max_inclusive == null) { + max_inclusive = Util.getLatestProtocol(gson); + } + + return from >= min_inclusive && from <= max_inclusive; + } + + return true; + } + } +} \ No newline at end of file diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/MapIconSlicerConverter1_20_5.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/MapIconSlicerConverter1_20_5.java new file mode 100644 index 00000000..96e89d30 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/MapIconSlicerConverter1_20_5.java @@ -0,0 +1,44 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_20.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.agentdid127.resourcepack.library.utilities.slicing.Slice; +import com.agentdid127.resourcepack.library.utilities.slicing.Slicer; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class MapIconSlicerConverter1_20_5 extends Converter { + private final int from; + + public MapIconSlicerConverter1_20_5(PackConverter packConverter, int from) { + super(packConverter); + this.from = from; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.20.4") && to >= Util.getVersionProtocol(gson, "1.20.5"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); + Path mapIconsPath = texturesPath.resolve("map/".replace("/", File.separator)); + if (!mapIconsPath.toFile().exists()) { + return; + } + + Gson gson = packConverter.getGson(); + JsonObject mapIconsJson = JsonUtil.readJsonResource(gson, "/forwards/map_icons.json", JsonObject.class); + if (mapIconsJson != null) { + Slicer.run(gson, Slice.parse(mapIconsJson), mapIconsPath, new GuiSlicerConverter1_20_2.GuiPredicateRunnable(gson), from, false); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/TitleConverter1_20.java similarity index 64% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.java rename to conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/TitleConverter1_20.java index bf6812ef..b915965b 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_20/textures/TitleConverter1_20.java @@ -1,47 +1,44 @@ -package com.agentdid127.resourcepack.forwards.impl.textures; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; +package com.agentdid127.resourcepack.forwards.impl.v1_20.textures; import com.agentdid127.resourcepack.library.Converter; import com.agentdid127.resourcepack.library.PackConverter; import com.agentdid127.resourcepack.library.pack.Pack; import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; -public class TitleConverter extends Converter { - public TitleConverter(PackConverter packConverter) { +public class TitleConverter1_20 extends Converter { + public TitleConverter1_20(PackConverter packConverter) { super(packConverter); } - /** - * Updates Minecraft Title and Minecraft Realms Title - * - * @param pack - * @throws IOException - */ + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.19.4") && to >= Util.getVersionProtocol(gson, "1.20"); + } + @Override public void convert(Pack pack) throws IOException { Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures".replace("/", File.separator)); - if (!texturesPath.toFile().exists()) - return; - Path titleFolderPath = texturesPath.resolve("gui/title".replace("/", File.separator)); - if (!titleFolderPath.toFile().exists()) - return; - Path minecraftTilePath = titleFolderPath.resolve("minecraft.png"); - if (!minecraftTilePath.toFile().exists()) + if (!minecraftTilePath.toFile().exists()) { return; + } int oldWidth = 256, oldHeight = 256; ImageConverter image = new ImageConverter(oldWidth, oldHeight, minecraftTilePath); - if (!image.fileIsPowerOfTwo() || !image.isSquare()) + if (!image.fileIsPowerOfTwo() || !image.isSquare()) { return; + } // Normal Minecraft Title image.newImage(274, 64); - // TODO: fix bigger resolutions not aligning properly?? and having extra space + // TODO: Fix bigger resolutions not aligning properly?? and having extra space // to the right of the image image.subImage(0, 0, 156, 44); image.subImage(0, 45, 119, 90, 155, 0); diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/ArmorMoverConverter1_21_2.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/ArmorMoverConverter1_21_2.java new file mode 100644 index 00000000..b00f7b12 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/ArmorMoverConverter1_21_2.java @@ -0,0 +1,113 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_21.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class ArmorMoverConverter1_21_2 extends Converter { + private static final List MATERIALS = new ArrayList<>(); + + static { + MATERIALS.add("chainmail"); + MATERIALS.add("diamond"); + MATERIALS.add("gold"); + MATERIALS.add("iron"); + MATERIALS.add("leather"); + MATERIALS.add("netherite"); + } + + // TODO: Trims/Wolf Armor/Llama/Horse/Elytra + public ArmorMoverConverter1_21_2(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.21.1") && to >= Util.getVersionProtocol(gson, "1.21.2"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path texturesPath = pack.getWorkingPath().resolve("assets/minecraft/textures/".replace("/", File.separator)); + if (!texturesPath.toFile().exists()) { + return; + } + + Path modelsPath = texturesPath.resolve("models"); + if (!modelsPath.toFile().exists()) { + return; + } + + Path modelsArmorPath = modelsPath.resolve("armor"); + if (!modelsArmorPath.toFile().exists()) { + return; + } + + // Just incase a pack wants to be special and include both versions 💀 + Path equipmentFolderPath = texturesPath.resolve("entity/equipment".replace("/", File.separator)); + if (equipmentFolderPath.toFile().exists()) { + equipmentFolderPath.toFile().delete(); + } + equipmentFolderPath.toFile().mkdirs(); + + Path humanoidPath = equipmentFolderPath.resolve("humanoid"); + Path humanoidLeggingsPath = equipmentFolderPath.resolve("humanoid_leggings"); + for (String material : MATERIALS) { + moveArmorLayers(material, modelsArmorPath, humanoidPath, humanoidLeggingsPath); + if (material.equals("leather")) { + moveLeatherArmorFiles(modelsArmorPath, humanoidPath, humanoidLeggingsPath); + } + } + + // Special + Path turtleLayer1 = modelsArmorPath.resolve("turtle_layer_1.png"); + if (turtleLayer1.toFile().exists()) { + Files.move(turtleLayer1, humanoidPath.resolve("turtle_scute.png")); + } + + // Cleanup + modelsArmorPath.toFile().delete(); // TODO/NOTE: For some reason, it will only delete "models" folder if I delete this first + modelsPath.toFile().delete(); + } + + private Path ensureFolder(Path filePath, Path intendedFolderPath) { + if (filePath.toFile().exists() && !intendedFolderPath.toFile().exists()) { + intendedFolderPath.toFile().mkdirs(); + } + + return filePath; + } + + private void moveArmorLayers(String material, Path modelsArmorPath, Path humanoidPath, Path humanoidLeggingsPath) throws IOException { + Path layer1Path = ensureFolder(modelsArmorPath.resolve(material + "_layer_1.png"), humanoidPath); + if (layer1Path.toFile().exists()) { + Files.move(layer1Path, humanoidPath.resolve(material + ".png")); + } + + Path layer2Path = ensureFolder(modelsArmorPath.resolve(material + "_layer_2.png"), humanoidLeggingsPath); + if (layer2Path.toFile().exists()) { + Files.move(layer2Path, humanoidLeggingsPath.resolve(material + ".png")); + } + } + + private void moveLeatherArmorFiles(Path modelsArmorPath, Path humanoidPath, Path humanoidLeggingsPath) throws IOException { + Path overlayLayer1 = ensureFolder(modelsArmorPath.resolve("leather_layer_1_overlay.png"), humanoidPath); + if (overlayLayer1.toFile().exists()) { + Files.move(overlayLayer1, humanoidPath.resolve("leather_overlay.png")); + } + + Path overlayLayer2 = ensureFolder(modelsArmorPath.resolve("leather_layer_2_overlay.png"), humanoidLeggingsPath); + if (overlayLayer2.toFile().exists()) { + Files.move(overlayLayer2, humanoidLeggingsPath.resolve("leather_overlay.png")); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/PostEffectShadersConverter1_21_2.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/PostEffectShadersConverter1_21_2.java new file mode 100644 index 00000000..29857164 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_21/textures/PostEffectShadersConverter1_21_2.java @@ -0,0 +1,204 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_21.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.*; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +// https://www.minecraft.net/en-us/article/minecraft-java-edition-1-21-2 +public class PostEffectShadersConverter1_21_2 extends Converter { + private static final Map MAPPING = new HashMap<>(); + + static { + // TODO/NOTE: There could be more? + MAPPING.put("largeBlur", "large_blur"); + MAPPING.put("smallBlur", "small_blur"); + MAPPING.put("itemEntity", "item_entity"); + } + + public PostEffectShadersConverter1_21_2(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.21.1") && to >= Util.getVersionProtocol(gson, "1.21.2"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path minecraftPath = pack.getWorkingPath().resolve("assets/minecraft".replace("/", File.separator)); + Path shadersPath = minecraftPath.resolve("shaders"); + Path shadersPostPath = shadersPath.resolve("post"); + if (!shadersPostPath.toFile().exists()) { + return; + } + + Logger.addTab(); + Path postEffectPath = minecraftPath.resolve("post_effect"); + if (postEffectPath.toFile().exists()) { + // TODO: Should we merge directories? + postEffectPath.toFile().delete(); + } + Files.move(shadersPostPath, postEffectPath); + + // Update shaders + // TODO: Determind if legacy post-effect & ignore it/delete it + List postShaders = new ArrayList<>(); + try (Stream pathStream = Files.list(postEffectPath).filter(file -> file.toString().endsWith(".json"))) { + pathStream.forEach(file -> { + postShaders.add(file.getFileName()); + try { + JsonObject json = JsonUtil.readJson(packConverter.getGson(), file); + + // Targets + if (json.has("targets") && json.get("targets").isJsonArray()) { + JsonObject targets = new JsonObject(); + JsonArray targetElements = json.remove("targets").getAsJsonArray(); + for (JsonElement element : targetElements) { + if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) { + String target = element.getAsString(); + targets.add(MAPPING.getOrDefault(target, target), new JsonObject()); + } else if (element.isJsonObject()) { + JsonObject object = element.getAsJsonObject(); + if (object.has("name")) { + String name = object.remove("name").getAsString(); + targets.add(name, object); + } + } + } + json.add("targets", targets); + } + + // Passes + if (json.has("passes") && json.get("passes").isJsonArray()) { + JsonArray passes = new JsonArray(); + JsonArray passesElements = json.remove("passes").getAsJsonArray(); + for (JsonObject object : passesElements.asList().stream().map(JsonElement::getAsJsonObject).collect(Collectors.toList())) { + passes.add(this.remapPass(object)); + } + json.add("passes", passes); + } + + JsonUtil.writeJson(this.packConverter.getGson(), file, json); + Logger.debug("Saving updated post-effect shader \"" + file + "\""); + } catch (Exception e) { + Logger.debug("Failed to update post-effect shader \"" + file + "\": " + e.getMessage()); + } + }); + } catch (Exception e) { + Logger.debug("Failed to update shader, unable to open file stream."); + } + + Path shadersProgramPath = shadersPath.resolve("program"); + if (!shadersProgramPath.toFile().exists()) { + return; + } + + // TODO: Could be just all ".json" in "shaders/program"? + postShaders.forEach(postShaderPath -> { + Path programShaderPath = shadersProgramPath.resolve(postShaderPath); + if (!programShaderPath.toFile().exists()) { + return; + } + + if (!shadersPostPath.toFile().exists()) { + shadersPostPath.toFile().mkdirs(); + } + + try { + Logger.debug("Moved post-effect program shader \"" + programShaderPath + "\" to post folder."); + Files.move(programShaderPath, shadersPostPath.resolve(postShaderPath)); + } catch (IOException e) { + Logger.debug("Failed to move post-effect program shader to post folder."); + } + }); + + Logger.subTab(); + } + + private String fixNamespace(String name) { + // TODO: Fix for other namespaces + if (!name.startsWith("minecraft:")) { + return "minecraft:" + name; + } else { + return name; + } + } + + private JsonObject remapPass(JsonObject object) { + JsonObject pass = object.deepCopy(); + if (pass.has("name")) { + String name = pass.remove("name").getAsString(); + pass.addProperty("program", fixNamespace("post/" + name)); + } + + if (pass.has("intarget")) { + String inTarget = pass.remove("intarget").getAsString(); + JsonObject inputObject = new JsonObject(); + inputObject.addProperty("sampler_name", "In"); + inputObject.addProperty("target", fixNamespace(MAPPING.getOrDefault(inTarget, inTarget))); + if (pass.has("use_linear_filter") && pass.get("use_linear_filter").isJsonPrimitive()) { + inputObject.addProperty("bilinear", pass.remove("use_linear_filter").getAsBoolean()); + } + + JsonArray inputs = new JsonArray(); + inputs.add(inputObject); + pass.add("inputs", inputs); + } else if (pass.has("use_linear_filter")) { + pass.remove("use_linear_filter"); // TODO/NOTE: ? If this is a thing, idk what to do with it + } + + if (pass.has("auxtargets")) { + JsonArray inputs; + if (pass.has("inputs")) { + inputs = pass.remove("inputs").getAsJsonArray(); + } else { + inputs = new JsonArray(); + } + + JsonArray auxTargets = pass.remove("auxtargets").getAsJsonArray(); + for (JsonObject target : auxTargets.asList().stream().map(JsonElement::getAsJsonObject).collect(Collectors.toList())) { + JsonObject newTarget = target.deepCopy(); + if (newTarget.has("name")) { + String name = newTarget.remove("name").getAsString(); + newTarget.addProperty("sampler_name", name); + } + + if (newTarget.has("id")) { + String id = newTarget.remove("id").getAsString(); + if (id.endsWith(":depth")) { + newTarget.add("use_depth_buffer", new JsonPrimitive(true)); + id = id.substring(0, id.length() - ":depth".length()); + } + newTarget.addProperty("target", fixNamespace(MAPPING.getOrDefault(id, id))); + } + + inputs.add(newTarget); + } + + pass.add("inputs", inputs); + } + + if (pass.has("outtarget")) { + String outTarget = pass.remove("outtarget").getAsString(); + pass.addProperty("output", MAPPING.getOrDefault(outTarget, outTarget)); + } + + return pass; + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/CompassConverter1_9.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/CompassConverter1_9.java new file mode 100644 index 00000000..333e6c38 --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/CompassConverter1_9.java @@ -0,0 +1,57 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_9.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class CompassConverter1_9 extends Converter { + private final int to; + + public CompassConverter1_9(PackConverter packConverter, int to) { + super(packConverter); + this.to = to; + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.8.9") && to >= Util.getVersionProtocol(gson, "1.9"); + } + + @Override + public void convert(Pack pack) throws IOException { + String itemsT = to >= Util.getVersionProtocol(packConverter.getGson(), "1.13") ? "item" : "items"; + Path compassPath = pack.getWorkingPath().resolve(("assets/minecraft/textures/" + itemsT + "/compass.png").replace("/", File.separator)); + Path items = compassPath.getParent(); + if (compassPath.toFile().exists()) { + ImageConverter imageConverter = new ImageConverter(16, 512, compassPath); + if (imageConverter.fileIsPowerOfTwo()) { + for (int i = 0; i < 32; i++) { + int h = i * 16; + String it = String.valueOf(i); + if (i < 10) { + it = "0" + it; + } + compass(items, 0, h, 16, 16, "compass_" + it, imageConverter); + } + + if (items.resolve("compass.png.mcmeta").toFile().exists()) { + Files.delete(items.resolve("compass.png.mcmeta")); + } + } + } + } + + private void compass(Path items, int x, int y, int width, int height, String name, ImageConverter imageConverter) throws IOException { + imageConverter.newImage(16, 16); + imageConverter.subImage(x, y, x + width, y + height, 0, 0); + imageConverter.store(items.resolve(name + ".png")); + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/OffHandCreator1_9.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/OffHandCreator1_9.java new file mode 100644 index 00000000..ffee996a --- /dev/null +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/v1_9/textures/OffHandCreator1_9.java @@ -0,0 +1,78 @@ +package com.agentdid127.resourcepack.forwards.impl.v1_9.textures; + +import com.agentdid127.resourcepack.library.Converter; +import com.agentdid127.resourcepack.library.PackConverter; +import com.agentdid127.resourcepack.library.pack.Pack; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.Util; +import com.google.gson.Gson; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; + +public class OffHandCreator1_9 extends Converter { + public OffHandCreator1_9(PackConverter packConverter) { + super(packConverter); + } + + @Override + public boolean shouldConvert(Gson gson, int from, int to) { + return from <= Util.getVersionProtocol(gson, "1.8.9") && to >= Util.getVersionProtocol(gson, "1.9"); + } + + @Override + public void convert(Pack pack) throws IOException { + Path widgetsPath = pack.getWorkingPath().resolve("assets/minecraft/textures/gui/widgets.png".replace("/", File.separator)); + if (!widgetsPath.toFile().exists()) { + return; + } + + ImageConverter converter = new ImageConverter(256, 256, widgetsPath); + int hotbar_end_x = 182; + int hotbar_offhand_width = 29; + int hotbar_offhand_height = 24; + int hotbar_offhand_l_x = 24; + int hotbar_offhand_r_x = 60; + int hotbar_offhand_y = 22; + int slice_w = 11; + int slice_h = 22; + + converter.fillEmpty(hotbar_offhand_l_x, hotbar_offhand_y, hotbar_offhand_width, hotbar_offhand_height); + converter.fillEmpty(hotbar_offhand_r_x, hotbar_offhand_y, hotbar_offhand_width, hotbar_offhand_height); + + // OffHand (Left) + converter.subImage( + 0, + 0, + slice_w, + slice_h, + hotbar_offhand_l_x, + hotbar_offhand_y + 1); + converter.subImage( + hotbar_end_x - slice_w, + 0, + hotbar_end_x, + slice_h, + hotbar_offhand_l_x + slice_w, + hotbar_offhand_y + 1); + + // OffHand (Right) + converter.subImage( + 0, + 0, + slice_w, + slice_h, + hotbar_offhand_r_x, + hotbar_offhand_y + 1); + converter.subImage( + hotbar_end_x - slice_w, + 0, + hotbar_end_x, + slice_h, + hotbar_offhand_r_x + slice_w, + hotbar_offhand_y + 1); + + converter.store(); + } +} diff --git a/conversions/Forwards/src/main/resources/forwards/map_icons.json b/conversions/Forwards/src/main/resources/forwards/map_icons.json new file mode 100644 index 00000000..b37875f4 --- /dev/null +++ b/conversions/Forwards/src/main/resources/forwards/map_icons.json @@ -0,0 +1,347 @@ +{ + "path": "map_icons.png", + "width": 128, + "height": 128, + "delete": true, + "textures": [ + { + "path": "decorations/player.png", + "box": { + "x": 0, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/frame.png", + "box": { + "x": 8, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/red_marker.png", + "box": { + "x": 16, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/target_x.png", + "box": { + "x": 24, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/target_point.png", + "box": { + "x": 32, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/player_off_map.png", + "box": { + "x": 40, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/player_off_limits.png", + "box": { + "x": 48, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/woodland_mansion.png", + "box": { + "x": 56, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/ocean_monument.png", + "box": { + "x": 64, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/white_banner.png", + "box": { + "x": 72, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/orange_banner.png", + "box": { + "x": 80, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/magenta_banner.png", + "box": { + "x": 88, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/light_blue_banner.png", + "box": { + "x": 96, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/yellow_banner.png", + "box": { + "x": 104, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/lime_banner.png", + "box": { + "x": 112, + "y": 0, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/pink_banner.png", + "box": { + "x": 0, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/gray_banner.png", + "box": { + "x": 8, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/light_gray_banner.png", + "box": { + "x": 16, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/cyan_banner.png", + "box": { + "x": 24, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/purple_banner.png", + "box": { + "x": 32, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/blue_banner.png", + "box": { + "x": 40, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/brown_banner.png", + "box": { + "x": 48, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/green_banner.png", + "box": { + "x": 56, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/red_banner.png", + "box": { + "x": 64, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/black_banner.png", + "box": { + "x": 72, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/red_x.png", + "box": { + "x": 80, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/desert_village.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 88, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/plains_village.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 96, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/savanna_village.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 104, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/snowy_village.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 112, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/taiga_village.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 120, + "y": 8, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/jungle_temple.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 0, + "y": 16, + "width": 8, + "height": 8 + } + }, + { + "path": "decorations/swamp_hut.png", + "predicate": { + "protocol": { + "min_inclusive": 764, + "max_inclusive": null + } + }, + "box": { + "x": 8, + "y": 16, + "width": 8, + "height": 8 + } + } + ] +} \ No newline at end of file diff --git a/conversions/Forwards/src/main/resources/forwards/particles.json b/conversions/Forwards/src/main/resources/forwards/particles.json index 20a495ba..1dacc8e5 100644 --- a/conversions/Forwards/src/main/resources/forwards/particles.json +++ b/conversions/Forwards/src/main/resources/forwards/particles.json @@ -1,7 +1,7 @@ { "path": "particles.png", - "width": 256, - "height": 256, + "width": 128, + "height": 128, "delete": true, "textures": [ { diff --git a/conversions/pom.xml b/conversions/pom.xml index 6fde6fb2..de11e72a 100644 --- a/conversions/pom.xml +++ b/conversions/pom.xml @@ -5,7 +5,7 @@ ResourcePackConverter com.agentdid127 - 2.2.4 + 2.2.5 4.0.0 @@ -32,7 +32,7 @@ com.agentdid127.resourcepack Library - 2.2.4 + 2.2.5 diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.html index 50a4a9fe..74aa9069 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/AnimationConverter.html @@ -88,7 +88,7 @@

Class AnimationConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.AnimationConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.AnimationConverter1_13
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.html index 4fc130e1..1d52f0c1 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/AtlasConverter.html @@ -88,7 +88,7 @@

Class AtlasConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.AtlasConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_19.AtlasConverter1_19_3
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.html index 7d004f61..e1bda4e1 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/BlockStateConverter.html @@ -88,7 +88,7 @@

Class BlockStateConverter
java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.BlockStateConverter
+
com.agentdid127.resourcepack.forwards.impl.other.BlockStateConverter
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.html index e958b393..2311970a 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/EnchantPathConverter.html @@ -88,7 +88,7 @@

Class EnchantPathConverter<
java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.EnchantPathConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_19.EnchantPathConverter1_19_4
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/LangConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/LangConverter.html index 6d44176b..84051539 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/LangConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/LangConverter.html @@ -88,7 +88,7 @@

Class LangConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.LangConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.LangConverter1_13
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.html index 96266b98..e657bcbe 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/MCPatcherConverter.html @@ -88,7 +88,7 @@

Class MCPatcherConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.MCPatcherConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.MCPatcherConverter1_13
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/ModelConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/ModelConverter.html index efbdfb99..b77f5da4 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/ModelConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/ModelConverter.html @@ -88,7 +88,7 @@

Class ModelConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.ModelConverter
+
com.agentdid127.resourcepack.forwards.impl.other.ModelConverter
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/NameConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/NameConverter.html index 8cb3da8a..728d5c34 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/NameConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/NameConverter.html @@ -88,7 +88,7 @@

Class NameConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.NameConverter
+
com.agentdid127.resourcepack.forwards.impl.other.NameConverter
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.html index a1d7cd56..2a7ab6a8 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/PackMetaConverter.html @@ -88,7 +88,7 @@

Class PackMetaConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.PackMetaConverter
+
com.agentdid127.resourcepack.forwards.impl.other.PackMetaConverter
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.html index 046fe78b..c7254133 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/ParticleConverter.html @@ -88,7 +88,7 @@

Class ParticleConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.ParticleConverter
+
com.agentdid127.resourcepack.forwards.impl.other.ParticleConverter1_18
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.html index dc26b026..8a308bad 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/SoundsConverter.html @@ -88,7 +88,7 @@

Class SoundsConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.SoundsConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.SoundsConverter1_13
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.html index d031414c..1f9e0b0e 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/SpacesConverter.html @@ -88,7 +88,7 @@

Class SpacesConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.SpacesConverter
+
com.agentdid127.resourcepack.forwards.impl.other.RemoveSpacesConverter1_11
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.html index 43cc5c9c..f66b1671 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/ChestConverter.html @@ -88,7 +88,7 @@

Class ChestConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.ChestConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_15.textures.ChestConverter1_15
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.html index d51807b3..7ab0cc3f 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/CompassConverter.html @@ -88,7 +88,7 @@

Class CompassConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.CompassConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_9.textures.CompassConverter1_9
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.html index b1238660..37b1203d 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/CreativeTabsConverter.html @@ -88,7 +88,7 @@

Class CreativeTabsConverte
java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.CreativeTabsConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_19.textures.CreativeTabsConverter1_19_3
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.html index bd85dfd0..d60e3b26 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/EnchantConverter.html @@ -88,7 +88,7 @@

Class EnchantConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.EnchantConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_15.textures.EnchantConverter1_15
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.html index e867a45c..b34e3834 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/MapIconConverter.html @@ -88,7 +88,7 @@

Class MapIconConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.MapIconConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.textures.MapIconConverter1_13
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.html index d87ae89a..45124b97 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/MobEffectAtlasConverter.html @@ -88,7 +88,7 @@

Class MobEffectAtlasConv
java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.MobEffectAtlasConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_14.textures.MobEffectAtlasSlicerConverter1_14
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.html index 5e1ef2ff..6f37a0e9 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/OffHandCreator.html @@ -88,7 +88,7 @@

Class OffHandCreator

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.OffHandCreator
+
com.agentdid127.resourcepack.forwards.impl.v1_9.textures.OffHandCreator1_9
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.html index 5e66460b..cf2e2b60 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/PaintingConverter.html @@ -88,7 +88,7 @@

Class PaintingConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.PaintingConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_14.textures.PaintingSlicerConverter1_14_4
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.html index a582065d..fd7353ee 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/ParticleTextureConverter.html @@ -88,7 +88,7 @@

Class ParticleTextureCo
java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.ParticleTextureConverter
+
com.agentdid127.resourcepack.forwards.impl.textures.ParticlesSlicerConverter
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.PredicateRunnable.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.PredicateRunnable.html index 2ca31727..0b3459a9 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.PredicateRunnable.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.PredicateRunnable.html @@ -87,7 +87,7 @@

Class SlicerConverter.PredicateRunnable

java.lang.Object -
com.agentdid127.resourcepack.forwards.impl.textures.SlicerConverter.PredicateRunnable
+
com.agentdid127.resourcepack.forwards.impl.v1_20.textures.GuiSlicerConverter1_20_2.PredicateRunnable
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.html index e474c728..4a8b7bc0 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlicerConverter.html @@ -88,7 +88,7 @@

Class SlicerConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.SlicerConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_20.textures.GuiSlicerConverter1_20_2
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.html index 73f13916..19d70d30 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/SlidersCreator.html @@ -88,7 +88,7 @@

Class SlidersCreator

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.SlidersCreator
+
com.agentdid127.resourcepack.forwards.impl.v1_19.textures.WidgetSlidersCreator1_19_4
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.html index 5a40e163..5f209080 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/TitleConverter.html @@ -88,7 +88,7 @@

Class TitleConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.TitleConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_20.textures.TitleConverter1_20
diff --git a/docs/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.html b/docs/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.html index e868ae5e..80714d3e 100644 --- a/docs/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.html +++ b/docs/com/agentdid127/resourcepack/forwards/impl/textures/WaterConverter.html @@ -88,7 +88,7 @@

Class WaterConverter

java.lang.Object
com.agentdid127.resourcepack.library.Converter -
com.agentdid127.resourcepack.forwards.impl.textures.WaterConverter
+
com.agentdid127.resourcepack.forwards.impl.v1_13.textures.WaterConverter1_13
diff --git a/docs/deprecated-list.html b/docs/deprecated-list.html index 3892d3d6..51856e13 100644 --- a/docs/deprecated-list.html +++ b/docs/deprecated-list.html @@ -64,7 +64,7 @@

Contents

Description
- +
diff --git a/library/pom.xml b/library/pom.xml index bf0cfb9c..d49628c7 100644 --- a/library/pom.xml +++ b/library/pom.xml @@ -10,7 +10,7 @@ com.agentdid127 ResourcePackConverter - 2.2.4 + 2.2.5 ../pom.xml \ No newline at end of file diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/Converter.java b/library/src/main/java/com/agentdid127/resourcepack/library/Converter.java index b7a8d704..50e2a233 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/Converter.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/Converter.java @@ -1,6 +1,7 @@ package com.agentdid127.resourcepack.library; import com.agentdid127.resourcepack.library.pack.Pack; +import com.google.gson.Gson; import java.io.IOException; @@ -11,5 +12,7 @@ public Converter(PackConverter packConverter) { this.packConverter = packConverter; } + public abstract boolean shouldConvert(Gson gson, int from, int to); + public abstract void convert(Pack pack) throws IOException; } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java b/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java index 7bcebc1e..b392d2e6 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/PackConverter.java @@ -8,7 +8,6 @@ public abstract class PackConverter { protected final Map, Converter> converters = new LinkedHashMap<>(); protected Gson gson; - public static boolean DEBUG = true; /** * Registers Converter. diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java b/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java index 8dde31fa..938fcee0 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/pack/Pack.java @@ -24,16 +24,19 @@ public Handler createHandler() { /** * Checks the type of pack it is. - * + * * @param path Pack Path * @return The Pack information */ public static Pack parse(Path path) { - if (!path.toString().contains(CONVERTED_SUFFIX)) - if (path.toFile().isDirectory() && path.resolve("pack.mcmeta").toFile().exists()) + if (!path.toString().contains(CONVERTED_SUFFIX)) { + if (path.toFile().isDirectory() && path.resolve("pack.mcmeta").toFile().exists()) { return new Pack(path); - else if (path.toString().endsWith(".zip")) + } else if (path.toString().endsWith(".zip")) { return new ZipPack(path); + } + } + return null; } @@ -55,9 +58,7 @@ public String getFileName() { @Override public String toString() { - return "ResourcePack{" + - "path=" + path + - '}'; + return "ResourcePack{path=" + path + "}"; } public static class Handler { @@ -69,28 +70,27 @@ public Handler(Pack pack) { /** * Deletes existing conversions and sets up pack for conversion - * + * * @throws IOException Issues with conversion */ public void setup() throws IOException { if (pack.getWorkingPath().toFile().exists()) { - Logger.log(" Deleting existing conversion"); + Logger.log("Deleting existing conversion"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); } - Logger.log(" Copying existing pack"); + Logger.log("Copying existing pack"); FileUtil.copyDirectory(pack.getOriginalPath(), pack.getWorkingPath()); - bomRemover(pack.getWorkingPath()); } static void bomRemover(Path workingPath) throws IOException { - BomDetector bom = new BomDetector( - workingPath.toString(), - ".txt", ".json", ".mcmeta", ".properties", ".lang"); + BomDetector bom = new BomDetector(workingPath.toString(), ".txt", ".json", ".mcmeta", ".properties", ".lang"); int count = bom.findBOMs().size(); - if (count > 0) - Logger.log("Removing BOMs from " + count + " files."); + if (count > 0) { + Logger.debug("Removing BOMs from " + count + " files."); + } + bom.removeBOMs(); } @@ -99,9 +99,7 @@ public void finish() throws IOException { @Override public String toString() { - return "Handler{" + - "pack=" + pack + - '}'; + return "Handler{pack=" + pack + "}"; } } } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java b/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java index d07036d7..40688214 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/pack/ZipPack.java @@ -3,7 +3,6 @@ import com.agentdid127.resourcepack.library.utilities.FileUtil; import com.agentdid127.resourcepack.library.utilities.Logger; import com.agentdid127.resourcepack.library.utilities.Util; - import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; @@ -37,20 +36,20 @@ public Path getConvertedZipPath() { /** * Removes Existing Conversions and starts new one - * + * * @throws IOException Any issue with loading the pack, or deleting previous * packs */ @Override public void setup() throws IOException { if (pack.getWorkingPath().toFile().exists()) { - Logger.log(" Deleting existing conversion"); + Logger.log("Deleting existing conversion"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); } Path convertedZipPath = getConvertedZipPath(); if (convertedZipPath.toFile().exists()) { - Logger.log(" Deleting existing conversion zip"); + Logger.log("Deleting existing conversion zip"); convertedZipPath.toFile().delete(); } @@ -69,13 +68,13 @@ public void setup() throws IOException { /** * Runs after program is finished. Zips directory. - * + * * @throws IOException Any IO exception */ @Override public void finish() throws IOException { try { - Logger.log(" Zipping working directory"); + Logger.debug("Zipping working directory"); ZipFile zipFile = new ZipFile(getConvertedZipPath().toFile()); ZipParameters parameters = new ZipParameters(); parameters.setIncludeRootFolder(false); @@ -85,8 +84,9 @@ public void finish() throws IOException { Util.propagate(e); } - Logger.log(" Deleting working directory"); + Logger.debug("Deleting working directory"); FileUtil.deleteDirectoryAndContents(pack.getWorkingPath()); + Logger.log("Finished!"); } @Override diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java index 3853ced1..ff27e2b6 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/FileUtil.java @@ -5,31 +5,26 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.Objects; public final class FileUtil { public static void moveIfExists(Path in, Path out) throws IOException { - if (!in.toFile().exists()) - return; - ensureParentExists(out); - Files.move(in, out); - } - - public static void copyIfExists(Path in, Path out) throws IOException { - if (!in.toFile().exists()) - return; - ensureParentExists(out); - Files.copy(in, out); + if (in.toFile().exists()) { + ensureParentExists(out); + Files.move(in, out); + } } public static void ensureParentExists(Path path) { Path parentPath = path.getParent(); - if (!parentPath.toFile().exists()) + if (!parentPath.toFile().exists()) { parentPath.toFile().mkdirs(); + } } /** * Copies Directory - * + * * @param src directory Source * @param dest Directory Destination * @throws IOException @@ -48,23 +43,22 @@ public static void copyDirectory(Path src, Path dest) throws IOException { /** * Deletes full directory - * + * * @param dirPath Path of Directory to delete * @throws IOException */ public static void deleteDirectoryAndContents(Path dirPath) throws IOException { - if (!dirPath.toFile().exists()) - return; - - // noinspection ResultOfMethodCallIgnored - Files.walk(dirPath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(file -> { - file.delete(); - }); + if (dirPath.toFile().exists()) { + // noinspection ResultOfMethodCallIgnored + Files.walk(dirPath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(file -> { + file.delete(); + }); + } } /** * If file exists, return file with correct casing. - * + * * @param path * @return * @throws IOException @@ -73,13 +67,30 @@ public static boolean fileExistsCorrectCasing(Path path) throws IOException { return path.toFile().exists() && path.toString().equals(path.toFile().getCanonicalPath()); } + /** + * @return null if file doesn't exist, {@code true} if successfully renamed, + * {@code false} if failed + */ + public static Boolean renameFile(Path file, Path newFile) { + if (!file.toFile().exists()) { + return null; + } + try { + Files.move(file, newFile); + return true; + } catch (IOException e) { + return false; + } + } + /** * @return null if file doesn't exist, {@code true} if successfully renamed, * {@code false} if failed */ public static Boolean renameFile(Path file, String newName) { - if (!file.toFile().exists()) + if (!file.toFile().exists()) { return null; + } try { Files.move(file, file.getParent().resolve(newName)); return true; @@ -90,32 +101,33 @@ public static Boolean renameFile(Path file, String newName) { /** * Takes dir2 and merges it into dir1 - * + * * @param dir1 * @param dir2 */ - public static Boolean mergeDirectories(File dir1, File dir2) throws IOException { - if (!dir1.exists() && !dir2.exists()) - return null; + public static void mergeDirectories(File dir1, File dir2) throws IOException { + if (!dir1.exists() && !dir2.exists()) { + return; + } String targetDirPath = dir1.getAbsolutePath(); File[] files = dir2.listFiles(); + assert files != null; for (File file : files) { if (file.isDirectory()) { - Logger.log(targetDirPath + File.separator + file.getName()); + Logger.debug(targetDirPath + File.separator + file.getName()); File file3 = new File(targetDirPath + File.separator + file.getName()); file3.mkdirs(); - Logger.log("Created" + file3.getName()); + Logger.debug("Created" + file3.getName()); mergeDirectories(file3, file); } else { - Logger.log(targetDirPath + File.separator + file.getName()); + Logger.debug(targetDirPath + File.separator + file.getName()); file.renameTo(new File(targetDirPath + File.separator + file.getName())); } } - if (dir2.list().length == 0) + if (Objects.requireNonNull(dir2.list()).length == 0) { Files.delete(dir2.toPath()); - - return true; + } } } \ No newline at end of file diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java index 1c4d6041..eb808a31 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/ImageConverter.java @@ -39,24 +39,27 @@ public ImageConverter(Integer defaultWIn, Integer defaultHIn, Path locationIn) t } if (!fileIsPowerOfTwo()) { - Logger.log("Image (" + image.getWidth() + "x" + image.getHeight() + ") '" + locationIn.getFileName() + Logger.debug("Image (" + image.getWidth() + "x" + image.getHeight() + ") '" + locationIn.getFileName() + "' resolution size is not a power of 2. Converting to be so."); int fixed_width = (int) Math.ceil(Math.log(image.getWidth()) / Math.log(2)); - if (fixed_width < 1) + if (fixed_width < 1) { fixed_width = 1; + } int fixed_height = (int) Math.ceil(Math.log(image.getHeight()) / Math.log(2)); - if (fixed_height < 1) + if (fixed_height < 1) { fixed_height = 1; + } newImage = new BufferedImage(fixed_width, fixed_height, image.getType()); Graphics2D g = newImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(image, 0, 0, fixed_width, fixed_height, 0, 0, imageWidth, imageHeight, null); g.dispose(); - } else + } else { newImage = image; + } } /** @@ -105,9 +108,10 @@ public void fillEmpty(int x, int y, int width, int height) { */ public void setImage(int defaultWIn, int defaultHIn) throws IOException { if (!imageIsPowerOfTwo(newImage)) { - Logger.log("Image '" + location.getFileName() + "' is not a power of 2"); + Logger.debug("Image '" + location.getFileName() + "' is not a power of 2"); return; } + image = newImage; defaultW = defaultWIn; defaultH = defaultHIn; @@ -143,10 +147,10 @@ public void newImage(int newWidth, int newHeight) { * @throws IOException */ public void addImage(Path imagePath, int x, int y) throws IOException { - if (!imagePath.toFile().exists()) - return; - BufferedImage image = ImageIO.read(imagePath.toFile()); - g2d.drawImage(image, scaleX(x), scaleY(y), null); + if (imagePath.toFile().exists()) { + BufferedImage image = ImageIO.read(imagePath.toFile()); + g2d.drawImage(image, scaleX(x), scaleY(y), null); + } } /** @@ -197,8 +201,7 @@ public void subImage(int x, int y, int x2, int y2, int storeX, int storeY, boole int width2 = (int) (x2 * scaleW - x * scaleW); int height2 = (int) (y2 * scaleH - y * scaleH); BufferedImage part = getSubImage(scaleX(x), scaleY(y), width2, height2); - g2d.drawImage(createFlipped(part, flip), Math.round((float) (storeX * scaleW)), - Math.round((float) (storeY * scaleH)), null); + g2d.drawImage(createFlipped(part, flip), Math.round((float) (storeX * scaleW)), Math.round((float) (storeY * scaleH)), null); } /** @@ -222,8 +225,7 @@ public void subImage(int x, int y, int x2, int y2, int storeX, int storeY, int f int y3 = (int) (y * scaleH); BufferedImage part = getSubImage(x3, y3, width2, height2); - g2d.drawImage(createFlipped(part, flip), Math.round((float) (storeX * scaleW)), - Math.round((float) (storeY * scaleH)), null); + g2d.drawImage(createFlipped(part, flip), Math.round((float) (storeX * scaleW)), Math.round((float) (storeY * scaleH)), null); } public void subImageSized(int x, int y, int width, int height, int storeX, int storeY) { @@ -234,32 +236,23 @@ public void subImageSized(int x, int y, int width, int height) { subImage(x, y, x + width, y + height, 0, 0); } - /** - * Recolor the entire image. - * - * @param color - */ - public void colorize(Color color) { - g2d.setPaint(color); - g2d.drawImage(image, 0, 0, null); - g2d.fillRect(0, 0, newImage.getWidth(), newImage.getHeight()); - } - /** * Recolor the grayscale image. * * @param color */ - public void colorizeClipped(Color color) { - this.newImage(this.getWidth(), this.getHeight()); + public void colorizeGrayscale(Color color) { + this.newImage(this.defaultW, this.defaultH); this.g2d.drawImage(this.image, 0, 0, null); for (int y = 0; y < this.getHeight(); y++) { for (int x = 0; x < this.getWidth(); x++) { - int imageRGBA = newImage.getRGB(x, y); - int alpha = (imageRGBA >> 24) & 0xFF; - if (alpha == 0) + int rgba = this.newImage.getRGB(x, y); + int alpha = (rgba >> 24) & 0xFF; + if (alpha == 0) { continue; - int grayscaleValue = (imageRGBA >> 16) & 0xFF; + } + + int grayscaleValue = (rgba >> 16) & 0xFF; int red = (grayscaleValue * color.getRed()) / 255; int green = (grayscaleValue * color.getGreen()) / 255; int blue = (grayscaleValue * color.getBlue()) / 255; @@ -272,8 +265,8 @@ public void colorizeClipped(Color color) { * Grayscale the image using the NTSC grayscale formula */ public void grayscale() { - BufferedImage gray = new BufferedImage(newImage.getWidth(), newImage.getHeight(), BufferedImage.TYPE_INT_ARGB); - gray.createGraphics().drawImage(image, 0, 0, null); + BufferedImage gray = new BufferedImage(this.newImage.getWidth(), this.newImage.getHeight(), BufferedImage.TYPE_INT_ARGB); + gray.createGraphics().drawImage(this.image, 0, 0, null); for (int y = 0; y < gray.getHeight(); y++) { for (int x = 0; x < gray.getWidth(); x++) { int rgba = gray.getRGB(x, y); @@ -285,7 +278,7 @@ public void grayscale() { gray.setRGB(x, y, (alpha << 24) | (average << 16) | (average << 8) | average); } } - newImage = gray; + this.newImage = gray; } /** @@ -297,13 +290,15 @@ public void grayscale() { */ private static BufferedImage createFlipped(BufferedImage image, int flip) { AffineTransform at = new AffineTransform(); - if (flip != 1) + if (flip != 1) { return image; - at.concatenate(AffineTransform.getScaleInstance(1, -1)); - at.concatenate(AffineTransform.getTranslateInstance(0, -image.getHeight())); - at.concatenate(AffineTransform.getScaleInstance(-1, 1)); - at.concatenate(AffineTransform.getTranslateInstance(-image.getWidth(), 0)); - return createTransformed(image, at); + } else { + at.concatenate(AffineTransform.getScaleInstance(1, -1)); + at.concatenate(AffineTransform.getTranslateInstance(0, -image.getHeight())); + at.concatenate(AffineTransform.getScaleInstance(-1, 1)); + at.concatenate(AffineTransform.getTranslateInstance(-image.getWidth(), 0)); + return createTransformed(image, at); + } } /** diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java index 7a1a1b0f..789f111d 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/JsonUtil.java @@ -1,106 +1,107 @@ package com.agentdid127.resourcepack.library.utilities; +import com.agentdid127.resourcepack.library.PackConverter; +import com.google.gson.*; +import com.google.gson.stream.JsonReader; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; -import com.agentdid127.resourcepack.library.PackConverter; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.stream.JsonReader; - public class JsonUtil { - public static JsonArray add(JsonArray lhs, JsonArray rhs) { - return add(lhs, rhs, (byte) 1); - } - - public static JsonArray add(JsonArray lhs, JsonArray rhs, byte sign) { - JsonArray newArray = new JsonArray(); - for (int i = 0; i < 3; i++) - newArray.add(add(lhs, rhs, i, sign)); - return newArray; - } - - public static float add(JsonArray lhs, JsonArray rhs, int i, byte sign) { - return lhs.get(i).getAsFloat() + sign * rhs.get(i).getAsFloat(); - } - - public static float multiply(JsonArray lhs, JsonArray rhs, int i) { - return lhs.get(i).getAsFloat() * rhs.get(i).getAsFloat(); - } - - public static float divide(JsonArray lhs, JsonArray rhs, int i) { - return lhs.get(i).getAsFloat() / rhs.get(i).getAsFloat(); - } - - public static JsonArray subtract(JsonArray lhs, JsonArray rhs) { - return add(lhs, rhs, (byte) -1); - } - - public static JsonArray multiply(JsonArray lhs, JsonArray rhs) { - JsonArray newArray = new JsonArray(); - for (int i = 0; i < 3; i++) - newArray.add(multiply(lhs, rhs, i)); - return newArray; - } - - public static JsonArray divide(JsonArray lhs, JsonArray rhs) { - JsonArray newArray = new JsonArray(); - for (int i = 0; i < 3; i++) - newArray.add(divide(lhs, rhs, i)); - return newArray; - } - - public static JsonArray asArray(Gson gson, String raw) { - return gson.fromJson(raw, JsonArray.class); - } - - public static void writeJson(Gson gson, Path out, JsonElement json) throws IOException { - Files.write(out, Collections.singleton(gson.toJson(json)), Charset.forName("UTF-8")); - } - - public static boolean isJson(Gson gson, String Json) { - try { - gson.fromJson(Json, Object.class); - return true; - } catch (com.google.gson.JsonSyntaxException ex) { - return false; - } - } - - public static T readJson(Gson gson, Path path, Class clazz) throws IOException { - String json = Util.readFromFile(path); - if (!isJson(gson, json)) - return null; - JsonReader reader = new JsonReader(new StringReader(json)); - reader.setLenient(true); - return gson.fromJson(reader, clazz); - } - - public static JsonObject readJson(Gson gson, Path path) throws IOException { - return readJson(gson, path, JsonObject.class); - } - - public static T readJsonResource(Gson gson, String path, Class clazz) { - try (InputStream stream = PackConverter.class.getResourceAsStream(path)) { - if (stream == null) - return null; - try (InputStreamReader streamReader = new InputStreamReader(stream)) { - return gson.fromJson(streamReader, clazz); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static JsonObject readJsonResource(Gson gson, String path) { - return readJsonResource(gson, path, JsonObject.class); - } + public static JsonArray add(JsonArray lhs, JsonArray rhs) { + return add(lhs, rhs, (byte) 1); + } + + public static JsonArray add(JsonArray lhs, JsonArray rhs, byte sign) { + JsonArray newArray = new JsonArray(); + for (int i = 0; i < 3; i++) + newArray.add(add(lhs, rhs, i, sign)); + return newArray; + } + + public static float add(JsonArray lhs, JsonArray rhs, int i, byte sign) { + return lhs.get(i).getAsFloat() + sign * rhs.get(i).getAsFloat(); + } + + public static float multiply(JsonArray lhs, JsonArray rhs, int i) { + return lhs.get(i).getAsFloat() * rhs.get(i).getAsFloat(); + } + + public static float divide(JsonArray lhs, JsonArray rhs, int i) { + return lhs.get(i).getAsFloat() / rhs.get(i).getAsFloat(); + } + + public static JsonArray subtract(JsonArray lhs, JsonArray rhs) { + return add(lhs, rhs, (byte) -1); + } + + public static JsonArray multiply(JsonArray lhs, JsonArray rhs) { + JsonArray newArray = new JsonArray(); + for (int i = 0; i < 3; i++) + newArray.add(multiply(lhs, rhs, i)); + return newArray; + } + + public static JsonArray divide(JsonArray lhs, JsonArray rhs) { + JsonArray newArray = new JsonArray(); + for (int i = 0; i < 3; i++) + newArray.add(divide(lhs, rhs, i)); + return newArray; + } + + public static JsonArray asArray(Gson gson, String raw) { + return gson.fromJson(raw, JsonArray.class); + } + + public static void writeJson(Gson gson, Path out, JsonElement json) throws IOException { + Files.write(out, Collections.singleton(gson.toJson(json)), StandardCharsets.UTF_8); + } + + public static boolean isJson(Gson gson, String Json) { + try { + gson.fromJson(Json, Object.class); + return true; + } catch (com.google.gson.JsonSyntaxException ex) { + return false; + } + } + + public static T readJson(Gson gson, Path path, Class clazz) throws IOException { + String json = Util.readFromFile(path); + if (isJson(gson, json)) { + JsonReader reader = new JsonReader(new StringReader(json)); + reader.setStrictness(Strictness.LENIENT); + return gson.fromJson(reader, clazz); + } else { + return null; + } + } + + public static JsonObject readJson(Gson gson, Path path) throws IOException { + return readJson(gson, path, JsonObject.class); + } + + public static T readJsonResource(Gson gson, String path, Class clazz) { + try (InputStream stream = PackConverter.class.getResourceAsStream(path)) { + if (stream == null) { + return null; + } + + try (InputStreamReader streamReader = new InputStreamReader(stream)) { + return gson.fromJson(streamReader, clazz); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static JsonObject readJsonResource(Gson gson, String path) { + return readJsonResource(gson, path, JsonObject.class); + } } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java index d6b28220..9cc53ae5 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java @@ -4,16 +4,61 @@ public class Logger { private static PrintStream stream; + private static boolean debug; + + private static int tabs = 0; + + public static void addTab() { + tabs++; + } + + public static void subTab() { + tabs--; + if (tabs < 0) { + tabs = 0; + } + } + + public static void resetTab() { + tabs = 0; + } + + private static String getTabs() { + StringBuilder out = new StringBuilder(); + for (int i = 0; i < tabs; i++) { + out.append(" "); + } + return out.toString(); + } public static void setStream(PrintStream stream) { Logger.stream = stream; } + public static void setDebug(boolean debug) { + Logger.debug = debug; + } + + public static void debug(String message) { + if (debug) { + log(message); + } + } + public static void log(String message) { - stream.println(message); + stream.println(getTabs() + message); + } + + public static void error(String message) { + log(message); + } + + private static void debug(Object thing) { + debug(thing.toString()); } public static void log(Object thing) { - stream.println(String.valueOf(thing)); + String tabs = getTabs(); + stream.println(tabs + String.valueOf(thing).replaceAll("\n", "\n" + tabs)); } } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Mapping.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Mapping.java index 62f8d127..f8c96a00 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Mapping.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Mapping.java @@ -1,12 +1,12 @@ package com.agentdid127.resourcepack.library.utilities; -import java.util.HashMap; -import java.util.Map; - import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.HashMap; +import java.util.Map; + public class Mapping { protected final Map mapping = new HashMap<>(); @@ -15,12 +15,12 @@ public Mapping(Gson gson, String path, String key, boolean backwards) { } protected void load(Gson gson, String source, String path, String key) { - JsonObject object = JsonUtil.readJsonResource(gson, "/" + source + "/" + path + ".json") - .getAsJsonObject(key); - if (object == null) - return; - for (Map.Entry entry : object.entrySet()) - this.mapping.put(entry.getKey(), entry.getValue().getAsString()); + JsonObject object = JsonUtil.readJsonResource(gson, "/" + source + "/" + path + ".json").getAsJsonObject(key); + if (object != null) { + for (Map.Entry entry : object.entrySet()) { + this.mapping.put(entry.getKey(), entry.getValue().getAsString()); + } + } } /** diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/PropertiesEx.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/PropertiesEx.java index 8a5972e3..2e8c64f3 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/PropertiesEx.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/PropertiesEx.java @@ -10,8 +10,8 @@ import java.util.Properties; public class PropertiesEx extends Properties { - private static final char[] hexDigit = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', - 'D', 'E', 'F' }; + private static final char[] hexDigit = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', + 'D', 'E', 'F'}; private static void writeComments(BufferedWriter bw, String comments) throws IOException { bw.write("#"); @@ -19,7 +19,7 @@ private static void writeComments(BufferedWriter bw, String comments) throws IOE int current = 0; int last = 0; - for (char[] uu = new char[] { '\\', 'u', '\u0000', '\u0000', '\u0000', '\u0000' }; current < len; ++current) { + for (char[] uu = new char[]{'\\', 'u', '\u0000', '\u0000', '\u0000', '\u0000'}; current < len; ++current) { char c = comments.charAt(current); if (c > 255 || c == '\n' || c == '\r') { if (last != current) diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Util.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Util.java index 38f5baf5..6727692c 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Util.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Util.java @@ -2,17 +2,17 @@ import com.agentdid127.resourcepack.library.PackConverter; import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collection; import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; public final class Util { private Util() { @@ -21,79 +21,134 @@ private Util() { /** * Reads Image as BufferedImage - * + * * @param path Path to file * @return Buffered Image */ public static BufferedImage readImageResource(String path) { try (InputStream stream = PackConverter.class.getResourceAsStream(path)) { - if (stream == null) + if (stream == null) { return null; - return ImageIO.read(stream); + } else { + return ImageIO.read(stream); + } } catch (IOException e) { throw new RuntimeException(e); } } /** - * Gets version protocol number - * - * @param gson Gson object to use - * @param version Minecraft Version Number. - * @return Protocol Integer Number + * Gets Minecraft Version from Protocol number + * + * @param gson Gson object to use + * @param protocol Protocol version number + * @return Minecraft Version number */ - public static int getVersionProtocol(Gson gson, String version) { + public static String getVersionFromProtocol(Gson gson, int protocol) { JsonObject protocols = JsonUtil.readJsonResource(gson, "/protocol.json"); - return protocols == null ? 0 : Integer.parseInt(protocols.get(version).getAsString()); - } + if (protocols != null) { + for (Map.Entry entry : protocols.entrySet()) { + JsonObject versionObj = entry.getValue().getAsJsonObject(); + if (versionObj.get("protocol_version").getAsInt() == protocol) { + return entry.getKey(); + } + } + } - public static int getLatestProtocol(Gson gson) { - return getVersionProtocol(gson, "1.20.4"); + return null; } /** - * Gets Minecraft Version from Protocol number - * + * Gets version object containing protocol_version and pack_format by protocol version + * * @param gson Gson object to use * @param protocol Protocol version number - * @return Minecraft Version number + * @return JsonObject containing version information */ - public static String getVersionFromProtocol(Gson gson, int protocol) { - AtomicReference version = new AtomicReference("ok boomer"); + public static JsonObject getVersionObjectByProtocol(Gson gson, int protocol) { + JsonObject protocols = JsonUtil.readJsonResource(gson, "/protocol.json"); + if (protocols != null) { + for (Map.Entry entry : protocols.entrySet()) { + JsonObject versionObj = entry.getValue().getAsJsonObject(); + if (versionObj.get("protocol_version").getAsInt() == protocol) { + return versionObj; + } + } + } + + return null; + } + + /** + * Gets version object containing protocol_version and pack_format + * + * @param gson Gson object to use + * @param version Minecraft Version Number. + * @return JsonObject containing version information + */ + public static JsonObject getVersionObject(Gson gson, String version) { JsonObject protocols = JsonUtil.readJsonResource(gson, "/protocol.json"); - if (protocols == null) + if (protocols != null) { + return protocols.getAsJsonObject(version); + } else { return null; - Collection keys = protocols.entrySet().stream().map(i -> i.getKey()) - .collect(Collectors.toCollection(ArrayList::new)); - keys.forEach(key -> { - if (Integer.parseInt(protocols.get(key).getAsString()) == protocol) - version.set(key); - }); - return version.toString(); + } + } + + /** + * Gets version protocol number + * + * @param gson Gson object to use + * @param version Minecraft Version Number. + * @return Protocol Integer Number + */ + public static int getVersionProtocol(Gson gson, String version) { + JsonObject versionObj = getVersionObject(gson, version); + if (versionObj != null) { + return versionObj.get("protocol_version").getAsInt(); + } else { + return 0; + } + } + + public static int getLatestProtocol(Gson gson) { + JsonObject protocols = JsonUtil.readJsonResource(gson, "/protocol.json"); + int latestProtocol = 0; + if (protocols != null) { + for (Map.Entry entry : protocols.entrySet()) { + JsonObject versionObj = entry.getValue().getAsJsonObject(); + int protocol = versionObj.get("protocol_version").getAsInt(); + if (protocol > latestProtocol) { + latestProtocol = protocol; + } + } + } + + return latestProtocol; } /** * Gets Supported versions of Resource Pack Converter - * + * * @param gson Gson object to use * @return String list of all minecraft versions */ public static String[] getSupportedVersions(Gson gson) { JsonObject protocols = JsonUtil.readJsonResource(gson, "/protocol.json"); - if (protocols == null) + if (protocols != null) { + return protocols.entrySet().stream().map(Map.Entry::getKey).toArray(String[]::new); + } else { return null; - return protocols.entrySet() - .stream() - .map(Map.Entry::getKey) - .toArray(String[]::new); + } } public static String readFromFile(Path path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path.toFile())); StringBuilder resultStringBuilder = new StringBuilder(); String line; - while ((line = br.readLine()) != null) + while ((line = br.readLine()) != null) { resultStringBuilder.append(line).append("\n"); + } br.close(); return resultStringBuilder.toString(); } diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Box.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Box.java similarity index 80% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Box.java rename to library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Box.java index 1f1b7ef7..ed9efe73 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Box.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Box.java @@ -1,12 +1,12 @@ -package com.agentdid127.resourcepack.forwards.impl.textures.slicing; +package com.agentdid127.resourcepack.library.utilities.slicing; import com.google.gson.JsonObject; public class Box { - private int x; - private int y; - private int width; - private int height; + private final int x; + private final int y; + private final int width; + private final int height; public Box(int x, int y, int width, int height) { this.x = x; diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/PredicateRunnable.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/PredicateRunnable.java new file mode 100644 index 00000000..e1d08388 --- /dev/null +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/PredicateRunnable.java @@ -0,0 +1,7 @@ +package com.agentdid127.resourcepack.library.utilities.slicing; + +import com.google.gson.JsonObject; + +public interface PredicateRunnable { + boolean run(int from, JsonObject predicate); +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slice.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slice.java similarity index 68% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slice.java rename to library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slice.java index c8269fe3..ebf78904 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Slice.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slice.java @@ -1,13 +1,13 @@ -package com.agentdid127.resourcepack.forwards.impl.textures.slicing; +package com.agentdid127.resourcepack.library.utilities.slicing; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import java.io.File; +import java.util.Collections; import java.util.LinkedList; import java.util.List; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - public class Slice { private String path; private String name; @@ -33,6 +33,18 @@ public int getHeight() { return height; } + public void setWidth(int width) { + if (this.width != width) { + this.width = width; + } + } + + public void setHeight(int height) { + if (this.height != height) { + this.height = height; + } + } + public JsonObject getPredicate() { return predicate; } @@ -49,33 +61,34 @@ public static Slice parse(JsonObject object) { Slice slice = new Slice(); slice.path = object.get("path").getAsString(); - if (object.has("name")) + if (object.has("name")) { slice.name = object.get("name").getAsString(); - else + } else { slice.name = null; + } slice.width = object.get("width").getAsInt(); slice.height = object.get("height").getAsInt(); - slice.textures = Texture.parseArray(object.get("textures").getAsJsonArray()); - if (object.has("delete")) + if (object.has("delete")) { slice.delete = object.get("delete").getAsBoolean(); - else + } else { slice.delete = true; + } - if (object.has("predicate")) + if (object.has("predicate")) { slice.predicate = object.get("predicate").getAsJsonObject(); - else + } else { slice.predicate = new JsonObject(); + } return slice; } - public static Slice[] parseArray(JsonArray array) { + public static List parseArray(JsonArray array) { List slices = new LinkedList<>(); - for (JsonElement element : array) - slices.add(Slice.parse(element.getAsJsonObject())); - return slices.toArray(new Slice[] {}); + array.forEach(element -> slices.add(Slice.parse(element.getAsJsonObject()))); + return Collections.unmodifiableList(slices); } } \ No newline at end of file diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slicer.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slicer.java new file mode 100644 index 00000000..e1b7e68c --- /dev/null +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Slicer.java @@ -0,0 +1,75 @@ +package com.agentdid127.resourcepack.library.utilities.slicing; + +import com.agentdid127.resourcepack.library.utilities.FileUtil; +import com.agentdid127.resourcepack.library.utilities.ImageConverter; +import com.agentdid127.resourcepack.library.utilities.JsonUtil; +import com.agentdid127.resourcepack.library.utilities.Logger; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.IOException; +import java.nio.file.Path; + +public class Slicer { + public static void run(Gson gson, Slice slice, Path root, /* nullable */ PredicateRunnable predicateRunnable, int from, boolean has_metadata) throws IOException { + if (predicateRunnable != null && !predicateRunnable.run(from, slice.getPredicate())) { + return; + } + + Path path = root.resolve(slice.getPath()); + if (!path.toFile().exists()) { + Logger.debug("Texture '" + slice.getPath() + "' doesn't exist, ignoring..."); + return; + } + + ImageConverter converter = new ImageConverter(slice.getWidth(), slice.getHeight(), path); + + Path imagePath = path; + if (slice.getPathName() != null) { + imagePath = path.resolveSibling(slice.getPathName()); + } + + for (Texture texture : slice.getTextures()) { + Path texturePath = root.resolve(texture.getPath()); + if (texturePath.toFile().exists()) { + // Sometimes packs include both newer and older textures but + // since we are upgrading to a newer version, we want what was shown in the original + // version so we delete the existing new one, so we can replace it with the + // version used in said version. + texturePath.toFile().delete(); + } + FileUtil.ensureParentExists(texturePath); + + if (predicateRunnable != null && !predicateRunnable.run(from, texture.getPredicate())) { + continue; + } + + try { + Box box = texture.getBox(); + converter.saveSlice(box.getX(), box.getY(), box.getWidth(), box.getHeight(), texturePath); + if (texture.shouldRemove()) { + converter.fillEmpty(box.getX(), box.getY(), box.getWidth(), box.getHeight()); + } + + if (has_metadata) { + JsonObject metadata = texture.getMetadata(); + if (!metadata.keySet().isEmpty() && !metadata.entrySet().isEmpty()) { + Path metadataPath = texturePath.resolveSibling(texturePath.getFileName() + ".mcmeta"); + JsonUtil.writeJson(gson, metadataPath, metadata); + } + } + } catch (Exception exception) { + Logger.log("Failed to slice texture '" + texture.getPath() + "' (error='" + exception.getLocalizedMessage() + "')"); + Logger.log(" - box: (x=" + texture.getBox().getX() + ", y=" + texture.getBox().getY() + ", width=" + texture.getBox().getWidth() + ", height=" + texture.getBox().getHeight() + ")"); + } + } + + if (!path.toFile().delete()) { + Logger.debug("Failed to remove '" + path.getFileName() + "' whilst slicing."); + } + + if (!slice.shouldDelete()) { + converter.store(imagePath); + } + } +} diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Texture.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Texture.java similarity index 78% rename from conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Texture.java rename to library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Texture.java index 830db3b7..084f0d7a 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/textures/slicing/Texture.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/slicing/Texture.java @@ -1,22 +1,23 @@ -package com.agentdid127.resourcepack.forwards.impl.textures.slicing; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +package com.agentdid127.resourcepack.library.utilities.slicing; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + public class Texture { - private String path; - private Box box; - private boolean remove; - private JsonObject predicate; - private JsonObject metadata; - - public static HashMap metatdataCache = new HashMap<>(); - static { + private final String path; + private final Box box; + private final boolean remove; + private final JsonObject predicate; + private final JsonObject metadata; + + public static HashMap METADATA_CACHE = new HashMap<>(); + + static { { // Button Widget JsonObject buttonWidget = new JsonObject(); @@ -36,7 +37,7 @@ public class Texture { gui.add("scaling", scaling); buttonWidget.add("gui", gui); - metatdataCache.put("button_widget", buttonWidget); + METADATA_CACHE.put("button_widget", buttonWidget); } } @@ -95,30 +96,33 @@ public JsonObject getMetadata() { public static Texture parse(JsonObject object) { String path = object.get("path").getAsString(); Box box = Box.parse(object.get("box").getAsJsonObject()); - + boolean remove = false; - if (object.has("remove")) - remove = object.get("remove").getAsBoolean(); + if (object.has("remove")) { + remove = object.get("remove").getAsBoolean(); + } JsonObject predicate; - if (object.has("predicate")) + if (object.has("predicate")) { predicate = object.get("predicate").getAsJsonObject(); - else + } else { predicate = new JsonObject(); - + } + JsonObject metadata; if (object.has("metadata")) { JsonElement metadataElement = object.get("metadata"); - metadata = metadataElement.isJsonObject() ? metadataElement.getAsJsonObject() : metatdataCache.getOrDefault(metadataElement.getAsString(), new JsonObject()); - } else metadata = new JsonObject(); + metadata = metadataElement.isJsonObject() ? metadataElement.getAsJsonObject() : METADATA_CACHE.getOrDefault(metadataElement.getAsString(), new JsonObject()); + } else { + metadata = new JsonObject(); + } return new Texture(path, box, remove, predicate, metadata); } public static Texture[] parseArray(JsonArray array) { List textures = new LinkedList<>(); - for (JsonElement element : array) - textures.add(Texture.parse(element.getAsJsonObject())); - return textures.toArray(new Texture[] {}); + array.forEach(element -> textures.add(Texture.parse(element.getAsJsonObject()))); + return textures.toArray(new Texture[]{}); } } diff --git a/library/src/main/resources/protocol.json b/library/src/main/resources/protocol.json index 81238833..1c48eea3 100644 --- a/library/src/main/resources/protocol.json +++ b/library/src/main/resources/protocol.json @@ -1,80 +1,354 @@ { - "1.7.2": "4", - "1.7.3": "4", - "1.7.4": "4", - "1.7.5": "4", - "1.7.6": "5", - "1.7.7": "5", - "1.7.8": "5", - "1.7.9": "5", - "1.7.10": "5", - "1.8": "47", - "1.8.0": "47", - "1.8.1": "47", - "1.8.2": "47", - "1.8.3": "47", - "1.8.4": "47", - "1.8.5": "47", - "1.8.6": "47", - "1.8.7": "47", - "1.8.8": "47", - "1.8.9": "47", - "1.9": "107", - "1.9.0": "107", - "1.9.1": "108", - "1.9.2": "109", - "1.9.3": "110", - "1.9.4": "110", - "1.10": "210", - "1.10.0": "210", - "1.10.1": "210", - "1.10.2": "210", - "1.11": "315", - "1.11.0": "315", - "1.11.1": "316", - "1.11.2": "316", - "1.12": "335", - "1.12.0": "335", - "1.12.1": "338", - "1.12.2": "340", - "1.13": "393", - "1.13.0": "393", - "1.13.1": "401", - "1.13.2": "404", - "1.14": "477", - "1.14.0": "477", - "1.14.1": "480", - "1.14.2": "485", - "1.14.3": "490", - "1.14.4": "498", - "1.15": "573", - "1.15.0": "573", - "1.15.1": "575", - "1.15.2": "578", - "1.16": "735", - "1.16.0": "735", - "1.16.1": "736", - "1.16.2": "751", - "1.16.3": "753", - "1.16.4": "754", - "1.16.5": "754", - "1.17": "755", - "1.17.0": "755", - "1.17.1": "756", - "1.18": "757", - "1.18.0": "757", - "1.18.1": "758", - "1.18.2": "758", - "1.19": "759", - "1.19.0": "759", - "1.19.1": "760", - "1.19.2": "760", - "1.19.3": "761", - "1.19.4": "762", - "1.20": "763", - "1.20.0": "763", - "1.20.1": "763", - "1.20.2": "764", - "1.20.3": "765", - "1.20.4": "765" + "1.7.2": { + "protocol_version": 4, + "pack_format": 1 + }, + "1.7.3": { + "protocol_version": 4, + "pack_format": 1 + }, + "1.7.4": { + "protocol_version": 4, + "pack_format": 1 + }, + "1.7.5": { + "protocol_version": 4, + "pack_format": 1 + }, + "1.7.6": { + "protocol_version": 5, + "pack_format": 1 + }, + "1.7.7": { + "protocol_version": 5, + "pack_format": 1 + }, + "1.7.8": { + "protocol_version": 5, + "pack_format": 1 + }, + "1.7.9": { + "protocol_version": 5, + "pack_format": 1 + }, + "1.7.10": { + "protocol_version": 5, + "pack_format": 1 + }, + "1.8": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.0": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.1": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.2": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.3": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.4": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.5": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.6": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.7": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.8": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.8.9": { + "protocol_version": 47, + "pack_format": 1 + }, + "1.9": { + "protocol_version": 107, + "pack_format": 2 + }, + "1.9.0": { + "protocol_version": 107, + "pack_format": 2 + }, + "1.9.1": { + "protocol_version": 108, + "pack_format": 2 + }, + "1.9.2": { + "protocol_version": 109, + "pack_format": 2 + }, + "1.9.3": { + "protocol_version": 110, + "pack_format": 2 + }, + "1.9.4": { + "protocol_version": 110, + "pack_format": 2 + }, + "1.10": { + "protocol_version": 210, + "pack_format": 2 + }, + "1.10.0": { + "protocol_version": 210, + "pack_format": 2 + }, + "1.10.1": { + "protocol_version": 210, + "pack_format": 2 + }, + "1.10.2": { + "protocol_version": 210, + "pack_format": 2 + }, + "1.11": { + "protocol_version": 315, + "pack_format": 3 + }, + "1.11.0": { + "protocol_version": 315, + "pack_format": 3 + }, + "1.11.1": { + "protocol_version": 316, + "pack_format": 3 + }, + "1.11.2": { + "protocol_version": 316, + "pack_format": 3 + }, + "1.12": { + "protocol_version": 335, + "pack_format": 3 + }, + "1.12.0": { + "protocol_version": 335, + "pack_format": 3 + }, + "1.12.1": { + "protocol_version": 338, + "pack_format": 3 + }, + "1.12.2": { + "protocol_version": 340, + "pack_format": 3 + }, + "1.13": { + "protocol_version": 393, + "pack_format": 4 + }, + "1.13.0": { + "protocol_version": 393, + "pack_format": 4 + }, + "1.13.1": { + "protocol_version": 401, + "pack_format": 4 + }, + "1.13.2": { + "protocol_version": 404, + "pack_format": 4 + }, + "1.14": { + "protocol_version": 477, + "pack_format": 4 + }, + "1.14.0": { + "protocol_version": 477, + "pack_format": 4 + }, + "1.14.1": { + "protocol_version": 480, + "pack_format": 4 + }, + "1.14.2": { + "protocol_version": 485, + "pack_format": 4 + }, + "1.14.3": { + "protocol_version": 490, + "pack_format": 4 + }, + "1.14.4": { + "protocol_version": 498, + "pack_format": 4 + }, + "1.15": { + "protocol_version": 573, + "pack_format": 5 + }, + "1.15.0": { + "protocol_version": 573, + "pack_format": 5 + }, + "1.15.1": { + "protocol_version": 575, + "pack_format": 5 + }, + "1.15.2": { + "protocol_version": 578, + "pack_format": 5 + }, + "1.16": { + "protocol_version": 735, + "pack_format": 5 + }, + "1.16.0": { + "protocol_version": 735, + "pack_format": 5 + }, + "1.16.1": { + "protocol_version": 736, + "pack_format": 5 + }, + "1.16.2": { + "protocol_version": 751, + "pack_format": 6 + }, + "1.16.3": { + "protocol_version": 753, + "pack_format": 6 + }, + "1.16.4": { + "protocol_version": 754, + "pack_format": 6 + }, + "1.16.5": { + "protocol_version": 754, + "pack_format": 6 + }, + "1.17": { + "protocol_version": 755, + "pack_format": 7 + }, + "1.17.0": { + "protocol_version": 755, + "pack_format": 7 + }, + "1.17.1": { + "protocol_version": 756, + "pack_format": 7 + }, + "1.18": { + "protocol_version": 757, + "pack_format": 8 + }, + "1.18.0": { + "protocol_version": 757, + "pack_format": 8 + }, + "1.18.1": { + "protocol_version": 758, + "pack_format": 8 + }, + "1.18.2": { + "protocol_version": 758, + "pack_format": 8 + }, + "1.19": { + "protocol_version": 759, + "pack_format": 9 + }, + "1.19.0": { + "protocol_version": 759, + "pack_format": 9 + }, + "1.19.1": { + "protocol_version": 760, + "pack_format": 9 + }, + "1.19.2": { + "protocol_version": 760, + "pack_format": 9 + }, + "1.19.3": { + "protocol_version": 761, + "pack_format": 12 + }, + "1.19.4": { + "protocol_version": 762, + "pack_format": 13 + }, + "1.20": { + "protocol_version": 763, + "pack_format": 15 + }, + "1.20.0": { + "protocol_version": 763, + "pack_format": 15 + }, + "1.20.1": { + "protocol_version": 763, + "pack_format": 15 + }, + "1.20.2": { + "protocol_version": 764, + "pack_format": 18 + }, + "1.20.3": { + "protocol_version": 765, + "pack_format": 22 + }, + "1.20.4": { + "protocol_version": 765, + "pack_format": 22 + }, + "1.20.5": { + "protocol_version": 766, + "pack_format": 32 + }, + "1.20.6": { + "protocol_version": 766, + "pack_format": 32 + }, + "1.21": { + "protocol_version": 767, + "pack_format": 34 + }, + "1.21.1": { + "protocol_version": 767, + "pack_format": 34 + }, + "1.21.2": { + "protocol_version": 768, + "pack_format": 42 + }, + "1.21.3": { + "protocol_version": 768, + "pack_format": 42 + }, + "1.21.4": { + "protocol_version": 769, + "pack_format": 46 + }, + "1.21.5": { + "protocol_version": 770, + "pack_format": 55 + }, + "1.21.6": { + "protocol_version": 771, + "pack_format": 63 + }, + "1.21.7": { + "protocol_version": 772, + "pack_format": 64 + } } \ No newline at end of file diff --git a/licenses/hypixel b/licenses/hypixel new file mode 100644 index 00000000..a0f0e2ac --- /dev/null +++ b/licenses/hypixel @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Hypixel Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/pom.xml b/pom.xml index a6a5a34f..e8852f25 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.agentdid127 ResourcePackConverter - 2.2.4 + 2.2.5 pom ResourcePackConverter @@ -34,7 +34,7 @@ com.google.code.gson gson - 2.8.9 + 2.11.0 net.sf.jopt-simple