Add support for slightly older versions of Bukkit 1.7.9.

This commit is contained in:
sk89q 2015-01-08 00:25:07 -08:00
parent c3bdc33215
commit 53132ce83a
4 changed files with 57 additions and 23 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.bukkit; package com.sk89q.worldguard.bukkit;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
@ -26,28 +27,21 @@
import com.sk89q.worldedit.blocks.ItemID; import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldguard.blacklist.target.MaterialTarget; import com.sk89q.worldguard.blacklist.target.MaterialTarget;
import com.sk89q.worldguard.blacklist.target.Target; import com.sk89q.worldguard.blacklist.target.Target;
import org.bukkit.ChatColor; import org.bukkit.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.*;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.Tameable;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List; import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
public class BukkitUtil { public class BukkitUtil {
private static Method ONLINE_PLAYERS_METHOD;
private BukkitUtil() { private BukkitUtil() {
} }
@ -350,4 +344,42 @@ public static Target createTarget(Material material) {
return new MaterialTarget(material.getId(), (short) 0); return new MaterialTarget(material.getId(), (short) 0);
} }
/**
* Get a collection of the currently online players.
*
* @return The online players
*/
@SuppressWarnings("unchecked")
public static Collection<? extends Player> getOnlinePlayers() {
try {
return Bukkit.getServer().getOnlinePlayers();
} catch (NoSuchMethodError ignored) {
}
try {
if (ONLINE_PLAYERS_METHOD == null) {
ONLINE_PLAYERS_METHOD = getOnlinePlayersMethod();
}
Object result = ONLINE_PLAYERS_METHOD.invoke(Bukkit.getServer());
if (result instanceof Player[]) {
return ImmutableList.copyOf((Player[]) result);
} else if (result instanceof Collection<?>) {
return (Collection<? extends Player>) result;
} else {
throw new RuntimeException("Result of getOnlinePlayers() call was not a known data type");
}
} catch (Exception e) {
throw new RuntimeException("WorldGuard is not compatible with this version of Bukkit", e);
}
}
private static Method getOnlinePlayersMethod() throws NoSuchMethodException {
try {
return Server.class.getMethod("getOnlinePlayers");
} catch (NoSuchMethodException e1) {
return Server.class.getMethod("_INVALID_getOnlinePlayers");
}
}
} }

View File

