Merge branch 'development'

This commit is contained in:
Brianna 2019-10-23 21:14:02 -04:00
commit e32f5bab8b
8 changed files with 194 additions and 49 deletions

View File

@ -4,7 +4,7 @@ stages:
variables:
name: "SongodaCore"
path: "/builds/$CI_PROJECT_PATH"
version: "2.2.4"
version: "2.2.6"
build:
stage: build

View File

@ -251,7 +251,7 @@ public class CommandManager implements CommandExecutor, TabCompleter {
// grab the specific command that's being called
SimpleNestedCommand nested = commands.get(command.getName().toLowerCase());
if (nested != null) {
if (args.length == 0) {
if (args.length == 0 || nested.children.isEmpty()) {
return nested.parent != null ? nested.parent.onTab(sender, args) : null;
}
// check for each sub-command that they have access to

View File

@ -1171,7 +1171,7 @@ public enum CompatibleMaterial {
}
/**
* Lookup a Legacy Material by its modern id name. <br />
* Lookup a Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
@ -1183,7 +1183,7 @@ public enum CompatibleMaterial {
}
/**
* Lookup a Legacy Material by its modern id name. <br />
* Lookup a Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
@ -1196,7 +1196,7 @@ public enum CompatibleMaterial {
}
/**
* Lookup a Legacy Material by bukkit material.
* Lookup a Material by bukkit material.
*
* @param mat item to lookup
* @return LegacyMaterial or null if none found
@ -1206,7 +1206,7 @@ public enum CompatibleMaterial {
}
/**
* Lookup a Legacy Material by Itemstack.
* Lookup a Material by Itemstack.
*
* @param item item to lookup
* @return LegacyMaterial or null if none found
@ -1218,7 +1218,66 @@ public enum CompatibleMaterial {
String key = item.getType() + ":";
CompatibleMaterial m = lookupMap.get(key);
return m != null ? m : lookupMap.get(key + item.getDurability());
}
}
/**
* Lookup a Block Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
* @param name item to lookup
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(String name) {
if (name == null) {
return null;
} else if (useLegacy) {
LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(name.toUpperCase());
if (legacyBlock != null) {
return lookupMap.get(legacyBlock.name());
}
}
return lookupMap.get(name.toUpperCase());
}
/**
* Lookup a Block Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
* @param name item to lookup
* @param def default item if this is not a valid material
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(String name, CompatibleMaterial def) {
if (name == null) {
return def;
} else if (useLegacy) {
LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(name.toUpperCase());
if (legacyBlock != null) {
return lookupMap.get(legacyBlock.name());
}
}
return lookupMap.getOrDefault(name.toUpperCase(), def);
}
/**
* Lookup a Block Material by bukkit material.
*
* @param mat item to lookup
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(Material mat) {
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());
}
static LinkedHashSet<CompatibleMaterial> all = null;
@ -2057,12 +2116,14 @@ 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 POTATOES:
case POTATO:
case PUMPKIN_STEM:
case SUGAR_CANE:
case WHEAT:

View File

@ -18,6 +18,7 @@ public enum LegacyMaterialBlockType {
BIRCH_DOOR("BIRCH_DOOR", true),
FURNACE("FURNACE", "BURNING_FURNACE"),
CAKE("CAKE_BLOCK"),
CARROTS("CARROT"), // totally makes sense, lol
CAULDRON("CAULDRON_BLOCK"),
COMPARATOR("REDSTONE_COMPARATOR_OFF", "REDSTONE_COMPARATOR_ON"),
DARK_OAK_DOOR("DARK_OAK_DOOR", true),
@ -29,29 +30,33 @@ public enum LegacyMaterialBlockType {
FLOWER_POT("FLOWER_POT"),
IRON_DOOR("IRON_DOOR_BLOCK", true),
JUNGLE_DOOR("JUNGLE_DOOR", true),
LAVA("STATIONARY_LAVA"),
NETHER_WART("NETHER_WARTS"),
/*
< PURPUR_DOUBLE_SLAB
*/
POTATOES("POTATO"),
REDSTONE_LAMP("REDSTONE_LAMP_OFF", "REDSTONE_LAMP_ON"),
REDSTONE_ORE("REDSTONE_ORE", "GLOWING_REDSTONE_ORE"),
REDSTONE_TORCH("REDSTONE_TORCH_ON", "REDSTONE_TORCH_OFF"),
SPRUCE_DOOR("SPRUCE_DOOR"),
/*
< STATIONARY_LAVA,
< STATIONARY_WATER,
*/
SUGAR_CANE("SUGAR_CANE_BLOCK"),
WATER("STATIONARY_WATER"),
WHEAT("CROPS");
final String blockMaterialName;
final String alternateBlockMaterialName;
final Material blockMaterial, alternateBlockMaterial;
final boolean requiresData; // some blocks require data to render properly (double blocks)
final static Map<String, LegacyMaterialBlockType> lookupTable = new HashMap();
final static Map<String, LegacyMaterialBlockType> reverseLookupTable = new HashMap();
static {
for (LegacyMaterialBlockType t : values()) {
lookupTable.put(t.name(), t);
reverseLookupTable.put(t.blockMaterialName, t);
if(t.alternateBlockMaterialName != null) {
reverseLookupTable.put(t.alternateBlockMaterialName, t);
}
}
}
@ -99,4 +104,8 @@ public enum LegacyMaterialBlockType {
return lookupTable.get(lookup);
}
public static LegacyMaterialBlockType getFromLegacy(String lookup) {
return reverseLookupTable.get(lookup);
}
}

View File

@ -94,13 +94,13 @@ public class WorldGuardFlagHandler {
flags.put(flag, addFlag);
} catch (Exception ex) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add flag {0} to WorldGuard", addFlag.getName());
Flag wgFlag = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get(addFlag.getName());
if (wgFlag == null) {
Object wgFlag = WorldGuard.getInstance().getFlagRegistry().get(addFlag.getName());
if (wgFlag == null || !(wgFlag instanceof StateFlag)) {
wgPlugin = false;
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard");
} else {
flags.put(flag, wgFlag);
Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[]{wgFlag.getName(), wgFlag.getClass().getSimpleName()});
Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[]{((Flag) wgFlag).getName(), wgFlag.getClass().getSimpleName()});
}
}
}

