Repository navigation
Skin custom 3D masks for RPCharacters #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ public enum PackKind { | |
| ITEM_3D, | ||
| SHIELD, | ||
| HELMET_3D, | ||
| MASK, | ||
| GUN, | ||
| BOOK | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/net/tfminecraft/armourshop/pack/writer/mask/MasksYml.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package net.tfminecraft.armourshop.pack.writer.mask; | ||
|
|
||
| import net.tfminecraft.armourshop.pack.util.YamlUtil; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Upsert / remove RPCharacters {@code custom-masks.yml} entries (Bukkit-free). | ||
| * Each entry is the ItemsAdder path of an approved mask skin. | ||
| */ | ||
| public final class MasksYml { | ||
|
|
||
| private MasksYml() {} | ||
|
|
||
| public static void upsert(Path masksYml, String slug, String namespace) | ||
| throws IOException | ||
| { | ||
| if (masksYml == null) { | ||
| throw new IllegalArgumentException("masksYml is null"); | ||
| } | ||
| String key = requireSlug(slug); | ||
| String item = "ia." + requireNamespace(namespace) + ":" + key; | ||
| Path parent = masksYml.getParent(); | ||
| if (parent != null) { | ||
| Files.createDirectories(parent); | ||
| } | ||
| Map<String, String> entries = read(masksYml); | ||
| entries.put(key, item); | ||
| write(masksYml, entries); | ||
| } | ||
|
|
||
| public static boolean remove(Path masksYml, String slug) throws IOException { | ||
| if (masksYml == null || !Files.isRegularFile(masksYml)) { | ||
| return false; | ||
| } | ||
| String key = requireSlug(slug); | ||
| Map<String, String> entries = read(masksYml); | ||
| if (entries.remove(key) == null) { | ||
| return false; | ||
| } | ||
| write(masksYml, entries); | ||
| return true; | ||
| } | ||
|
|
||
| static Map<String, String> read(Path masksYml) throws IOException { | ||
| Map<String, String> entries = new LinkedHashMap<>(); | ||
| if (masksYml == null || !Files.isRegularFile(masksYml)) { | ||
| return entries; | ||
| } | ||
| String current = null; | ||
| for (String raw : Files.readString(masksYml, StandardCharsets.UTF_8).split("\\R")) { | ||
| String line = raw.stripTrailing(); | ||
| if (line.startsWith(" ") && !line.startsWith(" ") && line.endsWith(":")) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| String key = line.substring(2, line.length() - 1).trim(); | ||
| current = key.isEmpty() ? null : key; | ||
| continue; | ||
| } | ||
| if (current != null && line.startsWith(" item:")) { | ||
| String item = line.substring(" item:".length()).trim(); | ||
| if (!item.isEmpty()) { | ||
| entries.put(current, item); | ||
| } | ||
| current = null; | ||
| } | ||
| } | ||
| return entries; | ||
| } | ||
|
|
||
| private static void write(Path masksYml, Map<String, String> entries) throws IOException { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("# Custom RP masks written by ArmourShop. Loaded by RPCharacters with masks.yml.\n"); | ||
| sb.append("masks:\n"); | ||
| for (Map.Entry<String, String> entry : entries.entrySet()) { | ||
| sb.append(" ").append(entry.getKey()).append(":\n"); | ||
| sb.append(" item: ").append(entry.getValue()).append('\n'); | ||
| } | ||
| Files.writeString(masksYml, sb.toString(), StandardCharsets.UTF_8); | ||
|
Drefvelin marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static String requireSlug(String slug) { | ||
| if (slug == null || slug.isBlank()) { | ||
| throw new IllegalArgumentException("missing slug"); | ||
| } | ||
| String key = slug.trim(); | ||
| YamlUtil.validateSlug(key); | ||
| return key; | ||
| } | ||
|
|
||
| private static String requireNamespace(String namespace) { | ||
| if (namespace == null || namespace.isBlank()) { | ||
| throw new IllegalArgumentException("missing namespace"); | ||
| } | ||
| String ns = namespace.trim(); | ||
| for (int i = 0; i < ns.length(); i++) { | ||
| char c = ns.charAt(i); | ||
| boolean ok = (c >= 'a' && c <= 'z') | ||
| || (c >= '0' && c <= '9') | ||
| || c == '_' | ||
| || c == '-'; | ||
| if (!ok) { | ||
| throw new IllegalArgumentException("invalid namespace: " + namespace); | ||
| } | ||
| } | ||
| return ns; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/net/tfminecraft/armourshop/pack/writer/mask/MasksYmlTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package net.tfminecraft.armourshop.pack.writer.mask; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class MasksYmlTest { | ||
|
|
||
| @Test | ||
| void upsertAndRemoveKeepOtherMasks(@TempDir Path dir) throws Exception { | ||
| Path file = dir.resolve("custom-masks.yml"); | ||
| MasksYml.upsert(file, "oni_mask", "tfmc_submissions"); | ||
| MasksYml.upsert(file, "ghost_mask", "tfmc_armorshop"); | ||
|
|
||
| assertEquals( | ||
| "ia.tfmc_submissions:oni_mask", | ||
| MasksYml.read(file).get("oni_mask") | ||
| ); | ||
| assertEquals( | ||
| "ia.tfmc_armorshop:ghost_mask", | ||
| MasksYml.read(file).get("ghost_mask") | ||
| ); | ||
|
|
||
| assertTrue(MasksYml.remove(file, "oni_mask")); | ||
| assertFalse(MasksYml.read(file).containsKey("oni_mask")); | ||
| assertEquals( | ||
| "ia.tfmc_armorshop:ghost_mask", | ||
| MasksYml.read(file).get("ghost_mask") | ||
| ); | ||
| assertFalse(MasksYml.remove(file, "oni_mask")); | ||
| } | ||
|
|
||
| @Test | ||
| void rewriteReplacesTheSameSlug(@TempDir Path dir) throws Exception { | ||
| Path file = dir.resolve("custom-masks.yml"); | ||
| MasksYml.upsert(file, "oni_mask", "tfmc_submissions"); | ||
| MasksYml.upsert(file, "oni_mask", "tfmc_armorshop"); | ||
| assertEquals(1, MasksYml.read(file).size()); | ||
| assertEquals( | ||
| "ia.tfmc_armorshop:oni_mask", | ||
| MasksYml.read(file).get("oni_mask") | ||
| ); | ||
| assertTrue(Files.readString(file).contains("masks:")); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/test/java/net/tfminecraft/armourshop/pack/writer/model3d/Item3dWriterMaskTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package net.tfminecraft.armourshop.pack.writer.model3d; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import net.tfminecraft.armourshop.pack.model.PackKind; | ||
| import net.tfminecraft.armourshop.pack.model.PackSubmission; | ||
|
|
||
| class Item3dWriterMaskTest { | ||
|
|
||
| @Test | ||
| void maskYamlIsAnUnplaceableHat() { | ||
| String yaml = Item3dWriter.buildYaml( | ||
| new PackSubmission("oni_mask", "Oni", PackKind.MASK, Map.of()), | ||
| "CARVED_PUMPKIN", | ||
| null, | ||
| true, | ||
| "tfmc_submissions" | ||
| ); | ||
| assertTrue(yaml.contains("material: CARVED_PUMPKIN")); | ||
| assertTrue(yaml.contains("generate: false")); | ||
| assertTrue(yaml.contains("hat: true")); | ||
| assertTrue(yaml.contains("model_path: item/oni_mask")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.