Update plugin to jikoo/master - numerous fixes and changes
* Added permissions to commands in plugin.yml
* Removed item wand functionality - see 3549431fbc
for reasoning
* Changed a lot of player loading logic
* Added config option DisableSaving - see Jikoo#6
* Fixed closing SilentChest not dropping item on cursor
* Added SilentChest support for shulker boxes
This commit is contained in:
@@ -1,163 +0,0 @@
|
||||
package com.lishid.openinv;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.lishid.openinv.utils.UUIDUtils;
|
||||
|
||||
public class ConfigUpdater {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
private static final int CONFIG_VERSION = 2;
|
||||
|
||||
public ConfigUpdater(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private int getConfigVersion() {
|
||||
return plugin.getConfig().getInt("config-version", 1);
|
||||
}
|
||||
|
||||
private boolean isConfigOutdated() {
|
||||
return getConfigVersion() < CONFIG_VERSION;
|
||||
}
|
||||
|
||||
public void checkForUpdates() {
|
||||
if (isConfigOutdated()) {
|
||||
plugin.getLogger().info("[Config] Update found! Performing update...");
|
||||
performUpdate();
|
||||
} else {
|
||||
plugin.getLogger().info("[Config] Update not required.");
|
||||
}
|
||||
}
|
||||
|
||||
private void performUpdate() {
|
||||
// Update according to the right version
|
||||
switch (getConfigVersion()) {
|
||||
case 1:
|
||||
updateConfig1To2();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConfig1To2() {
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
|
||||
// Backup the old config file
|
||||
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
File oldConfigFile = new File(plugin.getDataFolder(), "config_old.yml");
|
||||
|
||||
configFile.renameTo(oldConfigFile);
|
||||
|
||||
if (configFile.exists()) {
|
||||
configFile.delete();
|
||||
}
|
||||
|
||||
plugin.getLogger().info("[Config] Backup of old config.yml file created.");
|
||||
|
||||
// Get the old config settings
|
||||
int itemOpenInvItemId = config.getInt("ItemOpenInvItemID", 280);
|
||||
boolean notifySilentChest = config.getBoolean("NotifySilentChest", true);
|
||||
boolean notifyAnyChest = config.getBoolean("NotifyAnyChest", true);
|
||||
|
||||
Map<UUID, Boolean> anyChestToggles = null;
|
||||
Map<UUID, Boolean> itemOpenInvToggles = null;
|
||||
Map<UUID, Boolean> silentChestToggles = null;
|
||||
|
||||
if (config.isSet("AnyChest")) {
|
||||
anyChestToggles = updateToggles("AnyChest");
|
||||
}
|
||||
|
||||
if (config.isSet("ItemOpenInv")) {
|
||||
itemOpenInvToggles = updateToggles("ItemOpenInv");
|
||||
}
|
||||
|
||||
if (config.isSet("SilentChest")) {
|
||||
silentChestToggles = updateToggles("SilentChest");
|
||||
}
|
||||
|
||||
// Clear the old config
|
||||
for (String key : config.getKeys(false)) {
|
||||
config.set(key, null);
|
||||
}
|
||||
|
||||
// Set the new config options
|
||||
plugin.saveDefaultConfig();
|
||||
plugin.reloadConfig();
|
||||
|
||||
config = plugin.getConfig(); // Refresh the referenced plugin config
|
||||
|
||||
config.set("config-version", 2);
|
||||
config.set("items.open-inv", getMaterialById(itemOpenInvItemId).toString());
|
||||
config.set("notify.any-chest", notifyAnyChest);
|
||||
config.set("notify.silent-chest", notifySilentChest);
|
||||
|
||||
if (anyChestToggles != null && !anyChestToggles.isEmpty()) {
|
||||
for (Map.Entry<UUID, Boolean> entry : anyChestToggles.entrySet()) {
|
||||
config.set("toggles.any-chest." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (itemOpenInvToggles != null && !itemOpenInvToggles.isEmpty()) {
|
||||
for (Map.Entry<UUID, Boolean> entry : itemOpenInvToggles.entrySet()) {
|
||||
config.set("toggles.items.open-inv." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (silentChestToggles != null && !silentChestToggles.isEmpty()) {
|
||||
for (Map.Entry<UUID, Boolean> entry : silentChestToggles.entrySet()) {
|
||||
config.set("toggles.silent-chest." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// Save the new config
|
||||
plugin.saveConfig();
|
||||
plugin.getLogger().info("[Config] Update complete.");
|
||||
}
|
||||
|
||||
private Map<UUID, Boolean> updateToggles(String sectionName) {
|
||||
Map<UUID, Boolean> toggles = new HashMap<UUID, Boolean>();
|
||||
|
||||
ConfigurationSection section = plugin.getConfig().getConfigurationSection(sectionName);
|
||||
Set<String> keys = section.getKeys(false);
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int total = keys.size();
|
||||
int converted = 0;
|
||||
|
||||
for (String playerName : keys) {
|
||||
UUID uuid = UUIDUtils.getPlayerUUID(playerName);
|
||||
|
||||
if (uuid != null) {
|
||||
boolean toggled = section.getBoolean(playerName + ".toggle", false);
|
||||
toggles.put(uuid, toggled);
|
||||
converted++;
|
||||
}
|
||||
}
|
||||
|
||||
plugin.getLogger().info("[Config] Converted (" + converted + "/" + total + ") " + sectionName + " toggle player usernames to UUIDs.");
|
||||
|
||||
return toggles;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private Material getMaterialById(int id) {
|
||||
Material material = Material.getMaterial(id);
|
||||
|
||||
if (material == null) {
|
||||
material = Material.STICK;
|
||||
}
|
||||
|
||||
return material;
|
||||
}
|
||||
}
|
@@ -1,153 +0,0 @@
|
||||
package com.lishid.openinv;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
private Material openInvItem;
|
||||
private boolean notifySilentChest;
|
||||
private boolean notifyAnyChest;
|
||||
|
||||
public Configuration(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
// Check for config updates
|
||||
ConfigUpdater configUpdater = new ConfigUpdater(plugin);
|
||||
configUpdater.checkForUpdates();
|
||||
|
||||
// Load the config settings
|
||||
load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads OpenInv's config settings.
|
||||
*/
|
||||
public void load() {
|
||||
// OpenInv Item
|
||||
if (!plugin.getConfig().isSet("items.open-inv")) {
|
||||
saveToConfig("items.open-inv", "STICK");
|
||||
}
|
||||
|
||||
String itemName = plugin.getConfig().getString("items.open-inv", "STICK");
|
||||
Material material = Material.getMaterial(itemName);
|
||||
|
||||
if (material == null) {
|
||||
plugin.getLogger().warning("OpenInv item '" + itemName + "' does not match to a valid item. Defaulting to stick.");
|
||||
material = Material.STICK;
|
||||
}
|
||||
|
||||
openInvItem = material;
|
||||
|
||||
// Other Values
|
||||
notifySilentChest = plugin.getConfig().getBoolean("notify.silent-chest", true);
|
||||
notifyAnyChest = plugin.getConfig().getBoolean("notify.any-chest", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads OpenInv's config settings.
|
||||
*/
|
||||
public void reload() {
|
||||
load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a value to the plugin config at the specified path.
|
||||
*
|
||||
* @param path the path to set the value to
|
||||
* @param value the value to set to the path
|
||||
*/
|
||||
private void saveToConfig(String path, Object value) {
|
||||
plugin.getConfig().set(path, value);
|
||||
plugin.saveConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the OpenInv item Material.
|
||||
*
|
||||
* @return the OpenInv item Material
|
||||
*/
|
||||
public Material getOpenInvItem() {
|
||||
return openInvItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not notify silent chest is enabled.
|
||||
*
|
||||
* @return true if notify silent chest is enabled; false otherwise
|
||||
*/
|
||||
public boolean notifySilentChest() {
|
||||
return notifySilentChest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not notify any chest is enabled.
|
||||
*
|
||||
* @return true if notify any chest is enabled; false otherwise
|
||||
*/
|
||||
public boolean notifyAnyChest() {
|
||||
return notifyAnyChest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a player's item OpenInv status.
|
||||
*
|
||||
* @param player the player to get the item OpenInv status of
|
||||
* @return the player's item OpenInv status
|
||||
*/
|
||||
public boolean getPlayerItemOpenInvStatus(Player player) {
|
||||
return plugin.getConfig().getBoolean("toggles.items.open-inv." + player.getUniqueId(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a player's any chest status.
|
||||
*
|
||||
* @param player the player to get the any chest status of
|
||||
* @return the player's any chest status
|
||||
*/
|
||||
public boolean getPlayerAnyChestStatus(Player player) {
|
||||
return plugin.getConfig().getBoolean("toggles.any-chest." + player.getUniqueId(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's any chest status.
|
||||
*
|
||||
* @param player the player to set the any chest status of
|
||||
* @param status the status to set with
|
||||
*/
|
||||
public void setPlayerAnyChestStatus(Player player, boolean status) {
|
||||
saveToConfig("toggles.any-chest." + player.getUniqueId(), status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's item OpenInv status.
|
||||
*
|
||||
* @param player the player to set the item OpenInv status of
|
||||
* @param status the status to set with
|
||||
*/
|
||||
public void setPlayerItemOpenInvStatus(Player player, boolean status) {
|
||||
saveToConfig("toggles.items.open-inv." + player.getUniqueId(), status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a player's silent chest status.
|
||||
*
|
||||
* @param player the player to get the silent chest status of
|
||||
* @return the player's silent chest status
|
||||
*/
|
||||
public boolean getPlayerSilentChestStatus(Player player) {
|
||||
return plugin.getConfig().getBoolean("toggles.silent-chest." + player.getUniqueId(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's silent chest status.
|
||||
*
|
||||
* @param player the player to set the silent chest status of
|
||||
* @param status the status to set with
|
||||
*/
|
||||
public void setPlayerSilentChestStatus(Player player, boolean status) {
|
||||
saveToConfig("toggles.silent-chest." + player.getUniqueId(), status);
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
@@ -17,57 +17,97 @@
|
||||
package com.lishid.openinv;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.lishid.openinv.commands.AnyChestCommand;
|
||||
import com.lishid.openinv.commands.OpenEnderCommand;
|
||||
import com.lishid.openinv.commands.OpenInvCommand;
|
||||
import com.lishid.openinv.commands.SearchEnderCommand;
|
||||
import com.lishid.openinv.commands.SearchInvCommand;
|
||||
import com.lishid.openinv.commands.SilentChestCommand;
|
||||
import com.lishid.openinv.commands.ToggleOpenInvCommand;
|
||||
import com.lishid.openinv.internal.IAnySilentChest;
|
||||
import com.lishid.openinv.commands.AnyChestPluginCommand;
|
||||
import com.lishid.openinv.commands.OpenEnderPluginCommand;
|
||||
import com.lishid.openinv.commands.OpenInvPluginCommand;
|
||||
import com.lishid.openinv.commands.SearchInvPluginCommand;
|
||||
import com.lishid.openinv.commands.SilentChestPluginCommand;
|
||||
import com.lishid.openinv.internal.IAnySilentContainer;
|
||||
import com.lishid.openinv.internal.IInventoryAccess;
|
||||
import com.lishid.openinv.internal.IPlayerDataManager;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
import com.lishid.openinv.internal.InternalAccessor;
|
||||
import com.lishid.openinv.listeners.OpenInvEntityListener;
|
||||
import com.lishid.openinv.listeners.OpenInvInventoryListener;
|
||||
import com.lishid.openinv.listeners.OpenInvPlayerListener;
|
||||
import com.lishid.openinv.util.Cache;
|
||||
import com.lishid.openinv.util.Function;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
/**
|
||||
* Open other player's inventory
|
||||
*
|
||||
* @author lishid
|
||||
*/
|
||||
public class OpenInv extends JavaPlugin {
|
||||
|
||||
private final Map<UUID, ISpecialPlayerInventory> inventories = new HashMap<UUID, ISpecialPlayerInventory>();
|
||||
private final Map<UUID, ISpecialEnderChest> enderChests = new HashMap<UUID, ISpecialEnderChest>();
|
||||
private final Map<String, ISpecialPlayerInventory> inventories = new HashMap<String, ISpecialPlayerInventory>();
|
||||
private final Map<String, ISpecialEnderChest> enderChests = new HashMap<String, ISpecialEnderChest>();
|
||||
private final Cache<String, Player> playerCache = new Cache<String, Player>(300000L,
|
||||
new Function<Player>() {
|
||||
@Override
|
||||
public boolean run(Player value) {
|
||||
String key = playerLoader.getPlayerDataID(value);
|
||||
return inventories.containsKey(key) && inventories.get(key).isInUse()
|
||||
|| enderChests.containsKey(key) && enderChests.get(key).isInUse();
|
||||
}
|
||||
},
|
||||
new Function<Player>() {
|
||||
@Override
|
||||
public boolean run(Player value) {
|
||||
String key = playerLoader.getPlayerDataID(value);
|
||||
|
||||
private Configuration configuration;
|
||||
// Check if inventory is stored, and if it is, remove it and eject all viewers
|
||||
if (inventories.containsKey(key)) {
|
||||
Inventory inv = inventories.remove(key).getBukkitInventory();
|
||||
for (HumanEntity entity : inv.getViewers()) {
|
||||
entity.closeInventory();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if ender chest is stored, and if it is, remove it and eject all viewers
|
||||
if (enderChests.containsKey(key)) {
|
||||
Inventory inv = enderChests.remove(key).getBukkitInventory();
|
||||
for (HumanEntity entity : inv.getViewers()) {
|
||||
entity.closeInventory();
|
||||
}
|
||||
}
|
||||
|
||||
if (!OpenInv.this.disableSaving() && !value.isOnline()) {
|
||||
value.saveData();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
private InternalAccessor accessor;
|
||||
private IPlayerDataManager playerLoader;
|
||||
private IInventoryAccess inventoryAccess;
|
||||
private IAnySilentChest anySilentChest;
|
||||
private IAnySilentContainer anySilentContainer;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Save the default config.yml if it doesn't already exist
|
||||
saveDefaultConfig();
|
||||
|
||||
// Config
|
||||
configuration = new Configuration(this);
|
||||
|
||||
// Get plugin manager
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
|
||||
accessor = new InternalAccessor(this);
|
||||
// Version check
|
||||
if (!accessor.initialize(getServer())) {
|
||||
if (!accessor.isSupported()) {
|
||||
getLogger().info("Your version of CraftBukkit (" + accessor.getVersion() + ") is not supported.");
|
||||
getLogger().info("Please look for an updated version of OpenInv.");
|
||||
pm.disablePlugin(this);
|
||||
@@ -76,195 +116,473 @@ public class OpenInv extends JavaPlugin {
|
||||
|
||||
playerLoader = accessor.newPlayerDataManager();
|
||||
inventoryAccess = accessor.newInventoryAccess();
|
||||
anySilentChest = accessor.newAnySilentChest();
|
||||
anySilentContainer = accessor.newAnySilentContainer();
|
||||
|
||||
FileConfiguration config = getConfig();
|
||||
boolean dirtyConfig = false;
|
||||
if (!config.isBoolean("NotifySilentChest")) {
|
||||
config.set("NotifySilentChest", true);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
if (!config.isBoolean("NotifyAnyChest")) {
|
||||
config.set("NotifyAnyChest", true);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
if (!config.isBoolean("DisableSaving")) {
|
||||
config.set("DisableSaving", false);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
config.addDefault("NotifySilentChest", true);
|
||||
config.addDefault("NotifyAnyChest", true);
|
||||
config.addDefault("DisableSaving", false);
|
||||
config.options().copyDefaults(true);
|
||||
if (dirtyConfig) {
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
// Register the plugin's events
|
||||
pm.registerEvents(new OpenInvPlayerListener(this), this);
|
||||
pm.registerEvents(new OpenInvEntityListener(this), this);
|
||||
pm.registerEvents(new OpenInvInventoryListener(this), this);
|
||||
|
||||
// Register the plugin's commands
|
||||
getCommand("openinv").setExecutor(new OpenInvCommand(this));
|
||||
getCommand("openender").setExecutor(new OpenEnderCommand(this));
|
||||
getCommand("searchinv").setExecutor(new SearchInvCommand(this));
|
||||
getCommand("searchender").setExecutor(new SearchEnderCommand());
|
||||
getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this));
|
||||
getCommand("anychest").setExecutor(new AnyChestCommand(this));
|
||||
getCommand("silentchest").setExecutor(new SilentChestCommand(this));
|
||||
getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
|
||||
getCommand("searchinv").setExecutor(new SearchInvPluginCommand());
|
||||
getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this));
|
||||
getCommand("anychest").setExecutor(new AnyChestPluginCommand(this));
|
||||
getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
if (this.disableSaving()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.playerCache.invalidateAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin Configuration.
|
||||
*
|
||||
* @return the plugin Configuration
|
||||
* Checks if the server version is supported by OpenInv.
|
||||
*
|
||||
* @return true if the server version is supported
|
||||
*/
|
||||
public Configuration getConfiguration() {
|
||||
return configuration;
|
||||
public boolean isSupportedVersion() {
|
||||
return accessor.isSupported();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of PlayerDataManager.
|
||||
*
|
||||
* @return an instance of PlayerDataManager
|
||||
*/
|
||||
public IPlayerDataManager getPlayerLoader() {
|
||||
return playerLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of InventoryAccess.
|
||||
*
|
||||
* @return an instance of InventoryAccess
|
||||
* Gets the active IInventoryAccess implementation. May return null if the server version is
|
||||
* unsupported.
|
||||
*
|
||||
* @return the IInventoryAccess
|
||||
*/
|
||||
public IInventoryAccess getInventoryAccess() {
|
||||
return inventoryAccess;
|
||||
return this.inventoryAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of AnySilentChest.
|
||||
*
|
||||
* @return an instance of AnySilentChest
|
||||
* Gets the active ISilentContainer implementation. May return null if the server version is
|
||||
* unsupported.
|
||||
*
|
||||
* @return the ISilentContainer
|
||||
*/
|
||||
public IAnySilentChest getAnySilentChest() {
|
||||
return anySilentChest;
|
||||
public IAnySilentContainer getAnySilentContainer() {
|
||||
return this.anySilentContainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a player's SpecialPlayerInventory.
|
||||
*
|
||||
* @param player the player to get the SpecialPlayerInventory of
|
||||
* @param createIfNull whether or not to create it if it doesn't exist
|
||||
* @return the player's SpecialPlayerInventory or null
|
||||
* Gets an ISpecialPlayerInventory for the given Player.
|
||||
*
|
||||
* @param player the Player
|
||||
* @param online true if the Player is currently online
|
||||
* @return the ISpecialPlayerInventory
|
||||
*/
|
||||
public ISpecialPlayerInventory getPlayerInventory(Player player, boolean createIfNull) {
|
||||
ISpecialPlayerInventory inventory = inventories.get(player.getUniqueId());
|
||||
public ISpecialPlayerInventory getInventory(Player player, boolean online) {
|
||||
String id = playerLoader.getPlayerDataID(player);
|
||||
if (inventories.containsKey(id)) {
|
||||
return inventories.get(id);
|
||||
}
|
||||
ISpecialPlayerInventory inv = accessor.newSpecialPlayerInventory(player, online);
|
||||
inventories.put(id, inv);
|
||||
playerCache.put(id, player);
|
||||
return inv;
|
||||
}
|
||||
|
||||
if (inventory == null && createIfNull) {
|
||||
inventory = accessor.newSpecialPlayerInventory(player, !player.isOnline());
|
||||
inventories.put(player.getUniqueId(), inventory);
|
||||
/**
|
||||
* Gets an ISpecialEnderChest for the given Player.
|
||||
*
|
||||
* @param player the Player
|
||||
* @param online true if the Player is currently online
|
||||
* @return the ISpecialEnderChest
|
||||
*/
|
||||
public ISpecialEnderChest getEnderChest(Player player, boolean online) {
|
||||
String id = playerLoader.getPlayerDataID(player);
|
||||
if (enderChests.containsKey(id)) {
|
||||
return enderChests.get(id);
|
||||
}
|
||||
ISpecialEnderChest inv = accessor.newSpecialEnderChest(player, online);
|
||||
enderChests.put(id, inv);
|
||||
playerCache.put(id, player);
|
||||
return inv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forcibly unload a cached Player's data.
|
||||
*
|
||||
* @param player the OfflinePlayer to unload
|
||||
*/
|
||||
public void unload(OfflinePlayer player) {
|
||||
this.playerCache.invalidate(this.playerLoader.getPlayerDataID(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the configuration value for whether or not OpenInv saves player data when unloading
|
||||
* players. This is exclusively for users who do not allow editing of inventories, only viewing,
|
||||
* and wish to prevent any possibility of bugs such as lishid#40. If true, OpenInv will not ever
|
||||
* save any edits made to players.
|
||||
*
|
||||
* @return false unless configured otherwise
|
||||
*/
|
||||
public boolean disableSaving() {
|
||||
return getConfig().getBoolean("DisableSaving", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the configuration value for whether or not OpenInv displays a notification to the user
|
||||
* when a container is activated with SilentChest.
|
||||
*
|
||||
* @return true unless configured otherwise
|
||||
*/
|
||||
public boolean notifySilentChest() {
|
||||
return getConfig().getBoolean("NotifySilentChest", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the configuration value for whether or not OpenInv displays a notification to the user
|
||||
* when a container is activated with AnyChest.
|
||||
*
|
||||
* @return true unless configured otherwise
|
||||
*/
|
||||
public boolean notifyAnyChest() {
|
||||
return getConfig().getBoolean("NotifyAnyChest", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a player's SilentChest setting.
|
||||
*
|
||||
* @param player the OfflinePlayer
|
||||
* @return true if SilentChest is enabled
|
||||
*/
|
||||
public boolean getPlayerSilentChestStatus(OfflinePlayer player) {
|
||||
return getConfig().getBoolean("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's SilentChest setting.
|
||||
*
|
||||
* @param player the OfflinePlayer
|
||||
* @param status the status
|
||||
*/
|
||||
public void setPlayerSilentChestStatus(OfflinePlayer player, boolean status) {
|
||||
getConfig().set("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", status);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the provided player's AnyChest setting.
|
||||
*
|
||||
* @param player the OfflinePlayer
|
||||
* @return true if AnyChest is enabled
|
||||
*/
|
||||
public boolean getPlayerAnyChestStatus(OfflinePlayer player) {
|
||||
return getConfig().getBoolean("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's AnyChest setting.
|
||||
*
|
||||
* @param player the OfflinePlayer
|
||||
* @param status the status
|
||||
*/
|
||||
public void setPlayerAnyChestStatus(OfflinePlayer player, boolean status) {
|
||||
getConfig().set("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", status);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an OfflinePlayer by name.
|
||||
* <p>
|
||||
* Note: This method is potentially very heavily blocking. It should not ever be called on the
|
||||
* main thread, and if it is, a stack trace will be displayed alerting server owners to the
|
||||
* call.
|
||||
*
|
||||
* @param name the name of the Player
|
||||
* @return the OfflinePlayer with the closest matching name or null if no players have ever logged in
|
||||
*/
|
||||
public OfflinePlayer matchPlayer(String name) {
|
||||
|
||||
// Warn if called on the main thread - if we resort to searching offline players, this may take several seconds.
|
||||
if (getServer().isPrimaryThread()) {
|
||||
getLogger().warning("Call to OpenInv#matchPlayer made on the main thread!");
|
||||
getLogger().warning("This can cause the server to hang, potentially severely.");
|
||||
getLogger().warning("Trace:");
|
||||
for (StackTraceElement element : new Throwable().fillInStackTrace().getStackTrace()) {
|
||||
getLogger().warning(element.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a player's SpecialEnderChest.
|
||||
*
|
||||
* @param player the player to get the SpecialEnderChest of
|
||||
* @param createIfNull whether or not to create it if it doesn't exist
|
||||
* @return the player's SpecialEnderChest or null
|
||||
*/
|
||||
public ISpecialEnderChest getPlayerEnderChest(Player player, boolean createIfNull) {
|
||||
ISpecialEnderChest enderChest = enderChests.get(player.getUniqueId());
|
||||
|
||||
if (enderChest == null && createIfNull) {
|
||||
enderChest = accessor.newSpecialEnderChest(player, player.isOnline());
|
||||
enderChests.put(player.getUniqueId(), enderChest);
|
||||
// Ensure name is valid if server is in online mode to avoid unnecessary searching
|
||||
if (getServer().getOnlineMode() && !name.matches("[a-zA-Z0-9_]{3,16}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return enderChest;
|
||||
}
|
||||
OfflinePlayer player = getServer().getPlayerExact(name);
|
||||
|
||||
/**
|
||||
* Removes a player's loaded inventory if it exists.
|
||||
*
|
||||
* @param player the player to remove the loaded inventory of
|
||||
*/
|
||||
public void removeLoadedInventory(Player player) {
|
||||
if (inventories.containsKey(player.getUniqueId())) {
|
||||
inventories.remove(player.getUniqueId());
|
||||
if (player != null) {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a player's loaded ender chest if it exists.
|
||||
*
|
||||
* @param player the player to remove the loaded ender chest of
|
||||
*/
|
||||
public void removeLoadedEnderChest(Player player) {
|
||||
if (enderChests.containsKey(player.getUniqueId())) {
|
||||
enderChests.remove(player.getUniqueId());
|
||||
player = getServer().getOfflinePlayer(name);
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message to console.
|
||||
*
|
||||
* @param text the message to log
|
||||
*/
|
||||
public void log(String text) {
|
||||
getLogger().info(text);
|
||||
}
|
||||
player = getServer().getPlayer(name);
|
||||
|
||||
/**
|
||||
* Logs a Throwable to console.
|
||||
*
|
||||
* @param e the Throwable to log
|
||||
*/
|
||||
public void log(Throwable e) {
|
||||
getLogger().severe(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (player != null) {
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an OpenInv message to a player.
|
||||
*
|
||||
* @param sender the CommandSender to message
|
||||
* @param message the message to send
|
||||
*/
|
||||
public static void sendMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(ChatColor.AQUA + "[OpenInv] " + ChatColor.WHITE + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs OpenInv help information to a CommandSender.
|
||||
*
|
||||
* @param sender the CommandSender to show help to
|
||||
*/
|
||||
public static void showHelp(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.GREEN + "/openinv <player> - Opens a player's inventory.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/openender <player> - Opens a player's ender chest.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: oe)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/searchinv <item> [minAmount] -");
|
||||
sender.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their inventory.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: si)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/searchender <item> [minAmount] -");
|
||||
sender.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their ender chest.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: se)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggles the item openinv function.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/anychest - Toggles the any chest function.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: ac)");
|
||||
|
||||
sender.sendMessage(ChatColor.GREEN + "/silentchest - Toggles the silent chest function.");
|
||||
sender.sendMessage(ChatColor.GREEN + " (aliases: sc, silent)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not a player has a permission.
|
||||
*
|
||||
* @param player the player to check
|
||||
* @param permission the permission node to check for
|
||||
* @return true if the player has the permission; false otherwise
|
||||
*/
|
||||
public static boolean hasPermission(Permissible player, String permission) {
|
||||
String[] parts = permission.split("\\.");
|
||||
String perm = "";
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (player.hasPermission(perm + "*")) {
|
||||
return true;
|
||||
int bestMatch = Integer.MAX_VALUE;
|
||||
for (OfflinePlayer offline : getServer().getOfflinePlayers()) {
|
||||
if (offline.getName() == null) {
|
||||
// Loaded by UUID only, name has never been looked up.
|
||||
continue;
|
||||
}
|
||||
|
||||
perm += parts[i] + ".";
|
||||
// Compatibility: Lang3 is only bundled with 1.8+
|
||||
int currentMatch = org.apache.commons.lang.StringUtils.getLevenshteinDistance(name, offline.getName());
|
||||
|
||||
if (currentMatch == 0) {
|
||||
return offline;
|
||||
}
|
||||
|
||||
if (currentMatch < bestMatch) {
|
||||
bestMatch = currentMatch;
|
||||
player = offline;
|
||||
}
|
||||
}
|
||||
|
||||
return player.hasPermission(permission);
|
||||
// Only null if no players have played ever, otherwise even the worst match will do.
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a Player from an OfflinePlayer. May return null under some circumstances.
|
||||
*
|
||||
* @param offline the OfflinePlayer to load a Player for
|
||||
* @return the Player
|
||||
*/
|
||||
public Player loadPlayer(final OfflinePlayer offline) {
|
||||
|
||||
if (offline == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String key = this.playerLoader.getPlayerDataID(offline);
|
||||
if (this.playerCache.containsKey(key)) {
|
||||
return this.playerCache.get(key);
|
||||
}
|
||||
|
||||
Player loaded;
|
||||
|
||||
if (offline.isOnline()) {
|
||||
loaded = offline.getPlayer();
|
||||
this.playerCache.put(key, loaded);
|
||||
return loaded;
|
||||
}
|
||||
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
return this.playerLoader.loadPlayer(offline);
|
||||
}
|
||||
|
||||
Future<Player> future = Bukkit.getScheduler().callSyncMethod(this,
|
||||
new Callable<Player>() {
|
||||
@Override
|
||||
public Player call() throws Exception {
|
||||
return playerLoader.loadPlayer(offline);
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for handling a Player coming online.
|
||||
*
|
||||
* @param player the Player
|
||||
*/
|
||||
public void setPlayerOnline(final Player player) {
|
||||
|
||||
String key = this.playerLoader.getPlayerDataID(player);
|
||||
|
||||
// Check if the player is cached. If not, neither of their inventories is open.
|
||||
if (!this.playerCache.containsKey(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.playerCache.put(key, player);
|
||||
|
||||
if (this.inventories.containsKey(key)) {
|
||||
this.inventories.get(key).setPlayerOnline(player);
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (player.isOnline()) {
|
||||
player.updateInventory();
|
||||
}
|
||||
}
|
||||
}.runTask(this);
|
||||
}
|
||||
|
||||
if (this.enderChests.containsKey(key)) {
|
||||
this.enderChests.get(key).setPlayerOnline(player);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for handling a Player going offline.
|
||||
*
|
||||
* @param player the Player
|
||||
*/
|
||||
public void setPlayerOffline(final Player player) {
|
||||
|
||||
String key = this.playerLoader.getPlayerDataID(player);
|
||||
|
||||
// Check if the player is cached. If not, neither of their inventories is open.
|
||||
if (!this.playerCache.containsKey(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.inventories.containsKey(key)) {
|
||||
this.inventories.get(key).setPlayerOffline();
|
||||
}
|
||||
|
||||
if (this.enderChests.containsKey(key)) {
|
||||
this.enderChests.get(key).setPlayerOffline();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evicts all viewers lacking cross-world permissions from a Player's inventory.
|
||||
*
|
||||
* @param player the Player
|
||||
*/
|
||||
public void changeWorld(final Player player) {
|
||||
|
||||
String key = this.playerLoader.getPlayerDataID(player);
|
||||
|
||||
// Check if the player is cached. If not, neither of their inventories is open.
|
||||
if (!this.playerCache.containsKey(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.inventories.containsKey(key)) {
|
||||
Iterator<HumanEntity> iterator = this.inventories.get(key).getBukkitInventory().getViewers().iterator();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
if (this.enderChests.containsKey(key)) {
|
||||
Iterator<HumanEntity> iterator = this.enderChests.get(key).getBukkitInventory().getViewers().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
HumanEntity human = iterator.next();
|
||||
if (Permissions.CROSSWORLD.hasPermission(human) || human.getWorld() == null
|
||||
|| human.getWorld().equals(player.getWorld())) {
|
||||
continue;
|
||||
}
|
||||
human.closeInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays all applicable help for OpenInv commands.
|
||||
*
|
||||
* @param player the Player to help
|
||||
*/
|
||||
public void showHelp(Player player) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Send usage
|
||||
player.sendMessage(command.getUsage());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class OpenInvInventoryListener implements Listener {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public OpenInvInventoryListener(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (cancelInteract(event.getWhoClicked(), event.getInventory())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onInventoryDrag(InventoryDragEvent event) {
|
||||
if (cancelInteract(event.getWhoClicked(), event.getInventory())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean cancelInteract(HumanEntity entity, Inventory inventory) {
|
||||
return plugin.getInventoryAccess().isSpecialPlayerInventory(inventory)
|
||||
&& !Permissions.EDITINV.hasPermission(entity)
|
||||
|| plugin.getInventoryAccess().isSpecialEnderChest(inventory)
|
||||
&& !Permissions.EDITENDER.hasPermission(entity);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onWorldChange(PlayerChangedWorldEvent event) {
|
||||
plugin.changeWorld(event.getPlayer());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class OpenInvPlayerListener implements Listener {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public OpenInvPlayerListener(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPlayerJoin(final PlayerJoinEvent event) {
|
||||
plugin.setPlayerOnline(event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
plugin.setPlayerOffline(event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getPlayer().isSneaking()
|
||||
|| event.useInteractedBlock() == Result.DENY
|
||||
|| !plugin.getAnySilentContainer().isAnySilentContainer(event.getClickedBlock())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
boolean anychest = Permissions.ANYCHEST.hasPermission(player) && plugin.getPlayerAnyChestStatus(player);
|
||||
boolean needsAnyChest = plugin.getAnySilentContainer().isAnyContainerNeeded(player, event.getClickedBlock());
|
||||
|
||||
if (!anychest && needsAnyChest) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean silentchest = Permissions.SILENT.hasPermission(player) && plugin.getPlayerSilentChestStatus(player);
|
||||
|
||||
// If anychest or silentchest is active
|
||||
if ((anychest || silentchest) && plugin.getAnySilentContainer().activateContainer(player, silentchest, event.getClickedBlock())) {
|
||||
if (silentchest && plugin.notifySilentChest() && needsAnyChest && plugin.notifyAnyChest()) {
|
||||
player.sendMessage("You are opening a blocked container silently.");
|
||||
} else if (silentchest && plugin.notifySilentChest()) {
|
||||
player.sendMessage("You are opening a container silently.");
|
||||
} else if (needsAnyChest && plugin.notifyAnyChest()) {
|
||||
player.sendMessage("You are opening a blocked container.");
|
||||
}
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,19 +1,38 @@
|
||||
package com.lishid.openinv;
|
||||
|
||||
public final class Permissions {
|
||||
import org.bukkit.permissions.Permissible;
|
||||
|
||||
private Permissions() {}
|
||||
public enum Permissions {
|
||||
|
||||
OPENINV("OpenInv.openinv"),
|
||||
OVERRIDE("OpenInv.override"),
|
||||
EXEMPT("OpenInv.exempt"),
|
||||
CROSSWORLD("OpenInv.crossworld"),
|
||||
SILENT("OpenInv.silent"),
|
||||
ANYCHEST("OpenInv.anychest"),
|
||||
ENDERCHEST("OpenInv.openender"),
|
||||
ENDERCHEST_ALL("OpenInv.openenderall"),
|
||||
SEARCH("OpenInv.search"),
|
||||
EDITINV("OpenInv.editinv"),
|
||||
EDITENDER("OpenInv.editender"),
|
||||
OPENSELF("OpenInv.openself");
|
||||
|
||||
private final String permission;
|
||||
|
||||
private Permissions(String permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public boolean hasPermission(Permissible permissible) {
|
||||
String[] parts = permission.split("\\.");
|
||||
String perm = "";
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (permissible.hasPermission(perm + "*")) {
|
||||
return true;
|
||||
}
|
||||
perm += parts[i] + ".";
|
||||
}
|
||||
return permissible.hasPermission(permission);
|
||||
}
|
||||
|
||||
public static final String PERM_OPENINV = "OpenInv.openinv";
|
||||
public static final String PERM_OVERRIDE = "OpenInv.override";
|
||||
public static final String PERM_EXEMPT = "OpenInv.exempt";
|
||||
public static final String PERM_CROSSWORLD = "OpenInv.crossworld";
|
||||
public static final String PERM_SILENT = "OpenInv.silent";
|
||||
public static final String PERM_ANYCHEST = "OpenInv.anychest";
|
||||
public static final String PERM_ENDERCHEST = "OpenInv.openender";
|
||||
public static final String PERM_ENDERCHEST_ALL = "OpenInv.openenderall";
|
||||
public static final String PERM_SEARCH = "OpenInv.search";
|
||||
public static final String PERM_EDITINV = "OpenInv.editinv";
|
||||
public static final String PERM_EDITENDER = "OpenInv.editender";
|
||||
public static final String PERM_OPENSELF = "OpenInv.openself";
|
||||
}
|
||||
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.Configuration;
|
||||
|
||||
public class AnyChestCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Configuration configuration;
|
||||
|
||||
public AnyChestCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
configuration = plugin.getConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("anychest")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this command from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_ANYCHEST)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to use any chest.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length > 0) {
|
||||
if (args[0].equalsIgnoreCase("check")) {
|
||||
String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "Any Chest is " + status + ChatColor.RESET + ".");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
configuration.setPlayerAnyChestStatus(player, !configuration.getPlayerAnyChestStatus(player));
|
||||
|
||||
String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "Any Chest is now " + status + ChatColor.RESET + ".");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
|
||||
public class AnyChestPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public AnyChestPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("check")) {
|
||||
sender.sendMessage("AnyChest is " + (plugin.getPlayerAnyChestStatus(player) ? "ON" : "OFF") + ".");
|
||||
return true;
|
||||
}
|
||||
|
||||
plugin.setPlayerAnyChestStatus(player, !plugin.getPlayerAnyChestStatus(player));
|
||||
sender.sendMessage("AnyChest is now " + (plugin.getPlayerAnyChestStatus(player) ? "ON" : "OFF") + ".");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -1,161 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import com.lishid.openinv.utils.UUIDUtils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class OpenEnderCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Map<UUID, UUID> openEnderHistory = new ConcurrentHashMap<UUID, UUID>();
|
||||
|
||||
public OpenEnderCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("openender")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this command from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_ENDERCHEST)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to access player ender chests.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
OpenInv.showHelp(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
UUID history = openEnderHistory.get(player.getUniqueId());
|
||||
if (history == null) {
|
||||
history = player.getUniqueId();
|
||||
openEnderHistory.put(player.getUniqueId(), history);
|
||||
}
|
||||
|
||||
final UUID uuid;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
if (history != null) {
|
||||
uuid = history;
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.RED + "OpenEnder history is empty!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uuid = UUIDUtils.getPlayerUUID(args[0]);
|
||||
if (uuid == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
|
||||
Player target = Bukkit.getPlayer(uuid);
|
||||
if (target == null) {
|
||||
// Targeted player was not found online, start asynchronous lookup in files
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Try loading the player's data asynchronously
|
||||
final Player target = plugin.getPlayerLoader().loadPlayer(uuid);
|
||||
if (target == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Open target's inventory synchronously
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Player player = Bukkit.getPlayer(playerUUID);
|
||||
// If sender is no longer online after loading the target, abort!
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
openInventory(player, target);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
openInventory(player, target);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void openInventory(Player player, Player target) {
|
||||
// Null target check
|
||||
if (target == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Permissions checks
|
||||
if (target != player && !OpenInv.hasPermission(player, Permissions.PERM_ENDERCHEST_ALL)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have permission to access other player's ender chests.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE) && OpenInv.hasPermission(target, Permissions.PERM_EXEMPT)) {
|
||||
player.sendMessage(ChatColor.RED + target.getDisplayName() + "'s ender chest is protected!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Crossworld check
|
||||
if ((!OpenInv.hasPermission(player, Permissions.PERM_CROSSWORLD) && !OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE)) && target.getWorld() != player.getWorld()) {
|
||||
player.sendMessage(ChatColor.RED + target.getDisplayName() + " is not in your world!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Record the target
|
||||
openEnderHistory.put(player.getUniqueId(), target.getUniqueId());
|
||||
|
||||
// Get the inventory and open it
|
||||
ISpecialEnderChest enderChest = plugin.getPlayerEnderChest(target, true);
|
||||
player.openInventory(enderChest.getBukkitInventory());
|
||||
}
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class OpenEnderPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final HashMap<Player, String> openEnderHistory = new HashMap<Player, String>();
|
||||
|
||||
public OpenEnderPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
plugin.showHelp((Player) sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
String history = openEnderHistory.get(player);
|
||||
|
||||
if (history == null || history == "") {
|
||||
history = player.getName();
|
||||
openEnderHistory.put(player, history);
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
name = history;
|
||||
} else {
|
||||
name = args[0];
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);
|
||||
|
||||
if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
openInventory(player, offlinePlayer);
|
||||
}
|
||||
}.runTask(plugin);
|
||||
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void openInventory(Player player, OfflinePlayer target) {
|
||||
|
||||
Player onlineTarget;
|
||||
boolean online = target.isOnline();
|
||||
|
||||
if (!online) {
|
||||
// Try loading the player's data
|
||||
onlineTarget = plugin.loadPlayer(target);
|
||||
|
||||
if (onlineTarget == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
onlineTarget = target.getPlayer();
|
||||
}
|
||||
|
||||
|
||||
if (!onlineTarget.equals(player) && !Permissions.ENDERCHEST_ALL.hasPermission(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have permission to access other player's enderchest");
|
||||
return;
|
||||
}
|
||||
|
||||
// Record the target
|
||||
openEnderHistory.put(player, onlineTarget.getName());
|
||||
|
||||
// Create the inventory
|
||||
ISpecialEnderChest chest = plugin.getEnderChest(onlineTarget, online);
|
||||
|
||||
// Open the inventory
|
||||
player.openInventory(chest.getBukkitInventory());
|
||||
}
|
||||
|
||||
}
|
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
import com.lishid.openinv.utils.UUIDUtils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class OpenInvCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Map<UUID, UUID> openInvHistory = new ConcurrentHashMap<UUID, UUID>();
|
||||
|
||||
public OpenInvCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("openinv")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this command from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_OPENINV)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
OpenInv.showHelp(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
UUID history = openInvHistory.get(player.getUniqueId());
|
||||
if (history == null) {
|
||||
history = player.getUniqueId();
|
||||
openInvHistory.put(player.getUniqueId(), history);
|
||||
}
|
||||
|
||||
final UUID uuid;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
uuid = history;
|
||||
} else {
|
||||
uuid = UUIDUtils.getPlayerUUID(args[0]);
|
||||
if (uuid == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
|
||||
Player target = Bukkit.getPlayer(uuid);
|
||||
if (target == null) {
|
||||
// Targeted player was not found online, start asynchronous lookup in files
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Try loading the player's data asynchronously
|
||||
final Player target = plugin.getPlayerLoader().loadPlayer(uuid);
|
||||
if (target == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Open target's inventory synchronously
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Player player = Bukkit.getPlayer(playerUUID);
|
||||
// If sender is no longer online after loading the target, abort!
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
openInventory(player, target);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
openInventory(player, target);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void openInventory(Player player, Player target) {
|
||||
// Null target check
|
||||
if (target == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Permissions checks
|
||||
if (!OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE) && OpenInv.hasPermission(target, Permissions.PERM_EXEMPT)) {
|
||||
player.sendMessage(ChatColor.RED + target.getDisplayName() + "'s inventory is protected!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Crossworld check
|
||||
if ((!OpenInv.hasPermission(player, Permissions.PERM_CROSSWORLD) && !OpenInv.hasPermission(player, Permissions.PERM_OVERRIDE)) && target.getWorld() != player.getWorld()) {
|
||||
player.sendMessage(ChatColor.RED + target.getDisplayName() + " is not in your world!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Self-open check
|
||||
if (!OpenInv.hasPermission(player, Permissions.PERM_OPENSELF) && target.equals(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You're not allowed to openinv yourself.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Record the target
|
||||
openInvHistory.put(player.getUniqueId(), target.getUniqueId());
|
||||
|
||||
// Get the inventory and open it
|
||||
ISpecialPlayerInventory inventory = plugin.getPlayerInventory(target, true);
|
||||
player.openInventory(inventory.getBukkitInventory());
|
||||
}
|
||||
}
|
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class OpenInvPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final HashMap<Player, String> openInvHistory = new HashMap<Player, String>();
|
||||
|
||||
public OpenInvPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
plugin.showHelp((Player) sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
String history = openInvHistory.get(player);
|
||||
|
||||
if (history == null || history == "") {
|
||||
history = player.getName();
|
||||
openInvHistory.put(player, history);
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
name = history;
|
||||
} else {
|
||||
name = args[0];
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);
|
||||
|
||||
if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
openInventory(player, offlinePlayer);
|
||||
}
|
||||
}.runTask(plugin);
|
||||
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void openInventory(Player player, OfflinePlayer target) {
|
||||
|
||||
|
||||
Player onlineTarget;
|
||||
boolean online = target.isOnline();
|
||||
|
||||
if (!online) {
|
||||
// Try loading the player's data
|
||||
onlineTarget = plugin.loadPlayer(target);
|
||||
|
||||
if (onlineTarget == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
onlineTarget = target.getPlayer();
|
||||
}
|
||||
|
||||
// Permissions checks
|
||||
if (!Permissions.OVERRIDE.hasPermission(player) && Permissions.EXEMPT.hasPermission(onlineTarget)) {
|
||||
player.sendMessage(ChatColor.RED + onlineTarget.getDisplayName() + "'s inventory is protected!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Crosswork check
|
||||
if ((!Permissions.CROSSWORLD.hasPermission(player) && !Permissions.OVERRIDE.hasPermission(player)) && onlineTarget.getWorld() != player.getWorld()) {
|
||||
player.sendMessage(ChatColor.RED + onlineTarget.getDisplayName() + " is not in your world!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Self-open check
|
||||
if (!Permissions.OPENSELF.hasPermission(player) && onlineTarget.equals(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You're not allowed to openinv yourself.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Record the target
|
||||
openInvHistory.put(player, onlineTarget.getName());
|
||||
|
||||
// Create the inventory
|
||||
ISpecialPlayerInventory inv = plugin.getInventory(onlineTarget, online);
|
||||
|
||||
// Open the inventory
|
||||
player.openInventory(inv.getBukkitInventory());
|
||||
}
|
||||
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
|
||||
public class SearchEnderCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("searchender")) {
|
||||
if (sender instanceof Player) {
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_SEARCH)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to search player ender chests.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Material material = null;
|
||||
int count = 1;
|
||||
|
||||
if (args.length >= 1) {
|
||||
String[] gData;
|
||||
gData = args[0].split(":");
|
||||
material = Material.matchMaterial(gData[0]);
|
||||
}
|
||||
|
||||
if (args.length >= 2) {
|
||||
try {
|
||||
count = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(ChatColor.RED + "'" + args[1] + "' is not a number!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (material == null) {
|
||||
sender.sendMessage(ChatColor.RED + "Unknown item.");
|
||||
return false;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) {
|
||||
if (onlinePlayer.getEnderChest().contains(material, count)) {
|
||||
sb.append(onlinePlayer.getName());
|
||||
sb.append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
String playerList = sb.toString();
|
||||
sender.sendMessage("Players with the item " + ChatColor.GRAY + material.toString() + ChatColor.RESET + " in their ender chest: " + playerList);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
|
||||
public class SearchInvCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public SearchInvCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("searchinv")) {
|
||||
if (sender instanceof Player) {
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_SEARCH)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to search player inventories.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Material material = null;
|
||||
int count = 1;
|
||||
|
||||
if (args.length >= 1) {
|
||||
String[] gData;
|
||||
gData = args[0].split(":");
|
||||
material = Material.matchMaterial(gData[0]);
|
||||
}
|
||||
|
||||
if (args.length >= 2) {
|
||||
try {
|
||||
count = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(ChatColor.RED + "'" + args[1] + "' is not a number!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (material == null) {
|
||||
sender.sendMessage(ChatColor.RED + "Unknown item.");
|
||||
return false;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) {
|
||||
if (onlinePlayer.getInventory().contains(material, count)) {
|
||||
sb.append(onlinePlayer.getName());
|
||||
sb.append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
String playerList = sb.toString();
|
||||
sender.sendMessage("Players with the item " + ChatColor.GRAY + material.toString() + ChatColor.RESET + " in their inventory: " + playerList);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class SearchInvPluginCommand implements CommandExecutor {
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
||||
Material material = null;
|
||||
int count = 1;
|
||||
|
||||
if (args.length >= 1) {
|
||||
String[] gData = null;
|
||||
gData = args[0].split(":");
|
||||
material = Material.matchMaterial(gData[0]);
|
||||
}
|
||||
|
||||
if (args.length >= 2) {
|
||||
try {
|
||||
count = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException ex) {
|
||||
sender.sendMessage(ChatColor.RED + "'" + args[1] + "' is not a number!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (material == null) {
|
||||
sender.sendMessage(ChatColor.RED + "Unknown item");
|
||||
return false;
|
||||
}
|
||||
|
||||
StringBuilder players = new StringBuilder();
|
||||
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
|
||||
if (player.getInventory().contains(material, count)) {
|
||||
players.append(player.getName()).append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
// Matches found, delete trailing comma and space
|
||||
if (players.length() > 0) {
|
||||
players.delete(players.length() - 2, players.length());
|
||||
} else {
|
||||
sender.sendMessage("No players found with " + material.toString());
|
||||
}
|
||||
|
||||
sender.sendMessage("Players with the item " + material.toString() + ": " + players.toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.Configuration;
|
||||
|
||||
public class SilentChestCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Configuration configuration;
|
||||
|
||||
public SilentChestCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
configuration = plugin.getConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("silentchest")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this command from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_SILENT)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to use silent chest.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length > 0) {
|
||||
if (args[0].equalsIgnoreCase("check")) {
|
||||
String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + ".");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
configuration.setPlayerSilentChestStatus(player, !configuration.getPlayerSilentChestStatus(player));
|
||||
|
||||
String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + ".");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2014 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
|
||||
public class SilentChestPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public SilentChestPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("check")) {
|
||||
sender.sendMessage("SilentChest is " + (plugin.getPlayerAnyChestStatus(player) ? "ON" : "OFF") + ".");
|
||||
return true;
|
||||
}
|
||||
|
||||
plugin.setPlayerSilentChestStatus(player, !plugin.getPlayerSilentChestStatus(player));
|
||||
sender.sendMessage("SilentChest is now " + (plugin.getPlayerSilentChestStatus(player) ? "ON" : "OFF") + ".");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.Configuration;
|
||||
|
||||
public class ToggleOpenInvCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Configuration configuration;
|
||||
|
||||
public ToggleOpenInvCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
configuration = plugin.getConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (command.getName().equalsIgnoreCase("toggleopeninv")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this command from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OpenInv.hasPermission(sender, Permissions.PERM_OPENINV)) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length > 0) {
|
||||
if (args[0].equalsIgnoreCase("check")) {
|
||||
String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + ".");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
configuration.setPlayerItemOpenInvStatus(player, !configuration.getPlayerItemOpenInvStatus(player));
|
||||
|
||||
String status = configuration.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
|
||||
OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + configuration.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + ".");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.listeners;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.Configuration;
|
||||
|
||||
public class OpenInvEntityListener implements Listener {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Configuration configuration;
|
||||
|
||||
public OpenInvEntityListener(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
configuration = plugin.getConfiguration();
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
|
||||
Entity attacker = event.getDamager();
|
||||
Entity defender = event.getEntity();
|
||||
|
||||
if (!(attacker instanceof Player) || !(defender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = (Player) attacker;
|
||||
|
||||
if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem()) {
|
||||
if (!configuration.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = (Player) defender;
|
||||
|
||||
event.setDamage(0);
|
||||
event.setCancelled(true);
|
||||
|
||||
player.performCommand("openinv " + target.getName());
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.listeners;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
|
||||
public class OpenInvInventoryListener implements Listener {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public OpenInvInventoryListener(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
Inventory inventory = event.getInventory();
|
||||
HumanEntity player = event.getWhoClicked();
|
||||
|
||||
if (!plugin.getInventoryAccess().check(inventory, player)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2016 lishid. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.listeners;
|
||||
|
||||
import com.lishid.openinv.Configuration;
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.Permissions;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class OpenInvPlayerListener implements Listener {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final Configuration configuration;
|
||||
|
||||
public OpenInvPlayerListener(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
configuration = plugin.getConfiguration();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ISpecialPlayerInventory inventory = plugin.getPlayerInventory(player, false);
|
||||
if (inventory != null) {
|
||||
inventory.playerOnline(player);
|
||||
player.updateInventory();
|
||||
}
|
||||
|
||||
ISpecialEnderChest enderChest = plugin.getPlayerEnderChest(player, false);
|
||||
if (enderChest != null) {
|
||||
enderChest.playerOnline(player);
|
||||
}
|
||||
}
|
||||
}.runTask(plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
ISpecialPlayerInventory inventory = plugin.getPlayerInventory(player, false);
|
||||
if (inventory != null) {
|
||||
inventory.playerOffline();
|
||||
if (inventory.isInUse()) {
|
||||
plugin.removeLoadedInventory(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
ISpecialEnderChest enderChest = plugin.getPlayerEnderChest(player, false);
|
||||
if (enderChest != null) {
|
||||
enderChest.playerOffline();
|
||||
if (!enderChest.isInUse()) {
|
||||
plugin.removeLoadedEnderChest(event.getPlayer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
if (player.isSneaking()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Action action = event.getAction();
|
||||
Block block = event.getClickedBlock();
|
||||
|
||||
switch (action) {
|
||||
case RIGHT_CLICK_BLOCK:
|
||||
if (event.useInteractedBlock() == Result.DENY) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ender Chests
|
||||
if (block.getType() == Material.ENDER_CHEST) {
|
||||
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) {
|
||||
event.setCancelled(true);
|
||||
player.openInventory(player.getEnderChest());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Chests
|
||||
if (block.getState() instanceof Chest) {
|
||||
boolean silentChest = false;
|
||||
boolean anyChest = false;
|
||||
int x = block.getX();
|
||||
int y = block.getY();
|
||||
int z = block.getZ();
|
||||
|
||||
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) {
|
||||
silentChest = true;
|
||||
}
|
||||
|
||||
if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) {
|
||||
try {
|
||||
anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z);
|
||||
} catch (Exception e) {
|
||||
player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// If the anyChest or silentChest is active
|
||||
if (anyChest || silentChest) {
|
||||
if (!plugin.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Signs
|
||||
if (block.getState() instanceof Sign) {
|
||||
try {
|
||||
Sign sign = (Sign) block.getState();
|
||||
|
||||
if (OpenInv.hasPermission(player, Permissions.PERM_OPENINV) && sign.getLine(0).equalsIgnoreCase("[openinv]")) {
|
||||
String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(3).trim();
|
||||
player.performCommand("openinv " + text);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
player.sendMessage(ChatColor.RED + "An internal error occured.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case RIGHT_CLICK_AIR:
|
||||
// OpenInv item
|
||||
if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem() && configuration.getPlayerItemOpenInvStatus(player) && OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
|
||||
player.performCommand("openinv");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,108 +0,0 @@
|
||||
package com.lishid.openinv.utils;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
|
||||
private static final double PROFILES_PER_REQUEST = 100;
|
||||
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
|
||||
private final JSONParser jsonParser = new JSONParser();
|
||||
private final List<String> names;
|
||||
private final boolean rateLimiting;
|
||||
|
||||
public UUIDFetcher(List<String> names, boolean rateLimiting) {
|
||||
this.names = ImmutableList.copyOf(names);
|
||||
this.rateLimiting = rateLimiting;
|
||||
}
|
||||
|
||||
public UUIDFetcher(List<String> names) {
|
||||
this(names, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, UUID> call() throws Exception {
|
||||
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
|
||||
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
|
||||
|
||||
for (int i = 0; i < requests; i++) {
|
||||
HttpURLConnection connection = createConnection();
|
||||
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
|
||||
writeBody(connection, body);
|
||||
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
||||
|
||||
for (Object profile : array) {
|
||||
JSONObject jsonProfile = (JSONObject) profile;
|
||||
String id = (String) jsonProfile.get("id");
|
||||
String name = (String) jsonProfile.get("name");
|
||||
UUID uuid = UUIDFetcher.getUUID(id);
|
||||
uuidMap.put(name.toLowerCase(), uuid);
|
||||
}
|
||||
|
||||
if (rateLimiting && i != requests - 1) {
|
||||
Thread.sleep(100L);
|
||||
}
|
||||
}
|
||||
|
||||
return uuidMap;
|
||||
}
|
||||
|
||||
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
|
||||
OutputStream stream = connection.getOutputStream();
|
||||
stream.write(body.getBytes());
|
||||
stream.flush();
|
||||
stream.close();
|
||||
}
|
||||
|
||||
private static HttpURLConnection createConnection() throws Exception {
|
||||
URL url = new URL(PROFILE_URL);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setUseCaches(false);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
private static UUID getUUID(String id) {
|
||||
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
|
||||
}
|
||||
|
||||
public static byte[] toBytes(UUID uuid) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
|
||||
byteBuffer.putLong(uuid.getMostSignificantBits());
|
||||
byteBuffer.putLong(uuid.getLeastSignificantBits());
|
||||
|
||||
return byteBuffer.array();
|
||||
}
|
||||
|
||||
public static UUID fromBytes(byte[] array) {
|
||||
if (array.length != 16) {
|
||||
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
|
||||
}
|
||||
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
|
||||
long mostSignificant = byteBuffer.getLong();
|
||||
long leastSignificant = byteBuffer.getLong();
|
||||
|
||||
return new UUID(mostSignificant, leastSignificant);
|
||||
}
|
||||
|
||||
public static UUID getUUIDOf(String name) throws Exception {
|
||||
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
|
||||
}
|
||||
}
|
@@ -1,81 +0,0 @@
|
||||
package com.lishid.openinv.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class UUIDUtils {
|
||||
|
||||
private UUIDUtils() {}
|
||||
|
||||
private static Player getPlayer(String name) {
|
||||
Validate.notNull(name, "Name cannot be null");
|
||||
|
||||
Player found = null;
|
||||
String lowerName = name.toLowerCase();
|
||||
int delta = Integer.MAX_VALUE;
|
||||
|
||||
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
|
||||
for (Player player : players) {
|
||||
if (player.getName().toLowerCase().startsWith(lowerName)) {
|
||||
int curDelta = player.getName().length() - lowerName.length();
|
||||
|
||||
if (curDelta < delta) {
|
||||
found = player;
|
||||
delta = curDelta;
|
||||
}
|
||||
|
||||
if (curDelta == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static UUID getUUIDLocally(String name) {
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(name);
|
||||
return offlinePlayer.hasPlayedBefore() ? offlinePlayer.getUniqueId() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the UUID of a player by their name.
|
||||
*
|
||||
* @param name the name of the player to get the UUID of
|
||||
* @return the player's UUID or null
|
||||
*/
|
||||
public static UUID getPlayerUUID(String name) {
|
||||
UUID uuid;
|
||||
Player player = getPlayer(name);
|
||||
|
||||
if (player != null) {
|
||||
uuid = player.getUniqueId();
|
||||
} else {
|
||||
if (Bukkit.getServer().getOnlineMode()) {
|
||||
if (!Bukkit.getServer().isPrimaryThread()) {
|
||||
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
|
||||
Map<String, UUID> response;
|
||||
|
||||
try {
|
||||
response = fetcher.call();
|
||||
uuid = response.get(name.toLowerCase());
|
||||
} catch (Exception e) {
|
||||
uuid = getUUIDLocally(name);
|
||||
}
|
||||
} else {
|
||||
uuid = getUUIDLocally(name);
|
||||
}
|
||||
} else {
|
||||
uuid = getUUIDLocally(name);
|
||||
}
|
||||
}
|
||||
|
||||
return uuid;
|
||||
}
|
||||
}
|
@@ -1,6 +0,0 @@
|
||||
config-version: 2
|
||||
notify:
|
||||
any-chest: true
|
||||
silent-chest: true
|
||||
items:
|
||||
open-inv: STICK
|
Reference in New Issue
Block a user