@ -207,7 +207,7 @@ public void run() {
} }
worldListener.registerEvents(); worldListener.registerEvents();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : BukkitUtil.getOnlinePlayers()) {
ProcessPlayerEvent event = new ProcessPlayerEvent(player); ProcessPlayerEvent event = new ProcessPlayerEvent(player);
Events.fire(event); Events.fire(event);
} }
@ -510,7 +510,7 @@ public Player checkPlayer(CommandSender sender)
* @return A {@link List} of players who match {@code filter} * @return A {@link List} of players who match {@code filter}
*/ */
public List<Player> matchPlayerNames(String filter) { public List<Player> matchPlayerNames(String filter) {
Collection<? extends Player> players = getServer().getOnlinePlayers(); Collection<? extends Player> players = BukkitUtil.getOnlinePlayers();
filter = filter.toLowerCase(); filter = filter.toLowerCase();
@ -591,12 +591,12 @@ protected Iterable<? extends Player> checkPlayerMatch(List<? extends Player> pla
public Iterable<? extends Player> matchPlayers(CommandSender source, String filter) public Iterable<? extends Player> matchPlayers(CommandSender source, String filter)
throws CommandException { throws CommandException {
if (getServer().getOnlinePlayers().isEmpty()) { if (BukkitUtil.getOnlinePlayers().isEmpty()) {
throw new CommandException("No players matched query."); throw new CommandException("No players matched query.");
} }
if (filter.equals("*")) { if (filter.equals("*")) {
return checkPlayerMatch(Lists.newArrayList(getServer().getOnlinePlayers())); return checkPlayerMatch(Lists.newArrayList(BukkitUtil.getOnlinePlayers()));
} }
// Handle special hash tag groups // Handle special hash tag groups
@ -608,7 +608,7 @@ public Iterable<? extends Player> matchPlayers(CommandSender source, String filt
Player sourcePlayer = checkPlayer(source); Player sourcePlayer = checkPlayer(source);
World sourceWorld = sourcePlayer.getWorld(); World sourceWorld = sourcePlayer.getWorld();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : BukkitUtil.getOnlinePlayers()) {
if (player.getWorld().equals(sourceWorld)) { if (player.getWorld().equals(sourceWorld)) {
players.add(player); players.add(player);
} }
@ -624,7 +624,7 @@ public Iterable<? extends Player> matchPlayers(CommandSender source, String filt
org.bukkit.util.Vector sourceVector org.bukkit.util.Vector sourceVector
= sourcePlayer.getLocation().toVector(); = sourcePlayer.getLocation().toVector();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : BukkitUtil.getOnlinePlayers()) {
if (player.getWorld().equals(sourceWorld) if (player.getWorld().equals(sourceWorld)
&& player.getLocation().toVector().distanceSquared( && player.getLocation().toVector().distanceSquared(
sourceVector) < 900) { sourceVector) < 900) {
@ -916,7 +916,7 @@ public void createDefaultConfiguration(File actual,
public void broadcastNotification(String msg) { public void broadcastNotification(String msg) {
getServer().broadcast(msg, "worldguard.notify"); getServer().broadcast(msg, "worldguard.notify");
Set<Permissible> subs = getServer().getPluginManager().getPermissionSubscriptions("worldguard.notify"); Set<Permissible> subs = getServer().getPluginManager().getPermissionSubscriptions("worldguard.notify");
for (Player player : getServer().getOnlinePlayers()) { for (Player player : BukkitUtil.getOnlinePlayers()) {
if (!(subs.contains(player) && player.hasPermission("worldguard.notify")) && if (!(subs.contains(player) && player.hasPermission("worldguard.notify")) &&
hasPermission(player, "worldguard.notify")) { // Make sure the player wasn't already broadcasted to. hasPermission(player, "worldguard.notify")) { // Make sure the player wasn't already broadcasted to.
player.sendMessage(msg); player.sendMessage(msg);
@ -994,7 +994,7 @@ public PlayerMoveListener getPlayerMoveListener() {
* @return The message with macros replaced * @return The message with macros replaced
*/ */
public String replaceMacros(CommandSender sender, String message) { public String replaceMacros(CommandSender sender, String message) {
Collection<? extends Player> online = getServer().getOnlinePlayers(); Collection<? extends Player> online = BukkitUtil.getOnlinePlayers();
message = message.replace("%name%", toName(sender)); message = message.replace("%name%", toName(sender));
message = message.replace("%id%", toUniqueName(sender)); message = message.replace("%id%", toUniqueName(sender));

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.bukkit.listener; package com.sk89q.worldguard.bukkit.listener;
import com.sk89q.worldguard.bukkit.BukkitUtil;
import com.sk89q.worldguard.bukkit.ConfigurationManager; import com.sk89q.worldguard.bukkit.ConfigurationManager;
import com.sk89q.worldguard.bukkit.WorldConfiguration; import com.sk89q.worldguard.bukkit.WorldConfiguration;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -62,7 +63,7 @@ public FlagStateManager(WorldGuardPlugin plugin) {
*/ */
@Override @Override
public void run() { public void run() {
Collection<? extends Player> players = plugin.getServer().getOnlinePlayers(); Collection<? extends Player> players = BukkitUtil.getOnlinePlayers();
ConfigurationManager config = plugin.getGlobalStateManager(); ConfigurationManager config = plugin.getGlobalStateManager();
for (Player player : players) { for (Player player : players) {

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.bukkit.util.report; package com.sk89q.worldguard.bukkit.util.report;
import com.sk89q.worldguard.bukkit.BukkitUtil;
import com.sk89q.worldguard.util.report.DataReport; import com.sk89q.worldguard.util.report.DataReport;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
@ -34,7 +35,7 @@ public ServerReport() {
append("Server Name", server.getServerName()); append("Server Name", server.getServerName());
append("Bukkit Version", server.getBukkitVersion()); append("Bukkit Version", server.getBukkitVersion());
append("Implementation", server.getVersion()); append("Implementation", server.getVersion());
append("Player Count", "%d/%d", server.getOnlinePlayers().size(), server.getMaxPlayers()); append("Player Count", "%d/%d", BukkitUtil.getOnlinePlayers().size(), server.getMaxPlayers());
DataReport spawning = new DataReport("Spawning"); DataReport spawning = new DataReport("Spawning");
spawning.append("Ambient Spawn Limit", server.getAmbientSpawnLimit()); spawning.append("Ambient Spawn Limit", server.getAmbientSpawnLimit());