From d1a83c53ca3896ec72d04950dea38f9e2a89f64e Mon Sep 17 00:00:00 2001 From: lishd Date: Fri, 3 Jun 2011 10:32:54 -0400 Subject: [PATCH] 1.3.1 --- src/lishid/openinv/OpenInv.java | 70 +++++++++++ src/lishid/openinv/OpenInvEntityListener.java | 47 ++++++++ src/lishid/openinv/OpenInvPlayerListener.java | 74 ++++++++++++ src/lishid/openinv/PermissionRelay.java | 15 +++ .../commands/OpenInvPluginCommand.java | 111 ++++++++++++++++++ .../commands/SearchInvPluginCommand.java | 62 ++++++++++ src/lishid/openinv/utils/OpenInvHistory.java | 14 +++ .../openinv/utils/OpenInvToggleState.java | 7 ++ .../openinv/utils/PlayerInventoryChest.java | 31 +++++ src/plugin.yml | 17 +++ 10 files changed, 448 insertions(+) create mode 100644 src/lishid/openinv/OpenInv.java create mode 100644 src/lishid/openinv/OpenInvEntityListener.java create mode 100644 src/lishid/openinv/OpenInvPlayerListener.java create mode 100644 src/lishid/openinv/PermissionRelay.java create mode 100644 src/lishid/openinv/commands/OpenInvPluginCommand.java create mode 100644 src/lishid/openinv/commands/SearchInvPluginCommand.java create mode 100644 src/lishid/openinv/utils/OpenInvHistory.java create mode 100644 src/lishid/openinv/utils/OpenInvToggleState.java create mode 100644 src/lishid/openinv/utils/PlayerInventoryChest.java create mode 100644 src/plugin.yml diff --git a/src/lishid/openinv/OpenInv.java b/src/lishid/openinv/OpenInv.java new file mode 100644 index 0000000..e542fde --- /dev/null +++ b/src/lishid/openinv/OpenInv.java @@ -0,0 +1,70 @@ + +package lishid.openinv; + +import lishid.openinv.commands.*; +import lishid.openinv.utils.PlayerInventoryChest; + +import net.minecraft.server.ContainerPlayer; +import net.minecraft.server.EntityPlayer; + +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.event.Event; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import com.nijiko.permissions.PermissionHandler; +import com.nijikokun.bukkit.Permissions.Permissions; +import org.bukkit.plugin.Plugin; + +/** + * Open other player's inventory + * + * @author lishid + */ +public class OpenInv extends JavaPlugin { + private final OpenInvPlayerListener playerListener = new OpenInvPlayerListener(this); + private final OpenInvEntityListener entityListener = new OpenInvEntityListener(this); + public static PermissionHandler permissionHandler; + public void onDisable() { + } + + private void setupPermissions() { + Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions"); + + if (permissionHandler == null) { + if (permissionsPlugin != null) { + permissionHandler = ((Permissions) permissionsPlugin).getHandler(); + } else { + //log.info("Permission system not detected, defaulting to OP"); + } + } + } + + public void onEnable() { + + PluginManager pm = getServer().getPluginManager(); + pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this); + //pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Event.Priority.Normal, this); + pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Normal, this); + pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Event.Priority.Normal, this); + setupPermissions(); + // EXAMPLE: Custom code, here we just output some info so we can check all is well + PluginDescriptionFile pdfFile = this.getDescription(); + System.out.println("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is enabled!" ); + + getCommand("openinv").setExecutor(new OpenInvPluginCommand(this)); + getCommand("searchinv").setExecutor(new SearchInvPluginCommand(this)); + getCommand("toggleopeninv").setExecutor(new OpenInvPluginCommand(this)); + } + + public static void ReplaceInv(CraftPlayer player) + { + EntityPlayer entityplayer = player.getHandle(); + entityplayer.inventory = new PlayerInventoryChest(entityplayer.inventory); + entityplayer.defaultContainer = new ContainerPlayer(entityplayer.inventory, !entityplayer.world.isStatic); + entityplayer.activeContainer = entityplayer.defaultContainer; + player.setHandle(entityplayer); + } + + +} \ No newline at end of file diff --git a/src/lishid/openinv/OpenInvEntityListener.java b/src/lishid/openinv/OpenInvEntityListener.java new file mode 100644 index 0000000..28fee5d --- /dev/null +++ b/src/lishid/openinv/OpenInvEntityListener.java @@ -0,0 +1,47 @@ +package lishid.openinv; + +import lishid.openinv.utils.OpenInvToggleState; + +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityListener; + +public class OpenInvEntityListener extends EntityListener{ + OpenInv plugin; + public OpenInvEntityListener(OpenInv scrap) { + plugin = scrap; + } + + @Override + public void onEntityDamage(EntityDamageEvent event) { + if (event instanceof EntityDamageByEntityEvent) { + EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent) event; + Entity attacker = evt.getDamager(); + Entity defender = evt.getEntity(); + + if(!(attacker instanceof Player)||!(defender instanceof Player)) + { + return; + } + + Player player = (Player)attacker; + + if(!(player.getItemInHand().getType() == Material.STICK) + || (OpenInvToggleState.openInvState.get(player.getName()) == null) + || !(OpenInvToggleState.openInvState.get(player.getName()) == 1) + || !PermissionRelay.hasPermission(player, "openinv")) + { + return; + } + + Player target = (Player)defender; + player.performCommand("openinv " + target.getName()); + + evt.setDamage(0); + evt.setCancelled(true); + } + } +} diff --git a/src/lishid/openinv/OpenInvPlayerListener.java b/src/lishid/openinv/OpenInvPlayerListener.java new file mode 100644 index 0000000..d571b26 --- /dev/null +++ b/src/lishid/openinv/OpenInvPlayerListener.java @@ -0,0 +1,74 @@ +package lishid.openinv; + +import lishid.openinv.utils.OpenInvToggleState; + +import org.bukkit.Material; +import org.bukkit.block.Sign; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.player.PlayerRespawnEvent; + +public class OpenInvPlayerListener extends PlayerListener{ + OpenInv plugin; + public OpenInvPlayerListener(OpenInv scrap) { + plugin = scrap; + } + + @Override + public void onPlayerJoin(PlayerJoinEvent event) + { + OpenInv.ReplaceInv((CraftPlayer) event.getPlayer()); + } + + @Override + public void onPlayerRespawn(PlayerRespawnEvent event) + { + OpenInv.ReplaceInv((CraftPlayer) event.getPlayer()); + } + + @Override + public void onPlayerInteract(PlayerInteractEvent event) + { + if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) + { + Player player = event.getPlayer(); + + if(!(player.getItemInHand().getType() == Material.STICK) + || (OpenInvToggleState.openInvState.get(player.getName()) == null) + || !(OpenInvToggleState.openInvState.get(player.getName()) == 1) + || !PermissionRelay.hasPermission(player, "openinv")) + { + return; + } + + player.performCommand("openinv"); + } + + if(event.getAction() == Action.LEFT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign) + { + Player player = event.getPlayer(); + try{ + if (PermissionRelay.hasPermission(player, "openinv") && + ((Sign)event.getClickedBlock().getState()).getLine(1).equalsIgnoreCase("[openinv]")) + { + if(plugin.getServer().getPlayer(((Sign)event.getClickedBlock().getState()).getLine(2)) != null) + { + player.performCommand("openinv " + ((Sign)event.getClickedBlock().getState()).getLine(2)); + } + else + { + player.sendMessage("Player not found."); + } + } + } + catch(Exception ex) + { + player.sendMessage("Internal Error."); + } + } + } +} diff --git a/src/lishid/openinv/PermissionRelay.java b/src/lishid/openinv/PermissionRelay.java new file mode 100644 index 0000000..b819469 --- /dev/null +++ b/src/lishid/openinv/PermissionRelay.java @@ -0,0 +1,15 @@ +package lishid.openinv; + +import org.bukkit.entity.Player; + + +public class PermissionRelay { + public static boolean hasPermission(Player player, String permission) + { + if (OpenInv.permissionHandler == null) { + return player.isOp(); + }else{ + return OpenInv.permissionHandler.has(player, permission); + } + } +} diff --git a/src/lishid/openinv/commands/OpenInvPluginCommand.java b/src/lishid/openinv/commands/OpenInvPluginCommand.java new file mode 100644 index 0000000..d9caa5a --- /dev/null +++ b/src/lishid/openinv/commands/OpenInvPluginCommand.java @@ -0,0 +1,111 @@ +package lishid.openinv.commands; + +import java.util.HashMap; + +import lishid.openinv.PermissionRelay; +import lishid.openinv.OpenInv; +import lishid.openinv.utils.OpenInvToggleState; +import lishid.openinv.utils.PlayerInventoryChest; +import lishid.openinv.utils.OpenInvHistory; + +import net.minecraft.server.EntityPlayer; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +public class OpenInvPluginCommand implements CommandExecutor { + private final OpenInv plugin; + public static HashMap theOpenInvHistory = new HashMap(); + public OpenInvPluginCommand(OpenInv plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!PermissionRelay.hasPermission((Player) sender, "OpenInv.openinv")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories"); + return true; + } + + Player player = (Player)sender; + OpenInvHistory history = theOpenInvHistory.get(player); + + if(history == null) + { + history = new OpenInvHistory(player); + theOpenInvHistory.put(player, history); + } + + if(command.getName().equalsIgnoreCase("toggleopeninv")) + { + if(OpenInvToggleState.openInvState.get(player.getName()) != null && OpenInvToggleState.openInvState.get(player.getName()) == 1) + { + OpenInvToggleState.openInvState.put(player.getName(), 0); + player.sendMessage("OpenInv with stick is OFF."); + } + else + { + OpenInvToggleState.openInvState.put(player.getName(), 1); + player.sendMessage("OpenInv with stick is ON."); + } + return true; + } + + Player target; + + if (args.length < 1) { + if(history.lastPlayer != null) + { + target = this.plugin.getServer().getPlayer(history.lastPlayer); + //EntityPlayer entply = new EntityPlayer(((CraftServer)this.plugin.getServer()).getServer(), ((CraftPlayer)player).getHandle().world, "", null); + //CraftPlayer ply = new CraftPlayer((CraftServer) this.plugin.getServer(), null); + } + else + { + sender.sendMessage("OpenInv history is empty!"); + return false; + } + } + else + { + target = this.plugin.getServer().getPlayer(args[0]); + } + + + if(target == null) + { + sender.sendMessage("Player not found!"); + return false; + } + if(target == player) + { + sender.sendMessage("Cannot target yourself!"); + return false; + } + + if (!PermissionRelay.hasPermission(player, "OpenInv.override") && PermissionRelay.hasPermission(target, "OpenInv.exempt")) { + sender.sendMessage(ChatColor.RED + target.getDisplayName() + "'s inventory is protected!"); + return true; + } + + history.lastPlayer = target.getName(); + + // Get the EntityPlayer handle from the sender + EntityPlayer entityplayer = ((CraftPlayer) player).getHandle(); + + // Get the EntityPlayer from the Target + EntityPlayer entitytarget = ((CraftPlayer) target).getHandle(); + + if(!(entitytarget.inventory instanceof PlayerInventoryChest)) + { + OpenInv.ReplaceInv((CraftPlayer) target); + } + + entityplayer.a(entitytarget.inventory); + + return true; + } +} diff --git a/src/lishid/openinv/commands/SearchInvPluginCommand.java b/src/lishid/openinv/commands/SearchInvPluginCommand.java new file mode 100644 index 0000000..ba4648b --- /dev/null +++ b/src/lishid/openinv/commands/SearchInvPluginCommand.java @@ -0,0 +1,62 @@ +package lishid.openinv.commands; + +import lishid.openinv.PermissionRelay; +import lishid.openinv.OpenInv; + +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 { + private final OpenInv plugin; + public SearchInvPluginCommand(OpenInv plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!PermissionRelay.hasPermission((Player) sender, "OpenInv.search")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories"); + return true; + } + + ; + + String PlayerList = ""; + + 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; + } + + for(Player templayer : plugin.getServer().getOnlinePlayers()) + { + if(templayer.getInventory().contains(material, count)) + { + PlayerList += templayer.getName() + " "; + } + } + + sender.sendMessage("Players with the item " + material.toString() + ": " + PlayerList); + return true; + } +} diff --git a/src/lishid/openinv/utils/OpenInvHistory.java b/src/lishid/openinv/utils/OpenInvHistory.java new file mode 100644 index 0000000..78a53ab --- /dev/null +++ b/src/lishid/openinv/utils/OpenInvHistory.java @@ -0,0 +1,14 @@ +package lishid.openinv.utils; + +import org.bukkit.entity.Player; + +public class OpenInvHistory { + + public Player player = null; + public String lastPlayer = ""; + + public OpenInvHistory(Player player) + { + this.player = player; + } +} diff --git a/src/lishid/openinv/utils/OpenInvToggleState.java b/src/lishid/openinv/utils/OpenInvToggleState.java new file mode 100644 index 0000000..3d5853a --- /dev/null +++ b/src/lishid/openinv/utils/OpenInvToggleState.java @@ -0,0 +1,7 @@ +package lishid.openinv.utils; + +import java.util.HashMap; + +public class OpenInvToggleState { + public static HashMap openInvState = new HashMap(); +} diff --git a/src/lishid/openinv/utils/PlayerInventoryChest.java b/src/lishid/openinv/utils/PlayerInventoryChest.java new file mode 100644 index 0000000..1e5b11d --- /dev/null +++ b/src/lishid/openinv/utils/PlayerInventoryChest.java @@ -0,0 +1,31 @@ +package lishid.openinv.utils; + +import net.minecraft.server.ContainerPlayer; +import net.minecraft.server.EntityHuman; +import net.minecraft.server.EntityPlayer; +import net.minecraft.server.InventoryPlayer; + +public class PlayerInventoryChest extends InventoryPlayer +{ + public PlayerInventoryChest(InventoryPlayer inventory) { + super(inventory.d); + this.armor = inventory.armor; + this.items = inventory.items; + this.itemInHandIndex = inventory.itemInHandIndex; + this.e = inventory.e; + this.b(inventory.j()); + inventory.d.defaultContainer = new ContainerPlayer(this, !inventory.d.world.isStatic); + inventory.d.activeContainer = inventory.d.defaultContainer; + } + + @Override + public String getName() { + return ((EntityPlayer)this.d).displayName; + } + + @Override + public boolean a_(EntityHuman entityhuman) + { + return true; + } +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..ef950ff --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,17 @@ +name: OpenInv +main: lishid.openinv.OpenInv +version: 1.3.1 +author: lishid +description: > + This plugin allows you to open another player's inventory as a chest +commands: + openinv: + description: Open a player's inventory + usage: | + / + searchinv: + description: Search and list players having a specific item + usage: | + / [MinAmount] - Item can be the Item ID or the CraftBukkit Item Name, MinAmount is the minimum amount to be considered. + toggleopeninv: + description: Toggle the stick usage \ No newline at end of file