Merge pull request #39 from ShadowRanger/master

General refactoring
This commit is contained in:
ShadowRanger 2016-04-12 14:38:37 +10:00
commit 27de16cba9
24 changed files with 454 additions and 197 deletions

View file

@ -4,7 +4,7 @@
<groupId>com.lishid</groupId>
<artifactId>openinv</artifactId>
<packaging>jar</packaging>
<version>2.3.6</version>
<version>2.3.7</version>
<name>OpenInv</name>
<url>http://dev.bukkit.org/bukkit-plugins/openinv/</url>
@ -23,14 +23,14 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
<version>1.9.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
<version>1.9.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View file

@ -0,0 +1,153 @@
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);
}
}

View file

@ -20,9 +20,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
@ -50,165 +48,144 @@ public class OpenInv extends JavaPlugin {
public static final Map<UUID, SpecialPlayerInventory> inventories = new HashMap<UUID, SpecialPlayerInventory>();
public static final Map<UUID, SpecialEnderChest> enderChests = new HashMap<UUID, SpecialEnderChest>();
public static OpenInv mainPlugin;
private Configuration configuration;
private static PlayerDataManager playerLoader;
private static InventoryAccess inventoryAccess;
private static AnySilentChest anySilentChest;
private PlayerDataManager playerLoader;
private InventoryAccess inventoryAccess;
private AnySilentChest anySilentChest;
@Override
public void onEnable() {
// Plugin
mainPlugin = this;
// Config Updater
ConfigUpdater configUpdater = new ConfigUpdater(this);
configUpdater.checkForUpdates();
// Initialize
playerLoader = new PlayerDataManager();
inventoryAccess = new InventoryAccess();
anySilentChest = new AnySilentChest();
// Save the default config.yml if it doesn't already exist
saveDefaultConfig();
// Register the plugin's events & commands
registerEvents();
registerCommands();
}
// Config
configuration = new Configuration(this);
private void registerEvents() {
// Initialize
playerLoader = new PlayerDataManager(this);
inventoryAccess = new InventoryAccess(this);
anySilentChest = new AnySilentChest(this);
// Register the plugin's events
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new OpenInvPlayerListener(), this);
pm.registerEvents(new OpenInvEntityListener(), this);
pm.registerEvents(new OpenInvInventoryListener(), this);
}
pm.registerEvents(new OpenInvPlayerListener(this), this);
pm.registerEvents(new OpenInvEntityListener(this), this);
pm.registerEvents(new OpenInvInventoryListener(this), this);
private void registerCommands() {
// Register the plugin's commands
getCommand("openinv").setExecutor(new OpenInvCommand(this));
getCommand("openender").setExecutor(new OpenEnderCommand(this));
getCommand("searchinv").setExecutor(new SearchInvCommand());
getCommand("searchinv").setExecutor(new SearchInvCommand(this));
getCommand("searchender").setExecutor(new SearchEnderCommand());
getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand());
getCommand("anychest").setExecutor(new AnyChestCommand());
getCommand("silentchest").setExecutor(new SilentChestCommand());
getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this));
getCommand("anychest").setExecutor(new AnyChestCommand(this));
getCommand("silentchest").setExecutor(new SilentChestCommand(this));
}
public static PlayerDataManager getPlayerLoader() {
/**
* Returns the plugin Configuration.
*
* @return the plugin Configuration
*/
public Configuration getConfiguration() {
return configuration;
}
/**
* Returns an instance of PlayerDataManager.
*
* @return an instance of PlayerDataManager
*/
public PlayerDataManager getPlayerLoader() {
return playerLoader;
}
public static InventoryAccess getInventoryAccess() {
/**
* Returns an instance of InventoryAccess.
*
* @return an instance of InventoryAccess
*/
public InventoryAccess getInventoryAccess() {
return inventoryAccess;
}
public static AnySilentChest getAnySilentChest() {
/**
* Returns an instance of AnySilentChest.
*
* @return an instance of AnySilentChest
*/
public AnySilentChest getAnySilentChest() {
return anySilentChest;
}
public static Object getFromConfig(String path, Object defaultValue) {
Object val = mainPlugin.getConfig().get(path);
if (val == null) {
mainPlugin.getConfig().set(path, defaultValue);
return defaultValue;
} else {
return val;
}
/**
* Logs a message to console.
*
* @param text the message to log
*/
public void log(String text) {
getLogger().info(text);
}
public static void saveToConfig(String path, Object value) {
mainPlugin.getConfig().set(path, value);
mainPlugin.saveConfig();
}
public static Material getOpenInvItem() {
if (!mainPlugin.getConfig().isSet("items.open-inv")) {
saveToConfig("items.open-inv", "STICK");
}
String itemName = mainPlugin.getConfig().getString("items.open-inv", "STICK");
Material material = Material.getMaterial(itemName);
if (material == null) {
mainPlugin.getLogger().warning("OpenInv item '" + itemName + "' does not match to a valid item. Defaulting to stick.");
material = Material.STICK;
}
return material;
}
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(), true);
}
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);
}
public static void log(String text) {
mainPlugin.getLogger().info("[OpenInv] " + text);
}
public static void log(Throwable e) {
mainPlugin.getLogger().severe("[OpenInv] " + e.toString());
/**
* Logs a Throwable to console.
*
* @param e the Throwable to log
*/
public void log(Throwable e) {
getLogger().severe(e.toString());
e.printStackTrace();
}
/**
* 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 player.
*
* @param player the player to show help to
*/
public static void showHelp(Player player) {
player.sendMessage(ChatColor.GREEN + "/openinv <player> - Open a player's inventory.");
player.sendMessage(ChatColor.GREEN + "/openinv <player> - Opens a player's inventory.");
player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)");
player.sendMessage(ChatColor.GREEN + "/openender <player> - Open a player's ender chest.");
player.sendMessage(ChatColor.GREEN + "/openender <player> - Opens a player's ender chest.");
player.sendMessage(ChatColor.GREEN + " (aliases: oe)");
player.sendMessage(ChatColor.GREEN + "/searchinv <item> [minAmount] -");
player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item.");
player.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their inventory.");
player.sendMessage(ChatColor.GREEN + " (aliases: si)");
player.sendMessage(ChatColor.GREEN + "/searchender <item> [minAmount] -");
player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item.");
player.sendMessage(ChatColor.GREEN + " Searches and lists players that have a specific item in their ender chest.");
player.sendMessage(ChatColor.GREEN + " (aliases: se)");
player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggle the item openinv function.");
player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggles the item openinv function.");
player.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)");
player.sendMessage(ChatColor.GREEN + "/anychest - Toggle the any chest function.");
player.sendMessage(ChatColor.GREEN + "/anychest - Toggles the any chest function.");
player.sendMessage(ChatColor.GREEN + " (aliases: ac)");
player.sendMessage(ChatColor.GREEN + "/silentchest - Toggle the silent chest function.");
player.sendMessage(ChatColor.GREEN + "/silentchest - Toggles the silent chest function.");
player.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 = "";

