Added /openender and read-only inventories.

This commit is contained in:
lishid 2012-08-10 13:36:36 -04:00
parent e9466b9a4a
commit bc558ff691
14 changed files with 424 additions and 36 deletions

View file

@ -20,6 +20,7 @@ import java.util.HashMap;
import lishid.openinv.commands.*;
import lishid.openinv.utils.Metrics;
import lishid.openinv.utils.OpenInvEnderChest;
import lishid.openinv.utils.OpenInvPlayerInventory;
import org.bukkit.ChatColor;
@ -35,9 +36,8 @@ import org.bukkit.plugin.java.JavaPlugin;
*/
public class OpenInv extends JavaPlugin
{
private final OpenInvPlayerListener playerListener = new OpenInvPlayerListener(this);
private final OpenInvEntityListener entityListener = new OpenInvEntityListener(this);
public static HashMap<String, OpenInvPlayerInventory> inventories = new HashMap<String, OpenInvPlayerInventory>();
public static HashMap<String, OpenInvEnderChest> enderChests = new HashMap<String, OpenInvEnderChest>();
public static OpenInv mainPlugin;
private static Metrics metrics;
@ -55,14 +55,16 @@ public class OpenInv extends JavaPlugin
mainPlugin.saveConfig();
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(playerListener, this);
pm.registerEvents(entityListener, this);
pm.registerEvents(new OpenInvPlayerListener(), this);
pm.registerEvents(new OpenInvEntityListener(), this);
pm.registerEvents(new OpenInvInventoryListener(), this);
getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
getCommand("searchinv").setExecutor(new SearchInvPluginCommand(this));
getCommand("toggleopeninv").setExecutor(new ToggleOpenInvPluginCommand());
getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this));
getCommand("anychest").setExecutor(new AnyChestPluginCommand(this));
getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
// Metrics
try
@ -145,6 +147,8 @@ public class OpenInv extends JavaPlugin
{
player.sendMessage(ChatColor.GREEN + "/openinv <Player> - Open a player's inventory");
player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)");
player.sendMessage(ChatColor.GREEN + "/openender <Player> - Open a player's enderchest");
player.sendMessage(ChatColor.GREEN + " (aliases: oe, enderchest)");
player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggle item openinv function");
player.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)");
player.sendMessage(ChatColor.GREEN + "/searchinv <Item> [MinAmount] - ");

View file

