Fix getOnlinePlayers call to support all versions

Moved IPlayerDataManager from api module to common module - it is not part of the API as there is no supported way to obtain an instance of it.
This commit is contained in:
Jikoo 2016-12-22 09:55:51 -05:00
parent a41f89b011
commit f05110c9b8
24 changed files with 204 additions and 6 deletions

View file

@ -16,6 +16,8 @@
package com.lishid.openinv.internal;
import java.util.Collection;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@ -47,4 +49,11 @@ public interface IPlayerDataManager {
*/
public OfflinePlayer getPlayerByID(String identifier);
/**
* Gets a Collection of all Players currently online.
*
* @return the Collection of Players
*/
public Collection<? extends Player> getOnlinePlayers();
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_4_5;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_4_6;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_10_R1;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_11_R1;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_4_R1;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -70,4 +73,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_5_R2;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_5_R3;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_6_R1;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_6_R2;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_6_R3;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -69,4 +72,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_7_R1;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -70,4 +73,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,9 @@
package com.lishid.openinv.internal.v1_7_R2;
import java.util.Arrays;
import java.util.Collection;
import com.lishid.openinv.internal.IPlayerDataManager;
import org.bukkit.Bukkit;
@ -71,4 +74,9 @@ public class PlayerDataManager implements IPlayerDataManager {
return player;
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -16,6 +16,8 @@
package com.lishid.openinv.internal.v1_7_R3;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -78,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Arrays.asList(Bukkit.getOnlinePlayers());
}
}

View file

@ -17,6 +17,12 @@
<artifactId>openinvcommon</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.7.10-R0.1-snapshot</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_7_R4;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -78,4 +79,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_8_R1;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_8_R2;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_8_R3;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_9_R1;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,7 @@
package com.lishid.openinv.internal.v1_9_R2;
import java.util.Collection;
import java.util.UUID;
import com.lishid.openinv.internal.IPlayerDataManager;
@ -79,4 +80,9 @@ public class PlayerDataManager implements IPlayerDataManager {
}
}
@Override
public Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
}

View file

@ -16,6 +16,10 @@
package com.lishid.openinv;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -143,10 +147,10 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
// Register commands to their executors
getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
SearchInvPluginCommand searchInv = new SearchInvPluginCommand();
SearchInvPluginCommand searchInv = new SearchInvPluginCommand(this);
getCommand("searchinv").setExecutor(searchInv);
getCommand("searchender").setExecutor(searchInv);
getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand());
getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand(this));
getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this));
getCommand("anychest").setExecutor(new AnyChestPluginCommand(this));
@ -526,6 +530,36 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
this.pluginUsage.removeAll(plugin.getClass());
}
@SuppressWarnings("unchecked")
public Collection<? extends Player> getOnlinePlayers() {
if (this.playerLoader != null) {
return this.playerLoader.getOnlinePlayers();
}
Method getOnlinePlayers;
try {
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
} catch (Exception e) {
e.printStackTrace();
return Collections.emptyList();
}
Object onlinePlayers;
try {
onlinePlayers = getOnlinePlayers.invoke(null);
} catch (Exception e) {
e.printStackTrace();
return Collections.emptyList();
}
if (onlinePlayers instanceof List) {
return (Collection<Player>) onlinePlayers;
}
return Arrays.asList((Player[]) onlinePlayers);
}
/**
* Method for handling a Player coming online.
*

View file

@ -1,6 +1,7 @@
package com.lishid.openinv.commands;
import org.bukkit.Bukkit;
import com.lishid.openinv.OpenInv;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -19,6 +20,12 @@ import org.bukkit.inventory.meta.ItemMeta;
*/
public class SearchEnchantPluginCommand implements CommandExecutor {
private final OpenInv plugin;
public SearchEnchantPluginCommand(OpenInv plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
@ -45,7 +52,7 @@ public class SearchEnchantPluginCommand implements CommandExecutor {
}
StringBuilder players = new StringBuilder();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
for (Player player : plugin.getOnlinePlayers()) {
boolean flagInventory = containsEnchantment(player.getInventory(), enchant, level);
boolean flagEnder = containsEnchantment(player.getEnderChest(), enchant, level);

View file

@ -16,7 +16,8 @@
package com.lishid.openinv.commands;
import org.bukkit.Bukkit;
import com.lishid.openinv.OpenInv;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
@ -27,6 +28,12 @@ import org.bukkit.inventory.Inventory;
public class SearchInvPluginCommand implements CommandExecutor {
private final OpenInv plugin;
public SearchInvPluginCommand(OpenInv plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -54,7 +61,7 @@ public class SearchInvPluginCommand implements CommandExecutor {
}
StringBuilder players = new StringBuilder();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
for (Player player : plugin.getOnlinePlayers()) {
Inventory inventory = command.getName().equals("searchinv") ? player.getInventory() : player.getEnderChest();
if (inventory.contains(material, count)) {
players.append(player.getName()).append(", ");