OpenInv/src/main/java/com/lishid/openinv/OpenInv.java

234 lines
8.5 KiB
Java
Raw Normal View History

2012-02-20 15:09:44 -05:00
/*
2013-12-07 04:49:15 -05:00
* Copyright (C) 2011-2014 lishid. All rights reserved.
2012-02-20 15:09:44 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* 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/>.
*/
2011-06-03 10:32:54 -04:00
package com.lishid.openinv;
2011-06-03 10:32:54 -04:00
import java.util.HashMap;
import java.util.Map;
2015-06-22 12:03:03 +10:00
import java.util.UUID;
2012-01-12 14:23:53 -05:00
import org.bukkit.ChatColor;
2015-06-23 13:31:26 +10:00
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
2012-01-12 14:23:53 -05:00
import org.bukkit.entity.Player;
2013-12-06 02:31:14 -05:00
import org.bukkit.permissions.Permissible;
2011-06-03 10:32:54 -04:00
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
2015-06-23 13:31:26 +10:00
import com.lishid.openinv.commands.AnyChestCommand;
import com.lishid.openinv.commands.OpenEnderCommand;
import com.lishid.openinv.commands.OpenInvCommand;
import com.lishid.openinv.commands.SearchInvCommand;
import com.lishid.openinv.commands.SilentChestCommand;
import com.lishid.openinv.commands.ToggleOpenInvCommand;
2015-06-22 12:03:03 +10:00
import com.lishid.openinv.internal.AnySilentChest;
import com.lishid.openinv.internal.InventoryAccess;
import com.lishid.openinv.internal.PlayerDataManager;
import com.lishid.openinv.internal.SpecialEnderChest;
import com.lishid.openinv.internal.SpecialPlayerInventory;
import com.lishid.openinv.listeners.OpenInvEntityListener;
import com.lishid.openinv.listeners.OpenInvInventoryListener;
import com.lishid.openinv.listeners.OpenInvPlayerListener;
2011-06-03 10:32:54 -04:00
/**
* Open other player's inventory
*
2011-06-03 10:32:54 -04:00
* @author lishid
*/
2013-12-07 04:44:40 -05:00
public class OpenInv extends JavaPlugin {
2015-06-22 12:03:03 +10:00
public static final Map<UUID, SpecialPlayerInventory> inventories = new HashMap<UUID, SpecialPlayerInventory>();
public static final Map<UUID, SpecialEnderChest> enderChests = new HashMap<UUID, SpecialEnderChest>();
2013-12-07 04:44:40 -05:00
public static OpenInv mainPlugin;
2015-06-23 13:31:26 +10:00
private static PlayerDataManager playerLoader;
private static InventoryAccess inventoryAccess;
private static AnySilentChest anySilentChest;
2013-12-07 04:44:40 -05:00
2015-06-22 12:03:03 +10:00
@Override
2013-12-07 04:44:40 -05:00
public void onEnable() {
2015-06-23 13:31:26 +10:00
// Plugin
mainPlugin = this;
// Config
ConfigUpdater configUpdater = new ConfigUpdater(this);
configUpdater.checkForUpdates();
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
// Initialize
2015-06-22 12:03:03 +10:00
playerLoader = new PlayerDataManager();
inventoryAccess = new InventoryAccess();
anySilentChest = new AnySilentChest();
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
// Save the default config.yml if it doesn't already exist
saveDefaultConfig();
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
// Register the plugin's events & commands
registerEvents();
registerCommands();
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
private void registerEvents() {
PluginManager pm = getServer().getPluginManager();
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
pm.registerEvents(new OpenInvPlayerListener(), this);
pm.registerEvents(new OpenInvEntityListener(), this);
pm.registerEvents(new OpenInvInventoryListener(), this);
2011-12-16 05:30:28 -05:00
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
private void registerCommands() {
getCommand("openinv").setExecutor(new OpenInvCommand(this));
getCommand("searchinv").setExecutor(new SearchInvCommand());
getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand());
getCommand("silentchest").setExecutor(new SilentChestCommand());
getCommand("anychest").setExecutor(new AnyChestCommand());
getCommand("openender").setExecutor(new OpenEnderCommand(this));
2011-12-16 05:30:28 -05:00
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
public static PlayerDataManager getPlayerLoader() {
return playerLoader;
2012-01-12 14:23:53 -05:00
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
public static InventoryAccess getInventoryAccess() {
return inventoryAccess;
2012-01-12 14:23:53 -05:00
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
public static AnySilentChest getAnySilentChest() {
return anySilentChest;
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
/*
2015-06-22 20:15:28 +10:00
public static Object getFromConfig(String data, Object defaultValue) {
Object val = mainPlugin.getConfig().get(data);
2013-12-07 04:44:40 -05:00
if (val == null) {
mainPlugin.getConfig().set(data, defaultValue);
2011-09-18 19:34:06 -04:00
return defaultValue;
}
2013-12-07 04:44:40 -05:00
else {
return val;
2011-09-18 19:34:06 -04:00
}
}
2015-06-23 13:31:26 +10:00
*/
2013-12-07 04:44:40 -05:00
2015-06-22 20:15:28 +10:00
public static void saveToConfig(String data, Object value) {
mainPlugin.getConfig().set(data, value);
mainPlugin.saveConfig();
2011-09-18 19:34:06 -04:00
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
public static Material getOpenInvItem() {
if (!mainPlugin.getConfig().isSet("items.open-inv")) {
saveToConfig("items.open-inv", "STICK");
}
2015-06-23 16:46:14 +10:00
2015-06-23 13:31:26 +10:00
String itemName = mainPlugin.getConfig().getString("items.open-inv", "STICK");
2015-06-23 16:17:51 +10:00
Material material = Material.getMaterial(itemName);
2015-06-23 16:46:14 +10:00
if (material == null) {
mainPlugin.getLogger().info("OpenInv item '" + itemName + "' does not match to a valid item. Defaulting to stick.");
material = Material.STICK;
}
return material;
2015-06-23 13:31:26 +10:00
}
public static boolean notifySilentChest() {
return mainPlugin.getConfig().getBoolean("notify.silent-chest", true);
}
public static boolean notifyAnyChest() {
return mainPlugin.getConfig().getBoolean("notify.any-chest", true);
}
public static boolean getPlayerAnyChestStatus(Player player) {
return mainPlugin.getConfig().getBoolean("toggles.any-chest." + player.getUniqueId(), false);
}
public static void setPlayerAnyChestStatus(Player player, boolean status) {
saveToConfig("toggles.any-chest." + player.getUniqueId(), status);
}
public static boolean getPlayerItemOpenInvStatus(Player player) {
return mainPlugin.getConfig().getBoolean("toggles.items.open-inv" + player.getUniqueId(), false);
}
public static void setPlayerItemOpenInvStatus(Player player, boolean status) {
saveToConfig("toggles.items.open-inv." + player.getUniqueId(), status);
}
public static boolean getPlayerSilentChestStatus(Player player) {
return mainPlugin.getConfig().getBoolean("toggles.silent-chest." + player.getUniqueId(), false);
}
public static void setPlayerSilentChestStatus(Player player, boolean status) {
saveToConfig("toggles.silent-chest." + player.getUniqueId(), status);
}
/**
2015-06-23 13:31:26 +10:00
* Logs a given message to console.
*
* @param text the text to log
*/
2013-12-07 04:44:40 -05:00
public static void log(String text) {
2015-06-23 13:31:26 +10:00
mainPlugin.getLogger().info("[OpenInv] " + text);
}
2013-12-07 04:44:40 -05:00
/**
2015-06-23 13:31:26 +10:00
* Logs an error to console.
*
* @param e the throwable error to log
*/
2013-12-07 04:44:40 -05:00
public static void log(Throwable e) {
2015-06-23 13:31:26 +10:00
mainPlugin.getLogger().severe("[OpenInv] " + e.toString());
e.printStackTrace();
}
2013-12-07 04:44:40 -05:00
2015-06-23 13:31:26 +10:00
/**
* Sends a specified message to a given CommandSender with the OpenInv prefix.
*
* @param sender the CommandSender to message
* @param message the message to send to the player
*/
public static void sendMessage(CommandSender sender, String message) {
sender.sendMessage(ChatColor.AQUA + "[OpenInv] " + ChatColor.WHITE + message);
}
2015-06-22 20:15:28 +10:00
public static void showHelp(Player player) {
2015-06-23 16:17:51 +10:00
player.sendMessage(ChatColor.GREEN + "/openinv <player> - Open a player's inventory");
player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)");
2015-06-23 16:17:51 +10:00
player.sendMessage(ChatColor.GREEN + "/openender <player> - Open a player's ender chest");
player.sendMessage(ChatColor.GREEN + " (aliases: oe, enderchest)");
player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggle item openinv function");
player.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)");
2015-06-23 16:17:51 +10:00
player.sendMessage(ChatColor.GREEN + "/searchinv <item> [minAmount] - ");
player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item.");
player.sendMessage(ChatColor.GREEN + " (aliases: si, search)");
player.sendMessage(ChatColor.GREEN + "/anychest - Toggle anychest function");
player.sendMessage(ChatColor.GREEN + " (aliases: ac)");
player.sendMessage(ChatColor.GREEN + "/silentchest - Toggle silent chest function");
player.sendMessage(ChatColor.GREEN + " (aliases: sc, silent)");
2012-01-12 14:23:53 -05:00
}
2013-12-07 04:44:40 -05:00
2013-12-06 02:31:14 -05:00
public static boolean hasPermission(Permissible player, String permission) {
String[] parts = permission.split("\\.");
String perm = "";
2013-12-07 04:44:40 -05:00
for (int i = 0; i < parts.length; i++) {
if (player.hasPermission(perm + "*")) {
2013-12-06 02:31:14 -05:00
return true;
}
perm += parts[i] + ".";
}
return player.hasPermission(permission);
}
2015-06-22 12:03:03 +10:00
}