Teams

Discussion in 'Plugin Development' started by Lactem, Mar 14, 2013.

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

    Lactem

    Hello people. I need to make teams for a plugin where players are disguised as mobs and fight other players that are also disguised as mobs. Skeletons have to not be able to hurt fellow skeletons and all other mobs have to only be able to hurt skeletons. I'm using DisguiseCraft for turning into mobs, by the way. What should I do?

    Thanks,
    Lactem
     
  2. Offline

    TheUpdater

    want to make teams? like spawn Location of them as well?

    use array list or hashmap
     
  3. Offline

    skipperguy12

    Wait...so it's all one team?

    You can have lobby and ingame arraylists, and on game end add them to lobby, when game begins remove from lobby and add to ingame.

    If you're using DisguiseCraft, they need to have an API for you to implement into your plugin, or do it yourself.
     
  4. Offline

    Darq

    you're asking us to help with wayyyy to broad a topic, that being "can you tell me how to write this entire plugin?"
    Sounds like you'll need to listen to EntityDamageEntityEvent to prevent that.
    To keep track of teams, I'd make a object named Team with the methods you're going to need as well as a Set/List of all the players in that team. Put teams into some other collection. It depends what you need to do.
     
  5. Offline

    Lactem

    Thanks.

    It seems like you misunderstood. I already have the DisguiseCraft stuff all set up. There are supposed to be two teams: skeletons and non-skeletons. Players cannot hurt players that are on their team. I think I'll be trying Darq's suggestion first.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    TheUpdater

    put all players in array lists
    and if player are in that array list thay cant hurt players who are in same array list as them
     
  7. Offline

    Lactem

    No, no, no. I'm not asking how to write a whole plugin. This is only a small part of it. Perhaps I should explain it better. There's a skeleton team and a non-skeleton team. Players inside the skeleton team cannot hurt eachother and players inside the non-skeleton team cannot hurt eachother either. Does that help at all?

    Sorry, but I'm relatively new to Java and making plugins. I've heard of array lists and know a little about them, but not that much. Could you tell me the basic syntax of an array list, please?

    Thanks,
    Lactem

    Look what I have:
    publicclass teams extends JavaPlugin {
    newplayer = new java.util.ArrayList<STRING>();
    newplayer.add( "player1" );
    newplayer.add( "player2" );
    newplayer.add( "player3" );
    newlpayer.add( "player4" );
    }

    It doesn't like the beginning and closing brackets because

    "Syntax error on token "{", { expected after this
    token" and "Syntax error, insert "}" to complete ClassBody."

    That makes absolutely no sense. Why do I need to replace "{" with "{" and "}" with "}?" I just don't get it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    TheUpdater

    Put this under that

    public class <classname> extends JavaPlugin {
    ArrayList<Player> players = new ArrayList<Player>();
    }

    if want it to launch on command you do
    @Override
    public boolean onCommand(CommandSender sender, Command command,
    String cl, String[] args) {
    Player p = (Player) sender;
    if(cl.equalsIgnoreCase("day")){
    p.sendMessage(ChatColor.GOLD+"Time Set To Day");
    p.getWorld().getTime();
    p.getWorld().setTime(0);
    players.somthing


    this is not correct code but en example

    player.
    is the array thing so use player. to see what you can do

    just do what it says lol keap adding until it works

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

    Lactem

    Okay thanks. I know the command stuff, by the way. Lol.
     
  10. Offline

    TheUpdater

    u need to do array location but i dont know how or set damage off and shit
     
  11. Offline

    Lactem

    Look, I have this:

    public class teams extends JavaPlugin {
    ArrayList<Player> players = new ArrayList<Player>();
    newplayer = new java.util.ArrayList<Player>();
    }

    and it still doesn't like it. I see nothing wrong with this whatsoever. The error is on the second line.

    Syntax error on token ";", , expected
     
  12. Offline

    TheUpdater

    u see you have
    ArrayList<Player> players = new ArrayList<Player>();


    try
    players = new java.util.ArrayList<Player>();
    or player.add(new java.util.ArrayList<Player>());
     
  13. Offline

    Lactem

    I think I got it fixed. I guess it wanted this:

    public class teams extends JavaPlugin {
    ArrayList<Player> players = new ArrayList<Player>();
    ArrayList<STRING> newplayer = new java.util.ArrayList<STRING>();
    }

    Thanks.

    Alright...I made the arraylist. For some reason, it doesn't like the player.add or newplayer.add even though that worked before.

    So strange...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  14. Offline

    TheUpdater

    :confused: lol... hm i gona look if i can make a code wait
     
  15. Offline

    Lactem

    Okay thanks.
     
  16. Offline

    TheUpdater

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command command, String cl, String[] args) {
            Player p = (Player) sender;
            if(cl.equalsIgnoreCase("join")){
                if(args.length == 0){
                    p.sendMessage("Usage: /join <name>");
                    if(args.length == 1){
                        pl.add(cl);   
                        p.sendMessage("Player added");
                    }
                }
            }
            if(cl.equalsIgnoreCase("Leave")){
                if(args.length == 0){
                    p.sendMessage("Usage: /Leave <name>");
                    if(args.length == 1){
                        pl.remove(cl);
                        p.sendMessage("Player Removed");
                    }
                }
               
            }
                    return false;
        }
     
    }
    you need to add join and leave in plugin.yml

    dont know if its the correct code tho hope so =)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  17. Offline

    macguy8

    Let me try to help here.

    (1) Never store the player object, store the name (A String), and use players.add(player.getName()) and players.contains(player.getName()).
    (2) instead of making an array list for each team, I'd recommend assigning an Id or making an enum for your teams, and then running...
    Code:
    public Boolean sameTeam(Player p1, Player p2) {
      return (map.get(p1.getName()) == map.get(p2.getName()));
    }
     
  18. Offline

    Lactem

    Thanks for your guys' help. I'll see what I can get to work!

    Okay TheUpdater, what is pl? It's not recognizing pl as anything. And I already have everything for joining. I just need them to become on a team when they join. You see, a player joins and is disguised as a mob. I need to make the players that got disguised as skeletons be on one team and everyone else to be on another team.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  19. Offline

    TheUpdater

    ??? more detail
     
  20. Offline

    Lactem

    "pl cannot be resolved"

    We never defined pl......

    I think you're confused about what I already have and what I still need. I already have the plugin doing something when they join. I just need it to also make them join a team. Then, players within each team have to not be allowed to hurt each other.

    Alright I figured one thing out. When a player joins, he/she becomes part of an array. Now I need to make certain arrays not be able to hurt other certain arrays. Do you know how I would do that? I'll give you two arrays that I don't want to be able to hurt each other: skeleton1 and skeleton2. These are part of the arraylist teamassign.

    I think I'll need to use the EntityDamageEntityEvent and setCancelled() for this part. Do you think you could help me out a little?

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

    Darq

    This is stripped down to only show you how to check if two players are on the same team, so it's certainly not fancy. :)
    Code:
    HashSet<String> team1 = new HashSet<String>();
    HashSet<String> team2 = new HashSet<String>();
     
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e) {
        if (!(e.getDamager() instanceof Player) || !(e.getEntity() instanceof Player)) {
            return; // doesn't involve two players, ignore - however you'll want to expand this a bit to handle projectiles, etc
        }
        Player damaged = (Player) e.getEntity();
        Player damager = (Player) e.getDamager();
       
        // if: both players are on team1 OR if both players are on team2? cancel damage event
        if ((team1.contains(damaged.getName()) && team1.contains(damager.getName()))
                || (team2.contains(damaged.getName()) && team2.contains(damager.getName()))) {
            e.setCancelled(true);
        }
    }
    
    I used HashSet because .contains() is faster than ArrayList's .contains(). Generally, if you're going to iterate a collection lots and/or need to support duplicate entires, use an ArrayList. Generally, if you don't need to iterate lots and you don't need support for duplicate entries, use a HashSet.
     
  22. Offline

    Lactem

    Thanks I just added a modified version of this and will test it with multiple players soon.

    Edit: Now that I'm using a hashmap, how would I add someone to this group? I've included a pastebin with my code in the main class. http://pastebin.com/Vvz57uax Please ignore the messages that it sends. Those are from a different plugin and need updating.
     
Thread Status:
Not open for further replies.

Share This Page