@ -26,13 +26,6 @@ import org.bukkit.event.Listener;
public class OpenInvEntityListener implements Listener
{
OpenInv plugin;
public OpenInvEntityListener(OpenInv scrap)
{
plugin = scrap;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityDamage(EntityDamageEvent event)
{

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2011-2012 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 lishid.openinv;
import lishid.openinv.utils.OpenInvEnderChest;
import lishid.openinv.utils.OpenInvPlayerInventory;
import net.minecraft.server.IInventory;
import org.bukkit.craftbukkit.inventory.CraftInventory;
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;
public class OpenInvInventoryListener implements Listener
{
@EventHandler(priority = EventPriority.NORMAL)
public void onInventoryClick(InventoryClickEvent event)
{
IInventory inv = ((CraftInventory) event.getInventory()).getInventory();
HumanEntity player = event.getWhoClicked();
if (inv instanceof OpenInvPlayerInventory && event.getView().convertSlot(event.getRawSlot()) == event.getRawSlot())
{
if (!player.hasPermission(Permissions.PERM_EDITINV))
{
event.setCancelled(true);
}
}
else if (inv instanceof OpenInvEnderChest && event.getView().convertSlot(event.getRawSlot()) == event.getRawSlot())
{
if (!player.hasPermission(Permissions.PERM_EDITENDER))
{
event.setCancelled(true);
}
}
}
}

View file

@ -18,6 +18,7 @@ package lishid.openinv;
import java.lang.reflect.Field;
import lishid.openinv.utils.OpenInvEnderChest;
import lishid.openinv.utils.OpenInvPlayerInventory;
import lishid.openinv.utils.SilentContainerChest;
import net.minecraft.server.Block;
@ -44,13 +45,6 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class OpenInvPlayerListener implements Listener
{
OpenInv plugin;
public OpenInvPlayerListener(OpenInv plugin)
{
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event)
{
@ -60,17 +54,30 @@ public class OpenInvPlayerListener implements Listener
{
inventory.PlayerGoOnline((CraftPlayer) event.getPlayer());
}
OpenInvEnderChest chest = OpenInv.enderChests.get(event.getPlayer().getName().toLowerCase());
if (chest != null)
{
chest.PlayerGoOnline((CraftPlayer) event.getPlayer());
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event)
{
OpenInvPlayerInventory inventory = OpenInv.inventories.get(event.getPlayer().getName().toLowerCase());
if(inventory != null)
if (inventory != null)
{
inventory.PlayerGoOffline();
inventory.InventoryRemovalCheck();
}
OpenInvEnderChest chest = OpenInv.enderChests.get(event.getPlayer().getName().toLowerCase());
if (chest != null)
{
chest.PlayerGoOffline();
chest.InventoryRemovalCheck();
}
}
@EventHandler(priority = EventPriority.MONITOR)
@ -87,19 +94,15 @@ public class OpenInvPlayerListener implements Listener
int y = event.getClickedBlock().getY();
int z = event.getClickedBlock().getZ();
if (event.getPlayer().hasPermission("OpenInv.silent") && OpenInv.GetPlayerSilentChestStatus(event.getPlayer().getName()))
if (event.getPlayer().hasPermission(Permissions.PERM_SILENT) && OpenInv.GetPlayerSilentChestStatus(event.getPlayer().getName()))
{
silentchest = true;
}
if (event.getPlayer().hasPermission("OpenInv.anychest") && OpenInv.GetPlayerAnyChestStatus(event.getPlayer().getName()))
if (event.getPlayer().hasPermission(Permissions.PERM_ANYCHEST) && OpenInv.GetPlayerAnyChestStatus(event.getPlayer().getName()))
{
try
{
// FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest
EntityPlayer player = ((CraftPlayer) event.getPlayer()).getHandle();
World world = player.world;
@ -203,7 +206,7 @@ public class OpenInvPlayerListener implements Listener
try
{
Sign sign = ((Sign) event.getClickedBlock().getState());
if (player.hasPermission("OpenInv.openinv") && sign.getLine(0).equalsIgnoreCase("[openinv]"))
if (player.hasPermission(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);
@ -220,7 +223,7 @@ public class OpenInvPlayerListener implements Listener
{
Player player = event.getPlayer();
if (!(player.getItemInHand().getType().getId() == OpenInv.GetItemOpenInvItem()) || (!OpenInv.GetPlayerItemOpenInvStatus(player.getName())) || !player.hasPermission("OpenInv.openinv"))
if (!(player.getItemInHand().getType().getId() == OpenInv.GetItemOpenInvItem()) || (!OpenInv.GetPlayerItemOpenInvStatus(player.getName())) || !player.hasPermission(Permissions.PERM_OPENINV))
{
return;
}

View file

@ -0,0 +1,15 @@
package lishid.openinv;
public class 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";
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_SEARCH = "OpenInv.search";
public static final String PERM_EDITINV = "OpenInv.editinv";
public static final String PERM_EDITENDER = "OpenInv.editender";
}

View file

@ -17,6 +17,7 @@
package lishid.openinv.commands;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
@ -38,7 +39,7 @@ public class AnyChestPluginCommand implements CommandExecutor
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
return true;
}
if (!sender.hasPermission("OpenInv.anychest"))
if (!sender.hasPermission(Permissions.PERM_ANYCHEST))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to use anychest.");
return true;

View file

@ -0,0 +1,170 @@
/*
* Copyright (C) 2011-2012 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 lishid.openinv.commands;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import lishid.openinv.utils.OpenInvEnderChest;
import lishid.openinv.utils.OpenInvPlayerInventory;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.ItemInWorldManager;
import net.minecraft.server.MinecraftServer;
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.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
public class OpenEnderPluginCommand implements CommandExecutor
{
private final OpenInv plugin;
public static HashMap<Player, OpenInvPlayerInventory> offlineEnder = new HashMap<Player, OpenInvPlayerInventory>();
public static HashMap<Player, String> openEnderHistory = new HashMap<Player, String>();
public OpenEnderPluginCommand(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;
}
if (!sender.hasPermission(Permissions.PERM_ENDERCHEST))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to access player enderchest");
return true;
}
if (args.length > 0 && args[0].equalsIgnoreCase("?"))
{
OpenInv.ShowHelp((Player) sender);
return true;
}
Player player = (Player) sender;
boolean offline = false;
// History management
String history = openEnderHistory.get(player);
if (history == null || history == "")
{
history = player.getName();
openEnderHistory.put(player, history);
}
// Target selecting
Player target;
String name = "";
// Read from history if target is not named
if (args.length < 1)
{
if (history != null && history != "")
{
name = history;
}
else
{
sender.sendMessage(ChatColor.RED + "OpenEnder history is empty!");
return true;
}
}
else
{
name = args[0];
}
target = this.plugin.getServer().getPlayer(name);
if (target == null)
{
// Offline ender here...
try
{
// Default player folder
File playerfolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "players");
if (!playerfolder.exists())
{
sender.sendMessage(ChatColor.RED + "Player " + name + " not found!");
return true;
}
String playername = OpenInvPluginCommand.matchUser(Arrays.asList(playerfolder.listFiles()), name);
if (playername == null)
{
sender.sendMessage(ChatColor.RED + "Player " + name + " not found!");
return true;
}
// Create an entity to load the player data
final MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
final EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), playername, new ItemInWorldManager(server.getWorldServer(0)));
target = (entity == null) ? null : (Player) entity.getBukkitEntity();
if (target != null)
{
target.loadData();
offline = true;
}
else
{
sender.sendMessage(ChatColor.RED + "Player " + name + " not found!");
return true;
}
}
catch (Exception e)
{
sender.sendMessage("Error while retrieving offline player data!");
e.printStackTrace();
return true;
}
}
// Record the target
history = target.getName();
openEnderHistory.put(player, history);
// Create the inventory
OpenInvEnderChest chest = OpenInv.enderChests.get(target.getName().toLowerCase());
if (chest == null)
{
chest = new OpenInvEnderChest((CraftPlayer) target, !offline);
OpenInv.enderChests.put(target.getName().toLowerCase(), chest);
}
// Open the inventory
(((CraftPlayer) player).getHandle()).openContainer(chest);
return true;
}
}

View file

@ -22,6 +22,7 @@ import java.util.Collection;
import java.util.HashMap;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import lishid.openinv.utils.OpenInvPlayerInventory;
import net.minecraft.server.EntityPlayer;
@ -55,7 +56,7 @@ public class OpenInvPluginCommand implements CommandExecutor
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
return true;
}
if (!sender.hasPermission("OpenInv.openinv"))
if (!sender.hasPermission(Permissions.PERM_OPENINV))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories");
return true;
@ -159,13 +160,13 @@ public class OpenInvPluginCommand implements CommandExecutor
}
// Permissions checks
if (!player.hasPermission("OpenInv.override") && target.hasPermission("OpenInv.exempt"))
if (!player.hasPermission(Permissions.PERM_OVERRIDE) && target.hasPermission(Permissions.PERM_EXEMPT))
{
sender.sendMessage(ChatColor.RED + target.getDisplayName() + "'s inventory is protected!");
return true;
}
if ((!player.hasPermission("OpenInv.crossworld") && !player.hasPermission("OpenInv.override")) && target.getWorld() != player.getWorld())
if ((!player.hasPermission(Permissions.PERM_CROSSWORLD) && !player.hasPermission(Permissions.PERM_OVERRIDE)) && target.getWorld() != player.getWorld())
{
sender.sendMessage(ChatColor.RED + target.getDisplayName() + " is not in your world!");
return true;
@ -193,7 +194,7 @@ public class OpenInvPluginCommand implements CommandExecutor
/**
* @author Balor (aka Antoine Aflalo)
*/
private String matchUser(final Collection<File> container, final String search)
public static String matchUser(final Collection<File> container, final String search)
{
String found = null;
if (search == null)

View file

@ -17,6 +17,7 @@
package lishid.openinv.commands;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -38,7 +39,7 @@ public class SearchInvPluginCommand implements CommandExecutor
{
if (sender instanceof Player)
{
if (!sender.hasPermission("OpenInv.search"))
if (!sender.hasPermission(Permissions.PERM_SEARCH))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories");
return true;

View file

@ -17,6 +17,7 @@
package lishid.openinv.commands;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
@ -38,7 +39,7 @@ public class SilentChestPluginCommand implements CommandExecutor
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
return true;
}
if (!sender.hasPermission("OpenInv.silent"))
if (!sender.hasPermission(Permissions.PERM_SILENT))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to use silent chest.");
return true;

View file

@ -17,6 +17,7 @@
package lishid.openinv.commands;
import lishid.openinv.OpenInv;
import lishid.openinv.Permissions;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -35,7 +36,7 @@ public class ToggleOpenInvPluginCommand implements CommandExecutor
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
return true;
}
if (!sender.hasPermission("OpenInv.openinv"))
if (!sender.hasPermission(Permissions.PERM_OPENINV))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories");
return true;

View file

@ -0,0 +1,140 @@
/*
* Copyright (C) 2011-2012 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 lishid.openinv.utils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import lishid.openinv.OpenInv;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.InventoryHolder;
import net.minecraft.server.EntityHuman;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryEnderChest;
import net.minecraft.server.InventorySubcontainer;
import net.minecraft.server.ItemStack;
public class OpenInvEnderChest extends InventorySubcontainer implements IInventory
{
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private CraftPlayer owner;
private InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
public OpenInvEnderChest(CraftPlayer player, boolean online)
{
super(player.getHandle().getEnderChest().getName(), player.getHandle().getEnderChest().getSize());
this.enderChest = player.getHandle().getEnderChest();
this.owner = player;
this.items = enderChest.getContents();
this.InventoryRemovalCheck();
}
public void InventoryRemovalCheck()
{
if (transaction.isEmpty() && !playerOnline)
{
owner.saveData();
OpenInv.enderChests.remove(owner.getName().toLowerCase());
}
}
public void PlayerGoOnline(CraftPlayer p)
{
if (!playerOnline)
{
try
{
InventoryEnderChest playerEnderChest = p.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e)
{
}
p.saveData();
playerOnline = true;
}
}
public void PlayerGoOffline()
{
playerOnline = false;
}
public ItemStack[] getContents()
{
return this.items;
}
public void onOpen(CraftHumanEntity who)
{
transaction.add(who);
}
public void onClose(CraftHumanEntity who)
{
transaction.remove(who);
}
public List<HumanEntity> getViewers()
{
return transaction;
}
public InventoryHolder getOwner()
{
return this.owner;
}
public void setMaxStackSize(int size)
{
maxStack = size;
}
public int getMaxStackSize()
{
return maxStack;
}
public boolean a(EntityHuman entityhuman)
{
return true;
}
public void startOpen()
{
}
public void f()
{
}
public void update()
{
enderChest.update();
}
}

View file

@ -35,6 +35,7 @@ public class OpenInvPlayerInventory extends PlayerInventory
{
super(p.getHandle());
this.owner = p;
this.playerOnline = online;
this.items = player.inventory.items;
this.armor = player.inventory.armor;
}

View file

@ -1,6 +1,6 @@
name: OpenInv
main: lishid.openinv.OpenInv
version: 1.8.6
version: 1.8.7
author: lishid
website: http://forums.bukkit.org/threads/15379/
description: >
@ -12,6 +12,11 @@ commands:
usage: |
/<command> - Open last person's inventory
/<command> <Player> - Open a player's inventory
openender:
aliases: [oe, enderchest]
description: Opens the enderchest of a player
usage: |
/<command> <Player> - Opens a player's enderchest
searchinv:
aliases: [si]
description: Search and list players having a specific item