Get inventory title?

Discussion in 'Plugin Development' started by ScottDurkin_, Oct 7, 2019.

  1. Offline

    ScottDurkin_

    Hi all,

    Working on a plugin and I require a chest inventory title, but for some reason the option of get title is not showing?

    upload_2019-10-7_16-56-54.png

    Any ideas?
     
  2. Offline

    DerDonut

    Only InventoryViews do have titles. Try item#getCustomName()
     
  3. Offline

    ScottDurkin_

    Doe
    Doesn't seem to work at all..
     
  4. Offline

    KarimAKL

    What happens? Could you post your current code?
     
  5. Offline

    ScottDurkin_

    Here you go.

    Code:
        public void OnChestPlace(BlockPlaceEvent e)
        {
           
            Chest chest = (Chest)e.getBlock();
           
            if(chest.getInventory()#getCustomName())
            {
               
            }
        }
        
     
  6. Offline

    DerDonut

    .getCustomName()

    Sorry about that
     
    ScottDurkin_ likes this.
  7. Online

    robertlit

    @ScottDurkin_ The block isn't neccessarily a chest, you should check if it is.
     
    ScottDurkin_ likes this.
  8. Offline

    KarimAKL

    @ScottDurkin_
    1. As @robertlit said, you should check if the block placed is a chest, before casting it to Chest.
    2. You should check and cast the block's BlockState to Chest, not the block itself.
    3. '#' isn't used in Java, i think it comes from URL anchors; whenever someone types '#', just replace it with a '.' (if that's appropriate there)
     
    DerDonut likes this.
  9. Offline

    ScottDurkin_

    Thanks everyone for the help.

    Just had a question on another snippet of code.
    I am trying to cancel the event of opening a chest then sending packets to the player of the chest opening without actually seeing the inventory.

    Here is the code:
    Code:
        @EventHandler
        public void onInventoryOpen(InventoryOpenEvent e)
        {           
            Player player = (Player)e.getPlayer();
            if(e.getView().getTitle().equals(m_Main.Format("&c&lMEGA &6&lMystery Box")))
            {
                e.setCancelled(true);
                OpenChestPacket(m_ChestLocation, player);
                player.sendMessage(m_Main.Format("&cYou tried to open a mystery crate with no key?"));
            }
        }
       
        public void OpenChestPacket(Location chest, Player player)
        {
            BlockPosition pos = new BlockPosition(chest.getBlockX(), chest.getBlockY(), chest.getBlockZ());
            PacketPlayOutBlockAction packet = new PacketPlayOutBlockAction(pos, Blocks.CHEST, 1, 1);
            ((CraftPlayer) m_Player).getHandle().playerConnection.sendPacket(packet);
        }

    The m_ChestLocation get's set when placing the chest down.

    I solved this by using a PlayerInteractEvent.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 8, 2019

Share This Page