Gun

Discussion in 'Plugin Development' started by NoSpanMan, Oct 4, 2015.

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

    NoSpanMan

    How can i add when the damager killed someone the chat says deathperson was killed by killer
    Code:
      @EventHandler
      public void onPlayerInteract(PlayerInteractEvent e) {
      if(e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
      if(e.getPlayer().getItemInHand().getType().equals(Material.IRON_HOE)) {
      Player player = e.getPlayer();
      Snowball bullet = player.launchProjectile(Snowball.class, player.getLocation().getDirection());
      bullets.add(bullet.getEntityId());
      bullet.setVelocity(e.getPlayer().getLocation().getDirection().multiply(5));
      }
      }
      }
       
       @EventHandler
      public void onEntityDamage(EntityDamageByEntityEvent e) {
      if (e.getDamager() instanceof Snowball) {
      Snowball s = (Snowball) e.getDamager();
      if (s.getShooter() instanceof Player) {
      Player shooter = (Player) s.getShooter();
      e.setDamage(8.0);
      }
      }
    }
    
     
  2. Offline

    RoboticPlayer

    @NoSpanMan Listen for the PlayerDeathEvent. When it occurs and certain conditions that you want are met, e.setDeathMessage(string);
     
  3. Offline

    NoSpanMan

    Yeah i understand but how can i set in there the killer and the death one.
     
  4. Offline

    DoggyCode™

    Listen to PlayerDeathEvent:
    Player killer = (Player)event.getPlayer().getKiller();
    String killerName = killer.getName();
     
  5. Offline

    Antybarrel

    @DoggyCode™

    You would want to check if a player killed them first, before casting it to a player.
     
  6. Offline

    DoggyCode™

    I know.. I just told him how to get the killer... not giving him the whole stuff.
     
  7. Offline

    Antybarrel

  8. Offline

    NoSpanMan

    @DoggyCode™

    Yeah but it is a GUN when i do a playerdeathevent with getkiller i only get the message when i kill somebody with left click. My gun is right click and i dont get a death message then. So what can i do that he nows who shootet who
     
  9. Offline

    DoggyCode™

    Just check if the death is caused by a projectile hit, and check if it is instanceof a snowball (the gun's ammo) and then go on.
     
  10. Offline

    NoSpanMan

  11. Offline

    DoggyCode™

    Code:
        @EventHandler //event annotation
         public void onEntityDamage(EntityDeathEvent e) { //calling the event
            Entity ent = (Entity)e.getEntity();  //getting the event (not player)
             EntityDamageEvent ede = ent.getLastDamageCause();  //getting the event's last damage source
             DamageCause dc = ede.getCause(); //getting the actual cause
             if (ent instanceof Player && dc==DamageCause.PROJECTILE) { //checking if the Entity is instance of Player and if the Damage Cause is a Projectile Hit
                 Player p = (Player)e.getEntity(); //getting the actual Player
                 Player k = (Player) e.getEntity().getKiller(); //getting the actual Killer
                 if(ede.getEntity() instanceof Snowball){ //checking if the Projectile is instance of a Snowball
                     String pName = p.getName(); //getting the player's Name
                     String kName = k.getName();  //getting the killer's Name
                     //do stuff
                 }
             }
        }
    
    that should work.

    THIS POST IS EDITED!!!
     
    Last edited: Oct 4, 2015
  12. Offline

    NoSpanMan

  13. Offline

    DoggyCode™

    send a screenshot of the code.

    EDIT:
    @NoSpanMan Nvm. I fixed it:
    Code:
        @EventHandler
         public void onEntityDamage(EntityDeathEvent e) { 
            Entity ent = (Entity)e.getEntity(); 
             EntityDamageEvent ede = ent.getLastDamageCause(); 
             DamageCause dc = ede.getCause();
             if (ent instanceof Player && dc==DamageCause.PROJECTILE) { 
                 Player p = (Player)e.getEntity();
                 Player k = (Player) e.getEntity().getKiller();
                 if(ede.getEntity() instanceof Snowball){
                     String pName = p.getName();
                     String kName = k.getName(); 
                     //do stuff
                 }
             }
        }
    
     
  14. Offline

    NoSpanMan

  15. Offline

    DoggyCode™

    @NoSpanMan Delete that code and replace it with the code I provided (I edited it)^ it says the explanation after the "//" if you look at my first original post (also edited)

    EDIT:
    remember to hover over the red lines after to import.
     
  16. Offline

    NoSpanMan

    And now i have 2 arraylists with the red and the blue team but i dont want that they can hurt their teammates with the guns. I know it for swords and that stuff but not for guns?
     
  17. Offline

    DoggyCode™

    @NoSpanMan If you didn't resolve your previous issue:

    And for your issue now, listen to EntityDamageEvent, check if the Entity damager and the Entity damaged is player, then check if the Arraylist that the player (damager) is in contains the name of the damaged Player.

    If it does, cancel the event (and send a message or do whatever you want), else, let the event pass through..
     
  18. Offline

    NoSpanMan

    I did:
    Code:
    if(arraylistblue.contains(damager.getname()) && arraylistblue.contains(entity.getname())) {
    e.setcanceled(true);
    } else {
    
    }
    
    But i still can hit my friends?
     
  19. Offline

    DoggyCode™

    Can I see your whole code?

    Try this, @NoSpanMan
    Code:
        private ArrayList<String> blueTeam = new ArrayList<String>();
        private ArrayList<String> redTeam = new ArrayList<String>();
       
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e){
            if(e instanceof Player && e.getDamager() instanceof Player){
                Player p = (Player)e;
                Player d = (Player)e.getDamager();
                if(blueTeam.contains(d.getName())){
                    if(blueTeam.contains(p.getName())){
                        e.setCancelled(true);
                        d.sendMessage(ChatColor.RED + "You may not Damage your Teammates!");
                        return;   
                    }
                } else if(redTeam.contains(d.getName())){
                    if(redTeam.contains(p.getName())){
                        e.setCancelled(true);
                        d.sendMessage(ChatColor.RED + "You may not Damage your Teammates!");
                        return;
                    }
                }
            }
        }
    Please come back to report..

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Oct 6, 2015
  20. Offline

    NoSpanMan

  21. Offline

    DoggyCode™

    You're 100% you two are contained in the same ArrayList?
     
  22. Offline

    RoboticPlayer

    Add debug messages.
     
  23. Offline

    NoSpanMan

    I have this event:
    Code:
      @EventHandler
      public void onDamage(EntityDamageByEntityEvent e){
      if(e instanceof Player && e.getDamager() instanceof Player){
      Player p = (Player)e;
      Player d = (Player)e.getDamager();
      if(inBuggs.contains(d.getName())){
      if(inBuggs.contains(p.getName())){
      e.setCancelled(true);
      d.sendMessage(ChatColor.RED + "You may not Damage your Teammates!");
      return;  
      }
      } else if(inDevs.contains(d.getName())){
      if(inDevs.contains(p.getName())){
      e.setCancelled(true);
      d.sendMessage(ChatColor.RED + "You may not Damage your Teammates!");
      return;
      }
      }
      }
      }
    
    and this arraylists:
    Code:
      final ArrayList<String> inDevs = new ArrayList<String>();
       final ArrayList<String> inBuggs = new ArrayList<String>();
    
     
  24. Offline

    MisterErwin

    @NoSpanMan You are checking, if the EntityDamageByEntityEvent is an instance of a player...
    Use 'EntityDamageByEntityEvent#getEntity()' instead, as the event will NEVER be a player ;)
     
  25. Offline

    DoggyCode™

    Entity can be a player?
     
    boomboompower likes this.
  26. Offline

    boomboompower

    DoggyCode™ likes this.
  27. Offline

    MisterErwin

    @boomboompower @DoggyCode™
    Code:
    EntityDamageByEntityEvent e;
    if(e instanceof Player) 
    
    According to the javadocs:
    And why would an event be a player? What sense would it make?
    The event only holds a reference to the player/entity.
     
  28. It can be Monkey too.

    That won't work if your weapon shoot snowballs

    Code:
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event) {
    if(event.getEntity() instanceof Player) {
      Player damaged = (Player) event.getEntity();
      Player damager = null;
    
       if(event.getDamager() instanceof Projectile) {
        Projectile projectile = (Projectile) event.getDamager();
    
        if(projectile.getShooter() instanceof Player) {
         damager = (Player) projectile.getShooter();
        }
       } else if(event.getDamager() instanceof Player) {
         damager = (Player) event.getDamager();
       }
    
       if(damager == null) {
         return;
       }
    
       //I did this for not having to check everything twice
      }
    }
    
     
    Last edited: Oct 11, 2015
  29. Offline

    RoboticPlayer

    He is saying that the event itself cannot be a player, but the entity involved in the event can.
     
  30. Offline

    NoSpanMan

    @MaTaMoR_ In your code he not check if im in the same team as my friend? So can you people please help me :).
     
Thread Status:
Not open for further replies.

Share This Page