PvP Plugin Help! REALLY CONFUSED!

Discussion in 'Plugin Development' started by Chibbey, Nov 18, 2013.

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

    Chibbey

    Hello, I need help with a pvp plugin im making. I want to make it so the people on the same team cant hurt each other. I made 2 ArrayLists, one called blueteam and other called redteam, so I tried to make it so when someone types /team join <blue|red> they join the team and get added to it and I have a EntityDamageByEntityEvent and heres my code for it, which I don't think is correct at all.


    Code:java
    1. ArrayList<String> redteam = new ArrayList<String>();
    2. ArrayList<String> blueteam = new ArrayList<String>();
    3.  
    4. public void onEnable() {
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8. @EventHandler
    9. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    10. if (event.getEntity() instanceof Player) {
    11. if (blueteam.contains(event.getEntityType().PLAYER.getName())) {
    12. if (blueteam.contains(event.getDamager().getType().PLAYER.getName())) {
    13. event.setCancelled(true);
    14. }
    15. }
    16. if (redteam.contains(event.getEntityType().PLAYER.getName())) {
    17. if (redteam.contains(event.getDamager().getType().PLAYER.getName())) {
    18. event.setCancelled(true);
    19. }
    20. }
    21. }
    22. }

    also I need help with a falldamage plugin where if a player has permission they can type
    /falldmg <on|off> and if they have it on they get hurt and off they don't get hurt, so heres my code for that it uses a arraylist also to see if a player has falldmg on or off, so heres the code for that.

    Code:java
    1. ArrayList<String> falldmg = new ArrayList<String>();
    2.  
    3. public void onEnable() {
    4. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. @SuppressWarnings("deprecation")
    8. @EventHandler
    9. public void onEntityDamage(EntityDamageEvent event) {
    10. if (!(event instanceof EntityDamageByEntityEvent)) {
    11. if (event.getCause() == DamageCause.FALL) {
    12. if (event.getEntity() instanceof Player) {
    13. event.getEntityType();
    14. if (falldmg.contains(EntityType.PLAYER.getName())) {
    15. event.setCancelled(true);
    16. }
    17. }
    18. }
    19. }
    20. }
    21.  
    22. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    23. Player p = (Player) sender;
    24.  
    25. if (label.equalsIgnoreCase("falldmg")) {
    26. if (args[0].equalsIgnoreCase("on")) {
    27. if (falldmg.contains(p.getName())) {
    28. falldmg.remove(p.getName());
    29. p.sendMessage(ChatColor.DARK_RED + "[KitPvP By:Chibbey]" + ChatColor.GREEN + "You turned fall damage on, you will now get hurt!");
    30. return true;
    31. }
    32. if (!falldmg.contains(p.getName())) {
    33. p.sendMessage(ChatColor.DARK_RED + "[KitPvP By:Chibbey]" + ChatColor.RED + "You already have fall damage on!");
    34. return true;
    35. }
    36. }
    37. if (args[0].equalsIgnoreCase("off")) {
    38. if (!falldmg.contains(p.getName())) {
    39. falldmg.add(p.getName());
    40. p.sendMessage(ChatColor.DARK_RED + "[KitPvP By:Chibbey]" + ChatColor.GREEN + "Fall damage is now off, you will not be hurt!");
    41. return true;
    42. }
    43. if (falldmg.contains(p.getName())) {
    44. p.sendMessage(ChatColor.DARK_RED + "[KitPvP By:Chibbey]" + ChatColor.RED + "You already have fall damage off!");
    45. return true;

    the thing that is not working is it stills lets them have falldmg, only if they had a getPlayer() in a entitydamage and entitydamagebyentity events it would be sooooo much easier. Please help me
     
  2. Offline

    L33m4n123

    You can sepereate them in teams via the scoreboard for example and disable friendly fire.

    and about the getPlayer()

    Code:java
    1. @EventHandler
    2. public void fallDamage (EntityDamageEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. Player p =(Player) e.getEntity();
    5. // rest of your code
    6. }
    7. }
     
  3. Offline

    AoH_Ruthless

    Chibbey
    This is for your first code. I just noticed 2 aesthetic things:
    If it's PVP you might want to also check if the damager was a player so use:
    Code:
    if((event.getEntity() instanceof Player) && event.getDamager() instanceof Player) {
    // code
    }
    Second: instead of typing event.getEntity() .... all the time, assign variables for both the damager and the entity.

    And maybe this isn't most efficient but I use HashMaps for this kind of stuff. Maybe try a HashMap<String, String>
     
  4. Offline

    Chibbey

    AoH_Ruthless
    Ok but how do I check if the player and the damager are in the same arraylist and cancle the event from them hurting eachother
     
  5. Offline

    L33m4n123

    check if arraylist contains player && arraylist contains damager if yes cancel
     
  6. Offline

    Chibbey

    L33m4n123
    how do I do that, because EntityDamageByEntityEvent cant get a player its get a entity and it wont let you get the entitys. so how do I save the players name, cause I would have to do something like this,

    event.getEntity().getType() or event.getEntityType() but then event.getEntity().getType().PLAYER.getName(); but that doesn't work so I need help and the damager would have to be event.getDamager().getType().PLAYER.getName();

    PLEASE HELP!
     
  7. Offline

    L33m4n123


    Ever heard of Casting? If not I would recommend you looking more into java before playing with the Bukkit API

    Cannot promise you that this bit of code works. However it should give you a push into the right direction

    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageByEntityEvent e) {
    3. Player damager;
    4. Player damagee;
    5. if (e.getDamager() instanceof Player && e.getEntity() instanceof Player) {
    6. damager = (Player) e.getDamager();
    7. damagee = (Player) e.getEntity();
    8. // Rest of your code
    9. }
    10. }
     
Thread Status:
Not open for further replies.

Share This Page