Permalink
Browse files

A lot more work towards abstract commands

  • Loading branch information...
me4502 committed Dec 18, 2018
1 parent 947f6ee commit 15999bec37824b26514368e87cc9828b75bafb21
Showing with 97 additions and 190 deletions.
  1. +2 −2 .../bukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/MessageFutureCallback.java
  2. +1 −1 ...guard/bukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/MessageTimerTask.java
  3. +6 −4 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitPlayer.java
  4. +0 −46 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitUtil.java
  5. +4 −3 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java
  6. +2 −1 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java
  7. +0 −31 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java
  8. +1 −0 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/AsyncCommandHelper.java
  9. +2 −2 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/CommandUtils.java
  10. +1 −0 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/FutureProgressListener.java
  11. +18 −23 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/GeneralCommands.java
  12. +26 −38 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/ToggleCommands.java
  13. +5 −4 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/WorldGuardCommands.java
  14. +9 −10 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/region/MemberCommands.java
  15. +20 −25 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/region/RegionCommands.java
@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands;
package com.sk89q.worldguard.commands;

import static com.google.common.base.Preconditions.checkNotNull;

@@ -83,7 +83,7 @@ public Builder onFailure(@Nullable String message) {
}

public <V> MessageFutureCallback<V> build() {
return new MessageFutureCallback<V>(worldGuard, sender, success, failure);
return new MessageFutureCallback<>(worldGuard, sender, success, failure);
}
}

@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands;
package com.sk89q.worldguard.commands;

import static com.google.common.base.Preconditions.checkNotNull;

