p.getInventory.remove();

Discussion in 'Plugin Development' started by ProStriker123, Aug 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ProStriker123

    why its dosent removes from my inventory the blaze rod?
    ????
    my lore and displayname of the item its

    @EventHandler
    public void EggBlaster(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(p.getInventory().getItemInHand().getType() == Material.BLAZE_ROD ) {
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    if(p.getInventory().contains(Material.BLAZE_ROD) == false) {
    e.setCancelled(true);
    p.sendMessage(ChatColor.AQUA + "Blaster> " + ChatColor.GRAY + "You are out of ammo.");
    return;
    }
    int x = 20;
    int y = 20;
    int z = 20;
    for (Entity entity : p.getNearbyEntities(x, y, z)) {
    if (entity instanceof Player) {
    ((Player) entity).playSound(entity.getLocation(), Sound.DOOR_CLOSE, 5F, 3F);
    }
    }
    p.playSound(p.getLocation(), Sound.DOOR_CLOSE, 5F, 3F);
    p.launchProjectile(Egg.class);
    }
    ItemStack aj = new ItemStack(Material.BLAZE_ROD);
    ItemMeta ajmeta = aj.getItemMeta();
    ajmeta.setDisplayName("§6- Blaze Rod -");
    ArrayList<String> ajlore = new ArrayList<String>();
    ajlore.add("§8§m§l---§f§m§l(--§r §b§lBlaze Rod §f§m§l--)§8§m§l---");
    ajlore.add("§7§lWhen you right click on it its ");
    ajlore.add("§7§llaunch a fireball");
    ajlore.add("§7§o(Ammo) §fBlaze rod");
    ajmeta.setLore(ajlore);
    aj.setItemMeta(ajmeta);
    p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD));
    p.updateInventory();
    }
    }

    @EventHandler
    public void EggBlaster(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(p.getInventory().getItemInHand().getType() == Material.BLAZE_ROD ) {
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    if(p.getInventory().contains(Material.BLAZE_ROD) == false) {
    e.setCancelled(true);
    p.sendMessage(ChatColor.AQUA + "Blaster> " + ChatColor.GRAY + "You are out of ammo.");
    return;
    }
    int x = 20;
    int y = 20;
    int z = 20;
    for (Entity entity : p.getNearbyEntities(x, y, z)) {
    if (entity instanceof Player) {
    ((Player) entity).playSound(entity.getLocation(), Sound.DOOR_CLOSE, 5F, 3F);
    }
    }
    p.playSound(p.getLocation(), Sound.DOOR_CLOSE, 5F, 3F);
    p.launchProjectile(Egg.class);
    }
    final ItemStack aj = new ItemStack(Material.BLAZE_ROD);
    ItemMeta ajmeta = aj.getItemMeta();
    ajmeta.setDisplayName("§6- Blaze Rod -");
    ArrayList<String> ajlore = new ArrayList<String>();
    ajlore.add("§8§m§l---§f§m§l(--§r §b§lBlaze Rod §f§m§l--)§8§m§l---");
    ajlore.add("§7§lWhen you right click on it its ");
    ajlore.add("§7§llaunch a fireball");
    ajlore.add("§7§o(Ammo) §fBlaze rod");
    ajmeta.setLore(ajlore);
    aj.setItemMeta(ajmeta);
    p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD));
    p.updateInventory();
    }
    }
     
  2. Offline

    hugokk

    It is actually removing the Blaze Rod from the inventory, but it isn't showed client-side.
    You could use:
    Code:java
    1. p.updateInventory();
    , but this is deprecated.
    I recommend to use:
    Code:java
    1. p.setItemInHand(p.getItemInHand());

    This may not work, but then just schedule it 1 tick later with a BukkitRunnable (http://wiki.bukkit.org/Scheduler_Programming)
     
  3. Offline

    Code0

    You always need to do player.updateInventory();

    ProStriker123
     
  4. Offline

    ProStriker123

    Not working for me could you explain more please....
     
  5. Offline

    hugokk

    ProStriker123 Could you please copy and paste your code out of your editor, and please use the code tags.
    And btw, this will never happen, because if there are no blaze rods in the inventory you won't be able to right click with them ofcourse:
    Code:java
    1. if(p.getInventory().contains(Material.BLAZE_ROD) == false) {
    2. e.setCancelled(true);
    3. p.sendMessage(ChatColor.AQUA + "Blaster> " + ChatColor.GRAY + "You are out of ammo.");
    4. return;
    5. }
     
Thread Status:
Not open for further replies.

Share This Page