diff --git a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java index 2a0a6b6..c897a10 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java +++ b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java @@ -44,7 +44,7 @@ public class ChipmunkMod implements ModInitializer { final File file = CONFIG_FILE; if (!file.exists()) { - InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json"); + InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("assets/chipmunkmod/default_config.json"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); final StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/ComponentMessage.java b/src/main/java/land/chipmunk/chipmunkmod/command/ComponentMessage.java deleted file mode 100644 index 8410da8..0000000 --- a/src/main/java/land/chipmunk/chipmunkmod/command/ComponentMessage.java +++ /dev/null @@ -1,26 +0,0 @@ -package land.chipmunk.chipmunkmod.command; - -import land.chipmunk.chipmunkmod.util.ComponentUtilities; -import net.kyori.adventure.text.Component; -import com.mojang.brigadier.Message; -import lombok.Getter; - -public class ComponentMessage implements Message { - @Getter private final Component component; - - private ComponentMessage (Component component) { - this.component = component; - } - - public static ComponentMessage wrap (Component component) { - return new ComponentMessage(component); - } - - public String getString () { - return ComponentUtilities.stringify(component); - } - - public String toString () { - return component.toString(); - } -} diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/TextMessage.java b/src/main/java/land/chipmunk/chipmunkmod/command/TextMessage.java new file mode 100644 index 0000000..e8b91f9 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/command/TextMessage.java @@ -0,0 +1,25 @@ +package land.chipmunk.chipmunkmod.command; + +import com.mojang.brigadier.Message; +import lombok.Getter; +import net.minecraft.text.Text; + +public class TextMessage implements Message { + @Getter private final Text text; + + private TextMessage(Text text) { + this.text = text; + } + + public static TextMessage wrap (Text text) { + return new TextMessage(text); + } + + public String getString () { + return text.getString(); + } + + public String toString () { + return text.toString(); + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java index 28faffe..cfb554e 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java @@ -1,22 +1,23 @@ package land.chipmunk.chipmunkmod.command.arguments; -import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; -import net.kyori.adventure.text.Component; -import land.chipmunk.chipmunkmod.command.ComponentMessage; -import java.util.Collection; -import java.util.Arrays; -import java.net.URL; +import land.chipmunk.chipmunkmod.command.TextMessage; +import net.minecraft.text.Text; + import java.net.MalformedURLException; +import java.net.URL; import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collection; public class LocationArgumentType implements ArgumentType { private static final Collection EXAMPLES = Arrays.asList("songs/amogus.mid", "images/cat.jpg", "videos/badapple.mp4"); - private static final SimpleCommandExceptionType OOB_FILEPATH = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("The specified file path is outside of the allowed directory"))); + private static final SimpleCommandExceptionType OOB_FILEPATH = new SimpleCommandExceptionType(TextMessage.wrap(Text.translatable("The specified file path is outside of the allowed directory"))); private boolean allowsUrls = false; private boolean allowsPaths = false; @@ -51,7 +52,7 @@ public class LocationArgumentType implements ArgumentType { try { return new URL(sb.toString()); } catch (MalformedURLException exception) { - throw new SimpleCommandExceptionType(ComponentMessage.wrap(Component.text(exception.getMessage()))).create(); + throw new SimpleCommandExceptionType(TextMessage.wrap(Text.literal(exception.getMessage()))).create(); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java index 68097b4..b12aeca 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java @@ -5,7 +5,7 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import land.chipmunk.chipmunkmod.command.CommandManager; -import land.chipmunk.chipmunkmod.command.ComponentMessage; +import land.chipmunk.chipmunkmod.command.TextMessage; import land.chipmunk.chipmunkmod.modules.SongPlayer; import land.chipmunk.chipmunkmod.song.Song; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; @@ -34,9 +34,9 @@ import static land.chipmunk.chipmunkmod.command.arguments.LocationArgumentType.* import static land.chipmunk.chipmunkmod.command.arguments.TimestampArgumentType.timestamp; public class MusicCommand { - private static SimpleCommandExceptionType NO_SONG_IS_CURRENTLY_PLAYING = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("No song is currently playing"))); - private static SimpleCommandExceptionType OOB_TIMESTAMP = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("Invalid timestamp for the current song"))); - private static SimpleCommandExceptionType DIRECTORY_DOES_NOT_EXIST = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("The specified directory does not exist"))); + private static final SimpleCommandExceptionType NO_SONG_IS_CURRENTLY_PLAYING = new SimpleCommandExceptionType(TextMessage.wrap(Text.translatable("No song is currently playing"))); + private static final SimpleCommandExceptionType OOB_TIMESTAMP = new SimpleCommandExceptionType(TextMessage.wrap(Text.translatable("Invalid timestamp for the current song"))); + private static final SimpleCommandExceptionType DIRECTORY_DOES_NOT_EXIST = new SimpleCommandExceptionType(TextMessage.wrap(Text.translatable("The specified directory does not exist"))); public static void register (CommandDispatcher dispatcher) { final MusicCommand instance = new MusicCommand(); @@ -196,8 +196,8 @@ public class MusicCommand { } final ArrayList mergedList = new ArrayList<>(); - for (Component component : directories) mergedList.add(component); - for (Component component : files) mergedList.add(component); + mergedList.addAll(directories); + mergedList.addAll(files); final Component component = Component.translatable("Songs - %s", Component.join(JoinConfiguration.separator(Component.space()), mergedList)).color(NamedTextColor.GREEN); MinecraftClient.getInstance().player.sendMessage(component); diff --git a/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderException.java b/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderException.java index 52ed2f1..dd5dc37 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderException.java +++ b/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderException.java @@ -1,24 +1,23 @@ package land.chipmunk.chipmunkmod.song; -import net.kyori.adventure.text.Component; import lombok.Getter; -import land.chipmunk.chipmunkmod.util.ComponentUtilities; +import net.minecraft.text.Text; public class SongLoaderException extends Exception { - @Getter private final Component message; + @Getter private final Text message; - public SongLoaderException (Component message) { + public SongLoaderException (Text message) { super(); this.message = message; } - public SongLoaderException (Component message, Throwable cause) { + public SongLoaderException (Text message, Throwable cause) { super(null, cause); this.message = message; } @Override public String getMessage () { - return ComponentUtilities.stringify(message); + return message.getString(); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderThread.java b/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderThread.java index 2353eb3..dc4c891 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderThread.java +++ b/src/main/java/land/chipmunk/chipmunkmod/song/SongLoaderThread.java @@ -2,9 +2,9 @@ package land.chipmunk.chipmunkmod.song; import land.chipmunk.chipmunkmod.modules.SongPlayer; import land.chipmunk.chipmunkmod.util.DownloadUtilities; -import net.kyori.adventure.text.Component; +import net.minecraft.text.Text; + import java.io.File; -import java.io.IOException; import java.net.URL; import java.nio.file.Path; import java.nio.file.Files; @@ -41,7 +41,7 @@ public class SongLoaderThread extends Thread { name = songPath.getName(); } } catch (Exception e) { - exception = new SongLoaderException(Component.text(e.getMessage()), e); + exception = new SongLoaderException(Text.literal(e.getMessage()), e); return; } @@ -58,7 +58,7 @@ public class SongLoaderThread extends Thread { } if (song == null) { - exception = new SongLoaderException(Component.translatable("Invalid song format")); + exception = new SongLoaderException(Text.translatable("Invalid song format")); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/ComponentUtilities.java b/src/main/java/land/chipmunk/chipmunkmod/util/ComponentUtilities.java deleted file mode 100644 index 519e4f0..0000000 --- a/src/main/java/land/chipmunk/chipmunkmod/util/ComponentUtilities.java +++ /dev/null @@ -1,105 +0,0 @@ -package land.chipmunk.chipmunkmod.util; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.TranslatableComponent; -import net.kyori.adventure.text.SelectorComponent; -import net.kyori.adventure.text.KeybindComponent; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ComponentUtilities { - private static final Map language = loadJsonStringMap("language.json"); - private static final Map keybinds = loadJsonStringMap("keybinds.json"); - - public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?(s|%)"); - - private ComponentUtilities () { - } - - private static Map loadJsonStringMap (String name) { - Map map = new HashMap<>(); - - InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(name); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - JsonObject json = JsonParser.parseReader(reader).getAsJsonObject(); - - for (Map.Entry entry : json.entrySet()) { - map.put(entry.getKey(), json.get(entry.getKey()).getAsString()); - } - - return map; - } - - private static String getOrReturnKey (Map map, String key) { - return map.containsKey(key) ? map.get(key) : key; - } - - public static String stringify (Component message) { - StringBuilder builder = new StringBuilder(); - - builder.append(stringifyPartially(message)); - - for (Component child : message.children()) builder.append(stringify(child)); - - return builder.toString(); - } - - public static String stringifyPartially (Component message) { - if (message instanceof TextComponent) return stringifyPartially((TextComponent) message); - if (message instanceof TranslatableComponent) return stringifyPartially((TranslatableComponent) message); - if (message instanceof SelectorComponent) return stringifyPartially((SelectorComponent) message); - if (message instanceof KeybindComponent) return stringifyPartially((KeybindComponent) message); - - return ""; - } - - public static String stringifyPartially (TextComponent message) { - return message.content(); - } - - public static String stringifyPartially (TranslatableComponent message) { - String format = getOrReturnKey(language, message.key()); - - // totallynotskidded™️ from HBot (and changed a bit) - Matcher matcher = ARG_PATTERN.matcher(format); - StringBuffer sb = new StringBuffer(); - - int i = 0; - while (matcher.find()) { - if (matcher.group().equals("%%")) { - matcher.appendReplacement(sb, "%"); - } else { - String idxStr = matcher.group(1); - int idx = idxStr == null ? i++ : (Integer.parseInt(idxStr) - 1); - if (idx >= 0 && idx < message.args().size()) { - matcher.appendReplacement(sb, Matcher.quoteReplacement( stringify(message.args().get(idx)) )); - } else { - matcher.appendReplacement(sb, ""); - } - } - } - matcher.appendTail(sb); - - return sb.toString(); - } - - public static String stringifyPartially (SelectorComponent message) { - return message.pattern(); // * Client-side selector components are equivalent to text ones, and do NOT list entities. - } - - public static String stringifyPartially (KeybindComponent message) { - String keybind = message.keybind(); - Component component = keybinds.containsKey(keybind) ? Component.translatable(keybind) : Component.text(keybind); // TODO: Fix some keys like `key.keyboard.a` - return stringifyPartially(component); - } -}