Solved disablepvp

Discussion in 'Plugin Development' started by lrdemolition, Aug 19, 2014.

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

    lrdemolition

    i need to cancel an enitydamageevent if the player hit is in the same arraylist(which is composed of player objects) as the attacker. how would i do this?

    heres some scrap of code i found that might be of use(idk if getDamager() is depreciated):


    Code:
    @EventHandler
    public void onDamage(EntityDamageEvent event){
    if (event.getEntity() instanceof Player) {
    Player p = (Player) event.getEntity();
    if (p.getDamager() instanceof Player) {
    Player p2 = (Player) p.getDamager();
    }
    }
     
  2. Offline

    ProStriker123

    Its would be more helpfuly if youll use a Scoreboard
    also the event called like that
    EntityDamageByEntityEvent
     
  3. Offline

    thepluginbros

    Try this:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageByEntityEvent event){
    3. if (event.getEntity() instanceof Player) {
    4. if (event.getDamager() instanceof Player) {
    5. event.setCancelled(true);
    6. }
    7. }
    8. }
    9.  
     
    lrdemolition likes this.
  4. Offline

    _Filip

    ArrayList#contains()
    Also don't store player objects in a collection.
    Also use a HashSet for this.

    thepluginbros That is NOT what he was asking, you really should read the person's post before you blindly spoondfeed OP.
     
  5. Offline

    lrdemolition

  6. Offline

    thepluginbros

    Your welcome!
     
  7. Offline

    lrdemolition

    does storing player objects use too much space?
    Filip
     
  8. Offline

    _Filip

    lrdemolition That is not the solution you asked for. That cancels are pvp.

    EDIT: Not that much, but the problem is that the garbage collector does not clean up collections, so if the player disconnects, the player object will still be there.
     
  9. Offline

    lrdemolition

    thepluginbros _Filip ProStriker123
    i have 4 array lists filled with player objects, i also have 4 filled with player names
    how would i check if the attacker and taker are in the same array lists?

    oh. thats not a problem i have it so it removes all traces of that player in both arraylists when the games over or player leaves the server

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    _Filip

  11. Offline

    lrdemolition

    _Filip
    no but i dont want to have 4 if statements saying if(blue.contains(attacker)&&blue.contains(taker))
    if(white.contains(attacker)&&white.contains(taker))
    if(green.contains(attacker)&&green.contains(taker))
    if(red.contains(attacker)&&red.contains(taker))
     
  12. Offline

    thepluginbros


    Try this:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageByEntityEvent event){
    3. ArrayList<Player> redteam = new ArrayList<Player>();
    4. ArrayList<Player> blueteam = new ArrayList<Player>();
    5. ArrayList<Player> greenteam = new ArrayList<Player>();
    6. ArrayList<Player> yellowteam = new ArrayList<Player>();
    7.  
    8.  
    9. if (event.getEntity() instanceof Player) {
    10. if (event.getDamager() instanceof Player) {
    11. Player damaged = (Player) event.getEntity();
    12. Player damager = (Player) event.getDamager();
    13.  
    14. if (redteam.contains(damaged) && redteam.contains(damager)){
    15. event.setCancelled(true);
    16. }else if (blueteam.contains(damaged) && blueteam.contains(damager)){
    17. event.setCancelled(true);
    18. }else if (greenteam.contains(damaged) && greenteam.contains(damager)){
    19. event.setCancelled(true);
    20. }else if (yellowteam.contains(damaged) && yellowteam.contains(damager)){
    21. event.setCancelled(true);
    22. }else{
    23.  
    24. }
    25. }
    26. }
    27. }


    I added the arraylists in the void for now! But you should make it static and that if an players leaves he gets removed!
     
    Tyler Christensen likes this.
  13. Offline

    lrdemolition

    thepluginbros
    ok, i think that might just be the simplist way to do it then(lol same way i thought)
    thanks
     
  14. Offline

    thepluginbros

    And it's really stupid to use a if statements each time! Its was better to use 1 if statements and 3 else if statements!
     
  15. Offline

    lrdemolition

  16. Offline

    Lorinthio


    If you're using the snippet of code he gave thats going to block ANY player hitting ANY OTHER player. Which isn't what you wanted.

    What you could do is use a HashMap<String, ArrayList<Player>> teams. This way you could reference the teams you want to check and see if the player attacking the other player is in any of those teams

    Put this in the onEnable()
    Code:java
    1. HashMap<String, ArrayList<Player>> teams = new HashMap<String, ArrayList<Player>>();
    2. ArrayList<Player> redTeam = new ArrayList<Player>();
    3. ArrayList<Player> blueTeam = new ArrayList<Player>();
    4. teams.put("red", redTeam);
    5. teams.put("blue", blueTeam)


    After that is made, then you need to put the players in a team somehow simply with...
    Code:java
    1. public void eventHappens(RandomEvent event){
    2. Player player = event.getPlayer();
    3. teams.get("red").put(player);

    and you need to figure out which event you're going to use for this, whether its some mini game event you made up or a trigger on a sign etc etc.

    THEN FINALLY...
    Code:java
    1. public void onPlayerHit(EntityDamageByEntityEvent event){
    2. if(event.getEntity() instanceof Player){
    3. Player player1 = (Player) event.getEntity();
    4. if(event.getDamager() instanceof Player){
    5. Player player2 = (Player) event.getDamager();
    6. for(String team : teams.getKeys()){
    7. if(teams.get(team).contains(player1) && teams.get(team).contains(player2)){
    8. event.setCancelled(true);
    9. return;
    10. }
    11. }
    12. }
    13. }
    14. }


    You could also use metadata on a player to define the name of the team, and fetch that later for quicker checking to see if player.getmetadata("Team") is the same as the other players... Instead of all the extra containers

    Hope that all makes sense! =D
     
  17. Offline

    thepluginbros

    Sorry I don't understand what you mean...
     
  18. Offline

    Lorinthio

    I see that this has blown up since working on this post so... I'll just see myself out, good luck irdemo!
     
  19. Offline

    lrdemolition

    thepluginbros

    Code:
                if (redtp.contains(damaged) && redtp.contains(damager)){
                event.setCancelled(true);
                }else if (bluetp.contains(damaged) && bluetp.contains(damager)){
                event.setCancelled(true);
                }else if (greentp.contains(damaged) && greentp.contains(damager)){
                event.setCancelled(true);
                }else if (whitetp.contains(damaged) && whitetp.contains(damager)){
                event.setCancelled(true);
                }else{
                    return;//to make it go to the beginning
    }
    also can an array list contain more than 1 of each object

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  20. Offline

    _Filip

    I feel bad for irdemo, everyone is giving him incorrect code except for me.
    Instead of an arraylist store the players in a hashset. THEN use .contains(player) on that hashset to check if it contains the player.

    @lrdemolition Yes but hashset cannot.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. Offline

    lrdemolition

    _Filip
    what does the hashset do better?
     
  22. Offline

    _Filip

    lrdemolition Elements are backed by a hashmap, instead of an array. It is much, much more efficient, but it has no index, which for the current purpose you do not need.
     
  23. Offline

    lrdemolition

    ok _Filip
    but i already have half of my plugin written using arraylists
    ill use hashsets in the future when nessesary
     
  24. Offline

    Lorinthio


    Lol my code is correct, yes your way is possibly/probably better. But my code still works, and as such is correct, just not optimal
     
  25. Offline

    _Filip

    lrdemolition You can just replace the type of the variable to hashset...........
     
  26. Offline

    lrdemolition

    _Filip
    ok thanks
    im closing this because i have more stuff to write before 8th grade starts
    thank you everyone
     
Thread Status:
Not open for further replies.

Share This Page