Skip to content

Commit

Permalink
Create PackMake (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
iJustLeyxo authored Dec 2, 2024
1 parent f3330c1 commit 866311b
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 284 deletions.
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand All @@ -46,11 +63,11 @@

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.0</version>
<scope>compile</scope>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.3</version>
</dependency>

<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/github/ijustleyxo/packmake/Duo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.ijustleyxo.packmake;

import org.jetbrains.annotations.NotNull;

public final class Duo<A, B> {
private @NotNull A a;
private @NotNull B b;

public Duo(@NotNull A a, @NotNull B b) {
this.a = a;
this.b = b;
}

public @NotNull A a() {
return this.a;
}

public @NotNull B b() {
return this.b;
}

public void a(@NotNull A a) {
this.a = a;
}

public void b(@NotNull B b) {
this.b = b;
}
}
106 changes: 98 additions & 8 deletions src/main/java/com/github/ijustleyxo/packmake/PackMake.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,110 @@
package com.github.ijustleyxo.packmake;

import com.github.ijustleyxo.packmake.util.console.Console;
import com.github.ijustleyxo.packmake.util.console.Type;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

/**
* Minecraft resource pack compiler
*/
public final class PackMake {
private static final @NotNull File SRC = new File("./src/");
private static final @NotNull File TAR = new File("./target/");

public static void main(String[] args) {
// TODO: Resource pack creation logic
PackMake.exit();
Yaml yaml = new Yaml();
List<Integer> formats = null;
try {
formats = yaml.load(new FileInputStream("./pm.yml"));
} catch (IOException e) {
System.out.println("Config load from pm.yml failed");
System.exit(1);
}
SRC.mkdirs();
TAR.mkdirs();
File[] files = SRC.listFiles();
if (files == null) return;
for (File file : files) make(
null, null,
SRC, formats.stream().map(b -> new Duo<>(b, new File(TAR, b + "/"))).toList(),
new File(""), new File(""), new File(file.getName()));
}

private static void make(
@Nullable Byte lower, @Nullable Byte upper,
@NotNull File srcBase, @NotNull List<Duo<Integer, File>> packBase,
@NotNull File srcFolder, @NotNull File packFolder,
@NotNull File srcFile) {
@NotNull String name = srcFile.getName();
int start = name.indexOf('#'); // Start of config
if (0 <= start) {
int i = start + 1; // Skip '#'
Duo<Byte, Integer> first = parseByte(name, i); // Parse first bound
if (first != null) {
lower = first.a();
i = first.b();
}
if (name.startsWith("...", i)) { // Parse separator
i += 3;

Duo<Byte, Integer> second = parseByte(name, i); // Parse second bound
if (second != null) {
upper = second.a();
i = second.b();
} else if (first == null)
System.out.println("Config warning: \"...\" is redundant in " + srcFile); // No bounds case
} else if (first != null) upper = first.a(); // Single format bound
name = name.substring(0, start) + name.substring(i); // Remove config from name
}

@Nullable Byte fLower = lower; // Filter which pack formats are still in range
@Nullable Byte fUpper = upper;
List<Duo<Integer, File>> selected = packBase.stream()
.filter(d -> (fLower == null || fLower <= d.a()) && (fUpper == null || d.a() <= fUpper)).toList();
if (selected.isEmpty()) return; // Nothing to do

File src = new File(srcBase + "/" + srcFolder + "/" + srcFile);

if (src.isDirectory()) { // Descend into directory

File[] files = src.listFiles();
if (files == null || files.length == 0) return;
for (File f : files) make(
lower, upper,
srcBase, packBase,
new File(srcFolder + "/" + srcFile),
new File(packFolder + "/" + name), new File(f.getName()));
} else { // Copy file to packs
for (Duo<Integer, File> pack : selected) {
File destFolder = new File(pack.b() + "/" + packFolder);
File dest = new File(destFolder + "/" + name);
if (dest.exists()) System.out.println(src + " is over defining " + dest);
else try {
destFolder.mkdirs();
Files.copy(src.toPath(), dest.toPath());
} catch (IOException e) {
System.out.println("Failed to copy " + src + " to " + dest);
}
}
}
}

public static void exit() {
Console.log(Type.DEBUG, "Exiting\n");
Console.sep();
System.exit(0);
private static @Nullable Duo<Byte, Integer> parseByte(@NotNull String string, int i) {
Byte result = null;
int k = i + 1;
while (k <= string.length()) try {
result = Byte.parseByte(string.substring(i, k));
k++;
} catch (NumberFormatException e) {
break;
}
if (result == null) return null;
else return new Duo<>(result, k - 1);
}
}
156 changes: 0 additions & 156 deletions src/main/java/com/github/ijustleyxo/packmake/util/console/Console.java

This file was deleted.

This file was deleted.

Loading

0 comments on commit 866311b

Please sign in to comment.