Player hit another player event

Discussion in 'Plugin Development' started by gabsloco, Dec 19, 2014.

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

    gabsloco

    Hi, i'm a beginner in programing, and i'm trying to do a plugin, in this part of the plugin, i want that if one player hit another , and this another have the itemstack "goiaba", the player that hit him, take the itemstack. I tried this in the code below, but in game, if I hit another player, nothing happens. Someone could help me?

    http://hastebin.com/efehitepux.coffee

    Code:
      public void entityDamage(EntityDamageByEntityEvent event)
      {
        ItemStack goiaba = new ItemStack(Material.INK_SACK, 1, (short)10);
        ItemMeta im = goiaba.getItemMeta();
        im.setDisplayName(ChatColor.GREEN + "Goiaba");
        goiaba.setItemMeta(im);
        if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
          if (((Player)event.getEntity()).getInventory().contains(goiaba))
          {
            ((Player)event.getEntity()).getInventory().remove(goiaba);
            ((Player)event.getDamager()).getInventory().addItem(new ItemStack[] { this.goiaba });
            ((Player)event.getEntity()).sendMessage(ChatColor.AQUA + ((Player)event.getDamager()).getPlayerListName() + "Tacou a mao na sua goiaba!");
            ((Player)event.getDamager()).sendMessage(ChatColor.AQUA + "Voce tascou a mao na goiaba de" + ((Player)event.getEntity()).getPlayerListName());
          }
          else
          {
            event.setCancelled(true);
          }
        }
      }
     
  2. Offline

    Rocoty

    Make sure you're registering events.
     
  3. Offline

    gabsloco

    How can i do this? I'm a beginner.
     
  4. Offline

    Konato_K

    @gabsloco Depends if your listener is your main class or in a separate class, I suggest reading this http://wiki.bukkit.org/Event_API_Reference

    Also, you should put your ItemStack as a field of the class, you're just creating a new object every time someone hits someone else, in the worse case, at least put it inside your if block, but making it into a field will be better.
     
  5. Offline

    gabsloco

    @Konato_K the listener is in my main class(Because the plugin is small). I have put the @EventHandler, but still not working. There are no errors, just nothing happens...
    The current code:
    Code:
          @EventHandler
          public void entityDamage(EntityDamageByEntityEvent event)
          {
            ItemStack goiaba = new ItemStack(Material.INK_SACK, 1, (short)10);
            ItemMeta im = goiaba.getItemMeta();
            im.setDisplayName(ChatColor.GREEN + "Goiaba");
            goiaba.setItemMeta(im);
            if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
              if (((Player)event.getEntity()).getInventory().contains(goiaba))
              {
                ((Player)event.getEntity()).getInventory().remove(goiaba);
                ((Player)event.getDamager()).getInventory().addItem(new ItemStack[] { this.goiaba });
                ((Player)event.getEntity()).sendMessage(ChatColor.AQUA + ((Player)event.getDamager()).getPlayerListName() + "Tacou a mao na sua goiaba!");
                ((Player)event.getDamager()).sendMessage(ChatColor.AQUA + "Voce tascou a mao na goiaba de" + ((Player)event.getEntity()).getPlayerListName());
              }
              else
              {
                event.setCancelled(true);
              }
            }
          }    
     
  6. Offline

    Webbeh

    If the event listener is in the main class, you still have to add the "registerEvents" line, but using "(this, this)" as arguments.
     
  7. Offline

    gabsloco

    @Webbeh This already was in the main class:
    Code:
        public void OnEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            log.info("Goiaba v.1.0 Foi habilitado!");
                   
        }
     
  8. Offline

    Skionz

    @gabsloco The method is 'onEnable()' not 'OnEnable().' Use good naming conventions.
     
  9. Offline

    gabsloco

  10. Offline

    mine-care

    @gabsloco
    Three things:
    1. what is log variable (see onEnable() method)
    2. Dont use ((Player)event.getEntity()), it makes it complex and puts java on the task of casting over and over. instead have a variable like Player p = (Player) event.getEntity(); (only if youre sure it is a player.) and then use p.methods(); Nice and tidy :)
    3. Instead of having a itemstack cretaed every time event occurs, (its like creating the same thing over and over and over again) initialise it onEnable or on event registration and use it later on in event-S
    Also about this^ why not just "addItem(gorila);"
     
    Konato_K likes this.
  11. Offline

    gabsloco

Thread Status:
Not open for further replies.

Share This Page