Solved [Factions API] for statement?

Discussion in 'Plugin Development' started by ProMCKingz, Mar 21, 2015.

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

    ProMCKingz

    I have the following statement; however, it only sends a message to the faction of the sender, not to the opposing faction.

    Code:java
    1. for(final Player pa : fac.getOnlinePlayers()){
    2. for (final Player pas : FactionColl.get().getByName(args[1]).getOnlinePlayers()){
    3. pa.sendMessage("");
    4. pa.sendMessage("");
    5. pa.sendMessage("");
    6. pa.sendMessage("");
    7. pa.sendMessage("");
    8. pa.sendMessage("");
    9. pWar.put(pa, 0);
    10. pWar.put(pas, 0);
    11. Opponent.put(args[1], null);
    12. pa.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
    13. pa.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
    14. pa.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
    15. pa.sendMessage(prefix + ChatColor.RED + "The war will be based on your faction home");
    16. pas.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
    17. pas.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
    18. pas.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
    19. pas.sendMessage(prefix + ChatColor.RED + "The war will be based on your opponent's faction home");
     
  2. Well what you are doing here, the 2nd for loop is pointless, since it just sends the message 2 times to the player. Remove the 2 arrow spots because if its sent to the factions players, and if the sender is in the faction, it will send it to him. So your sending it to him 2 times basically.

    [​IMG]
     
  3. Offline

    ProMCKingz

    @OfficerDeveloper
    The reason I need the 2 if statements is because: The plugin aims to create a war against 2 factions (where 'fac' represents the faction of the sender); hence the "pa" represents the online players of the players faction.
    The other statement ("pas") represents the opposing faction's members.
    Hence I implemented 2 for statements.
     
    Last edited: Mar 22, 2015
  4. Offline

    Fluf3

    @ProMCKingz Did you get the message and the other person didnt? Please tell me more so I can help :)
    Try this...

    Code:
        for(final Player pa : fac.getOnlinePlayers()){
        for (final Player pas : FactionColl.get().getByName(args[0]).getOnlinePlayers()){
        pa.sendMessage("");
        pa.sendMessage("");
        pa.sendMessage("");
        pa.sendMessage("");
        pa.sendMessage("");
        pa.sendMessage("");
        pWar.put(pa, 0);
        pWar.put(pas, 0);
        Opponent.put(args[0], null);
        pa.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
        pa.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
        pa.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
        pa.sendMessage(prefix + ChatColor.RED + "The war will be based on your faction home");
        pas.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
        pas.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
        pas.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
        pas.sendMessage(prefix + ChatColor.RED + "The war will be based on your opponent's faction home");
    Can you also give me the command because I changed (args[1]) to (args[0]).
    /factions myFaction war
    myFaction is equal to args 0 and war is equal to args 1 which might be your problem...
     
  5. Offline

    ProMCKingz

    @Fluf3
    • The command is: /war start [facname]
    • One person receives the message (the sender). At this point i am not sure if all the members in the faction receive the message. But i am sure that the opposing faction members are not receiving the message
    Here is the whole class, just incase it's a constructor issue:
    Main Class (open)

    Code:java
    1. package me.promckingz.battleday;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.World;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.PluginManager;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. import com.massivecraft.factions.entity.Faction;
    17. import com.massivecraft.factions.entity.FactionColl;
    18. import com.massivecraft.factions.entity.MPlayer;
    19.  
    20. public class Main extends JavaPlugin{
    21.  
    22. private static Faction fac;
    23.  
    24. public static HashMap<Faction, Integer> inList = new HashMap<Faction, Integer>();
    25. ArrayList<Faction> inWar = new ArrayList<Faction>();
    26. public static HashMap<String, Integer> Opponent = new HashMap<String, Integer>();
    27. public static HashMap<String, Integer> Creator = new HashMap<String, Integer>();
    28. public static HashMap<Player, Integer> pWar = new HashMap<Player, Integer>();
    29. public static int AScore;
    30. public static int BScore;
    31.  
    32. public void onEnable(){
    33. PluginManager register = Bukkit.getServer().getPluginManager();
    34. register.registerEvents(new KillListener(), this);
    35. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "BattleDay running...");
    36. saveDefaultConfig();
    37. getConfig().options().copyDefaults(true);
    38. }
    39.  
    40. public void onDisable(){
    41.  
    42. }
    43.  
    44. Faction faction = null;
    45.  
    46. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, final String[] args){
    47. if(sender instanceof Player){
    48. final String prefix = (ChatColor.GRAY + "[" + ChatColor.GREEN + "BattleDay" + ChatColor.GRAY + "] ");
    49. Player p = (Player) sender;
    50. MPlayer uplayer = MPlayer.get(p);
    51. fac = uplayer.getFaction();
    52. if(cmd.getName().equalsIgnoreCase("war")){
    53. if (args.length == 0){
    54. p.sendMessage("");
    55. p.sendMessage(ChatColor.GOLD + " ---->" + ChatColor.GREEN + " Made by: ProMCKingz " + ChatColor.GOLD + "<----");
    56. p.sendMessage(prefix + ChatColor.AQUA + "Use /War Help for plugin information");
    57. p.sendMessage(ChatColor.LIGHT_PURPLE + " Version 1.0 ");
    58. p.sendMessage("");
    59. }
    60. else if (args[0].equalsIgnoreCase("help")){
    61. if(!p.hasPermission("war.help")){
    62. return true;
    63. }
    64. p.sendMessage("");
    65. p.sendMessage(prefix + ChatColor.GOLD + "-=- BattleDay Help -=-");
    66. p.sendMessage(prefix + ChatColor.AQUA + "/war search |" + ChatColor.GREEN + " Search for other factions ready to battle");
    67. p.sendMessage(prefix + ChatColor.AQUA + "/war start [faction] |" + ChatColor.GREEN + " Start a war with another faction");
    68. p.sendMessage(prefix + ChatColor.AQUA + "/war cancel |" + ChatColor.GREEN + " Cancel war preperation [SOON!]");
    69. }
    70. else if (args[0].equalsIgnoreCase("search")){
    71. if(!p.hasPermission("war.search")){
    72. return true;
    73. }
    74. if(fac.isNone()){
    75. p.sendMessage(prefix + ChatColor.RED + "You do not have a faction!");
    76. return true;
    77. }
    78. if(!inList.containsKey(fac)){
    79. inList.put(fac, null);
    80. p.sendMessage(prefix + ChatColor.DARK_RED + "[Warning]" + ChatColor.RED + " Your faction has now been placed in the search list!");
    81. }
    82. p.sendMessage(prefix + ChatColor.GOLD + "Searching for factions...");
    83. p.sendMessage(prefix + ChatColor.GOLD + "Here are all the factions ready for battle:");
    84. for (Faction fact : inList.keySet()) { // If inList is a map, add keySet() or values()
    85. p.sendMessage(" - " + fact.getName() + ChatColor.GRAY + ChatColor.ITALIC + " (" + fact.getDescription().trim() + ")");
    86. }
    87. }
    88. else if (args[0].equalsIgnoreCase("start")){
    89. if(!p.hasPermission("war.help")){
    90. return true;
    91. }
    92. if(args.length != 2){
    93. p.sendMessage(prefix + ChatColor.RED + "Please use /war start [faction]");
    94. return true;
    95. }
    96. if(!FactionColl.get().isNameTaken(args[1])){
    97. p.sendMessage(prefix + ChatColor.RED + "That faction does not exist!");
    98. p.sendMessage(prefix + ChatColor.RED + "Factions are searched in case sensitive");
    99. return true;
    100. }
    101. if(fac.isNone()){
    102. p.sendMessage(prefix + ChatColor.RED + "You do not have a faction!");
    103. return true;
    104. }
    105. if(inWar.contains(fac)){
    106. p.sendMessage(prefix + ChatColor.RED + "Your faction is already in war!");
    107. return true;
    108. }
    109. if(fac.getLeader().getName() != p.getName()){
    110. p.sendMessage(prefix + ChatColor.RED + "You must be the leader of the faction to start war!");
    111. p.sendMessage(prefix + ChatColor.RED + "Current leader is: " + fac.getLeader().getName());
    112. return true;
    113. }
    114. if(fac.hasHome() == false){
    115. p.sendMessage(prefix + ChatColor.RED + "You do not have a set faction home!");
    116. p.sendMessage(prefix + ChatColor.RED + "Use /f claim");
    117. p.sendMessage(prefix + ChatColor.RED + " then");
    118. p.sendMessage(prefix + ChatColor.RED + "Use /f sethome");
    119. return true;
    120. }
    121. if(FactionColl.get().getByName(args[1]).hasHome() == false){
    122. p.sendMessage(prefix + ChatColor.RED + "Your opponent does not have a set faction home!");
    123. p.sendMessage(prefix + ChatColor.RED + "They should use /f claim");
    124. p.sendMessage(prefix + ChatColor.RED + " then..");
    125. p.sendMessage(prefix + ChatColor.RED + "They should use use /f sethome");
    126. return true;
    127. }
    128. for(Faction f : inList.keySet()){
    129. if(f.isNone()){
    130. p.sendMessage(prefix + ChatColor.RED + "That faction is not in the search list!");
    131. p.sendMessage(prefix + ChatColor.RED + "Factions are searched in case sensitive");
    132. return true;
    133. }
    134. }
    135. if(args.length == 2){
    136. for(Faction f : inList.keySet()){
    137. if(f.getName().contains(args[1])){
    138. if(FactionColl.get().isNameTaken(args[1])){
    139. Bukkit.getServer().broadcastMessage(" " + ChatColor.AQUA + fac.getName() + ChatColor.RED + " has started war against " + ChatColor.GREEN + args[1]);
    140. inWar.add(fac);
    141. inWar.add(FactionColl.get().getByName(args[1]));
    142. Opponent.put(FactionColl.get().getByName(args[1]).getMPlayers().toString(), null);
    143.  
    144. for(final Player pa : fac.getOnlinePlayers()){
    145. for (final Player pas : FactionColl.get().getByName(args[1]).getOnlinePlayers()){
    146. pa.sendMessage("");
    147. pa.sendMessage("");
    148. pa.sendMessage("");
    149. pa.sendMessage("");
    150. pa.sendMessage("");
    151. pa.sendMessage("");
    152. Creator.put(pa.getName(), 0);
    153. pWar.put(pa, 0);
    154. pWar.put(pas, 0);
    155. pa.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
    156. pa.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
    157. pa.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
    158. pa.sendMessage(prefix + ChatColor.RED + "The war will be based on your faction home");
    159. pas.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
    160. pas.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
    161. pas.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
    162. pas.sendMessage(prefix + ChatColor.RED + "The war will be based on your opponent's faction home");
    163. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.this, new Runnable() {
    164. public void run() {
    165. pa.sendMessage("");
    166. pa.sendMessage("");
    167. pa.sendMessage("");
    168. pa.sendMessage("");
    169. pa.sendMessage("");
    170. pa.sendMessage("");
    171. pa.sendMessage("");
    172. pa.sendMessage("");
    173. pa.sendMessage(prefix + ChatColor.RED + "Your preperation time has ended!");
    174. pa.sendMessage(prefix + ChatColor.RED + "You will be teleported to the battle ground in 3 seconds!");
    175. pas.sendMessage("");
    176. pas.sendMessage("");
    177. pas.sendMessage("");
    178. pas.sendMessage("");
    179. pas.sendMessage("");
    180. pas.sendMessage("");
    181. pas.sendMessage("");
    182. pas.sendMessage("");
    183. pas.sendMessage(prefix + ChatColor.RED + "Your preperation time has ended!");
    184. pas.sendMessage(prefix + ChatColor.RED + "You will be teleported to the battle ground in 3 seconds!");
    185. }
    186. }, 100L);
    187. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.this, new Runnable() {
    188. public void run() {
    189. World w = Bukkit.getServer().getWorld(fac.getHome().getWorld());
    190. Double x = fac.getHome().getLocationX();
    191. Double y = fac.getHome().getLocationY();
    192. Double z = fac.getHome().getLocationZ();
    193. pa.teleport(new Location(w,x,y,z));
    194. pas.teleport(new Location(w,x,y,z));
    195. pa.sendMessage("");
    196. pa.sendMessage("");
    197. pa.sendMessage("");
    198. pa.sendMessage("");
    199. pa.sendMessage("");
    200. pa.sendMessage("");
    201. pa.sendMessage("");
    202. pa.sendMessage("");
    203. pa.sendMessage(prefix + ChatColor.RED + "You are now in war!");
    204. pa.sendMessage(prefix + ChatColor.BLUE + "Kill as many players as you can to win the war!");
    205. pas.sendMessage("");
    206. pas.sendMessage("");
    207. pas.sendMessage("");
    208. pas.sendMessage("");
    209. pas.sendMessage("");
    210. pas.sendMessage("");
    211. pas.sendMessage("");
    212. pas.sendMessage("");
    213. pas.sendMessage(prefix + ChatColor.RED + "You are now in war!");
    214. pas.sendMessage(prefix + ChatColor.BLUE + "Kill as many players as you can to win the war!");
    215. }
    216. }, 160L);
    217. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.this, new Runnable() {
    218. public void run() {
    219. pWar.remove(pa);
    220. pWar.remove(pas);
    221. inWar.remove(fac);
    222. Opponent.remove(FactionColl.get().getByName(args[1]).getMPlayers().toString());
    223. Creator.remove(pa);
    224. AScore = 0;
    225. BScore = 0;
    226. pa.sendMessage("");
    227. pa.sendMessage("");
    228. pa.sendMessage("");
    229. pa.sendMessage("");
    230. pa.sendMessage("");
    231. pa.sendMessage("");
    232. pa.sendMessage(ChatColor.GOLD + "****************************");
    233. pa.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + " * WAR RESULTS * ");
    234. pa.sendMessage(prefix + ChatColor.RED + "You have lost the war!");
    235. pa.sendMessage(prefix + ChatColor.AQUA + "You have won the war!");
    236. pa.sendMessage(ChatColor.GOLD + "****************************");
    237. }
    238. }, 260L);
    239. }
    240. }
    241. }
    242. }
    243. }
    244. }
    245. }
    246. else p.sendMessage(prefix + ChatColor.RED + "Unknown command: " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " /war help");
    247. }
    248. }
    249. return true;
    250. }
    251. }
    252.  

     
  6. Offline

    MajorSkillage

    Code:
        @Override
        public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
            if(Sender instanceof Player){
                if(cmd.getName().equalsIgnoreCase("sup")){
                MPlayer p = MPlayer.get(p);
                for(Faction f : FactionColl.get().getAll()){
                    if(f.getRelationTo(p.getFaction()).equals(Rel.ENEMY)){
                        for(MPlayer P : f.getMPlayers()){
                            P.sendMessage("g\'day!");
                        }
                    }
                }
                }
            }
        }
     
  7. Offline

    ProMCKingz

  8. Offline

    MajorSkillage

    If a user types /sup (assuming they have a faction) it will send all their faction enemies players a message :D, oh damn didn't check if the players were even online, oh well ._.
     
  9. Offline

    ProMCKingz

    @MajorSkillage
    How would you implement that into arguments. So when a player types /war start [facname] it will send his faction and all of the other faction a message. Please check the code posted above (in a spoiler), and find the mistake I am making there. Thanks
     
  10. Offline

    MajorSkillage

    Code:
                for(Faction f : FactionColl.get().getAll()){
                    if(f.getRelationTo(p.getFaction()).equals(Rel.ENEMY)){
                        for(MPlayer pa : f.getMPlayers()){
                        if(pa.isOnline()){
                                                     pa.sendMessage("");
                                pa.sendMessage("");
                                pa.sendMessage("");
                                pa.sendMessage("");
                                pa.sendMessage("");
                                pa.sendMessage("");
                                pWar.put(pa, 0);
                                pWar.put(pas, 0);
                                Opponent.put(args[1], null);
                            pa.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
                            pa.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
                            pa.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
                            pa.sendMessage(prefix + ChatColor.RED + "The war will be based on your faction home");
                            pas.sendMessage(prefix + ChatColor.AQUA + "The war will last 10 minutes");
                            pas.sendMessage(prefix + ChatColor.AQUA + "You have 5 minutes of preperation time");
                            pas.sendMessage(prefix + ChatColor.GREEN + "If you win, you will receive more faction power!");
                            pas.sendMessage(prefix + ChatColor.RED + "The war will be based on your opponent's faction home");
                         }
                        }
                    }
                }
                }
            }
        }
    Sry for the messy reply, Bukkit playing up on meh
     
  11. Offline

    ProMCKingz

    @MajorSkillage
    I don't want to check if they are enemies or not; basically it checks if they are in the search list or not
     
  12. Offline

    MajorSkillage

    @ProMCKingz
    What exactly do you want in this "search List"?
     
  13. Offline

    Fluf3

    @ProMCKingz
    I cant see any constructor errors but I think that the problem is from...
    Code:
     for(final Player pa : fac.getOnlinePlayers()){
    for (final Player pas : FactionColl.get().getByName(args[1]).getOnlinePlayers()){
    Hope this helps!
     
  14. Offline

    ProMCKingz

    @MajorSkillage
    Thanks, I just realised that I was exporting it in the wrong place. Thanks for your help, it all seemed to work fine.

    ___________________________________
    @Fluf3 Alright, thanks for your help.
     
    Last edited: Mar 22, 2015
Thread Status:
Not open for further replies.

Share This Page