2012-02-20 20:09:44 +00:00
|
|
|
/*
|
2018-02-04 15:29:07 +00:00
|
|
|
* Copyright (C) 2011-2018 lishid. All rights reserved.
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2012-02-20 20:09:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2018-02-04 15:29:07 +00:00
|
|
|
* the Free Software Foundation, version 3.
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2012-02-20 20:09:44 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2018-02-04 15:29:07 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-02-20 20:09:44 +00:00
|
|
|
* GNU General Public License for more details.
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2012-02-20 20:09:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2011-06-03 14:32:54 +00:00
|
|
|
|
2012-12-13 03:41:18 +00:00
|
|
|
package com.lishid.openinv;
|
2011-06-03 14:32:54 +00:00
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
import com.google.common.collect.HashMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
2016-11-25 21:42:06 +00:00
|
|
|
import com.lishid.openinv.commands.AnyChestPluginCommand;
|
|
|
|
import com.lishid.openinv.commands.OpenEnderPluginCommand;
|
|
|
|
import com.lishid.openinv.commands.OpenInvPluginCommand;
|
2016-11-27 11:20:44 +00:00
|
|
|
import com.lishid.openinv.commands.SearchEnchantPluginCommand;
|
2016-11-25 21:42:06 +00:00
|
|
|
import com.lishid.openinv.commands.SearchInvPluginCommand;
|
|
|
|
import com.lishid.openinv.commands.SilentChestPluginCommand;
|
|
|
|
import com.lishid.openinv.internal.IAnySilentContainer;
|
2016-11-25 13:01:44 +00:00
|
|
|
import com.lishid.openinv.internal.IInventoryAccess;
|
|
|
|
import com.lishid.openinv.internal.ISpecialEnderChest;
|
|
|
|
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
2016-12-01 02:26:56 +00:00
|
|
|
import com.lishid.openinv.listeners.InventoryClickListener;
|
2017-07-04 17:22:03 +00:00
|
|
|
import com.lishid.openinv.listeners.InventoryCloseListener;
|
2016-12-01 02:26:56 +00:00
|
|
|
import com.lishid.openinv.listeners.InventoryDragListener;
|
|
|
|
import com.lishid.openinv.listeners.PlayerListener;
|
2016-12-16 20:13:04 +00:00
|
|
|
import com.lishid.openinv.listeners.PluginListener;
|
2016-11-25 21:42:06 +00:00
|
|
|
import com.lishid.openinv.util.Cache;
|
2016-12-01 02:26:56 +00:00
|
|
|
import com.lishid.openinv.util.ConfigUpdater;
|
2016-11-25 21:42:06 +00:00
|
|
|
import com.lishid.openinv.util.Function;
|
2016-12-01 02:26:56 +00:00
|
|
|
import com.lishid.openinv.util.InternalAccessor;
|
|
|
|
import com.lishid.openinv.util.Permissions;
|
2019-04-29 00:37:05 +00:00
|
|
|
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;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
import javax.annotation.Nullable;
|
2016-11-25 21:42:06 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.command.PluginCommand;
|
|
|
|
import org.bukkit.entity.HumanEntity;
|
2016-11-25 13:01:44 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2016-11-25 21:42:06 +00:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2016-12-15 00:49:18 +00:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2016-11-25 13:01:44 +00:00
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2016-11-25 21:42:06 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2019-04-29 00:37:05 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2016-11-25 13:01:44 +00:00
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
/**
|
|
|
|
* Open other player's inventory
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2016-11-25 21:42:06 +00:00
|
|
|
* @author lishid
|
|
|
|
*/
|
2016-12-01 02:26:56 +00:00
|
|
|
public class OpenInv extends JavaPlugin implements IOpenInv {
|
2016-03-04 02:45:54 +00:00
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
private final Map<String, ISpecialPlayerInventory> inventories = new HashMap<String, ISpecialPlayerInventory>();
|
|
|
|
private final Map<String, ISpecialEnderChest> enderChests = new HashMap<String, ISpecialEnderChest>();
|
2016-12-15 00:49:18 +00:00
|
|
|
private final Multimap<String, Class<? extends Plugin>> pluginUsage = HashMultimap.create();
|
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
private final Cache<String, Player> playerCache = new Cache<String, Player>(300000L,
|
|
|
|
new Function<Player>() {
|
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public boolean run(final Player value) {
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = OpenInv.this.accessor.getPlayerDataManager().getPlayerDataID(value);
|
2017-05-26 09:48:26 +00:00
|
|
|
return OpenInv.this.inventories.containsKey(key)
|
|
|
|
&& OpenInv.this.inventories.get(key).isInUse()
|
|
|
|
|| OpenInv.this.enderChests.containsKey(key)
|
|
|
|
&& OpenInv.this.enderChests.get(key).isInUse()
|
|
|
|
|| OpenInv.this.pluginUsage.containsKey(key);
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
2017-05-28 13:53:59 +00:00
|
|
|
}, new Function<Player>() {
|
2016-11-25 21:42:06 +00:00
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public boolean run(final Player value) {
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = OpenInv.this.accessor.getPlayerDataManager().getPlayerDataID(value);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
// Check if inventory is stored, and if it is, remove it and eject all viewers
|
2017-05-26 09:48:26 +00:00
|
|
|
if (OpenInv.this.inventories.containsKey(key)) {
|
2019-05-05 16:40:47 +00:00
|
|
|
Inventory inv = OpenInv.this.inventories.remove(key).getBukkitInventory();
|
2018-02-07 23:17:33 +00:00
|
|
|
List<HumanEntity> viewers = inv.getViewers();
|
2019-04-29 00:37:05 +00:00
|
|
|
for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) {
|
2016-11-25 21:42:06 +00:00
|
|
|
entity.closeInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if ender chest is stored, and if it is, remove it and eject all viewers
|
2017-05-26 09:48:26 +00:00
|
|
|
if (OpenInv.this.enderChests.containsKey(key)) {
|
2019-05-05 16:40:47 +00:00
|
|
|
Inventory inv = OpenInv.this.enderChests.remove(key).getBukkitInventory();
|
2018-02-07 23:17:33 +00:00
|
|
|
List<HumanEntity> viewers = inv.getViewers();
|
2019-04-29 00:37:05 +00:00
|
|
|
for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) {
|
2016-11-25 21:42:06 +00:00
|
|
|
entity.closeInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OpenInv.this.disableSaving() && !value.isOnline()) {
|
|
|
|
value.saveData();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2016-04-12 04:33:44 +00:00
|
|
|
|
2016-11-25 13:01:44 +00:00
|
|
|
private InternalAccessor accessor;
|
2012-12-13 03:41:18 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
/**
|
|
|
|
* Evicts all viewers lacking cross-world permissions from a Player's inventory.
|
|
|
|
*
|
|
|
|
* @param player the Player
|
|
|
|
*/
|
|
|
|
public void changeWorld(final Player player) {
|
2016-03-10 02:37:28 +00:00
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
// Check if the player is cached. If not, neither of their inventories is open.
|
|
|
|
if (!this.playerCache.containsKey(key)) {
|
2016-11-25 21:42:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.inventories.containsKey(key)) {
|
2019-05-05 16:40:47 +00:00
|
|
|
Iterator<HumanEntity> iterator = this.inventories.get(key).getBukkitInventory().getViewers().iterator();
|
2017-05-26 09:48:26 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
HumanEntity human = iterator.next();
|
|
|
|
// If player has permission or is in the same world, allow continued access
|
|
|
|
// Just in case, also allow null worlds.
|
|
|
|
if (Permissions.CROSSWORLD.hasPermission(human) || human.getWorld() == null
|
|
|
|
|| human.getWorld().equals(player.getWorld())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
human.closeInventory();
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 02:37:28 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.enderChests.containsKey(key)) {
|
2019-05-05 16:40:47 +00:00
|
|
|
Iterator<HumanEntity> iterator = this.enderChests.get(key).getBukkitInventory().getViewers().iterator();
|
2017-05-26 09:48:26 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
HumanEntity human = iterator.next();
|
|
|
|
if (Permissions.CROSSWORLD.hasPermission(human) || human.getWorld() == null
|
|
|
|
|| human.getWorld().equals(player.getWorld())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
human.closeInventory();
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 19:23:53 +00:00
|
|
|
}
|
2013-12-07 09:44:40 +00:00
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public boolean disableSaving() {
|
|
|
|
return this.getConfig().getBoolean("settings.disable-saving", false);
|
2012-01-12 19:23:53 +00:00
|
|
|
}
|
2013-12-07 09:44:40 +00:00
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
@NotNull
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2016-11-25 21:42:06 +00:00
|
|
|
public IAnySilentContainer getAnySilentContainer() {
|
2017-06-08 22:36:01 +00:00
|
|
|
return this.accessor.getAnySilentContainer();
|
2011-09-19 00:44:57 +00:00
|
|
|
}
|
2013-12-07 09:44:40 +00:00
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
@NotNull
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public IInventoryAccess getInventoryAccess() {
|
2017-06-08 22:36:01 +00:00
|
|
|
return this.accessor.getInventoryAccess();
|
2016-05-04 04:38:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:53:59 +00:00
|
|
|
private int getLevenshteinDistance(final String string1, final String string2) {
|
|
|
|
if (string1 == null || string2 == null) {
|
|
|
|
throw new IllegalArgumentException("Strings must not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string1.isEmpty()) {
|
|
|
|
return string2.length();
|
|
|
|
}
|
|
|
|
if (string2.isEmpty()) {
|
|
|
|
return string2.length();
|
|
|
|
}
|
|
|
|
if (string1.equals(string2)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len1 = string1.length();
|
|
|
|
int len2 = string2.length();
|
|
|
|
|
|
|
|
int[] prevDistances = new int[len1 + 1];
|
|
|
|
int[] distances = new int[len1 + 1];
|
|
|
|
|
|
|
|
for (int i = 0; i <= len1; ++i) {
|
|
|
|
prevDistances[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i <= len2; ++i) {
|
|
|
|
// TODO: include tweaks available in Simmetrics?
|
|
|
|
char string2char = string2.charAt(i - 1);
|
|
|
|
distances[0] = i;
|
|
|
|
|
|
|
|
for (int j = 1; j <= len1; ++j) {
|
|
|
|
int cost = string1.charAt(j - 1) == string2char ? 0 : 1;
|
|
|
|
|
|
|
|
distances[j] = Math.min(Math.min(distances[j - 1] + 1, prevDistances[j] + 1), prevDistances[j - 1] + cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] swap = prevDistances;
|
|
|
|
prevDistances = distances;
|
|
|
|
distances = swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prevDistances[len1];
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public Collection<? extends Player> getOnlinePlayers() {
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
if (this.accessor.isSupported()) {
|
|
|
|
return this.accessor.getPlayerDataManager().getOnlinePlayers();
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
2016-06-09 12:41:21 +00:00
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public boolean getPlayerAnyChestStatus(@NotNull final OfflinePlayer player) {
|
2017-11-06 01:00:22 +00:00
|
|
|
boolean defaultState = false;
|
|
|
|
|
|
|
|
if (player.isOnline()) {
|
|
|
|
Player onlinePlayer = player.getPlayer();
|
|
|
|
if (onlinePlayer != null) {
|
2017-11-08 22:37:22 +00:00
|
|
|
defaultState = Permissions.ANY_DEFAULT.hasPermission(onlinePlayer);
|
2017-11-06 01:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getConfig().getBoolean("toggles.any-chest." + this.accessor.getPlayerDataManager().getPlayerDataID(player), defaultState);
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
2016-05-04 04:38:10 +00:00
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
@NotNull
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public String getPlayerID(@NotNull final OfflinePlayer offline) {
|
2017-06-08 22:36:01 +00:00
|
|
|
return this.accessor.getPlayerDataManager().getPlayerDataID(offline);
|
2016-05-04 04:38:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public boolean getPlayerSilentChestStatus(@NotNull final OfflinePlayer offline) {
|
2017-11-06 01:00:22 +00:00
|
|
|
boolean defaultState = false;
|
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
if (offline.isOnline()) {
|
|
|
|
Player onlinePlayer = offline.getPlayer();
|
2017-11-06 01:00:22 +00:00
|
|
|
if (onlinePlayer != null) {
|
2017-11-08 22:37:22 +00:00
|
|
|
defaultState = Permissions.SILENT_DEFAULT.hasPermission(onlinePlayer);
|
2017-11-06 01:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
return this.getConfig().getBoolean("toggles.silent-chest." + this.accessor.getPlayerDataManager().getPlayerDataID(offline), defaultState);
|
2017-06-08 22:36:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
@NotNull
|
2017-06-08 22:36:01 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public ISpecialEnderChest getSpecialEnderChest(@NotNull final Player player, final boolean online)
|
2017-06-08 22:36:01 +00:00
|
|
|
throws InstantiationException {
|
|
|
|
String id = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
|
|
|
if (this.enderChests.containsKey(id)) {
|
|
|
|
return this.enderChests.get(id);
|
|
|
|
}
|
|
|
|
ISpecialEnderChest inv = this.accessor.newSpecialEnderChest(player, online);
|
|
|
|
this.enderChests.put(id, inv);
|
|
|
|
this.playerCache.put(id, player);
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:37:05 +00:00
|
|
|
@NotNull
|
2017-06-08 22:36:01 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public ISpecialPlayerInventory getSpecialInventory(@NotNull final Player player, final boolean online)
|
2017-06-08 22:36:01 +00:00
|
|
|
throws InstantiationException {
|
|
|
|
String id = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
|
|
|
if (this.inventories.containsKey(id)) {
|
|
|
|
return this.inventories.get(id);
|
|
|
|
}
|
|
|
|
ISpecialPlayerInventory inv = this.accessor.newSpecialPlayerInventory(player, online);
|
|
|
|
this.inventories.put(id, inv);
|
|
|
|
this.playerCache.put(id, player);
|
|
|
|
return inv;
|
2016-05-04 04:38:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public boolean isSupportedVersion() {
|
|
|
|
return this.accessor != null && this.accessor.isSupported();
|
2016-05-04 04:38:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
@Nullable
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public Player loadPlayer(@NotNull final OfflinePlayer offline) {
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(offline);
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.playerCache.containsKey(key)) {
|
|
|
|
return this.playerCache.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wrap Player to ensure all methods can safely be called offline
|
|
|
|
Player loaded;
|
|
|
|
|
|
|
|
if (offline.isOnline()) {
|
|
|
|
loaded = offline.getPlayer();
|
|
|
|
this.playerCache.put(key, loaded);
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
if (!this.isSupportedVersion()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (Bukkit.isPrimaryThread()) {
|
2017-06-08 22:36:01 +00:00
|
|
|
return this.accessor.getPlayerDataManager().loadPlayer(offline);
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Player> future = Bukkit.getScheduler().callSyncMethod(this,
|
|
|
|
new Callable<Player>() {
|
|
|
|
@Override
|
2018-02-04 15:29:07 +00:00
|
|
|
public Player call() {
|
2017-06-08 22:36:01 +00:00
|
|
|
return OpenInv.this.accessor.getPlayerDataManager().loadPlayer(offline);
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
int ticks = 0;
|
|
|
|
while (!future.isDone() && !future.isCancelled() && ticks < 10) {
|
|
|
|
++ticks;
|
|
|
|
try {
|
|
|
|
Thread.sleep(50L);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!future.isDone() || future.isCancelled()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
loaded = future.get();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loaded != null) {
|
|
|
|
this.playerCache.put(key, loaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return loaded;
|
2016-11-26 20:31:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
@Nullable
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public OfflinePlayer matchPlayer(@NotNull final String name) {
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
// Warn if called on the main thread - if we resort to searching offline players, this may take several seconds.
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.getServer().isPrimaryThread()) {
|
|
|
|
this.getLogger().warning("Call to OpenInv#matchPlayer made on the main thread!");
|
|
|
|
this.getLogger().warning("This can cause the server to hang, potentially severely.");
|
|
|
|
this.getLogger().warning("Trace:");
|
2016-11-25 21:42:06 +00:00
|
|
|
for (StackTraceElement element : new Throwable().fillInStackTrace().getStackTrace()) {
|
2017-05-26 09:48:26 +00:00
|
|
|
this.getLogger().warning(element.toString());
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
OfflinePlayer player;
|
2016-11-26 20:31:53 +00:00
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
if (this.isSupportedVersion()) {
|
|
|
|
// Attempt exact offline match first - adds UUID support for later versions
|
|
|
|
player = this.accessor.getPlayerDataManager().getPlayerByID(name);
|
|
|
|
|
|
|
|
if (player != null) {
|
|
|
|
return player;
|
|
|
|
}
|
2016-11-27 11:20:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
// Ensure name is valid if server is in online mode to avoid unnecessary searching
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.getServer().getOnlineMode() && !name.matches("[a-zA-Z0-9_]{3,16}")) {
|
2016-11-25 21:42:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
player = this.getServer().getPlayerExact(name);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
if (player != null) {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
player = this.getServer().getOfflinePlayer(name);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compatibility: Pre-UUID, getOfflinePlayer always returns an OfflinePlayer. Post-UUID,
|
|
|
|
* getOfflinePlayer will return null if no matching player is found. To preserve
|
|
|
|
* compatibility, only return the player if they have played before. Ignoring current online
|
|
|
|
* status is fine, they'd have been found by getPlayerExact otherwise.
|
|
|
|
*/
|
|
|
|
if (player != null && player.hasPlayedBefore()) {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
player = this.getServer().getPlayer(name);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
if (player != null) {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bestMatch = Integer.MAX_VALUE;
|
2017-05-26 09:48:26 +00:00
|
|
|
for (OfflinePlayer offline : this.getServer().getOfflinePlayers()) {
|
2016-11-25 21:42:06 +00:00
|
|
|
if (offline.getName() == null) {
|
|
|
|
// Loaded by UUID only, name has never been looked up.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:53:59 +00:00
|
|
|
int currentMatch = this.getLevenshteinDistance(name, offline.getName());
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
if (currentMatch == 0) {
|
|
|
|
return offline;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentMatch < bestMatch) {
|
|
|
|
bestMatch = currentMatch;
|
|
|
|
player = offline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only null if no players have played ever, otherwise even the worst match will do.
|
|
|
|
return player;
|
2012-12-13 03:41:18 +00:00
|
|
|
}
|
2013-12-07 09:44:40 +00:00
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
@Override
|
2017-05-26 09:48:26 +00:00
|
|
|
public boolean notifyAnyChest() {
|
|
|
|
return this.getConfig().getBoolean("notify.any-chest", true);
|
|
|
|
}
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
|
|
|
public boolean notifySilentChest() {
|
|
|
|
return this.getConfig().getBoolean("notify.silent-chest", true);
|
|
|
|
}
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
|
|
|
|
|
|
|
if (this.disableSaving()) {
|
|
|
|
return;
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
if (this.isSupportedVersion()) {
|
|
|
|
this.playerCache.invalidateAll();
|
|
|
|
}
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
// Get plugin manager
|
|
|
|
PluginManager pm = this.getServer().getPluginManager();
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
this.accessor = new InternalAccessor(this);
|
|
|
|
// Version check
|
|
|
|
if (!this.accessor.isSupported()) {
|
|
|
|
this.getLogger().info("Your version of CraftBukkit (" + this.accessor.getVersion() + ") is not supported.");
|
|
|
|
this.getLogger().info("If this version is a recent release, check for an update.");
|
|
|
|
this.getLogger().info("If this is an older version, ensure that you've downloaded the legacy support version.");
|
|
|
|
pm.disablePlugin(this);
|
|
|
|
return;
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 23:17:33 +00:00
|
|
|
this.saveDefaultConfig();
|
2017-05-26 09:48:26 +00:00
|
|
|
new ConfigUpdater(this).checkForUpdates();
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
// Register listeners
|
|
|
|
pm.registerEvents(new PlayerListener(this), this);
|
|
|
|
pm.registerEvents(new PluginListener(this), this);
|
|
|
|
pm.registerEvents(new InventoryClickListener(this), this);
|
2017-07-04 17:22:03 +00:00
|
|
|
pm.registerEvents(new InventoryCloseListener(this), this);
|
2017-05-26 09:48:26 +00:00
|
|
|
// Bukkit will handle missing events for us, attempt to register InventoryDragEvent without a version check
|
|
|
|
pm.registerEvents(new InventoryDragListener(this), this);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
// Register commands to their executors
|
|
|
|
this.getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
|
|
|
|
this.getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
|
|
|
|
SearchInvPluginCommand searchInv = new SearchInvPluginCommand(this);
|
|
|
|
this.getCommand("searchinv").setExecutor(searchInv);
|
|
|
|
this.getCommand("searchender").setExecutor(searchInv);
|
|
|
|
this.getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand(this));
|
|
|
|
this.getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this));
|
|
|
|
this.getCommand("anychest").setExecutor(new AnyChestPluginCommand(this));
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
public void releaseAllPlayers(final Plugin plugin) {
|
2019-04-28 16:11:59 +00:00
|
|
|
Iterator<Map.Entry<String, Class<? extends Plugin>>> iterator = this.pluginUsage.entries().iterator();
|
|
|
|
|
|
|
|
if (!iterator.hasNext()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Map.Entry<String, Class<? extends Plugin>> entry = iterator.next(); iterator.hasNext(); entry = iterator.next()) {
|
|
|
|
if (entry.getValue().equals(plugin.getClass())) {
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 03:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 00:49:18 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public void releasePlayer(@NotNull final Player player, @NotNull final Plugin plugin) {
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
2016-12-15 00:49:18 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (!this.pluginUsage.containsEntry(key, plugin.getClass())) {
|
2016-12-15 00:49:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
this.pluginUsage.remove(key, plugin.getClass());
|
2016-12-15 00:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public void retainPlayer(@NotNull final Player player, @NotNull final Plugin plugin) {
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
2016-12-15 00:49:18 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.pluginUsage.containsEntry(key, plugin.getClass())) {
|
2016-12-15 00:49:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
this.pluginUsage.put(key, plugin.getClass());
|
2016-12-15 00:49:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public void setPlayerAnyChestStatus(@NotNull final OfflinePlayer offline, final boolean status) {
|
|
|
|
this.getConfig().set("toggles.any-chest." + this.accessor.getPlayerDataManager().getPlayerDataID(offline), status);
|
2017-05-26 09:48:26 +00:00
|
|
|
this.saveConfig();
|
2016-12-16 20:13:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
/**
|
|
|
|
* Method for handling a Player going offline.
|
|
|
|
*
|
|
|
|
* @param player the Player
|
2017-06-08 22:36:01 +00:00
|
|
|
* @throws IllegalStateException if the server version is unsupported
|
2017-05-26 09:48:26 +00:00
|
|
|
*/
|
|
|
|
public void setPlayerOffline(final Player player) {
|
2016-12-22 14:55:51 +00:00
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
2016-12-22 14:55:51 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
// Check if the player is cached. If not, neither of their inventories is open.
|
|
|
|
if (!this.playerCache.containsKey(key)) {
|
|
|
|
return;
|
2016-12-22 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.inventories.containsKey(key)) {
|
|
|
|
this.inventories.get(key).setPlayerOffline();
|
2016-12-22 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
if (this.enderChests.containsKey(key)) {
|
|
|
|
this.enderChests.get(key).setPlayerOffline();
|
2016-12-22 14:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:37:28 +00:00
|
|
|
/**
|
2016-11-25 21:42:06 +00:00
|
|
|
* Method for handling a Player coming online.
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2016-11-25 21:42:06 +00:00
|
|
|
* @param player the Player
|
2017-06-08 22:36:01 +00:00
|
|
|
* @throws IllegalStateException if the server version is unsupported
|
2016-03-10 02:37:28 +00:00
|
|
|
*/
|
2016-11-25 21:42:06 +00:00
|
|
|
public void setPlayerOnline(final Player player) {
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
String key = this.accessor.getPlayerDataManager().getPlayerDataID(player);
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
// Check if the player is cached. If not, neither of their inventories is open.
|
|
|
|
if (!this.playerCache.containsKey(key)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-24 09:19:45 +00:00
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
this.playerCache.put(key, player);
|
|
|
|
|
|
|
|
if (this.inventories.containsKey(key)) {
|
|
|
|
this.inventories.get(key).setPlayerOnline(player);
|
|
|
|
new BukkitRunnable() {
|
2016-12-01 02:26:56 +00:00
|
|
|
@SuppressWarnings("deprecation") // Unlikely to ever be a viable alternative, Spigot un-deprecated.
|
2016-11-25 21:42:06 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (player.isOnline()) {
|
|
|
|
player.updateInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTask(this);
|
|
|
|
}
|
2015-06-24 09:19:45 +00:00
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
if (this.enderChests.containsKey(key)) {
|
|
|
|
this.enderChests.get(key).setPlayerOnline(player);
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 09:19:45 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public void setPlayerSilentChestStatus(@NotNull final OfflinePlayer offline, final boolean status) {
|
|
|
|
this.getConfig().set("toggles.silent-chest." + this.accessor.getPlayerDataManager().getPlayerDataID(offline), status);
|
2017-05-26 09:48:26 +00:00
|
|
|
this.saveConfig();
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays all applicable help for OpenInv commands.
|
2017-05-26 09:48:26 +00:00
|
|
|
*
|
2016-11-25 21:42:06 +00:00
|
|
|
* @param player the Player to help
|
|
|
|
*/
|
2017-05-26 09:48:26 +00:00
|
|
|
public void showHelp(final Player player) {
|
2016-11-25 21:42:06 +00:00
|
|
|
// Get registered commands
|
|
|
|
for (String commandName : this.getDescription().getCommands().keySet()) {
|
|
|
|
PluginCommand command = this.getCommand(commandName);
|
|
|
|
|
|
|
|
// Ensure command is successfully registered and player can use it
|
|
|
|
if (command == null || !command.testPermissionSilent(player)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-04 02:45:54 +00:00
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
// Send usage
|
2016-11-25 21:59:41 +00:00
|
|
|
player.sendMessage(command.getUsage().replace("<command>", commandName));
|
2016-11-25 21:42:06 +00:00
|
|
|
|
|
|
|
List<String> aliases = command.getAliases();
|
|
|
|
if (aliases.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble alias list
|
|
|
|
StringBuilder aliasBuilder = new StringBuilder(" (aliases: ");
|
|
|
|
for (String alias : aliases) {
|
|
|
|
aliasBuilder.append(alias).append(", ");
|
|
|
|
}
|
|
|
|
aliasBuilder.delete(aliasBuilder.length() - 2, aliasBuilder.length()).append(')');
|
|
|
|
|
|
|
|
// Send all aliases
|
|
|
|
player.sendMessage(aliasBuilder.toString());
|
|
|
|
}
|
2013-12-06 07:31:14 +00:00
|
|
|
}
|
2016-11-25 21:42:06 +00:00
|
|
|
|
2017-05-26 09:48:26 +00:00
|
|
|
@Override
|
2019-04-29 00:37:05 +00:00
|
|
|
public void unload(@NotNull final OfflinePlayer offline) {
|
|
|
|
this.playerCache.invalidate(this.accessor.getPlayerDataManager().getPlayerDataID(offline));
|
2017-05-26 09:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 02:03:03 +00:00
|
|
|
}
|