View File

@ -1,8 +1,11 @@
package com.songoda.core.locale;
import com.songoda.core.configuration.Config;
import com.songoda.core.configuration.ConfigSection;
import com.songoda.core.utils.TextUtils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -17,23 +20,25 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Assists in the utilization of localization files. <br>
* Created to be used by the Songoda Team. <br>
* NOTE: Using this class in multiple plugins requires shading! <br>
* Updated 2019-09-01 to support UTF encoded lang files - jascotty2
*
* @author Brianna O'Keefe - Songoda
*/
public class Locale {
private static final Pattern NODE_PATTERN = Pattern.compile("^([^ ]+)\\s*=\\s*\"?(.*?)\"?$");
private static final Pattern OLD_NODE_PATTERN = Pattern.compile("^([^ ]+)\\s*=\\s*\"?(.*?)\"?$");
private static final String FILE_EXTENSION = ".lang";
private final Map<String, String> nodes = new HashMap<>();
@ -189,8 +194,10 @@ public class Locale {
Charset existingCharset = TextUtils.detectCharset(existingIn, StandardCharsets.UTF_8);
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(existingFile, true), existingCharset);
BufferedReader defaultReader = new BufferedReader(new InputStreamReader(defaultIn, defaultCharset));
BufferedReader existingReader = new BufferedReader(new InputStreamReader(existingIn, existingCharset));) {
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());
@ -200,7 +207,7 @@ public class Locale {
continue;
}
String key = defaultValue.split("\\s*=")[0];
String key = defaultValue.split("\\s*:")[0];
if (!existingLines.contains(key)) {
if (!changed) {
@ -259,36 +266,72 @@ public class Locale {
// load in the file!
try (FileInputStream stream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) stream, charset));) {
String line;
for (int lineNumber = 0; (line = reader.readLine()) != null; lineNumber++) {
if (lineNumber == 0){
// remove BOM markers, if any
line = line.replaceAll("[\uFEFF\uFFFE\u200B]", "");
}
// convert to UTF-8 ?
/*if (charset == StandardCharsets.UTF_16LE || charset == StandardCharsets.UTF_16BE) {
byte[] encoded = line.getBytes(charset);
line = new String(encoded, 0, encoded.length, StandardCharsets.UTF_8);
} */
if ((line = line.trim()).isEmpty() || line.startsWith("#") /* Comment */) continue;
Matcher matcher = NODE_PATTERN.matcher(line);
if (!matcher.find()) {
System.err.println("Invalid locale syntax at (line=" + lineNumber + "): " + line);
continue;
}
nodes.put(matcher.group(1), matcher.group(2));
}
BufferedReader source = new BufferedReader(new InputStreamReader((InputStream) stream, charset));
BufferedReader reader = translatePropertyToYAML(source, charset);) {
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()));
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (InvalidConfigurationException ex) {
Logger.getLogger(Locale.class.getName()).log(Level.SEVERE, "Configuration error in language file \"" + file.getName() + "\"", ex);
return false;
}
return true;
}
protected static BufferedReader translatePropertyToYAML(BufferedReader source, Charset charset) throws IOException {
StringBuilder output = new StringBuilder();
String line, line1;
for (int lineNumber = 0; (line = source.readLine()) != null; lineNumber++) {
if (lineNumber == 0) {
// remove BOM markers, if any
line1 = line;
line = line.replaceAll("[\uFEFF\uFFFE\u200B]", "");
if(line1.length() != line.length()) {
output.append(line1.substring(0, line1.length() - line.length()));
}
}
Matcher matcher;
if ((line = line.trim().replace('\r', ' ')).isEmpty() || line.startsWith("#") /* Comment */
|| !(matcher = OLD_NODE_PATTERN.matcher(line)).find()) {
output.append(line).append("\n");
} else {
output.append(matcher.group(1)).append(": \"").append(matcher.group(2)).append("\"\n");
}
}
// I hate Java sometimes because of crap like this:
return new BufferedReader(new InputStreamReader(new BufferedInputStream(new ByteArrayInputStream(output.toString().getBytes(charset))), charset));
}
protected static void translateMsgRoot(Config lang, File file, Charset charset) throws IOException {
List<String> msgs = lang.getValues(true).entrySet().stream()
.filter(e -> e.getValue() instanceof ConfigSection)
.map(e -> e.getKey())
.collect(Collectors.toList());
if (!msgs.isEmpty()) {
try (FileInputStream stream = new FileInputStream(file);
BufferedReader source = new BufferedReader(new InputStreamReader((InputStream) stream, charset))) {
String line;
for (int lineNumber = 0; (line = source.readLine()) != null; 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.
@ -317,6 +360,9 @@ public class Locale {
* @return the message for the specified node
*/
public Message getMessage(String node) {
if(this.nodes.containsKey(node + ".message")) {
node += ".message";
}
return this.getMessageOrDefault(node, node);
}
@ -328,6 +374,9 @@ public class Locale {
* @return the message for the specified node. Default if none found
*/
public Message getMessageOrDefault(String node, String defaultValue) {
if(this.nodes.containsKey(node + ".message")) {
node += ".message";
}
return supplyPrefix(new Message(this.nodes.getOrDefault(node, defaultValue)));
}

View File

@ -13,6 +13,16 @@ import org.bukkit.entity.Player;
*/
public class Message {
private static boolean canActionBar = false;
static {
try {
Class.forName("net.md_5.bungee.api.ChatMessageType");
Class.forName("net.md_5.bungee.api.chat.TextComponent");
Player.Spigot.class.getDeclaredMethod("sendMessage", net.md_5.bungee.api.ChatMessageType.class, net.md_5.bungee.api.chat.TextComponent.class);
canActionBar = true;
} catch (Exception ex) {
}
}
private String prefix = null;
private String message;
@ -70,6 +80,22 @@ public class Message {
}
}
/**
* Format and send the held message to a player as an actionbar message
*
* @param sender command sender to send the message to
*/
public void sendActionBar(CommandSender sender) {
if (!(sender instanceof Player)) {
sender.sendMessage(this.getMessage());
} else if (!canActionBar) {
sendTitle(sender);
} else {
((Player) sender).spigot().sendMessage(net.md_5.bungee.api.ChatMessageType.ACTION_BAR,
new net.md_5.bungee.api.chat.TextComponent(getMessage()));
}
}
/**
* Format and send the held message with the
* appended plugin prefix to a command sender

View File

@ -359,7 +359,7 @@ public class BlockUtils {
} else if (!useLegacy) {
return BlockUtilsModern._isCropFullyGrown(block);
}
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType());
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat == null || !mat.isCrop()) {
return false;
} else {
@ -379,7 +379,7 @@ public class BlockUtils {
} else if (!useLegacy) {
return BlockUtilsModern._getMaxGrowthStage(block);
}
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType());
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat == null || !mat.isCrop()) {
return -1;
} else {
@ -399,7 +399,7 @@ public class BlockUtils {
} else if (!useLegacy) {
return BlockUtilsModern._getMaxGrowthStage(material);
}
CompatibleMaterial mat = CompatibleMaterial.getMaterial(material);
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(material);
if (mat == null || !mat.isCrop()) {
return -1;
} else {
@ -418,7 +418,7 @@ public class BlockUtils {
} else if (!useLegacy) {
BlockUtilsModern._setGrowthStage(block, stage);
} else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType());
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop()) {
try {
legacySetBlockData.invoke(block, (byte) Math.max(0, Math.min(stage, mat == CompatibleMaterial.BEETROOTS ? 3 : 7)));
@ -439,7 +439,7 @@ public class BlockUtils {
} else if (!useLegacy) {
BlockUtilsModern._incrementGrowthStage(block);
} else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType());
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop() && block.getData() < (mat == CompatibleMaterial.BEETROOTS ? 3 : 7)) {
try {
legacySetBlockData.invoke(block, (byte) (block.getData() + 1));
@ -460,7 +460,7 @@ public class BlockUtils {
} else if (!useLegacy) {
BlockUtilsModern._resetGrowthStage(block);
} else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType());
CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop()) {
try {
legacySetBlockData.invoke(block, (byte) 0);