Unwrap potential Player proxies in commands.

This commit is contained in:
garbagemule 2015-07-17 17:50:51 +02:00
parent 79ca18aa41
commit 39196c7b61
13 changed files with 39 additions and 19 deletions

View File

@ -1,7 +1,9 @@
package com.garbagemule.MobArena.commands;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -13,6 +15,24 @@ import com.garbagemule.MobArena.util.TextUtils;
public class Commands
{
/**
* Unwrap the given CommandSender reference, in case it is a proxy.
* <p>
* Because plugins like CommandSigns use horrible proxy hacks to do what
* they need to do, a Player reference is not necessarily a real Player,
* and using that reference brings MobArena into an inconsistent state.
* <p>
* The method returns the "real" Player reference by making a UUID lookup.
*
* @param sender a CommandSender reference, possibly a proxy, non-null
* @return the real Player reference, possibly the same as the argument
*/
public static Player unwrap(CommandSender sender) {
Player proxy = (Player) sender;
UUID id = proxy.getUniqueId();
return Bukkit.getPlayer(id);
}
public static boolean isPlayer(CommandSender sender) {
return (sender instanceof Player);
}

View File

@ -27,8 +27,8 @@ public class AddArenaCommand implements Command
// Require an arena name
if (args.length != 1) return false;
// Cast the sender.
Player p = (Player) sender;
// Unwrap the sender.
Player p = Commands.unwrap(sender);
Arena arena = am.getArenaWithName(args[0]);
if (arena != null) {

View File

@ -27,8 +27,8 @@ public class AutoGenerateCommand implements Command
// Require an arena name
if (args.length != 1) return false;
// Cast the sender.
Player p = (Player) sender;
// Unwrap the sender.
Player p = Commands.unwrap(sender);
// Check if arena already exists.
Arena arena = am.getArenaWithName(args[0]);

View File

@ -46,7 +46,7 @@ public class CheckSpawnsCommand implements Command
Messenger.tell(sender, "There are no spawnpoints in the selected arena.");
return true;
}
Player p = (Player) sender;
Player p = Commands.unwrap(sender);
arena.getRegion().checkSpawns(p);
return true;
}

View File

@ -40,7 +40,7 @@ public class ClassChestCommand implements Command {
return true;
}
Player p = (Player) sender;
Player p = Commands.unwrap(sender);
Block b = p.getTargetBlock((Set<Material>) null, 10);
switch (b.getType()) {

View File

@ -31,8 +31,8 @@ public class SetClassCommand implements Command
String arg1 = (args.length > 0 ? args[0] : "");
String arg2 = (args.length > 1 ? args[1] : "");
// Cast the sender.
Player p = (Player) sender;
// Unwrap the sender.
Player p = Commands.unwrap(sender);
// Check if we're overwriting.
boolean safe = arg1.equals("safe");

View File

@ -63,7 +63,7 @@ public class SetupCommand implements Command, Listener {
return true;
}
}
Player player = (Player) sender;
Player player = Commands.unwrap(sender);
// Create the setup object
Setup setup = new Setup(player, arena);

View File

@ -24,7 +24,7 @@ public class ArenaListCommand implements Command
List<Arena> arenas;
if (Commands.isPlayer(sender)) {
Player p = (Player) sender;
Player p = Commands.unwrap(sender);
arenas = am.getPermittedArenas(p);
} else {
arenas = am.getArenas();

View File

@ -24,8 +24,8 @@ public class JoinCommand implements Command
return true;
}
// Cast the sender, grab the argument, if any.
Player p = (Player) sender;
// Unwrap the sender, grab the argument, if any.
Player p = Commands.unwrap(sender);
String arg1 = (args.length > 0 ? args[0] : null);
// Run some rough sanity checks, and grab the arena to join.

View File

@ -24,8 +24,8 @@ public class LeaveCommand implements Command
return true;
}
// Cast the sender.
Player p = (Player) sender;
// Unwrap the sender.
Player p = Commands.unwrap(sender);
Arena arena = am.getArenaWithPlayer(p);
if (arena == null) {

View File

@ -32,7 +32,7 @@ public class NotReadyCommand implements Command
return false;
}
} else if (Commands.isPlayer(sender)) {
Player p = (Player) sender;
Player p = Commands.unwrap(sender);
arena = am.getArenaWithPlayer(p);
if (arena == null) {

View File

@ -36,8 +36,8 @@ public class PickClassCommand implements Command
// Require a class name
if (args.length != 1) return false;
// Cast the sender
Player p = (Player) sender;
// Unwrap the sender
Player p = Commands.unwrap(sender);
// Make sure the player is in an arena
Arena arena = am.getArenaWithPlayer(p);

View File

@ -24,8 +24,8 @@ public class SpecCommand implements Command
return false;
}
// Cast the sender, grab the argument, if any.
Player p = (Player) sender;
// Unwrap the sender, grab the argument, if any.
Player p = Commands.unwrap(sender);
String arg1 = (args.length > 0 ? args[0] : null);
// Run some rough sanity checks, and grab the arena to spec.