View file

@ -1,6 +1,9 @@
package com.lishid.openinv;
public class Permissions {
public final class Permissions {
private Permissions() {}
public static final String PERM_OPENINV = "OpenInv.openinv";
public static final String PERM_OVERRIDE = "OpenInv.override";
public static final String PERM_EXEMPT = "OpenInv.exempt";

View file

@ -24,9 +24,18 @@ 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")) {
@ -44,15 +53,15 @@ public class AnyChestCommand implements CommandExecutor {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("check")) {
String status = OpenInv.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "Any Chest is " + status + ChatColor.RESET + ".");
return true;
}
}
OpenInv.setPlayerAnyChestStatus(player, !OpenInv.getPlayerAnyChestStatus(player));
configuration.setPlayerAnyChestStatus(player, !configuration.getPlayerAnyChestStatus(player));
String status = OpenInv.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
String status = configuration.getPlayerAnyChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "Any Chest is now " + status + ChatColor.RESET + ".");
return true;

View file

@ -96,7 +96,7 @@ public class OpenEnderCommand implements CommandExecutor {
@Override
public void run() {
// Try loading the player's data asynchronously
final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid);
final Player target = plugin.getPlayerLoader().loadPlayer(uuid);
if (target == null) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;

View file

@ -48,6 +48,7 @@ public class OpenInvCommand implements CommandExecutor {
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;
@ -90,7 +91,7 @@ public class OpenInvCommand implements CommandExecutor {
@Override
public void run() {
// Try loading the player's data asynchronously
final Player target = OpenInv.getPlayerLoader().loadPlayer(uuid);
final Player target = plugin.getPlayerLoader().loadPlayer(uuid);
if (target == null) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;
@ -105,6 +106,7 @@ public class OpenInvCommand implements CommandExecutor {
if (player == null) {
return;
}
openInventory(player, target);
}
});
@ -148,14 +150,10 @@ public class OpenInvCommand implements CommandExecutor {
openInvHistory.put(player.getUniqueId(), target.getUniqueId());
// Create the inventory
/*
SpecialPlayerInventory inv = OpenInv.inventories.get(target.getUniqueId());
if (inv == null) {
inv = new SpecialPlayerInventory(target, target.isOnline());
}
*/
SpecialPlayerInventory inv = new SpecialPlayerInventory(target, target.isOnline());
// Open the inventory
player.openInventory(inv.getBukkitInventory());

View file

@ -42,7 +42,7 @@ public class SearchEnderCommand implements CommandExecutor {
}
if (material == null) {
sender.sendMessage(ChatColor.RED + "Unknown item");
sender.sendMessage(ChatColor.RED + "Unknown item.");
return false;
}

View file

@ -29,6 +29,12 @@ 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")) {
@ -58,7 +64,7 @@ public class SearchInvCommand implements CommandExecutor {
}
if (material == null) {
sender.sendMessage(ChatColor.RED + "Unknown item");
sender.sendMessage(ChatColor.RED + "Unknown item.");
return false;
}

View file

@ -24,9 +24,18 @@ 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")) {
@ -44,15 +53,15 @@ public class SilentChestCommand implements CommandExecutor {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("check")) {
String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "Silent Chest is " + status + ChatColor.RESET + ".");
return true;
}
}
OpenInv.setPlayerSilentChestStatus(player, !OpenInv.getPlayerSilentChestStatus(player));
configuration.setPlayerSilentChestStatus(player, !configuration.getPlayerSilentChestStatus(player));
String status = OpenInv.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
String status = configuration.getPlayerSilentChestStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "Silent Chest is now " + status + ChatColor.RESET + ".");
return true;

