Update for 1.9.4
This commit is contained in:
		@@ -0,0 +1,144 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.internal.IAnySilentChest;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.AxisAlignedBB;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.Block;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.BlockPosition;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.Entity;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityOcelot;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityPlayer;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.IInventory;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.ITileInventory;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.InventoryLargeChest;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.PacketPlayOutOpenWindow;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.TileEntityChest;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.World;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
 | 
			
		||||
 | 
			
		||||
public class AnySilentChest implements IAnySilentChest {
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isAnyChestNeeded(Player p, int x, int y, int z) {
 | 
			
		||||
        // FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest
 | 
			
		||||
        EntityPlayer player = ((CraftPlayer) p).getHandle();
 | 
			
		||||
        World world = player.world;
 | 
			
		||||
        // If block or ocelot on top
 | 
			
		||||
        if (world.getType(new BlockPosition(x, y + 1, z)).l() || hasOcelotOnTop(world, x, y, z))
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock());
 | 
			
		||||
 | 
			
		||||
        // If block next to chest is chest and has a block or ocelot on top
 | 
			
		||||
        if (isBlockedChest(world, id, x - 1, y, z))
 | 
			
		||||
            return true;
 | 
			
		||||
        if (isBlockedChest(world, id, x + 1, y, z))
 | 
			
		||||
            return true;
 | 
			
		||||
        if (isBlockedChest(world, id, x, y, z - 1))
 | 
			
		||||
            return true;
 | 
			
		||||
        if (isBlockedChest(world, id, x, y, z + 1))
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean isBlockedChest(World world, int id, int x, int y, int z) {
 | 
			
		||||
        BlockPosition position = new BlockPosition(x, y, z);
 | 
			
		||||
        if (Block.getId(world.getType(position).getBlock()) != id) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (world.getType(position).l()) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return hasOcelotOnTop(world, x, y, z);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean hasOcelotOnTop(World world, int x, int y, int z) {
 | 
			
		||||
        for (Entity localEntity : world.a(EntityOcelot.class,
 | 
			
		||||
                new AxisAlignedBB(x, y + 1, z, x + 1, y + 2, z + 1))) {
 | 
			
		||||
            EntityOcelot localEntityOcelot = (EntityOcelot) localEntity;
 | 
			
		||||
            if (localEntityOcelot.isSitting()) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean activateChest(Player p, boolean anychest, boolean silentchest, int x, int y, int z) {
 | 
			
		||||
        EntityPlayer player = ((CraftPlayer) p).getHandle();
 | 
			
		||||
        World world = player.world;
 | 
			
		||||
        Object chest = world.getTileEntity(new BlockPosition(x, y, z));
 | 
			
		||||
        if (chest == null)
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock());
 | 
			
		||||
 | 
			
		||||
        if (!anychest) {
 | 
			
		||||
            if (world.getType(new BlockPosition(x, y + 1, z)).l())
 | 
			
		||||
                return true;
 | 
			
		||||
            if ((Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x - 1, y + 1, z)).l()))
 | 
			
		||||
                return true;
 | 
			
		||||
            if ((Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x + 1, y + 1, z)).l()))
 | 
			
		||||
                return true;
 | 
			
