From f82a80a12b738e5624889e1f02b5efeacb55878f Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 25 Nov 2014 19:33:27 +0100 Subject: [PATCH] Fixes MassiveCraft/MassiveBooks#3 --- .../mixin/InventoryMixinDefault.java | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java b/src/main/java/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java index 5772b5ce..879c2fb0 100644 --- a/src/main/java/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java +++ b/src/main/java/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java @@ -1,16 +1,14 @@ package com.massivecraft.massivecore.mixin; -import java.util.List; +import java.lang.reflect.Constructor; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.PlayerInventory; +import com.massivecraft.massivecore.particleeffect.ReflectionUtils; + public class InventoryMixinDefault extends InventoryMixinAbstract { // -------------------------------------------- // @@ -20,6 +18,32 @@ public class InventoryMixinDefault extends InventoryMixinAbstract private static InventoryMixinDefault i = new InventoryMixinDefault(); public static InventoryMixinDefault get() { return i; } + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public static Class playerInventoryClass; + public static Class entityHumanClass; + public static Constructor playerInventoryConstructor; + public static Class craftInventoryPlayerClass; + public static Constructor craftInventoryPlayerConstructor; + + static + { + try + { + playerInventoryClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("PlayerInventory"); + entityHumanClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("EntityHuman"); + playerInventoryConstructor = ReflectionUtils.getConstructor(playerInventoryClass, entityHumanClass); + craftInventoryPlayerClass = ReflectionUtils.PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftInventoryPlayer"); + craftInventoryPlayerConstructor = ReflectionUtils.getConstructor(craftInventoryPlayerClass, playerInventoryClass); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // @@ -27,16 +51,17 @@ public class InventoryMixinDefault extends InventoryMixinAbstract @Override public PlayerInventory createPlayerInventory() { - List worlds = Bukkit.getWorlds(); - World world = worlds.get(0); - - Location location = world.getSpawnLocation().clone(); - location.setY(999); - - Player player = (Player) world.spawnEntity(location, EntityType.PLAYER); - PlayerInventory ret = player.getInventory(); - player.remove(); - return ret; + try + { + Object playerInventory = playerInventoryConstructor.newInstance(new Object[]{null}); + Object craftInventoryPlayer = craftInventoryPlayerConstructor.newInstance(playerInventory); + return (PlayerInventory)craftInventoryPlayer; + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } } @Override