View file

@ -24,9 +24,18 @@ 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")) {
@ -44,16 +53,16 @@ public class ToggleOpenInvCommand implements CommandExecutor {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("check")) {
String status = OpenInv.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + OpenInv.getOpenInvItem() + ChatColor.RESET + status + ChatColor.RESET + ".");
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;
}
}
OpenInv.setPlayerItemOpenInvStatus(player, !OpenInv.getPlayerItemOpenInvStatus(player));
configuration.setPlayerItemOpenInvStatus(player, !configuration.getPlayerItemOpenInvStatus(player));
String status = OpenInv.getPlayerItemOpenInvStatus(player) ? ChatColor.GREEN + "ON" : ChatColor.RED + "OFF";
OpenInv.sendMessage(player, "OpenInv with " + ChatColor.GRAY + OpenInv.getOpenInvItem() + ChatColor.RESET + " is now " + status + ChatColor.RESET + ".");
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;
}

View file

@ -18,18 +18,34 @@ package com.lishid.openinv.internal;
import java.util.Iterator;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import com.lishid.openinv.OpenInv;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import net.minecraft.server.v1_9_R1.AxisAlignedBB;
import net.minecraft.server.v1_9_R1.Block;
import net.minecraft.server.v1_9_R1.BlockChest;
import net.minecraft.server.v1_9_R1.BlockChest.Type;
import org.bukkit.craftbukkit.v1_9_R1.entity.*;
import net.minecraft.server.v1_9_R1.BlockPosition;
import net.minecraft.server.v1_9_R1.Entity;
import net.minecraft.server.v1_9_R1.EntityOcelot;
import net.minecraft.server.v1_9_R1.EntityPlayer;
import net.minecraft.server.v1_9_R1.EnumDirection;
import net.minecraft.server.v1_9_R1.ITileInventory;
import net.minecraft.server.v1_9_R1.InventoryLargeChest;
import net.minecraft.server.v1_9_R1.TileEntity;
import net.minecraft.server.v1_9_R1.TileEntityChest;
import net.minecraft.server.v1_9_R1.World;
public class AnySilentChest {
private final OpenInv plugin;
public AnySilentChest(OpenInv plugin) {
this.plugin = plugin;
}
public boolean isAnyChestNeeded(Player p, int x, int y, int z) {
// FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest
BlockPosition position = new BlockPosition(x, y, z);
@ -135,7 +151,7 @@ public class AnySilentChest {
if (silentChest) {
tileInventory = new SilentInventory(tileInventory);
if (OpenInv.notifySilentChest()) {
if (plugin.getConfiguration().notifySilentChest()) {
OpenInv.sendMessage(p, "You are opening a chest silently.");
}
@ -144,7 +160,7 @@ public class AnySilentChest {
player.openContainer(tileInventory);
if (anyChest && OpenInv.notifyAnyChest()) {
if (anyChest && plugin.getConfiguration().notifyAnyChest()) {
OpenInv.sendMessage(p, "You are opening a blocked chest.");
}

View file

@ -18,19 +18,23 @@ package com.lishid.openinv.internal;
import java.lang.reflect.Field;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.Permissions;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import org.bukkit.craftbukkit.v1_9_R1.inventory.*;
import net.minecraft.server.v1_9_R1.IInventory;
public class InventoryAccess {
private final OpenInv plugin;
public InventoryAccess(OpenInv plugin) {
this.plugin = plugin;
}
public boolean check(Inventory inventory, HumanEntity player) {
IInventory inv = grabInventory(inventory);
@ -64,7 +68,7 @@ public class InventoryAccess {
try {
result = (IInventory) f.get(inventory);
} catch (Exception e) {
OpenInv.log(e);
plugin.log(e);
}
}
}

View file

@ -20,18 +20,24 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
import org.bukkit.entity.Player;
import com.lishid.openinv.OpenInv;
import com.mojang.authlib.GameProfile;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import org.bukkit.craftbukkit.v1_9_R1.*;
import net.minecraft.server.v1_9_R1.EntityPlayer;
import net.minecraft.server.v1_9_R1.MinecraftServer;
import net.minecraft.server.v1_9_R1.PlayerInteractManager;
public class PlayerDataManager {
private final OpenInv plugin;
public PlayerDataManager(OpenInv plugin) {
this.plugin = plugin;
}
public Player loadPlayer(UUID uuid) {
try {
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
@ -55,7 +61,7 @@ public class PlayerDataManager {
return target;
}
} catch (Exception e) {
OpenInv.log(e);
plugin.log(e);
}
return null;

View file

@ -16,8 +16,9 @@
package com.lishid.openinv.internal;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import net.minecraft.server.v1_9_R1.ContainerChest;
import net.minecraft.server.v1_9_R1.EntityHuman;
import net.minecraft.server.v1_9_R1.IInventory;
public class SilentContainerChest extends ContainerChest {

View file

@ -7,8 +7,14 @@ import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.InventoryHolder;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import net.minecraft.server.v1_9_R1.ChestLock;
import net.minecraft.server.v1_9_R1.Container;
import net.minecraft.server.v1_9_R1.ContainerChest;
import net.minecraft.server.v1_9_R1.EntityHuman;
import net.minecraft.server.v1_9_R1.IChatBaseComponent;
import net.minecraft.server.v1_9_R1.ITileInventory;
import net.minecraft.server.v1_9_R1.ItemStack;
import net.minecraft.server.v1_9_R1.PlayerInventory;
public class SilentInventory implements ITileInventory {

View file

@ -16,17 +16,17 @@
package com.lishid.openinv.internal;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import com.lishid.openinv.OpenInv;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import org.bukkit.craftbukkit.v1_9_R1.entity.*;
import org.bukkit.craftbukkit.v1_9_R1.inventory.*;
import net.minecraft.server.v1_9_R1.InventoryEnderChest;
import net.minecraft.server.v1_9_R1.InventorySubcontainer;
public class SpecialEnderChest extends InventorySubcontainer {
@ -51,7 +51,6 @@ public class SpecialEnderChest extends InventorySubcontainer {
private void saveOnExit() {
if (transaction.isEmpty() && !playerOnline) {
owner.saveData();
OpenInv.enderChests.remove(owner.getUniqueId());
}
}
@ -82,6 +81,7 @@ public class SpecialEnderChest extends InventorySubcontainer {
public void onClose(CraftHumanEntity who) {
super.onClose(who);
saveOnExit();
OpenInv.enderChests.remove(owner.getUniqueId());
}
@Override

View file

@ -18,30 +18,31 @@ package com.lishid.openinv.internal;
import java.lang.reflect.Field;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
// Volatile
import net.minecraft.server.v1_9_R1.*;
import org.bukkit.craftbukkit.v1_9_R1.entity.*;
import org.bukkit.craftbukkit.v1_9_R1.inventory.*;
import com.lishid.openinv.OpenInv;
import net.minecraft.server.v1_9_R1.ContainerUtil;
import net.minecraft.server.v1_9_R1.EntityHuman;
import net.minecraft.server.v1_9_R1.ItemStack;
import net.minecraft.server.v1_9_R1.PlayerInventory;
public class SpecialPlayerInventory extends PlayerInventory {
private final CraftInventory inventory = new CraftInventory(this);
private final ItemStack[] extra = new ItemStack[4];
private final ItemStack[][] arrays;
private final CraftPlayer owner;
private ItemStack[][] arrays;
private boolean playerOnline;
public SpecialPlayerInventory(Player p, boolean online) {
super(((CraftPlayer) p).getHandle());
this.owner = (CraftPlayer) p;
reflectContents(getClass().getSuperclass(), player.inventory, this);
this.arrays = new ItemStack[][] { this.items, this.armor, this.extraSlots, this.extra };
this.playerOnline = online;
OpenInv.inventories.put(owner.getUniqueId(), this);
}
@ -68,6 +69,8 @@ public class SpecialPlayerInventory extends PlayerInventory {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
arrays = new ItemStack[][] { this.items, this.armor, this.extraSlots, this.extra };
}
public Inventory getBukkitInventory() {
@ -77,7 +80,6 @@ public class SpecialPlayerInventory extends PlayerInventory {
private void saveOnExit() {
if (transaction.isEmpty() && !playerOnline) {
owner.saveData();
OpenInv.inventories.remove(owner.getUniqueId());
}
}
@ -105,6 +107,7 @@ public class SpecialPlayerInventory extends PlayerInventory {
public void onClose(CraftHumanEntity who) {
super.onClose(who);
this.saveOnExit();
OpenInv.inventories.remove(owner.getUniqueId());
}
@Override

View file

@ -25,8 +25,18 @@ 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();
@ -38,8 +48,8 @@ public class OpenInvEntityListener implements Listener {
Player player = (Player) attacker;
if (player.getInventory().getItemInMainHand().getType() == OpenInv.getOpenInvItem()) {
if (!OpenInv.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem()) {
if (!configuration.getPlayerItemOpenInvStatus(player) || !OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
return;
}

View file

@ -25,12 +25,19 @@ 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 (!OpenInv.getInventoryAccess().check(inventory, player)) {
if (!plugin.getInventoryAccess().check(inventory, player)) {
event.setCancelled(true);
}
}

View file

@ -30,13 +30,24 @@ 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;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.Permissions;
import com.lishid.openinv.Configuration;
import com.lishid.openinv.internal.SpecialEnderChest;
import com.lishid.openinv.internal.SpecialPlayerInventory;
public class OpenInvPlayerListener implements Listener {
private final OpenInv plugin;
private final Configuration configuration;
public OpenInvPlayerListener(OpenInv plugin) {
this.plugin = plugin;
configuration = plugin.getConfiguration();
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
@ -46,24 +57,34 @@ public class OpenInvPlayerListener implements Listener {
inventory.playerOnline(event.getPlayer());
}
SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId());
if (chest != null) {
chest.playerOnline(event.getPlayer());
SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId());
if (enderChest != null) {
enderChest.playerOnline(event.getPlayer());
}
}
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
SpecialPlayerInventory inventory = OpenInv.inventories.get(player.getUniqueId());
final SpecialPlayerInventory inventory = OpenInv.inventories.get(player.getUniqueId());
if (inventory != null) {
inventory.playerOffline();
new BukkitRunnable() {
@Override
public void run() {
inventory.playerOffline();
}
}.runTaskLater(plugin, 1);
}
SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId());
if (chest != null) {
chest.playerOffline();
final SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId());
if (enderChest != null) {
new BukkitRunnable() {
@Override
public void run() {
enderChest.playerOffline();
}
}.runTaskLater(plugin, 1);
}
}
@ -86,7 +107,7 @@ public class OpenInvPlayerListener implements Listener {
// Ender Chests
if (block.getType() == Material.ENDER_CHEST) {
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) {
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) {
event.setCancelled(true);
player.openInventory(player.getEnderChest());
return;
@ -101,15 +122,14 @@ public class OpenInvPlayerListener implements Listener {
int y = block.getY();
int z = block.getZ();
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && OpenInv.getPlayerSilentChestStatus(player)) {
if (OpenInv.hasPermission(player, Permissions.PERM_SILENT) && configuration.getPlayerSilentChestStatus(player)) {
silentChest = true;
}
if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && OpenInv.getPlayerAnyChestStatus(player)) {
if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) {
try {
anyChest = OpenInv.getAnySilentChest().isAnyChestNeeded(player, x, y, z);
}
catch (Exception e) {
anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z);
} catch (Exception e) {
player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit.");
e.printStackTrace();
}
@ -117,7 +137,7 @@ public class OpenInvPlayerListener implements Listener {
// If the anyChest or silentChest is active
if (anyChest || silentChest) {
if (!OpenInv.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) {
if (!plugin.getAnySilentChest().activateChest(player, anyChest, silentChest, x, y, z)) {
event.setCancelled(true);
}
}
@ -128,14 +148,14 @@ public class OpenInvPlayerListener implements Listener {
// Signs
if (block.getState() instanceof Sign) {
try {
Sign sign = ((Sign) block.getState());
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("Internal Error.");
} catch (Exception e) {
player.sendMessage(ChatColor.RED + "An internal error occured.");
e.printStackTrace();
}
@ -143,7 +163,7 @@ public class OpenInvPlayerListener implements Listener {
}
case RIGHT_CLICK_AIR:
// OpenInv item
if (player.getInventory().getItemInMainHand().getType() == OpenInv.getOpenInvItem() && OpenInv.getPlayerItemOpenInvStatus(player) && OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
if (player.getInventory().getItemInMainHand().getType() == configuration.getOpenInvItem() && configuration.getPlayerItemOpenInvStatus(player) && OpenInv.hasPermission(player, Permissions.PERM_OPENINV)) {
player.performCommand("openinv");
}
}

View file

@ -15,6 +15,7 @@ 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();
@ -34,11 +35,13 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
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");
@ -46,10 +49,12 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name.toLowerCase(), uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
}
return uuidMap;
}
@ -62,12 +67,14 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
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;
}
@ -79,6 +86,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
@ -86,9 +94,11 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
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);
}

View file

@ -10,7 +10,10 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
public class UUIDUtil {
public final class UUIDUtil {
private UUIDUtil() {}
private static Player getPlayer(String name) {
Validate.notNull(name, "Name cannot be null");
@ -22,10 +25,12 @@ public class UUIDUtil {
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;
}
}
@ -39,14 +44,19 @@ public class UUIDUtil {
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 getUUIDOf(String name) {
UUID uuid;
Player player = getPlayer(name);
if (player != null) {
uuid = player.getUniqueId();
}
else {
} else {
if (Bukkit.getServer().getOnlineMode()) {
if (!Bukkit.getServer().isPrimaryThread()) {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
@ -55,8 +65,7 @@ public class UUIDUtil {
try {
response = fetcher.call();
uuid = response.get(name.toLowerCase());
}
catch (Exception e) {
} catch (Exception e) {
uuid = getUUIDLocally(name);
}
} else {

View file

@ -1,7 +1,8 @@
name: OpenInv
main: com.lishid.openinv.OpenInv
version: 2.3.6
version: 2.3.7
author: lishid
authors: [ShadowRanger]
description: >
This plugin allows you to open a player's inventory as a chest and interact with it in real time.
commands: