From 7d39d09317d7aa962139ee3e3b2241b51ad07162 Mon Sep 17 00:00:00 2001 From: Christian Langel Date: Sun, 3 Dec 2017 17:59:36 +0100 Subject: [PATCH] fix: remove plot-alias with "/p alias" (#1779) * fix: remove plot-alias with "/p alias" remove plot-alias with "/p alias" #1718 * Apply requested Review Changes Restored the original Permisson to keep backwards compatibility Clean unused imports. * added some improvements after testing added some improvements after testing --- Bukkit/src/main/resources/plugin.yml | 4 + .../plot/commands/Alias.java | 84 +++++++++++++++++-- .../intellectualcrafters/plot/config/C.java | 4 + Core/src/main/resources/german.yml | 1 + Nukkit/src/main/resources/plugin.yml | 2 + 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/Bukkit/src/main/resources/plugin.yml b/Bukkit/src/main/resources/plugin.yml index a9dc4ba72..8c0c8bd9f 100644 --- a/Bukkit/src/main/resources/plugin.yml +++ b/Bukkit/src/main/resources/plugin.yml @@ -132,6 +132,8 @@ permissions: plots.set.biome: true plots.set.home: true plots.set.alias: true + plots.alias.set: true + plots.alias.remove: true plots.set.description: true plots.description: true plots.alias: true @@ -238,6 +240,8 @@ permissions: plotme.use.nameplot: children: plots.set.alias: true + plots.alias.set: true + plots.alias.remove: true plotme.limit.*: children: plots.plot.*: true diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Alias.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Alias.java index f83b0a42a..19c10e2a1 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Alias.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Alias.java @@ -2,27 +2,83 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.StringWrapper; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.UUIDHandler; import com.plotsquared.general.commands.CommandDeclaration; @CommandDeclaration( command = "setalias", - permission = "plots.set.alias", + permission = "plots.alias", description = "Set the plot name", - usage = "/plot alias ", + usage = "/plot alias ", aliases = {"alias", "sa", "name", "rename", "setname", "seta", "nameplot"}, category = CommandCategory.SETTINGS, requiredType = RequiredType.NONE) -public class Alias extends SetCommand { +public class Alias extends SubCommand { - @Override - public boolean set(PlotPlayer player, Plot plot, String alias) { + @Override + public boolean onCommand(PlotPlayer player, String[] args) { + + if (args.length == 0) { + C.COMMAND_SYNTAX.send(player, "/plot alias "); + return false; + } + + Location loc = player.getLocation(); + Plot plot = loc.getPlotAbs(); + if (plot == null) { + return !sendMessage(player, C.NOT_IN_PLOT); + } + + if (!plot.hasOwner()) { + sendMessage(player, C.PLOT_NOT_CLAIMED); + return false; + } + + if (!plot.isOwner(player.getUUID())) { + MainUtil.sendMessage(player, C.NO_PLOT_PERMS); + return false; + } + + boolean result = false; + + switch (args[0].toLowerCase()) { + case "set": + if (args.length != 2) { + C.COMMAND_SYNTAX.send(player, "/plot alias "); + return false; + } + + if(canExecuteCommand(player, C.PERMISSION_ALIAS_SET, false) || canExecuteCommand(player, C.PERMISSION_ALIAS_SET_OBSOLETE, false)) { + result = setAlias(player, plot, args[1]); + } else { + MainUtil.sendMessage(player, C.NO_PERMISSION); + } + + break; + case "remove": + if(canExecuteCommand(player, C.PERMISSION_ALIAS_REMOVE, true)) { + result = removeAlias(player, plot); + } + break; + default: + C.COMMAND_SYNTAX.send(player, "/plot alias "); + result = false; + } + + return result; + } + + + private boolean setAlias(PlotPlayer player, Plot plot, String alias) { + if (alias.isEmpty()) { - C.COMMAND_SYNTAX.send(player, getUsage()); + C.COMMAND_SYNTAX.send(player, "/plot alias "); return false; } if (alias.length() >= 50) { @@ -47,4 +103,20 @@ public class Alias extends SetCommand { MainUtil.sendMessage(player, C.ALIAS_SET_TO.s().replaceAll("%alias%", alias)); return true; } + + private boolean removeAlias(PlotPlayer player, Plot plot) { + plot.setAlias(null); + MainUtil.sendMessage(player, C.ALIAS_REMOVED.s()); + return true; + } + + private boolean canExecuteCommand(PlotPlayer player, C caption, boolean sendMessage) { + if (!Permissions.hasPermission(player, caption)) { + if(sendMessage) { + MainUtil.sendMessage(player, C.NO_PERMISSION); + } + return false; + } + return true; + } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java index 343f494bb..8658cfd10 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -156,6 +156,9 @@ public enum C { PERMISSION_SHARED("plots.visit.shared", "static.permissions"), PERMISSION_VISIT_OTHER("plots.visit.other", "static.permissions"), PERMISSION_HOME("plots.home", "static.permissions"), + PERMISSION_ALIAS_SET_OBSOLETE("plots.set.alias", "static.permissions"), // Note this is for backwards compatibility + PERMISSION_ALIAS_SET("plots.alias.set", "static.permissions"), + PERMISSION_ALIAS_REMOVE("plots.alias.remove", "static.permissions"), /* * Static console @@ -395,6 +398,7 @@ public enum C { * Alias */ ALIAS_SET_TO("$2Plot alias set to $1%alias%", "Alias"), + ALIAS_REMOVED("$2Plot alias removed", "Alias"), MISSING_ALIAS("$2You need to specify an alias", "Alias"), ALIAS_TOO_LONG("$2The alias must be < 50 characters in length", "Alias"), ALIAS_IS_TAKEN("$2That alias is already taken", "Alias"), diff --git a/Core/src/main/resources/german.yml b/Core/src/main/resources/german.yml index bcb453010..4b1f69706 100644 --- a/Core/src/main/resources/german.yml +++ b/Core/src/main/resources/german.yml @@ -110,6 +110,7 @@ ALIAS_SET_TO: "$2Plot Alias auf $1%alias% gesetzt" MISSING_ALIAS: "$2Du musst einen Alias angeben" ALIAS_TOO_LONG: "$2Der Alias darf nicht länger als 50 Zeichen sein" ALIAS_IS_TAKEN: "$2Dieser Alias wird bereits verwendet" +ALIAS_REMOVED: "$2Der Alias wurde gelöscht" MISSING_POSITION: "$2Du musst eine Position angeben. Mögliche Werte: $1none" POSITION_SET: "$1Die Position wurde auf deinen aktuellen Standort gesetzt" HOME_ARGUMENT: "$2Verwende /plot set home [none]" diff --git a/Nukkit/src/main/resources/plugin.yml b/Nukkit/src/main/resources/plugin.yml index 35f72bdc6..ffa54fa1b 100644 --- a/Nukkit/src/main/resources/plugin.yml +++ b/Nukkit/src/main/resources/plugin.yml @@ -122,6 +122,8 @@ permissions: plots.set.biome: true plots.set.home: true plots.set.alias: true + plots.alias.set: true + plots.alias.remove: true plots.set.description: true plots.description: true plots.alias: true