mirror of
https://github.com/TotalFreedomMC/OpenInv.git
synced 2025-01-03 13:38:21 +00:00
1.3.1
This commit is contained in:
commit
d1a83c53ca
10 changed files with 448 additions and 0 deletions
70
src/lishid/openinv/OpenInv.java
Normal file
70
src/lishid/openinv/OpenInv.java
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
47
src/lishid/openinv/OpenInvEntityListener.java
Normal file
47
src/lishid/openinv/OpenInvEntityListener.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
74
src/lishid/openinv/OpenInvPlayerListener.java
Normal file
74
src/lishid/openinv/OpenInvPlayerListener.java
Normal file
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
src/lishid/openinv/PermissionRelay.java
Normal file
15
src/lishid/openinv/PermissionRelay.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
111
src/lishid/openinv/commands/OpenInvPluginCommand.java
Normal file
111
src/lishid/openinv/commands/OpenInvPluginCommand.java
Normal file
|
@ -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<Player, OpenInvHistory> theOpenInvHistory = new HashMap<Player, OpenInvHistory>();
|
||||
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;
|
||||
}
|
||||
}
|
62
src/lishid/openinv/commands/SearchInvPluginCommand.java
Normal file
62
src/lishid/openinv/commands/SearchInvPluginCommand.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
14
src/lishid/openinv/utils/OpenInvHistory.java
Normal file
14
src/lishid/openinv/utils/OpenInvHistory.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
7
src/lishid/openinv/utils/OpenInvToggleState.java
Normal file
7
src/lishid/openinv/utils/OpenInvToggleState.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package lishid.openinv.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class OpenInvToggleState {
|
||||
public static HashMap<String, Integer> openInvState = new HashMap<String, Integer>();
|
||||
}
|
31
src/lishid/openinv/utils/PlayerInventoryChest.java
Normal file
31
src/lishid/openinv/utils/PlayerInventoryChest.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
17
src/plugin.yml
Normal file
17
src/plugin.yml
Normal file
|
@ -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: |
|
||||
/<command> <Player>
|
||||
searchinv:
|
||||
description: Search and list players having a specific item
|
||||
usage: |
|
||||
/<command> <Item> [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
|
Loading…
Reference in a new issue