From 9491153e30215367cd3c2ca82252ce135994bcda Mon Sep 17 00:00:00 2001 From: lishid Date: Fri, 12 Sep 2014 23:17:41 -0700 Subject: [PATCH] Better way to get inventory. This prevents problems with other plugins. --- README | 2 +- .../internal/v1_7_R4/InventoryAccess.java | 28 +++++++++++++++++-- src/plugin.yml | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README b/README index cb8f1c9..209e1e8 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Copyright (C) 2011-2012 lishid. All rights reserved. +Copyright (C) 2011-2014 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 diff --git a/src/com/lishid/openinv/internal/v1_7_R4/InventoryAccess.java b/src/com/lishid/openinv/internal/v1_7_R4/InventoryAccess.java index cf9a172..8a3977d 100644 --- a/src/com/lishid/openinv/internal/v1_7_R4/InventoryAccess.java +++ b/src/com/lishid/openinv/internal/v1_7_R4/InventoryAccess.java @@ -16,6 +16,8 @@ package com.lishid.openinv.internal.v1_7_R4; +import java.lang.reflect.Field; + import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; @@ -29,8 +31,8 @@ import org.bukkit.craftbukkit.v1_7_R4.inventory.*; public class InventoryAccess implements IInventoryAccess { public boolean check(Inventory inventory, HumanEntity player) { - IInventory inv = ((CraftInventory) inventory).getInventory(); - + IInventory inv = grabInventory(inventory); + if (inv instanceof SpecialPlayerInventory) { if (!OpenInv.hasPermission(player, Permissions.PERM_EDITINV)) { return false; @@ -45,4 +47,26 @@ public class InventoryAccess implements IInventoryAccess { return true; } + + private IInventory grabInventory(Inventory inventory) { + if(inventory instanceof CraftInventory) { + return ((CraftInventory) inventory).getInventory(); + } + + //Use reflection to find the iiventory + Class clazz = inventory.getClass(); + IInventory result = null; + for(Field f : clazz.getDeclaredFields()) { + f.setAccessible(true); + if(IInventory.class.isAssignableFrom(f.getDeclaringClass())) { + try { + result = (IInventory) f.get(inventory); + } + catch (Exception e) { + OpenInv.log(e); + } + } + } + return result; + } } diff --git a/src/plugin.yml b/src/plugin.yml index 97d541b..bf8edfc 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: com.lishid.openinv.OpenInv -version: 2.2.2 +version: 2.2.4 author: lishid description: > This plugin allows you to open a player's inventory as a chest and interact with it in real time.