Get Players Team (ArrayLists)

Discussion in 'Plugin Development' started by Skrarity, Jan 21, 2015.

Thread Status:
Not open for further replies.
  1. So I have a Tracker Plugin but I want that the Tracker does not track Team Members!
    But how can I do it? I'd like to create an method in my main class
    something like "getTeam(Player) " so i could write "if(getTeam(p).contains(p01) ....."
    I think it shouldn't make sense if you d write my code cuz i have NO IDEA how i do it... I am thinking about it for 2 days and it doesn't work cause there are more than 1 teams (like 7)...
    .

    Need Help :s And Excuse me for my bad English
     
  2. Online

    timtower Administrator Administrator Moderator

  3. @timtower

    Main Class:
    Code:
    package me.skrarity.varo;
    
    import java.util.ArrayList;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin{
    
        private ArrayList<Player> Team01 = new ArrayList<Player>();
        private ArrayList<Player> Team02 = new ArrayList<Player>();
        private ArrayList<Player> Team03 = new ArrayList<Player>();
        private ArrayList<Player> Team04 = new ArrayList<Player>();
        private ArrayList<Player> Team05 = new ArrayList<Player>();
        private ArrayList<Player> Team06 = new ArrayList<Player>();
        private ArrayList<Player> Team07 = new ArrayList<Player>();
    //These Teams are only created , they haven't any sence yet!
    
    
    
        @Override
        public void onEnable() {
            System.out.println("[Varo] Started Plugin");
        
            this.getServer().getPluginManager().registerEvents(new EventBoard(this),this);
    
        }
    
        public ArrayList<Player> getTeam(Player p) {
                //I dont know that i should write
        }
    
    
    
    
    
    }
    
    My Tracker Class:
    Code:
    package me.skrarity.varo;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class EventBoard implements Listener {
        private main Varo;
        private String mcrotxt = ChatColor.GRAY + "[" + ChatColor.RED + "Tracking" + ChatColor.GRAY + "]" + ChatColor.GRAY + " ";
        private String X = ChatColor.GRAY + "[" + ChatColor.RED + "X" + ChatColor.GRAY + "] " + ChatColor.GRAY;
        private String Y = ChatColor.GRAY + "[" + ChatColor.RED + "Y" + ChatColor.GRAY + "] " + ChatColor.GRAY;
        private String Z = ChatColor.GRAY + "[" + ChatColor.RED + "Z" + ChatColor.GRAY + "] " + ChatColor.GRAY;
        public EventBoard(main main) {
            this.Varo = main;
        }
    
    
    
    
    
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
       
            if(p.getLevel() > 15){
            if(e.getMaterial() == Material.COMPASS) {
           
                //Track
                Player target = getNearestPlayer(p);
                if(target == null) {
                    p.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "Mcro" + ChatColor.GRAY + "] Kein Spieler kann getracked werden!");
                    return;
                }
           
                
    
           
           
                p.sendMessage(mcrotxt + "Dein Kompass zeigt auf " + ChatColor.RED + getNearestPlayer(p).getName());
                p.setLevel(-15);
                p.sendMessage(mcrotxt + "Du hast dabei 15 Level verloren!");
                p.sendMessage(mcrotxt + getNearestPlayer(p).getName() + "'s Koordinaten sind: ");
                p.sendMessage(X + getNearestPlayer(p).getLocation().getBlockX() + " " + Y + getNearestPlayer(p).getLocation().getBlockY() + " " + Z + getNearestPlayer(p).getLocation().getBlockZ());
                p.setCompassTarget(getNearestPlayer(p).getLocation());
           
            }
            } else {
                p.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "Mcro" + ChatColor.GRAY + "] Du hast nicht genĂ¼gend XP!");
            }
        }
    
        public Player getNearestPlayer(Player p ) {
       
            double distance = Double.MAX_VALUE;
            Player target = null;
       
            for(Entity entity : p.getNearbyEntities(8000,8000,8000)) {
                if(entity instanceof Player) {
               
                    double dis = p.getLocation().distance(entity.getLocation());
               
                    if(dis < distance) {
                        distance = dis;
                        target = (Player) entity;
                    }
               
                }
           
            }
            return target;
        }
    
    }
     
  4. Online

    timtower Administrator Administrator Moderator

    @Skrarity Why not make a list of teams instead? Or a hashmap.
    And you go through all the teams, check if the player is in that team, if so: return the team list, if not: continue checking the other teams.
     
  5. Wait so i should write that

    Code:
    public ArrayList<Player> getTeam(Player p) {
                if(Team01.contains(p) {
    return Team01; }
    
    
    With every team ??? Would it works , or in other words SHOULD it works??
     
  6. Offline

    Lolmewn

    @Skrarity It's not the prettiest solution, but yes.
     
  7. Offline

    1Rogue

    Make a Team class instead of using an arbitrary collection for them all.
     
    sethrem likes this.
  8. Offline

    Rufus5

    Code:
    public ArrayList getTeam(Player p){
    if(Team01.contains(p){
    return Team01;
    }
    else if(Team02.contains(p){
    return Team02;
    }
    else if(Team03.contains(p){
    return Team03;
    }
    else if(Team04.contains(p){
    return Team04;
    }
    else if(Team06.contains(p){
    return Team05;
    }
    else if(Team06.contains(p){
    return Team06;
    }
    else if(Team07.contains(p){
    return Team07;
    }
    else{
    return null;
    }
    }
    Try that
     
  9. Offline

    WesJD

    @Rufus5 @Skrarity Or you can use a switch statement with an int as the parameters for the method.
     
    Last edited: Jan 21, 2015
  10. Offline

    mythbusterma

    Rufus5 and sethrem like this.
  11. Offline

    teej107

    Use a Map with the Player as the key and the Team as the value. Value could be just a String, int, etc or what @1Rogue suggested.
     
  12. Offline

    PreFiXAUT

Thread Status:
Not open for further replies.

Share This Page