Fix issue when args are null

This commit is contained in:
Myles committed 2020-06-25 12:47:12 +01:00
1 parent ed3fd39965
commit c2996a6502
11 files changed
+85 -72

No files matched your search

@@ -17,7 +17,7 @@ public class Copy {
if (p.hasPermission(Permissions.COPY)) {
String oldName = null;
String newName = null;
if (args.length < 1 || args.length > 2) {
if (args == null || args.length < 1 || args.length > 2) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss copy [old-sign-name] <new-sign-name>" + ChatColor.GRAY + ".", null);
return;
}
@@ -39,9 +39,7 @@ public class Copy {
if (!SudoSigns.signs.containsKey(newName)) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to copy to!", null);
SudoSign newSign = new SudoSign(newName);
newSign.setPermissions(oldSign.getPermissions());
newSign.setPlayerCommands(oldSign.getPlayerCommands());
newSign.setConsoleCommands(oldSign.getConsoleCommands());
newSign.copyFrom(oldSign);
SudoSigns.users.get(p.getUniqueId()).setCopy(true);
SudoSigns.signs.put(newName, newSign);
} else {
@@ -15,16 +15,19 @@ public class Delete {
*/
public static void delete(Player p, String[] args) {
if (p.hasPermission(Permissions.DELETE)) {
String name = null;
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss delete [name]" + ChatColor.GRAY + ".", null);
return;
}
if (args.length == 1) name = args[0];
if (name == null) {
String name;
if (args != null) {
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss delete [name]" + ChatColor.GRAY + ".", null);
return;
}
} else {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to delete!", null);
SudoSigns.users.get(p.getUniqueId()).setDelete(true);
} else if (SudoSigns.signs.containsKey(name)) {
return;
}
name = args[0];
if (SudoSigns.signs.containsKey(name)) {
SudoSigns.signs.remove(name);
SudoSigns.config.deleteSign(name);
Util.sudoSignsMessage(p, ChatColor.GRAY, "Sign %NAME% successfully deleted!", name);
@@ -16,16 +16,18 @@ public class Edit {
*/
public static void edit(Player p, String[] args) {
if (p.hasPermission(Permissions.EDIT)) {
String name = null;
String name;
if (args == null) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to edit!", null);
SudoSigns.users.get(p.getUniqueId()).setEdit(true);
return;
}
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss edit [name]" + ChatColor.GRAY + ".", null);
return;
}
if (args.length == 1) name = args[0];
if (name == null) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to edit!", null);
SudoSigns.users.get(p.getUniqueId()).setEdit(true);
} else if (SudoSigns.signs.containsKey(name)) {
name = args[0];
if (SudoSigns.signs.containsKey(name)) {
SignEditor editor = new SignEditor(p, SudoSigns.signs.get(name), SudoSigns.users.get(p.getUniqueId()));
SudoSigns.users.get(p.getUniqueId()).setEditor(editor);
} else {
@@ -14,24 +14,8 @@ public class Fix {
*/
public static void fix(Player p, String[] args) {
if (p.hasPermission(Permissions.FIX)) {
String name = null;
if (args.length > 2) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss fix [name]" + ChatColor.GRAY + ".", null);
return;
}
if (args.length == 1) name = args[0];
if (name != null) {
if (SudoSigns.config.getInvalidEntriesManager().fixInvalidEntry(args[0], false)) {
SudoSigns.config.loadSigns();
if (SudoSigns.signs.containsKey(args[0])) {
Util.sudoSignsMessage(p, ChatColor.GREEN, "Sign " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " successfully fixed!", null);
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "Sign " + ChatColor.GOLD + args[0] + ChatColor.RED + " was unable to be fixed!", null);
}
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "Nothing fixed! That entry is not invalid.", null);
}
} else {
String name;
if (args == null) {
int size = SudoSigns.config.getInvalidEntriesManager().getInvalidEntries().size();
if (SudoSigns.config.getInvalidEntriesManager().fixInvalidEntry(null, true)) {
SudoSigns.config.loadSigns();
@@ -43,6 +27,22 @@ public class Fix {
Util.sudoSignsMessage(p, ChatColor.GREEN, "" + ChatColor.GOLD + (SudoSigns.config.getInvalidEntriesManager().getInvalidEntries().size() - size) + " invalid entries were able to be fixed automatically!" + ChatColor.GOLD + Integer.toString(SudoSigns.config.getInvalidEntriesManager().getInvalidEntries().size()) + ChatColor.RED + " were unable to be fixed!", null);
}
}
return;
}
if (args.length > 2) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss fix [name]" + ChatColor.GRAY + ".", null);
return;
}
name = args[0];
if (SudoSigns.config.getInvalidEntriesManager().fixInvalidEntry(name, false)) {
SudoSigns.config.loadSigns();
if (SudoSigns.signs.containsKey(name)) {
Util.sudoSignsMessage(p, ChatColor.GREEN, "Sign " + ChatColor.GOLD + name + ChatColor.GREEN + " successfully fixed!", null);
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "Sign " + ChatColor.GOLD + name + ChatColor.RED + " was unable to be fixed!", null);
}
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "Nothing fixed! That entry is not invalid.", null);
}
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "You don't have permission to do this!", null);
@@ -21,15 +21,16 @@ public class Near {
*/
public static void near(Player p, String[] args) {
if (p.hasPermission(Permissions.NEAR)) {
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss near [radius]" + ChatColor.GRAY + ".", null);
return;
}
String radius = null;
if (args.length == 1) {
radius = args[0];
}
int r;
String radius = null;
if (args != null) {
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss near [radius]" + ChatColor.GRAY + ".", null);
return;
} else if (args.length == 1) {
radius = args[0];
}
}
if (radius == null) {
r = 5;
} else {
@@ -1,6 +1,7 @@
package dev.mylesmor.sudosigns.commands;
import dev.mylesmor.sudosigns.SudoSigns;
import dev.mylesmor.sudosigns.data.SudoSign;
import dev.mylesmor.sudosigns.util.Permissions;
import dev.mylesmor.sudosigns.util.Util;
import org.bukkit.ChatColor;
@@ -14,16 +15,20 @@ public class Run {
*/
public static void run(Player p, String[] args) {
if (p.hasPermission(Permissions.RUN)) {
String name = null;
String name;
if (args == null) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to run!", null);
SudoSigns.users.get(p.getUniqueId()).setRun(true);
return;
}
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss run [name]" + ChatColor.GRAY + ".", null);
return;
}
if (args.length == 1) name = args[0];
if (name == null) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to run!", null);
SudoSigns.users.get(p.getUniqueId()).setCreate(true);
} else if (SudoSigns.signs.containsKey(name)) {
name = args[0];
if (SudoSigns.signs.containsKey(name)) {
SudoSign sign = SudoSigns.signs.get(name);
Util.sudoSignsMessage(p, ChatColor.GRAY, "Executing " + ChatColor.LIGHT_PURPLE + sign.getMessages().size() + ChatColor.GRAY + " messages and " + ChatColor.LIGHT_PURPLE + (sign.getPlayerCommands().size() + sign.getConsoleCommands().size()) + ChatColor.GRAY + " commands...", null);
SudoSigns.signs.get(name).executeCommands(p);
} else {
Util.sudoSignsMessage(p, ChatColor.RED, "A sign with name %NAME% doesn't exist!", name);
@@ -15,7 +15,7 @@ public class Select {
*/
public static void select(Player p, String[] args) {
if (p.hasPermission(Permissions.SELECT)) {
if (args.length != 1) {
if (args == null || args.length != 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss select <name>" + ChatColor.GRAY + ".", null);
} else {
String message = Util.getSelectString(p, args[0]);
@@ -16,7 +16,7 @@ public class Teleport {
*/
public static void tp(Player p, String[] args) {
if (p.hasPermission(Permissions.TP)) {
if (args.length != 1) {
if (args == null || args.length != 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss tp <name>" + ChatColor.GRAY + ".", null);
return;
}
@@ -19,15 +19,18 @@ public class View {
public static void view(Player p, String[] args) {
if (p.hasPermission(Permissions.VIEW)) {
String name = null;
if (args == null) {
Util.sudoSignsMessage(p, ChatColor.GRAY, "Please click the sign you'd like to view!", null);
SudoSigns.users.get(p.getUniqueId()).setView(true);
return;
}
if (args.length > 1) {
Util.sudoSignsMessage(p, ChatColor.RED, "Invalid syntax! " + ChatColor.GRAY + "Correct syntax: " + ChatColor.LIGHT_PURPLE + "/ss view [name]" + ChatColor.GRAY + ".", null);
return;
}
if (args.length == 1) name = args[0];
name = args[0];
SudoSign sign = SudoSigns.signs.get(name);
if (name == null) {
SudoSigns.users.get(p.getUniqueId()).setDelete(true);
} else if (sign != null) {
if (sign != null) {
Location signLoc = sign.getSign().getLocation();
String locString = "x=" + signLoc.getX() + " y=" + signLoc.getY() + " z=" + signLoc.getZ();
Util.sudoSignsMessage(p, ChatColor.GRAY, "Displaying details for sign %NAME%:", name);
@@ -100,8 +100,18 @@ public class SudoSign {
messages.remove(s);
}
public void copyFrom(SudoSign s) {
this.permissions = s.getPermissions();
this.playerCommands = s.getPlayerCommands();
this.consoleCommands = s.getConsoleCommands();
this.messages = s.getMessages();
}
public ArrayList<String> getMessages() { return messages; }
public void setMessages(ArrayList<String> messages) { this.messages = messages; }
public void setPlayerCommands(HashMap<SignCommand, Boolean> playerCommands) {
this.playerCommands = playerCommands;
}
@@ -22,6 +22,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
public class SignListener implements Listener {
@@ -68,7 +69,6 @@ public class SignListener implements Listener {
Sign sign = (Sign) b.getState();
for (Map.Entry<String, SudoSign> entry : SudoSigns.signs.entrySet()) {
if (entry.getValue().getSign().equals(sign)) {
entry.getValue().executeCommands(p);
user.setSelectToCopy(false);
p.performCommand("ss copy " + entry.getKey() + " " + user.getPassThru());
return;
@@ -82,7 +82,7 @@ public class SignListener implements Listener {
}
private void runSign(Player p, PlayerInteractEvent e) {
if (e.getClickedBlock().getState() instanceof Sign) {
if (Objects.requireNonNull(e.getClickedBlock()).getState() instanceof Sign) {
if (p.hasPermission(Permissions.SELECT) && e.getAction() == Action.RIGHT_CLICK_BLOCK || !p.hasPermission(Permissions.SELECT)) {
Sign sign = (Sign) e.getClickedBlock().getState();
for (Map.Entry<String, SudoSign> entry : SudoSigns.signs.entrySet()) {
@@ -115,6 +115,7 @@ public class SignListener implements Listener {
if (b.getState() instanceof Sign) {
Sign newSign = (Sign) b.getState();
SudoSigns.signs.get(user.getPassThru()).setSign(newSign);
SudoSigns.signs.get(user.getPassThru()).addLines();
SudoSigns.config.saveToFile(SudoSigns.signs.get(user.getPassThru()), true, p);
Util.sudoSignsMessage(p, ChatColor.GRAY, "Sign has been copied to sign %NAME% successfully!", user.getPassThru());
} else {
@@ -130,7 +131,7 @@ public class SignListener implements Listener {
boolean found = false;
for (Map.Entry<String, SudoSign> entry : SudoSigns.signs.entrySet()) {
if (entry.getValue().getSign().equals(sign)) {
entry.getValue().executeCommands(p);
p.performCommand("ss run " + entry.getKey());
found = true;
}
}
@@ -149,8 +150,7 @@ public class SignListener implements Listener {
boolean found = false;
for (Map.Entry<String, SudoSign> entry : SudoSigns.signs.entrySet()) {
if (entry.getValue().getSign().equals(sign)) {
SignEditor editor = new SignEditor(p, SudoSigns.signs.get(entry.getKey()), user);
user.setEditor(editor);
p.performCommand("ss edit " + entry.getKey());
found = true;
}
}
@@ -175,9 +175,7 @@ public class SignListener implements Listener {
if (name == null) {
Util.sudoSignsMessage(p, ChatColor.RED, "This is not a SudoSign!", null);
} else {
SudoSigns.signs.remove(name);
SudoSigns.config.deleteSign(name);
Util.sudoSignsMessage(p, ChatColor.GRAY, "Sign %NAME% successfully deleted!", name);
p.performCommand("ss delete " + name);
}
} else {
Util.sudoSignsMessage(p, ChatColor.RED,"A sign wasn't clicked! Cancelling...", null);
@@ -191,14 +189,7 @@ public class SignListener implements Listener {
boolean found = false;
for (Map.Entry<String, SudoSign> entry : SudoSigns.signs.entrySet()) {
if (entry.getValue().getSign().equals(sign)) {
SudoSign ssign = SudoSigns.signs.get(entry.getKey());
Location signLoc = ssign.getSign().getLocation();
String locString = "x=" + signLoc.getX() + " y=" + signLoc.getY() + " z=" + signLoc.getZ();
Util.sudoSignsMessage(p, ChatColor.GRAY, "Displaying details for sign %NAME%:", entry.getKey());
Util.sudoSignsMessage(p, ChatColor.GRAY,"Location: " + ChatColor.LIGHT_PURPLE + locString, null);
Util.sudoSignsMessage(p, ChatColor.GRAY, "Permissions: " + ChatColor.LIGHT_PURPLE + ssign.getPermissions().size(), null);
Util.sudoSignsMessage(p, ChatColor.GRAY,"Player Commands: " + ChatColor.LIGHT_PURPLE + ssign.getPlayerCommands().size(), null);
Util.sudoSignsMessage(p, ChatColor.GRAY,"Console Commands: " + ChatColor.LIGHT_PURPLE + ssign.getConsoleCommands().size(), null);
p.performCommand("ss view " + entry.getKey());
found = true;
}
}