Fix block/item readable names not having spaces

This commit is contained in:
libraryaddict 2020-08-23 20:11:59 +12:00
parent 4d0fe01bb7
commit 031666546c
4 changed files with 59 additions and 61 deletions

@ -24,8 +24,8 @@ public class DroppedItemWatcher extends FlagWatcher {
if (!getDisguise().isCustomDisguiseName()) { if (!getDisguise().isCustomDisguiseName()) {
getDisguise().setDisguiseName(TranslateType.DISGUISES.get(DisguiseType.DROPPED_ITEM.toReadable()) + " " + getDisguise().setDisguiseName(TranslateType.DISGUISES.get(DisguiseType.DROPPED_ITEM.toReadable()) + " " +
TranslateType.DISGUISE_OPTIONS_PARAMETERS TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(ReflectionManager
.get(ReflectionManager.toReadable((item == null ? Material.AIR : item.getType()).name()))); .toReadable((item == null ? Material.AIR : item.getType()).name(), " ")));
} }
} }
} }

@ -75,8 +75,7 @@ public class FallingBlockWatcher extends FlagWatcher {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, relMove, isGridLocked()); ProtocolLibrary.getProtocolManager().sendServerPacket(player, relMove, isGridLocked());
} }
} }
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -100,7 +99,7 @@ public class FallingBlockWatcher extends FlagWatcher {
if (!getDisguise().isCustomDisguiseName()) { if (!getDisguise().isCustomDisguiseName()) {
getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " + getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " +
TranslateType.DISGUISE_OPTIONS_PARAMETERS TranslateType.DISGUISE_OPTIONS_PARAMETERS
.get(ReflectionManager.toReadable(block.getType().name()))); .get(ReflectionManager.toReadable(block.getType().name(), " ")));
} }
if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) { if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) {
@ -130,7 +129,7 @@ public class FallingBlockWatcher extends FlagWatcher {
if (!getDisguise().isCustomDisguiseName()) { if (!getDisguise().isCustomDisguiseName()) {
getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " + getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " +
TranslateType.DISGUISE_OPTIONS_PARAMETERS TranslateType.DISGUISE_OPTIONS_PARAMETERS
.get(ReflectionManager.toReadable(block.getType().name()))); .get(ReflectionManager.toReadable(block.getType().name(), " ")));
} }
if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) { if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) {

@ -114,8 +114,7 @@ public class DisguiseParser {
addWatcherDefault(setMethod, getMethod, defaultValue); addWatcherDefault(setMethod, getMethod, defaultValue);
} }
} }
} } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -224,8 +223,7 @@ public class DisguiseParser {
} }
return stringBuilder.toString(); return stringBuilder.toString();
} } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -252,7 +250,7 @@ public class DisguiseParser {
} }
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm, private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm,
Collection<String> usedOptions) throws DisguiseParseException { Collection<String> usedOptions) throws DisguiseParseException {
if (!permissions.isAllowedDisguise(disguisePerm, usedOptions)) { if (!permissions.isAllowedDisguise(disguisePerm, usedOptions)) {
throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM, throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM,
@ -261,7 +259,7 @@ public class DisguiseParser {
} }
private static HashMap<String, HashMap<String, Boolean>> getDisguiseOptions(CommandSender sender, String permNode, private static HashMap<String, HashMap<String, Boolean>> getDisguiseOptions(CommandSender sender, String permNode,
DisguisePerm type) { DisguisePerm type) {
HashMap<String, HashMap<String, Boolean>> returns = new HashMap<>(); HashMap<String, HashMap<String, Boolean>> returns = new HashMap<>();
// libsdisguises.options.<command>.<disguise>.<method>.<options> // libsdisguises.options.<command>.<disguise>.<method>.<options>
@ -318,14 +316,16 @@ public class DisguiseParser {
public static DisguisePerm getDisguisePerm(String name) { public static DisguisePerm getDisguisePerm(String name) {
for (DisguisePerm perm : getDisguisePerms()) { for (DisguisePerm perm : getDisguisePerms()) {
if (!perm.toReadable().replaceAll("[ |_]", "").equalsIgnoreCase(name.replaceAll("[ |_]", ""))) if (!perm.toReadable().replaceAll("[ |_]", "").equalsIgnoreCase(name.replaceAll("[ |_]", ""))) {
continue; continue;
}
return perm; return perm;
} }
if (name.equalsIgnoreCase("p")) if (name.equalsIgnoreCase("p")) {
return getDisguisePerm(DisguiseType.PLAYER.toReadable()); return getDisguisePerm(DisguiseType.PLAYER.toReadable());
}
return null; return null;
} }
@ -361,8 +361,7 @@ public class DisguiseParser {
try { try {
Float.parseFloat(string); Float.parseFloat(string);
return true; return true;
} } catch (Exception ex) {
catch (Exception ex) {
return false; return false;
} }
} }
@ -371,8 +370,7 @@ public class DisguiseParser {
try { try {
Integer.parseInt(string); Integer.parseInt(string);
return true; return true;
} } catch (Exception ex) {
catch (Exception ex) {
return false; return false;
} }
} }
@ -383,7 +381,7 @@ public class DisguiseParser {
* Returns if command user can access the disguise creation permission type * Returns if command user can access the disguise creation permission type
*/ */
private static boolean hasPermissionOption(HashMap<String, HashMap<String, Boolean>> disguiseOptions, String method, private static boolean hasPermissionOption(HashMap<String, HashMap<String, Boolean>> disguiseOptions, String method,
String value) { String value) {
method = method.toLowerCase(); method = method.toLowerCase();
// If no permissions were defined, return true // If no permissions were defined, return true
@ -449,14 +447,14 @@ public class DisguiseParser {
} }
public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName, public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName,
String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) { String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) {
return parsePlaceholders(args, userName, userName, userSkin, targetName, targetName, targetSkin, equip, return parsePlaceholders(args, userName, userName, userSkin, targetName, targetName, targetSkin, equip,
targetEquip); targetEquip);
} }
public static String[] parsePlaceholders(String[] args, String userName, String userDisplayname, String userSkin, public static String[] parsePlaceholders(String[] args, String userName, String userDisplayname, String userSkin,
String targetName, String targetDisplayname, String targetSkin, EntityEquipment equip, String targetName, String targetDisplayname, String targetSkin,
EntityEquipment targetEquip) { EntityEquipment equip, EntityEquipment targetEquip) {
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
String arg = args[i]; String arg = args[i];
@ -580,8 +578,8 @@ public class DisguiseParser {
* @throws InvocationTargetException * @throws InvocationTargetException
*/ */
public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args, public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args,
DisguisePermissions permissions) throws DisguiseParseException, IllegalAccessException, DisguisePermissions permissions)
InvocationTargetException { throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
// Clone array so original array isn't modified // Clone array so original array isn't modified
args = Arrays.copyOf(args, args.length); args = Arrays.copyOf(args, args.length);
@ -594,8 +592,8 @@ public class DisguiseParser {
return parseDisguise(sender, null, permNode, args, permissions); return parseDisguise(sender, null, permNode, args, permissions);
} }
public static void modifyDisguise(Disguise disguise, Entity target, public static void modifyDisguise(Disguise disguise, Entity target, String[] params)
String[] params) throws IllegalAccessException, DisguiseParseException, InvocationTargetException { throws IllegalAccessException, DisguiseParseException, InvocationTargetException {
if (target != null) { if (target != null) {
params = DisguiseParser.parsePlaceholders(params, target, target); params = DisguiseParser.parsePlaceholders(params, target, target);
} }
@ -605,28 +603,28 @@ public class DisguiseParser {
new ArrayList<>(), params, "Disguise"); new ArrayList<>(), params, "Disguise");
} }
public static void modifyDisguise(Disguise disguise, public static void modifyDisguise(Disguise disguise, String[] params)
String[] params) throws IllegalAccessException, InvocationTargetException, DisguiseParseException { throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
modifyDisguise(disguise, null, params); modifyDisguise(disguise, null, params);
} }
public static void modifyDisguise(Disguise disguise, public static void modifyDisguise(Disguise disguise, String params)
String params) throws IllegalAccessException, DisguiseParseException, InvocationTargetException { throws IllegalAccessException, DisguiseParseException, InvocationTargetException {
modifyDisguise(disguise, DisguiseUtilities.split(params)); modifyDisguise(disguise, DisguiseUtilities.split(params));
} }
public static void modifyDisguise(Disguise disguise, Entity target, public static void modifyDisguise(Disguise disguise, Entity target, String params)
String params) throws IllegalAccessException, InvocationTargetException, DisguiseParseException { throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
modifyDisguise(disguise, target, DisguiseUtilities.split(params)); modifyDisguise(disguise, target, DisguiseUtilities.split(params));
} }
public static Disguise parseDisguise( public static Disguise parseDisguise(String disguise)
String disguise) throws IllegalAccessException, InvocationTargetException, DisguiseParseException { throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
return parseDisguise(Bukkit.getConsoleSender(), null, disguise); return parseDisguise(Bukkit.getConsoleSender(), null, disguise);
} }
public static Disguise parseDisguise(CommandSender sender, Entity target, public static Disguise parseDisguise(CommandSender sender, Entity target, String disguise)
String disguise) throws IllegalAccessException, InvocationTargetException, DisguiseParseException { throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise), return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise),
new DisguisePermissions(Bukkit.getConsoleSender(), "disguise")); new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"));
} }
@ -638,11 +636,10 @@ public class DisguiseParser {
* disguise has been feed a proper disguisetype. * disguise has been feed a proper disguisetype.
*/ */
public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args, public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args,
DisguisePermissions permissions) throws DisguiseParseException, IllegalAccessException, DisguisePermissions permissions)
InvocationTargetException { throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
if (!Bukkit.isPrimaryThread()) { if (!Bukkit.isPrimaryThread()) {
throw new IllegalStateException( throw new IllegalStateException("DisguiseParser should not be called async!");
"DisguiseParser should not be called async!");
} }
if (sender instanceof Player) { if (sender instanceof Player) {
@ -735,8 +732,8 @@ public class DisguiseParser {
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE); throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
} }
HashMap<String, HashMap<String, Boolean>> disguiseOptions = getDisguiseOptions(sender, permNode, HashMap<String, HashMap<String, Boolean>> disguiseOptions =
disguisePerm); getDisguiseOptions(sender, permNode, disguisePerm);
if (disguise == null) { if (disguise == null) {
if (disguisePerm.isPlayer()) { if (disguisePerm.isPlayer()) {
@ -804,8 +801,8 @@ public class DisguiseParser {
if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
if (NmsVersion.v1_13.isSupported()) { if (NmsVersion.v1_13.isSupported()) {
info = ParamInfoManager.getParamInfo(BlockData.class); info = ParamInfoManager.getParamInfo(BlockData.class);
blockData = info blockData = info.fromString(
.fromString(new ArrayList<>(Collections.singletonList(args[1]))); new ArrayList<>(Collections.singletonList(args[1])));
} else { } else {
info = ParamInfoManager.getParamInfoItemBlock(); info = ParamInfoManager.getParamInfoItemBlock();
@ -818,8 +815,7 @@ public class DisguiseParser {
itemStack = (ItemStack) info itemStack = (ItemStack) info
.fromString(new ArrayList<>(Collections.singletonList(args[1]))); .fromString(new ArrayList<>(Collections.singletonList(args[1])));
} }
} } catch (Exception ex) {
catch (Exception ex) {
break; break;
} }
@ -877,13 +873,13 @@ public class DisguiseParser {
disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
disguise = new MiscDisguise(disguisePerm.getType(), itemStack); disguise = new MiscDisguise(disguisePerm.getType(), itemStack);
if (!customName) {
name = disguise.getDisguiseName();
}
if (blockData != null && disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { if (blockData != null && disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
((FallingBlockWatcher) disguise.getWatcher()).setBlockData((BlockData) blockData); ((FallingBlockWatcher) disguise.getWatcher()).setBlockData((BlockData) blockData);
} }
if (!customName) {
name = disguise.getDisguiseName();
}
} else { } else {
disguise = new MiscDisguise(disguisePerm.getType(), miscId); disguise = new MiscDisguise(disguisePerm.getType(), miscId);
} }
@ -905,9 +901,9 @@ public class DisguiseParser {
} }
public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission, public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission,
DisguisePerm disguisePerm, Collection<String> usedOptions, String[] args, DisguisePerm disguisePerm, Collection<String> usedOptions, String[] args,
String permNode) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, String permNode)
DisguiseParseException { throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, DisguiseParseException {
Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getWatcher().getClass()); Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getWatcher().getClass());
List<String> list = new ArrayList<>(Arrays.asList(args)); List<String> list = new ArrayList<>(Arrays.asList(args));
HashMap<String, HashMap<String, Boolean>> disguiseOptions = getDisguiseOptions(sender, permNode, disguisePerm); HashMap<String, HashMap<String, Boolean>> disguiseOptions = getDisguiseOptions(sender, permNode, disguisePerm);
@ -950,14 +946,13 @@ public class DisguiseParser {
methodToUse = method; methodToUse = method;
// We've found a method which will accept a valid value, break // We've found a method which will accept a valid value, break
break; break;
} } catch (DisguiseParseException ex) {
catch (DisguiseParseException ex) {
parseException = ex; parseException = ex;
} } catch (Exception ignored) {
catch (Exception ignored) { parseException =
parseException = new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, paramInfo.getDescriptiveName(),
paramInfo.getDescriptiveName(), list.isEmpty() ? null : list.get(0), list.isEmpty() ? null : list.get(0),
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName())); TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
} }
} }

@ -1965,6 +1965,10 @@ public class ReflectionManager {
} }
public static String toReadable(String string) { public static String toReadable(String string) {
return StringUtils.join(splitReadable(string)); return toReadable(string, "");
}
public static String toReadable(String string, String joiner) {
return StringUtils.join(splitReadable(string), joiner);
} }
} }