Opening ender chest programmatically

Discussion in 'Spigot Plugin Development' started by Rixterz, Jun 15, 2016.

  1. Hi

    The following code works for normal chests but not for ender chests - what do I have to change?

    Code (Text):
    @SuppressWarnings("deprecation")
        public void changeChestState(Location loc, boolean open)
        {
            byte dataByte = (open) ? (byte) 1 : 0;
           
            for (Player p : Bukkit.getOnlinePlayers()) {
               
                p.playNote(loc, (byte) 1, dataByte);
                BlockPosition position = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
               
                PacketPlayOutBlockAction blockActionPacket = new PacketPlayOutBlockAction(position, net.minecraft.server.v1_10_R1.Block.getById(loc.getBlock().getTypeId()), (byte) 1, dataByte);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(blockActionPacket);
            }
        }
     
  2. If what you want to do is open the enderchest of a player, do
    Code (Java):
    player.openInventory(player.getEnderChest());
     
  3. I just want to do the animation, no inventory or anything
     
  4. I believe the point is to make the chest appear to be open all the time, not just to open the inventory of the chest.
     
  5. @nokoa asked a similar question recently. What isn't working? Are you getting errors? Is the sound not playing? It seemed to work for him.
     
  6. Here's a video of the problem.

    Code used for this video:

    Code (Text):
    int data = 1;

            Location loc = new Location(main.overworld, -1, 178, 10);
         
            for (Player p : main.s.getOnlinePlayers())
            {
                BlockPosition position = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
             
                PacketPlayOutBlockAction blockActionPacket = new PacketPlayOutBlockAction(position, Blocks.CHEST, 1, data);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(blockActionPacket);
            }
         
            loc = new Location(main.overworld, 1, 178, 10);
         
            for (Player p : main.s.getOnlinePlayers())
            {
                BlockPosition position = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
             
                PacketPlayOutBlockAction blockActionPacket = new PacketPlayOutBlockAction(position, Blocks.CHEST, 1, data);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(blockActionPacket);
            }


    EDIT: Found a solution which I had previously tried, but didn't work. Now it does
    Code (Text):
    public void changeChestState(Location loc, boolean open)
        {
            int data = open ? 1 : 0;
            ((CraftWorld) loc.getWorld()).getHandle().playBlockAction(new BlockPosition(loc.getX(), loc.getY(), loc.getZ()), CraftMagicNumbers.getBlock(main.overworld.getBlockAt(loc)), 1, data);
        }
     
    #8 Rixterz, Jun 16, 2016
    Last edited: Jun 16, 2016
    • Like Like x 1