Have DisguiseParseException throw a properly formatted (colored) error.

This commit is contained in:
libraryaddict 2021-10-04 18:25:51 +13:00
parent 9a91b91966
commit 0a7a2a5276
12 changed files with 38 additions and 31 deletions

View File

@ -47,9 +47,7 @@ public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter
disguise = DisguiseParser
.parseDisguise(sender, (Entity) sender, getPermNode(), DisguiseUtilities.split(StringUtils.join(args, " ")), getPermissions(sender));
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
} catch (Throwable ex) {

View File

@ -51,9 +51,7 @@ public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCom
try {
testDisguise = DisguiseParser.parseTestDisguise(sender, getPermNode(), disguiseArgs, getPermissions(sender));
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
} catch (Throwable ex) {

View File

@ -81,9 +81,7 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom
try {
disguise = DisguiseParser.parseDisguise(sender, entityTarget, getPermNode(), DisguiseUtilities.split(StringUtils.join(newArgs, " ")), permissions);
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
} catch (Throwable ex) {
ex.printStackTrace();

View File

@ -215,9 +215,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom
LibsMsg.DRADIUS_MISCDISG.send(sender, miscDisguises);
}
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
} catch (Throwable ex) {
ex.printStackTrace();
}

View File

@ -38,10 +38,7 @@ public class DisguiseEntityInteraction implements LibsEntityInteract {
try {
disguise = DisguiseParser.parseDisguise(p, entity, "disguiseentity", disguiseArgs, DisguiseParser.getPermissions(p, "disguiseentity"));
} catch (DisguiseParseException e) {
if (e.getMessage() != null) {
DisguiseUtilities.sendMessage(p, e.getMessage());
}
e.send(p);
return;
} catch (Throwable e) {
e.printStackTrace();

View File

@ -56,9 +56,7 @@ public class DisguiseModifyInteraction implements LibsEntityInteract {
DisguiseParser.callMethods(p, disguise, perms, disguisePerm, new ArrayList<>(Arrays.asList(options)), options, "DisguiseModifyEntity");
LibsMsg.LISTENER_MODIFIED_DISG.send(p);
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(p, ex.getMessage());
}
ex.send(p);
} catch (Throwable ex) {
ex.printStackTrace();
}

View File

@ -62,9 +62,7 @@ public class DisguiseModifyCommand extends DisguiseBaseCommand implements TabCom
"DisguiseModify");
}
catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
}

View File

@ -91,9 +91,7 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements
"DisguiseModifyPlayer");
}
catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
}

View File

@ -168,9 +168,7 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), tempArgs, "DisguiseModifyRadius");
modifiedDisguises++;
} catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, ex.getMessage());
}
ex.send(sender);
return true;
} catch (Throwable ex) {

View File

@ -72,7 +72,7 @@ public class SaveDisguiseCommand implements CommandExecutor {
LibsMsg.CUSTOM_DISGUISE_SAVED.send(sender, name);
} catch (DisguiseParseException e) {
if (e.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, e.getMessage());
e.send(sender);
} else {
LibsMsg.PARSE_CANT_LOAD.send(sender);
}
@ -171,7 +171,7 @@ public class SaveDisguiseCommand implements CommandExecutor {
DisguiseUtilities.setSaveDisguiseCommandUsed();
} catch (DisguiseParseException e) {
if (e.getMessage() != null) {
DisguiseUtilities.sendMessage(sender, e.getMessage());
e.send(sender);
} else {
LibsMsg.PARSE_CANT_LOAD.send(sender);
}

View File

@ -1,19 +1,27 @@
package me.libraryaddict.disguise.utilities.parser;
import lombok.Getter;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.bukkit.command.CommandSender;
/**
* Created by libraryaddict on 7/09/2018.
*/
@Getter
public class DisguiseParseException extends Exception {
private static final long serialVersionUID = 1276971370793124510L;
private LibsMsg msg;
private String[] params;
public DisguiseParseException() {
super();
}
public DisguiseParseException(LibsMsg message, String... params) {
super(message.get(params));
super(message.getVanillaFormat(params));
this.msg = message;
this.params = params;
}
public DisguiseParseException(String message) {
@ -27,4 +35,12 @@ public class DisguiseParseException extends Exception {
public DisguiseParseException(Throwable throwable) {
super(throwable);
}
public void send(CommandSender sender) {
if (this.getMsg() == null) {
return;
}
this.msg.send(sender, (Object[]) params);
}
}

View File

@ -302,6 +302,16 @@ public enum LibsMsg {
this.string = string;
}
public String getVanillaFormat(String[] params) {
String raw = get((Object[]) params);
for (ChatColor c : ChatColor.values()) {
raw = raw.replace("<" + c.name().toLowerCase(Locale.ROOT) + ">", "§" + c.getChar());
}
return raw;
}
public String getVanillaFormat() {
String raw = getRaw();