		||||
            if ((Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z - 1)).l()))
 | 
			
		||||
                return true;
 | 
			
		||||
            if ((Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z + 1)).l()))
 | 
			
		||||
                return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id)
 | 
			
		||||
            chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x - 1, y, z)), (ITileInventory) chest);
 | 
			
		||||
        if (Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id)
 | 
			
		||||
            chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x + 1, y, z)));
 | 
			
		||||
        if (Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id)
 | 
			
		||||
            chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z - 1)), (ITileInventory) chest);
 | 
			
		||||
        if (Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id)
 | 
			
		||||
            chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z + 1)));
 | 
			
		||||
 | 
			
		||||
        boolean returnValue = true;
 | 
			
		||||
        if (!silentchest) {
 | 
			
		||||
            player.openContainer((IInventory) chest);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            try {
 | 
			
		||||
                SilentContainerChest silentContainerChest = new SilentContainerChest(player.inventory, ((IInventory) chest), player);
 | 
			
		||||
                int windowId = player.nextContainerCounter();
 | 
			
		||||
                player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, "minecraft:chest", ((IInventory) chest).getScoreboardDisplayName(), ((IInventory) chest).getSize()));
 | 
			
		||||
                player.activeContainer = silentContainerChest;
 | 
			
		||||
                player.activeContainer.windowId = windowId;
 | 
			
		||||
                player.activeContainer.addSlotListener(player);
 | 
			
		||||
                returnValue = false;
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
                p.sendMessage(ChatColor.RED + "Error while sending silent chest.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return returnValue;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,74 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.HumanEntity;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.OpenInv;
 | 
			
		||||
import com.lishid.openinv.Permissions;
 | 
			
		||||
import com.lishid.openinv.internal.IInventoryAccess;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.IInventory;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftInventory;
 | 
			
		||||
 | 
			
		||||
public class InventoryAccess implements IInventoryAccess {
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean check(Inventory inventory, HumanEntity player) {
 | 
			
		||||
        IInventory inv = grabInventory(inventory);
 | 
			
		||||
        
 | 
			
		||||
        if (inv instanceof SpecialPlayerInventory) {
 | 
			
		||||
            if (!OpenInv.hasPermission(player, Permissions.PERM_EDITINV)) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        else if (inv instanceof SpecialEnderChest) {
 | 
			
		||||
            if (!OpenInv.hasPermission(player, Permissions.PERM_EDITENDER)) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private IInventory grabInventory(Inventory inventory) {
 | 
			
		||||
        if(inventory instanceof CraftInventory) {
 | 
			
		||||
            return ((CraftInventory) inventory).getInventory();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //Use reflection to find the iinventory
 | 
			
		||||
        Class<? extends Inventory> 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) {
 | 
			
		||||
                    e.printStackTrace();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,59 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.OfflinePlayer;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import com.mojang.authlib.GameProfile;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityPlayer;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.MinecraftServer;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.PlayerInteractManager;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.CraftServer;
 | 
			
		||||
 | 
			
		||||
public class PlayerDataManager extends com.lishid.openinv.internal.PlayerDataManager {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Player loadOfflinePlayer(OfflinePlayer offline) {
 | 
			
		||||
        if (offline == null || !offline.hasPlayedBefore()) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        GameProfile profile = new GameProfile(offline.getUniqueId(), offline.getName());
 | 
			
		||||
        MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
 | 
			
		||||
        // Create an entity to load the player data
 | 
			
		||||
        EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), profile, new PlayerInteractManager(server.getWorldServer(0)));
 | 
			
		||||
 | 
			
		||||
        // Get the bukkit entity
 | 
			
		||||
        Player target = (entity == null) ? null : entity.getBukkitEntity();
 | 
			
		||||
        if (target != null) {
 | 
			
		||||
            // Load data
 | 
			
		||||
            target.loadData();
 | 
			
		||||
            // Return the entity
 | 
			
		||||
            return target;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getPlayerDataID(OfflinePlayer player) {
 | 
			
		||||
        return player.getUniqueId().toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,38 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.ContainerChest;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityHuman;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.IInventory;
 | 
			
		||||
 | 
			
		||||
public class SilentContainerChest extends ContainerChest {
 | 
			
		||||
    public IInventory inv;
 | 
			
		||||
 | 
			
		||||
    public SilentContainerChest(IInventory i1, IInventory i2, EntityHuman e1) {
 | 
			
		||||
        super(i1, i2, e1);
 | 
			
		||||
        inv = i2;
 | 
			
		||||
        // close signal
 | 
			
		||||
        inv.closeContainer(e1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void b(EntityHuman paramEntityHuman) {
 | 
			
		||||
        // Don't send close signal twice, might screw up
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,149 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.HumanEntity;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
import org.bukkit.inventory.InventoryHolder;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.OpenInv;
 | 
			
		||||
import com.lishid.openinv.internal.ISpecialEnderChest;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityHuman;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.IInventory;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.InventoryEnderChest;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.InventorySubcontainer;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.ItemStack;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftHumanEntity;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftInventory;
 | 
			
		||||
 | 
			
		||||
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
 | 
			
		||||
 | 
			
		||||
    private final OpenInv plugin;
 | 
			
		||||
    private final InventoryEnderChest enderChest;
 | 
			
		||||
    private final CraftInventory inventory = new CraftInventory(this);
 | 
			
		||||
    public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
 | 
			
		||||
    public boolean playerOnline = false;
 | 
			
		||||
    private CraftPlayer owner;
 | 
			
		||||
    private int maxStack = MAX_STACK;
 | 
			
		||||
 | 
			
		||||
    public SpecialEnderChest(OpenInv plugin, Player p, Boolean online) {
 | 
			
		||||
        super(((CraftPlayer) p).getHandle().getEnderChest().getName(), ((CraftPlayer) p).getHandle().getEnderChest().hasCustomName(), ((CraftPlayer) p).getHandle().getEnderChest().getSize());
 | 
			
		||||
        this.plugin = plugin;
 | 
			
		||||
        CraftPlayer player = (CraftPlayer) p;
 | 
			
		||||
        this.enderChest = player.getHandle().getEnderChest();
 | 
			
		||||
        this.owner = player;
 | 
			
		||||
        this.items = enderChest.getContents();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Inventory getBukkitInventory() {
 | 
			
		||||
        return inventory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean inventoryRemovalCheck(boolean save) {
 | 
			
		||||
        boolean offline = transaction.isEmpty() && !playerOnline;
 | 
			
		||||
        if (offline && save && !plugin.disableSaving()) {
 | 
			
		||||
            owner.saveData();
 | 
			
		||||
        }
 | 
			
		||||
        return offline;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPlayerOnline(Player player) {
 | 
			
		||||
        if (!playerOnline) {
 | 
			
		||||
            try {
 | 
			
		||||
                owner = (CraftPlayer) player;
 | 
			
		||||
                InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
 | 
			
		||||
                Field field = playerEnderChest.getClass().getField("items");
 | 
			
		||||
                field.setAccessible(true);
 | 
			
		||||
                field.set(playerEnderChest, this.items);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e) {}
 | 
			
		||||
            playerOnline = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean setPlayerOffline() {
 | 
			
		||||
        playerOnline = false;
 | 
			
		||||
        return inventoryRemovalCheck(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack[] getContents() {
 | 
			
		||||
        return this.items;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onOpen(CraftHumanEntity who) {
 | 
			
		||||
        transaction.add(who);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onClose(CraftHumanEntity who) {
 | 
			
		||||
        transaction.remove(who);
 | 
			
		||||
        this.inventoryRemovalCheck(true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<HumanEntity> getViewers() {
 | 
			
		||||
        return transaction;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public InventoryHolder getOwner() {
 | 
			
		||||
        return this.owner;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setMaxStackSize(int size) {
 | 
			
		||||
        maxStack = size;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getMaxStackSize() {
 | 
			
		||||
        return maxStack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean a(EntityHuman entityhuman) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void startOpen() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void f() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void update() {
 | 
			
		||||
        enderChest.update();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,321 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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 com.lishid.openinv.internal.v1_9_R2;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.lang.reflect.Modifier;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.OpenInv;
 | 
			
		||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_9_R2.EntityHuman;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.ItemStack;
 | 
			
		||||
import net.minecraft.server.v1_9_R2.PlayerInventory;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftHumanEntity;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftInventory;
 | 
			
		||||
 | 
			
		||||
public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory {
 | 
			
		||||
 | 
			
		||||
    private final OpenInv plugin;
 | 
			
		||||
    private final ItemStack[] extra = new ItemStack[4];
 | 
			
		||||
    private final CraftInventory inventory = new CraftInventory(this);
 | 
			
		||||
    private CraftPlayer owner;
 | 
			
		||||
    private boolean playerOnline = false;
 | 
			
		||||
 | 
			
		||||
    public SpecialPlayerInventory(OpenInv plugin, Player p, Boolean online) {
 | 
			
		||||
        super(((CraftPlayer) p).getHandle());
 | 
			
		||||
        this.plugin = plugin;
 | 
			
		||||
        this.owner = ((CraftPlayer) p);
 | 
			
		||||
        this.playerOnline = online;
 | 
			
		||||
        setItemArrays(this, player.inventory.items, player.inventory.armor, player.inventory.extraSlots);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void setItemArrays(PlayerInventory inventory, ItemStack[] items, ItemStack[] armor,
 | 
			
		||||
            ItemStack[] extraSlots) {
 | 
			
		||||
        try {
 | 
			
		||||
            // Prepare to remove final modifier
 | 
			
		||||
            Field modifiers = Field.class.getDeclaredField("modifiers");
 | 
			
		||||
            modifiers.setAccessible(true);
 | 
			
		||||
 | 
			
		||||
            // Access and replace main inventory array
 | 
			
		||||
            Field field = PlayerInventory.class.getField("items");
 | 
			
		||||
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 | 
			
		||||
            field.set(inventory, items);
 | 
			
		||||
 | 
			
		||||
            // Access and replace armor inventory array
 | 
			
		||||
            field = PlayerInventory.class.getField("armor");
 | 
			
		||||
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 | 
			
		||||
            field.set(inventory, armor);
 | 
			
		||||
 | 
			
		||||
            // Access and replace offhand inventory array
 | 
			
		||||
            field = PlayerInventory.class.getField("extraSlots");
 | 
			
		||||
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 | 
			
		||||
            field.set(inventory, extraSlots);
 | 
			
		||||
 | 
			
		||||
            // Access and replace array containing all inventory arrays
 | 
			
		||||
            field = PlayerInventory.class.getDeclaredField("g");
 | 
			
		||||
            field.setAccessible(true);
 | 
			
		||||
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 | 
			
		||||
            field.set(inventory, new ItemStack[][] { items, armor, extraSlots });
 | 
			
		||||
        } catch (NoSuchFieldException e) {
 | 
			
		||||
            // Unable to set final fields to item arrays, we're screwed. Noisily fail.
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } catch (IllegalArgumentException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } catch (IllegalAccessException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Inventory getBukkitInventory() {
 | 
			
		||||
        return inventory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean inventoryRemovalCheck(boolean save) {
 | 
			
		||||
        boolean offline = transaction.isEmpty() && !playerOnline;
 | 
			
		||||
        if (offline && save && !plugin.disableSaving()) {
 | 
			
		||||
            owner.saveData();
 | 
			
		||||
        }
 | 
			
		||||
        return offline;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPlayerOnline(Player player) {
 | 
			
		||||
        if (!playerOnline) {
 | 
			
		||||
            owner = (CraftPlayer) player;
 | 
			
		||||
            this.player = owner.getHandle();
 | 
			
		||||
            setItemArrays(this.player.inventory, items, armor, extraSlots);
 | 
			
		||||
            playerOnline = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean setPlayerOffline() {
 | 
			
		||||
        playerOnline = false;
 | 
			
		||||
        return this.inventoryRemovalCheck(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onClose(CraftHumanEntity who) {
 | 
			
		||||
        super.onClose(who);
 | 
			
		||||
        this.inventoryRemovalCheck(true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack[] getContents() {
 | 
			
		||||
        ItemStack[] C = new ItemStack[getSize()];
 | 
			
		||||
        System.arraycopy(items, 0, C, 0, items.length);
 | 
			
		||||
        System.arraycopy(armor, 0, C, items.length, armor.length);
 | 
			
		||||
        System.arraycopy(extraSlots, 0, C, items.length + armor.length, extraSlots.length);
 | 
			
		||||
        return C;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getSize() {
 | 
			
		||||
        return super.getSize() + 5;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack getItem(int i) {
 | 
			
		||||
        ItemStack[] is = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.armor;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extraSlots;
 | 
			
		||||
        }
 | 
			
		||||
        else if (is == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extra;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // extraSlots is, for now, just an array with length 1. No need for special handling.
 | 
			
		||||
 | 
			
		||||
        return is[i];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack splitStack(int i, int j) {
 | 
			
		||||
        ItemStack[] is = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.armor;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extraSlots;
 | 
			
		||||
        }
 | 
			
		||||
        else if (is == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extra;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (is[i] != null) {
 | 
			
		||||
            ItemStack itemstack;
 | 
			
		||||
 | 
			
		||||
            if (is[i].count <= j) {
 | 
			
		||||
                itemstack = is[i];
 | 
			
		||||
                is[i] = null;
 | 
			
		||||
                return itemstack;
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                itemstack = is[i].cloneAndSubtract(j);
 | 
			
		||||
                if (is[i].count == 0) {
 | 
			
		||||
                    is[i] = null;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return itemstack;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack splitWithoutUpdate(int i) {
 | 
			
		||||
        ItemStack[] is = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.armor;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extraSlots;
 | 
			
		||||
        }
 | 
			
		||||
        else if (is == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extra;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (is[i] != null) {
 | 
			
		||||
            ItemStack itemstack = is[i];
 | 
			
		||||
 | 
			
		||||
            is[i] = null;
 | 
			
		||||
            return itemstack;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setItem(int i, ItemStack itemstack) {
 | 
			
		||||
        ItemStack[] is = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.armor;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extraSlots;
 | 
			
		||||
        }
 | 
			
		||||
        else if (is == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= is.length) {
 | 
			
		||||
            i -= is.length;
 | 
			
		||||
            is = this.extra;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Effects
 | 
			
		||||
        if (is == this.extra) {
 | 
			
		||||
            owner.getHandle().drop(itemstack, true);
 | 
			
		||||
            itemstack = null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        is[i] = itemstack;
 | 
			
		||||
 | 
			
		||||
        owner.getHandle().defaultContainer.b();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int getReversedItemSlotNum(int i) {
 | 
			
		||||
        if (i >= 27)
 | 
			
		||||
            return i - 27;
 | 
			
		||||
        else
 | 
			
		||||
            return i + 9;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int getReversedArmorSlotNum(int i) {
 | 
			
		||||
        if (i == 0)
 | 
			
		||||
            return 3;
 | 
			
		||||
        if (i == 1)
 | 
			
		||||
            return 2;
 | 
			
		||||
        if (i == 2)
 | 
			
		||||
            return 1;
 | 
			
		||||
        if (i == 3)
 | 
			
		||||
            return 0;
 | 
			
		||||
        else
 | 
			
		||||
            return i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        if (player.getName().length() > 16) {
 | 
			
		||||
            return player.getName().substring(0, 16);
 | 
			
		||||
        }
 | 
			
		||||
        return player.getName();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean a(EntityHuman entityhuman) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user