From 4578f40ffadc4c845b4bc62ad1352cee640d04e8 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 27 Oct 2019 12:56:25 -0500 Subject: [PATCH 1/5] add block method for compatible material lookup --- .../compatibility/CompatibleMaterial.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java b/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java index 4c434c3c..e5e9d73b 100644 --- a/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java +++ b/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java @@ -6,6 +6,7 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; @@ -1220,6 +1221,27 @@ public enum CompatibleMaterial { return m != null ? m : lookupMap.get(key + item.getDurability()); } + /** + * Lookup a Material by Block, corrected for legacy + * + * @param block block to check + * @return LegacyMaterial or null if none found + */ + public static CompatibleMaterial getMaterial(Block block) { + if (block == null) { + return null; + } + Material mat = block.getType(); + if(mat == null) return null; + else if (useLegacy) { + LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(mat.name()); + if (legacyBlock != null) { + return lookupMap.get(legacyBlock.name()); + } + } + return lookupMap.get(mat.name()); + } + /** * Lookup a Block Material by its modern id name.
* This also can grab materials by their legacy, but only if there is no From 448a92eebeeae30ef1638e7f88c92060338a29e8 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 27 Oct 2019 13:11:38 -0500 Subject: [PATCH 2/5] revert 312044c8 - issue was in implementation, not in core --- .../com/songoda/core/compatibility/CompatibleMaterial.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java b/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java index e5e9d73b..d5b14c5e 100644 --- a/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java +++ b/Core/src/main/java/com/songoda/core/compatibility/CompatibleMaterial.java @@ -2138,14 +2138,12 @@ public enum CompatibleMaterial { case BEETROOTS: case CACTUS: case CARROTS: - case CARROT: case CHORUS_FLOWER: // FROSTED_ICE is Ageable, but not a crop case KELP: case MELON_STEM: case NETHER_WART: - case POTATOES: - case POTATO: + case POTATOES: case PUMPKIN_STEM: case SUGAR_CANE: case WHEAT: From 039d73598a0eaeeb1ee59c15f61a12bce7c35ffc Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 27 Oct 2019 15:52:52 -0500 Subject: [PATCH 3/5] handle comments with newlines as individual lines, add anonymous config --- .../java/com/songoda/core/configuration/Comment.java | 11 ++++------- .../java/com/songoda/core/configuration/Config.java | 7 +++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/configuration/Comment.java b/Core/src/main/java/com/songoda/core/configuration/Comment.java index bbc97482..206471b9 100644 --- a/Core/src/main/java/com/songoda/core/configuration/Comment.java +++ b/Core/src/main/java/com/songoda/core/configuration/Comment.java @@ -23,24 +23,21 @@ public class Comment { } public Comment(String... lines) { - this.lines.addAll(Arrays.asList(lines)); + this(null, Arrays.asList(lines)); } public Comment(List lines) { - if (lines != null) { - this.lines.addAll(lines); - } + this(null, lines); } public Comment(CommentStyle commentStyle, String... lines) { - this.commentStyle = commentStyle; - this.lines.addAll(Arrays.asList(lines)); + this(commentStyle, Arrays.asList(lines)); } public Comment(CommentStyle commentStyle, List lines) { this.commentStyle = commentStyle; if (lines != null) { - this.lines.addAll(lines); + lines.forEach(s -> this.lines.addAll(Arrays.asList(s.split("\n")))); } } diff --git a/Core/src/main/java/com/songoda/core/configuration/Config.java b/Core/src/main/java/com/songoda/core/configuration/Config.java index d7909b19..0cee70c2 100644 --- a/Core/src/main/java/com/songoda/core/configuration/Config.java +++ b/Core/src/main/java/com/songoda/core/configuration/Config.java @@ -109,6 +109,13 @@ public class Config extends ConfigSection { */ int commentSpacing = 1; + public Config() { + this.plugin = null; + this.file = null; + dirName = null; + fileName = null; + } + public Config(@NotNull File file) { this.plugin = null; this.file = file.getAbsoluteFile(); From 5d82f2d55f6b82a3c4e7cb3abe7f9243d43cc6d8 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 27 Oct 2019 15:53:56 -0500 Subject: [PATCH 4/5] fix yaml lang support, add multiline scalars and a list method --- .../java/com/songoda/core/locale/Locale.java | 122 ++++++++++++------ .../java/com/songoda/core/locale/Message.java | 11 ++ 2 files changed, 92 insertions(+), 41 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/locale/Locale.java b/Core/src/main/java/com/songoda/core/locale/Locale.java index 73fcf37f..cf9da6d6 100644 --- a/Core/src/main/java/com/songoda/core/locale/Locale.java +++ b/Core/src/main/java/com/songoda/core/locale/Locale.java @@ -184,64 +184,70 @@ public class Locale { // Write new changes to existing files, if any at all private static boolean updateFiles(Plugin plugin, InputStream defaultFile, File existingFile, boolean builtin) { - boolean changed = false; - List defaultLines, existingLines; try (BufferedInputStream defaultIn = new BufferedInputStream(defaultFile); BufferedInputStream existingIn = new BufferedInputStream(new FileInputStream(existingFile))) { Charset defaultCharset = TextUtils.detectCharset(defaultIn, StandardCharsets.UTF_8); Charset existingCharset = TextUtils.detectCharset(existingIn, StandardCharsets.UTF_8); - try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(existingFile, true), existingCharset); - BufferedReader defaultReaderOriginal = new BufferedReader(new InputStreamReader(defaultIn, defaultCharset)); + try (BufferedReader defaultReaderOriginal = new BufferedReader(new InputStreamReader(defaultIn, defaultCharset)); BufferedReader existingReaderOriginal = new BufferedReader(new InputStreamReader(existingIn, existingCharset)); BufferedReader defaultReader = translatePropertyToYAML(defaultReaderOriginal, defaultCharset); BufferedReader existingReader = translatePropertyToYAML(existingReaderOriginal, existingCharset);) { - defaultLines = defaultReader.lines().map(s -> s.replaceAll("[\uFEFF\uFFFE\u200B]", "")).collect(Collectors.toList()); - existingLines = existingReader.lines().map(s -> s.replaceAll("[\uFEFF\uFFFE\u200B]", "").split("\\s*=")[0]).collect(Collectors.toList()); - for (String defaultValue : defaultLines) { + Config existingLang = new Config(existingFile); + existingLang.load(existingReader); + translateMsgRoot(existingLang, existingFile, existingCharset); - if (defaultValue.isEmpty() || defaultValue.startsWith("#")) { + Config defaultLang = new Config(); + String defaultData = defaultReader.lines().map(s -> s.replaceAll("[\uFEFF\uFFFE\u200B]", "")).collect(Collectors.joining("\n")); + defaultLang.loadFromString(defaultData); + translateMsgRoot(defaultLang, defaultData, defaultCharset); + + List added = new ArrayList(); + + for (String defaultValueKey : defaultLang.getKeys(true)) { + + Object val = defaultLang.get(defaultValueKey); + if (val instanceof ConfigSection) { continue; } - String key = defaultValue.split("\\s*:")[0]; - - if (!existingLines.contains(key)) { - if (!changed) { - writer.write("\n\n"); - // Leave a note alerting the user of the newly added messages. - writer.write("# New messages for " + plugin.getName() + " v" + plugin.getDescription().getVersion() + "."); - - // If changes were found outside of the default file leave a note explaining that. - if (!builtin) { - writer.write("\n"); - writer.write("# These translations were found untranslated, join\n"); - writer.write("# our translation Discord https://discord.gg/f7fpZEf\n"); - writer.write("# to request an official update!\n"); - } - } - - writer.write("\n"); - - // re-encode to target format? (transform down, not up) -// if (defaultCharset != existingCharset) { -// byte[] encoded = defaultValue.getBytes(defaultCharset); -// defaultValue = new String(encoded, 2, encoded.length - 2, existingCharset); -// } - writer.write(defaultValue); - - changed = true; + if (!existingLang.contains(defaultValueKey)) { + added.add(defaultValueKey); + existingLang.set(defaultValueKey, val); } } + + if (!added.isEmpty()) { + if (!builtin) { + existingLang.setHeader("New messages added for " + plugin.getName() + " v" + plugin.getDescription().getVersion() + ".", + "", + "These translations were found untranslated, join", + "our translation Discord https://discord.gg/f7fpZEf", + "to request an official update!", + "", + added.stream().collect(Collectors.joining("\n")) + ); + } else { + existingLang.setHeader("New messages added for " + plugin.getName() + " v" + plugin.getDescription().getVersion() + ".", + "", + added.stream().collect(Collectors.joining("\n")) + ); + } + existingLang.setRootNodeSpacing(0); + existingLang.save(); + } + return !added.isEmpty(); + } catch (InvalidConfigurationException ex) { + plugin.getLogger().log(Level.SEVERE, "Error checking config " + existingFile.getName(), ex); } } catch (IOException e) { return false; } - return changed; + return false; } /** @@ -271,8 +277,11 @@ public class Locale { Config lang = new Config(file); lang.load(reader); translateMsgRoot(lang, file, charset); - // todo: how should string lists be handled? - lang.getValues(true).forEach((k, v) -> nodes.put(k, v.toString())); + // load lists as strings with newlines + lang.getValues(true).forEach((k, v) -> nodes.put(k, + v instanceof List + ? (((List) v).stream().map(l -> l.toString()).collect(Collectors.joining("\n")).toString()) + : v.toString())); } catch (IOException e) { e.printStackTrace(); return false; @@ -296,9 +305,15 @@ public class Locale { } } Matcher matcher; - if ((line = line.trim().replace('\r', ' ')).isEmpty() || line.startsWith("#") /* Comment */ - || !(matcher = OLD_NODE_PATTERN.matcher(line)).find()) { - output.append(line).append("\n"); + if ((line = line.replace('\r', ' ')).trim().isEmpty() || line.trim().startsWith("#") /* Comment */ + // need to trim the search group because tab characters somehow ended up at the end of lines in a lot of these files + || !(matcher = OLD_NODE_PATTERN.matcher(line.trim())).find()) { + if (line.startsWith("//")) { + // someone used an improper comment in some files *grumble grumble* + output.append("#").append(line).append("\n"); + } else { + output.append(line).append("\n"); + } } else { output.append(matcher.group(1)).append(": \"").append(matcher.group(2)).append("\"\n"); } @@ -333,6 +348,31 @@ public class Locale { } } + protected static void translateMsgRoot(Config lang, String file, Charset charset) throws IOException { + List msgs = lang.getValues(true).entrySet().stream() + .filter(e -> e.getValue() instanceof ConfigSection) + .map(e -> e.getKey()) + .collect(Collectors.toList()); + if (!msgs.isEmpty()) { + String source[] = file.split("\n"); + String line; + for (int lineNumber = 0; lineNumber < source.length; lineNumber++) { + line = source[lineNumber]; + if (lineNumber == 0) { + // remove BOM markers, if any + line = line.replaceAll("[\uFEFF\uFFFE\u200B]", ""); + } + Matcher matcher; + if (!(line = line.trim()).isEmpty() && !line.startsWith("#") + && (matcher = OLD_NODE_PATTERN.matcher(line)).find()) { + if (msgs.contains(matcher.group(1))) { + lang.set(matcher.group(1) + ".message", matcher.group(2)); + } + } + } + } + } + /** * Supply the Message object with the plugins prefix. * diff --git a/Core/src/main/java/com/songoda/core/locale/Message.java b/Core/src/main/java/com/songoda/core/locale/Message.java index 9561441d..8472f4f6 100644 --- a/Core/src/main/java/com/songoda/core/locale/Message.java +++ b/Core/src/main/java/com/songoda/core/locale/Message.java @@ -1,6 +1,8 @@ package com.songoda.core.locale; import com.songoda.core.compatibility.ServerVersion; +import java.util.Arrays; +import java.util.List; import java.util.regex.Matcher; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -126,6 +128,15 @@ public class Message { return ChatColor.translateAlternateColorCodes('&', this.message); } + /** + * Get and format the held message + * + * @return the message + */ + public List getMessageLines() { + return Arrays.asList(ChatColor.translateAlternateColorCodes('&', this.message).split("\n|\\|")); + } + /** * Get the held message * From f522ff1ac9497e01ecdd9ef56ec5f80203f56fd6 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 27 Oct 2019 15:54:26 -0500 Subject: [PATCH 5/5] version 2.2.7 --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bc2c7f3d..f0b34429 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "SongodaCore" path: "/builds/$CI_PROJECT_PATH" - version: "2.2.6" + version: "2.2.7" build: stage: build