Update for 1.11
This commit is contained in:
		
							
								
								
									
										32
									
								
								OpenInvCraftbukkit1_11_R1/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								OpenInvCraftbukkit1_11_R1/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
			
		||||
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 | 
			
		||||
  <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
  <parent>
 | 
			
		||||
    <groupId>com.lishid</groupId>
 | 
			
		||||
    <artifactId>openinvparent</artifactId>
 | 
			
		||||
    <version>1.0-SNAPSHOT</version>
 | 
			
		||||
  </parent>
 | 
			
		||||
 | 
			
		||||
  <artifactId>OpenInvCraftbukkit1_11_R1</artifactId>
 | 
			
		||||
  <name>OpenInvCraftbukkit1_11_R1</name>
 | 
			
		||||
 | 
			
		||||
  <dependencies>
 | 
			
		||||
    <dependency>
 | 
			
		||||
      <groupId>com.lishid</groupId>
 | 
			
		||||
      <artifactId>openinvcore</artifactId>
 | 
			
		||||
      <version>1.0-SNAPSHOT</version>
 | 
			
		||||
    </dependency>
 | 
			
		||||
    <dependency>
 | 
			
		||||
      <groupId>com.lishid</groupId>
 | 
			
		||||
      <artifactId>openinvplugin</artifactId>
 | 
			
		||||
      <version>1.0-SNAPSHOT</version>
 | 
			
		||||
    </dependency>
 | 
			
		||||
    <dependency>
 | 
			
		||||
      <groupId>org.spigotmc</groupId>
 | 
			
		||||
      <artifactId>spigot</artifactId>
 | 
			
		||||
      <version>1.11-R0.1-SNAPSHOT</version>
 | 
			
		||||
      <scope>provided</scope>
 | 
			
		||||
    </dependency>
 | 
			
		||||
  </dependencies>
 | 
			
		||||
</project>
 | 
			
		||||
@@ -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_11_R1;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.internal.IAnySilentChest;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_11_R1.AxisAlignedBB;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.Block;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.BlockPosition;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.Entity;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.EntityOcelot;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.EntityPlayer;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.IInventory;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.ITileInventory;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.InventoryLargeChest;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.PacketPlayOutOpenWindow;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.TileEntityChest;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.World;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.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_11_R1;
 | 
			
		||||
 | 
			
		||||
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_11_R1.IInventory;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.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,62 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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_11_R1;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.internal.IPlayerDataManager;
 | 
			
		||||
 | 
			
		||||
import com.mojang.authlib.GameProfile;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.OfflinePlayer;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_11_R1.EntityPlayer;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.MinecraftServer;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.PlayerInteractManager;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.CraftServer;
 | 
			
		||||
 | 
			
		||||