@@ -30,8 +30,8 @@
public class BukkitPlayer extends com.sk89q.worldedit.bukkit.BukkitPlayer implements LocalPlayer {

private final WorldGuardPlugin plugin;
private final String name;
private final boolean silenced;
private String name;

public BukkitPlayer(WorldGuardPlugin plugin, Player player) {
this(plugin, player, false);
@@ -40,13 +40,15 @@ public BukkitPlayer(WorldGuardPlugin plugin, Player player) {
BukkitPlayer(WorldGuardPlugin plugin, Player player, boolean silenced) {
super((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"), player);
this.plugin = plugin;
// getName() takes longer than before in newer versions of Minecraft
this.name = player == null ? null : player.getName();
this.silenced = silenced;
}

@Override
public String getName() {
if (this.name == null) {
// getName() takes longer than before in newer versions of Minecraft
this.name = getPlayer().getName();
}
return name;
}

@@ -148,7 +150,7 @@ public void resetPlayerTime() {
@Override
public void printRaw(String msg) {
if (!silenced) {
getPlayer().sendMessage(msg);
super.printRaw(msg);
}
}

@@ -65,52 +65,6 @@ public static int getPotionEffectBits(ItemStack item) {
return item.getDurability() & 0x3F;
}

/**
* Replace color macros in a string. The macros are in the form of `[char]
* where char represents the color. R is for red, Y is for yellow,
* G is for green, C is for cyan, B is for blue, and P is for purple.
* The uppercase versions of those are the darker shades, while the
* lowercase versions are the lighter shades. For white, it's 'w', and
* 0-2 are black, dark grey, and grey, respectively.
*
* @param str
* @return color-coded string
*/
public static String replaceColorMacros(String str) {
str = str.replace("&r", ChatColor.RED.toString());
str = str.replace("&R", ChatColor.DARK_RED.toString());

str = str.replace("&y", ChatColor.YELLOW.toString());
str = str.replace("&Y", ChatColor.GOLD.toString());

str = str.replace("&g", ChatColor.GREEN.toString());
str = str.replace("&G", ChatColor.DARK_GREEN.toString());

str = str.replace("&c", ChatColor.AQUA.toString());
str = str.replace("&C", ChatColor.DARK_AQUA.toString());

str = str.replace("&b", ChatColor.BLUE.toString());
str = str.replace("&B", ChatColor.DARK_BLUE.toString());

str = str.replace("&p", ChatColor.LIGHT_PURPLE.toString());
str = str.replace("&P", ChatColor.DARK_PURPLE.toString());

str = str.replace("&0", ChatColor.BLACK.toString());
str = str.replace("&1", ChatColor.DARK_GRAY.toString());
str = str.replace("&2", ChatColor.GRAY.toString());
str = str.replace("&w", ChatColor.WHITE.toString());

str = str.replace("&k", ChatColor.MAGIC.toString());
str = str.replace("&l", ChatColor.BOLD.toString());
str = str.replace("&m", ChatColor.STRIKETHROUGH.toString());
str = str.replace("&n", ChatColor.UNDERLINE.toString());
str = str.replace("&o", ChatColor.ITALIC.toString());

str = str.replace("&x", ChatColor.RESET.toString());

return str;
}

/**
* Returns whether an entity should be removed for the halt activity mode.
*
@@ -27,6 +27,7 @@
import com.sk89q.worldedit.world.entity.EntityTypes;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.blacklist.Blacklist;
import com.sk89q.worldguard.blacklist.BlacklistLoggerHandler;
import com.sk89q.worldguard.blacklist.logger.ConsoleHandler;
@@ -141,7 +142,7 @@ public void loadConfiguration() {
opPermissions = getBoolean("op-permissions", true);

buildPermissions = getBoolean("build-permission-nodes.enable", false);
buildPermissionDenyMessage = CommandUtils.replaceColorMacros(
buildPermissionDenyMessage = WorldGuard.getInstance().getPlatform().replaceColorMacros(
getString("build-permission-nodes.deny-message", "&eSorry, but you are not permitted to do that here."));

strictEntitySpawn = getBoolean("event-handling.block-entity-spawns-with-untraceable-cause", false);
@@ -433,9 +434,9 @@ public ChestProtection getChestProtection() {
return chestProtection;
}

public int getMaxRegionCount(Player player) {
public int getMaxRegionCount(LocalPlayer player) {
int max = -1;
for (String group : plugin.getGroups(player)) {
for (String group : player.getGroups()) {
if (maxRegionCounts.containsKey(group)) {
int groupMax = maxRegionCounts.get(group);
if (max < groupMax) {
@@ -26,6 +26,7 @@
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.commands.CommandUtils;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.flags.FlagContext;
import com.sk89q.worldguard.bukkit.protection.events.flags.FlagContextCreateEvent;
@@ -66,7 +67,7 @@ public World getWorldByName(String worldName) {

@Override
public String replaceColorMacros(String string) {
return BukkitUtil.replaceColorMacros(string);
return CommandUtils.replaceColorMacros(string);
}

public String replaceMacros(Actor sender, String message) {
@@ -277,37 +277,6 @@ public boolean inGroup(Player player, String group) {
}
}

/**
* Gets the name of a command sender. This is a unique name and this
* method should never return a "display name".
*
* @param sender The sender to get the name of
* @return The unique name of the sender.
*/
public String toUniqueName(CommandSender sender) {
if (sender instanceof ConsoleCommandSender) {
return "*Console*";
} else {
return sender.getName();
}
}

/**
* Gets the name of a command sender. This play be a display name.
*
* @param sender The CommandSender to get the name of.
* @return The name of the given sender
*/
public String toName(CommandSender sender) {
if (sender instanceof ConsoleCommandSender) {
return "*Console*";
} else if (sender instanceof Player) {
return ((Player) sender).getDisplayName();
} else {
return sender.getName();
}
}

/**
* Checks permissions.
*
@@ -26,6 +26,7 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.commands.MessageFutureCallback;
import com.sk89q.worldguard.util.task.FutureForwardingTask;

import javax.annotation.Nullable;
@@ -155,9 +155,9 @@ public static String getOwnerName(@Nullable Object owner) {
* @param sender the sender
* @return a function
*/
public static java.util.function.Function<String, ?> messageFunction(final CommandSender sender) {
public static java.util.function.Function<String, ?> messageFunction(final Actor sender) {
return (Function<String, Object>) s -> {
sender.sendMessage(s);
sender.printRaw(s);
return null;
};
}
@@ -24,6 +24,7 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldguard.commands.MessageTimerTask;
import org.bukkit.ChatColor;

import java.util.Timer;
@@ -19,6 +19,7 @@

package com.sk89q.worldguard.bukkit.commands;

import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
@@ -27,12 +28,9 @@
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.config.ConfigurationManager;
import com.sk89q.worldguard.session.Session;
import com.sk89q.worldguard.session.handler.GodMode;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

@@ -43,7 +41,6 @@ public GeneralCommands(WorldGuard worldGuard) {
this.worldGuard = worldGuard;
}

@SuppressWarnings("deprecation")
@Command(aliases = {"god"}, usage = "[player]",
desc = "Enable godmode on a player", flags = "s", max = 1)
public void god(CommandContext args, Actor sender) throws CommandException, AuthorizationException {
@@ -57,7 +54,7 @@ public void god(CommandContext args, Actor sender) throws CommandException, Auth
targets = plugin.matchPlayers(worldGuard.checkPlayer(sender));

// Check permissions!
sender.hasPermission("worldguard.god");
sender.checkPermission("worldguard.god");
} else {
targets = plugin.matchPlayers(sender, args.getString(0));

@@ -78,7 +75,7 @@ public void god(CommandContext args, Actor sender) throws CommandException, Auth
// Keep track of this
included = true;
} else {
player.print("God enabled by " + plugin.toName(sender) + ".");
player.print("God enabled by " + sender.getDisplayName() + ".");

}
}
@@ -91,7 +88,6 @@ public void god(CommandContext args, Actor sender) throws CommandException, Auth
}
}

@SuppressWarnings("deprecation")
@Command(aliases = {"ungod"}, usage = "[player]",
desc = "Disable godmode on a player", flags = "s", max = 1)
public void ungod(CommandContext args, Actor sender) throws CommandException, AuthorizationException {
@@ -124,7 +120,7 @@ public void ungod(CommandContext args, Actor sender) throws CommandException, Au
// Keep track of this
included = true;
} else {
player.print("God disabled by " + plugin.toName(sender) + ".");
player.print("God disabled by " + sender.getDisplayName() + ".");

}
}
@@ -138,67 +134,66 @@ public void ungod(CommandContext args, Actor sender) throws CommandException, Au
}

@Command(aliases = {"heal"}, usage = "[player]", desc = "Heal a player", flags = "s", max = 1)
public void heal(CommandContext args,CommandSender sender) throws CommandException {
public void heal(CommandContext args, Actor sender) throws CommandException, AuthorizationException {

Iterable<? extends Player> targets = null;
Iterable<? extends LocalPlayer> targets = null;
boolean included = false;

// Detect arguments based on the number of arguments provided
if (args.argsLength() == 0) {
targets = plugin.matchPlayers(plugin.checkPlayer(sender));

// Check permissions!
plugin.checkPermission(sender, "worldguard.heal");
sender.checkPermission("worldguard.heal");
} else if (args.argsLength() == 1) {
targets = plugin.matchPlayers(sender, args.getString(0));

// Check permissions!
plugin.checkPermission(sender, "worldguard.heal.other");
sender.checkPermission("worldguard.heal.other");
}

for (Player player : targets) {
for (LocalPlayer player : targets) {
player.setHealth(player.getMaxHealth());
player.setFoodLevel(20);
player.setSaturation(20);
player.setExhaustion(0);

// Tell the user
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "Healed!");
player.print("Healed!");

// Keep track of this
included = true;
} else {
player.sendMessage(ChatColor.YELLOW + "Healed by "
+ plugin.toName(sender) + ".");
player.print("Healed by " + sender.getDisplayName() + ".");

}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included && args.hasFlag('s')) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players healed.");
sender.print("Players healed.");
}
}

@Command(aliases = {"slay"}, usage = "[player]", desc = "Slay a player", flags = "s", max = 1)
public void slay(CommandContext args, Actor sender) throws CommandException {
public void slay(CommandContext args, Actor sender) throws CommandException, AuthorizationException {

Iterable<? extends LocalPlayer> targets = null;
Iterable<? extends LocalPlayer> targets = Lists.newArrayList();
boolean included = false;

// Detect arguments based on the number of arguments provided
if (args.argsLength() == 0) {
targets = plugin.matchPlayers(worldGuard.checkPlayer(sender));

// Check permissions!
sender.hasPermission("worldguard.slay");
sender.checkPermission("worldguard.slay");
} else if (args.argsLength() == 1) {
targets = plugin.matchPlayers(sender, args.getString(0));

// Check permissions!
sender.hasPermission("worldguard.slay.other");
sender.checkPermission("worldguard.slay.other");
}

for (LocalPlayer player : targets) {
@@ -211,15 +206,15 @@ public void slay(CommandContext args, Actor sender) throws CommandException {
// Keep track of this
included = true;
} else {
player.print("Slain by " + plugin.toName(sender) + ".");
player.print("Slain by " + sender.getDisplayName() + ".");

}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included && args.hasFlag('s')) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players slain.");
sender.print("Players slain.");
}
}

Oops, something went wrong.

0 comments on commit 15999be

Please sign in to comment.