public class PlayerDataManager implements IPlayerDataManager {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Player loadPlayer(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,45 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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_11_R1;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_11_R1.ContainerChest;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.EntityHuman;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.IInventory;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.PlayerInventory;
 | 
			
		||||
 | 
			
		||||
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 entityHuman) {
 | 
			
		||||
        // Don't send close signal twice, might screw up
 | 
			
		||||
        PlayerInventory playerinventory = entityHuman.inventory;
 | 
			
		||||
 | 
			
		||||
        if (playerinventory.getCarried() != null) {
 | 
			
		||||
            entityHuman.drop(playerinventory.getCarried(), false);
 | 
			
		||||
            playerinventory.setCarried(null);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,105 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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_11_R1;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.lang.reflect.Modifier;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.internal.ISpecialEnderChest;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_11_R1.IInventory;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.InventoryEnderChest;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.InventorySubcontainer;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.ItemStack;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftInventory;
 | 
			
		||||
 | 
			
		||||
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
 | 
			
		||||
 | 
			
		||||
    private final InventoryEnderChest enderChest;
 | 
			
		||||
    private final CraftInventory inventory = new CraftInventory(this);
 | 
			
		||||
    public boolean playerOnline = false;
 | 
			
		||||
 | 
			
		||||
    public SpecialEnderChest(Player player, Boolean online) {
 | 
			
		||||
        super(((CraftPlayer) player).getHandle().getEnderChest().getName(), ((CraftPlayer) player).getHandle().getEnderChest().hasCustomName(), ((CraftPlayer) player).getHandle().getEnderChest().getSize());
 | 
			
		||||
        CraftPlayer craftPlayer = (CraftPlayer) player;
 | 
			
		||||
        this.enderChest = craftPlayer.getHandle().getEnderChest();
 | 
			
		||||
        this.bukkitOwner = craftPlayer;
 | 
			
		||||
        setItemLists(this, enderChest.getContents());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void setItemLists(InventorySubcontainer subcontainer, List<ItemStack> list) {
 | 
			
		||||
        try {
 | 
			
		||||
            // Prepare to remove final modifier
 | 
			
		||||
            Field modifiers = Field.class.getDeclaredField("modifiers");
 | 
			
		||||
            modifiers.setAccessible(true);
 | 
			
		||||
            // Access and replace main inventory array
 | 
			
		||||
            Field field = InventorySubcontainer.class.getField("items");
 | 
			
		||||
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 | 
			
		||||
            field.set(subcontainer, list);
 | 
			
		||||
        } catch (NoSuchFieldException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } catch (SecurityException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } catch (IllegalArgumentException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } catch (IllegalAccessException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Inventory getBukkitInventory() {
 | 
			
		||||
        return inventory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPlayerOnline(Player player) {
 | 
			
		||||
        if (!playerOnline) {
 | 
			
		||||
            try {
 | 
			
		||||
                this.bukkitOwner = player;
 | 
			
		||||
                CraftPlayer craftPlayer = (CraftPlayer) player;
 | 
			
		||||
                setItemLists(craftPlayer.getHandle().getEnderChest(), this.items);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e) {}
 | 
			
		||||
            playerOnline = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPlayerOffline() {
 | 
			
		||||
        playerOnline = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isInUse() {
 | 
			
		||||
        return !this.getViewers().isEmpty();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void update() {
 | 
			
		||||
        super.update();
 | 
			
		||||
        enderChest.update();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,257 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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_11_R1;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.lang.reflect.Modifier;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
 | 
			
		||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.server.v1_11_R1.ContainerUtil;
 | 
			
		||||
// Volatile
 | 
			
		||||
import net.minecraft.server.v1_11_R1.EntityHuman;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.ItemStack;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.NonNullList;
 | 
			
		||||
import net.minecraft.server.v1_11_R1.PlayerInventory;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
 | 
			
		||||
import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftInventory;
 | 
			
		||||
 | 
			
		||||
public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory {
 | 
			
		||||
 | 
			
		||||
    private final CraftInventory inventory = new CraftInventory(this);
 | 
			
		||||
    private boolean playerOnline = false;
 | 
			
		||||
 | 
			
		||||
    public SpecialPlayerInventory(Player bukkitPlayer, Boolean online) {
 | 
			
		||||
        super(((CraftPlayer) bukkitPlayer).getHandle());
 | 
			
		||||
        this.playerOnline = online;
 | 
			
		||||
        setItemArrays(this, player.inventory.items, player.inventory.armor, player.inventory.extraSlots);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void setItemArrays(PlayerInventory inventory, NonNullList<ItemStack> items,
 | 
			
		||||
            NonNullList<ItemStack> armor, NonNullList<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, Arrays.asList(new NonNullList[] { 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 void setPlayerOnline(Player player) {
 | 
			
		||||
        if (!playerOnline) {
 | 
			
		||||
            this.player = ((CraftPlayer) player).getHandle();
 | 
			
		||||
            setItemArrays(this.player.inventory, items, armor, extraSlots);
 | 
			
		||||
            playerOnline = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPlayerOffline() {
 | 
			
		||||
        playerOnline = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isInUse() {
 | 
			
		||||
        return !this.getViewers().isEmpty();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getSize() {
 | 
			
		||||
        return super.getSize() + 4;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack getItem(int i) {
 | 
			
		||||
        NonNullList<ItemStack> list = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.armor;
 | 
			
		||||
        } else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.extraSlots;
 | 
			
		||||
        } else if (list == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            return ItemStack.a;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return list.get(i);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack splitStack(int i, int j) {
 | 
			
		||||
        NonNullList<ItemStack> list = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.armor;
 | 
			
		||||
        } else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.extraSlots;
 | 
			
		||||
        } else if (list == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            return ItemStack.a;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return list == null || list.get(i).isEmpty() ? ItemStack.a : ContainerUtil.a(list, i, j);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ItemStack splitWithoutUpdate(int i) {
 | 
			
		||||
        NonNullList<ItemStack> list = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.armor;
 | 
			
		||||
        } else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.extraSlots;
 | 
			
		||||
        } else if (list == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            return ItemStack.a;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (list != null && !list.get(i).isEmpty()) {
 | 
			
		||||
            ItemStack itemstack = list.get(i);
 | 
			
		||||
 | 
			
		||||
             list.set(i, ItemStack.a);
 | 
			
		||||
             return itemstack;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return ItemStack.a;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setItem(int i, ItemStack itemstack) {
 | 
			
		||||
        NonNullList<ItemStack> list = this.items;
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.armor;
 | 
			
		||||
        } else {
 | 
			
		||||
            i = getReversedItemSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            i -= list.size();
 | 
			
		||||
            list = this.extraSlots;
 | 
			
		||||
        } else if (list == this.armor) {
 | 
			
		||||
            i = getReversedArmorSlotNum(i);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (i >= list.size()) {
 | 
			
		||||
            player.drop(itemstack, true);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (list != null) {
 | 
			
		||||
             list.set(i, itemstack);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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