Need help with a staff chat plugin

Discussion in 'Plugin Development' started by Benpat9, Jun 11, 2014.

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

    Benpat9

    So basically i'm developing a plugin for my server and it does multiple things one of those is (supposed) to be a staff chat, meaning a staff member can type /ac to toggle staff chat and talk in staff chat. All staff are supposed to be able to see the chat at all times and only chat in it when toggled, at the moment i can toggle it and the message will come up but when i talk it doesn't actually go into staff chat and all players of any rank can see it. Can someone show me or fix my errors in this code. (i'm not sure if there are any others errors because i cant test all of it if i cant actually type in staff chat xD) Thanks In Advance :)

    Code:java
    1. else
    2. if(cmd.getName().equalsIgnoreCase("ac")){
    3. if(args.length == 0){
    4. if(player.hasPermission("legacy.actoggle")){;
    5. if(!list.contains(player.getName())){
    6. list.add(player.getName());
    7. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Staff Chat");
    8.  
    9. } else {
    10. list.remove(player.getName());
    11. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Public Chat");
    12. }
    13. }
    14. }
    15.  
    16. if(args.length == 1)
    17. {
    18. if(args[0].equalsIgnoreCase("add"))
    19. {
    20. if(player.hasPermission("legacy.acadd"))
    21. {
    22. player.sendMessage(ChatColor.GRAY+ "/ac add <player>");
    23. }
    24. }
    25.  
    26. if(args[0].equalsIgnoreCase("kick"))
    27. {
    28. if(player.hasPermission("legacy.ackick"))
    29. {
    30. player.sendMessage(ChatColor.GRAY+ "/ac kick <player>");
    31. }
    32. }
    33. }
    34.  
    35. if(args.length == 2)
    36. {
    37. if(args[0].equalsIgnoreCase("add"))
    38. {
    39. if(player.hasPermission("legacy.acadd"))
    40. {
    41. Player target = Bukkit.getServer().getPlayer(args[1]);
    42. if(target != null)
    43. {
    44. if(!list.contains(target.getName()))
    45. {
    46. list.add(target.getName());
    47. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Staff Chat");
    48.  
    49. } else {
    50. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is already in the staff channel.");
    51. }
    52. } else {
    53. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    54. }
    55. }
    56. }
    57.  
    58. if(args[0].equalsIgnoreCase("kick"))
    59. {
    60. if(player.hasPermission("legacy.ackick"))
    61. {
    62. Player target = Bukkit.getServer().getPlayer(args[1]);
    63. if(target != null)
    64. {
    65. if(list.contains(target.getName()))
    66. {
    67. list.remove(target.getName());
    68. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Public Chat");
    69.  
    70. } else {
    71. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is not in the staff channel.");
    72. }
    73. } else {
    74. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    75. }
    76. }
    77. }
    78. }
    79. }
    80. return false;
    81. }
    82.  
    83. @EventHandler
    84. public void onChat(AsyncPlayerChatEvent e)
    85. {
    86. Player p = e.getPlayer();
    87. if(list.contains(p.getName()))
    88. {
    89. for(Player all : Bukkit.getServer().getOnlinePlayers())
    90. {
    91. if(list.contains(all.getName()))
    92. {
    93. e.setCancelled(false);
    94. all.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "StaffChat" + ChatColor.GOLD + "] " + p.getDisplayName() + ChatColor.GRAY+ ": " + ChatColor.GRAY + e.getMessage().replaceAll("&([l-ok0-8k9a-f])", "§$1"));
    95. }
    96. }
    97. }
    98. }
    99. }
    100.  
    101.  
    102.  
     
  2. Offline

    ChronicNinjaz

    Benpat9
    Code:java
    1. if(player.hasPermission("Your permission...")){
    2. event.setCancelled(true);
    3. for(String s: yourList){
    4. Player p = Bukkit.getPlayer(s);
    5. if(p.hasPermission("recive.chat")){
    6. p.sendMessage(player.getName() + ": " + event.getMessage());
    7. }
    8. }
    9. }



    that should work, you will need to listen for the chat event (AsyncPlayerChatEvent) and just paste in the code up there, also add your permission and your list and it should all be fine :) enjoy.
     
  3. Offline

    Benpat9

    ChronicNinjaz

    Sorry this could be a stupid mistake (and im "fairly" new) but in this code the 'player'.hasPermission.... and the 'event'.setcanceled (Just the bits in ' ' ) cannot be resolved any idea why, again thanks in advance :p

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onEvent(AsyncPlayerChatEvent e){
    4. if(player.hasPermission("legacy.aclisten")){
    5. event.setCancelled(true);
    6. for(String s: list){
    7. Player p = Bukkit.getPlayer(s);
    8. if(p.hasPermission("legacy.aclisten")){
    9. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "StaffChat" + ChatColor.GOLD + "] " + p.getDisplayName() + ChatColor.GRAY+ ": " + ChatColor.GRAY + e.getMessage().replaceAll("&([l-ok0-8k9a-f])", "§$1"));
    10. }
    11. }
    12. }
    13. }
    14. }
    15.  
     
  4. Offline

    ZodiacTheories

    Benpat9

    Code:java
    1. event.setCancelled(true);


    Code:java
    1. AsyncPlayerChatEvent e


    +

    Code:java
    1. if(p.hasPermission


    Code:java
    1. if(player.hasPermission


    Do you know Java? (No offence)

    Also, you are checking if they have the permission and if they do then you cancel the event, then it says if the player has that same permission the staffchat works.
     
  5. Offline

    Benpat9

    ZodiacTheories
    that didnt help it still has the errors.... and btw this is my second plugin so calm down

    ZodiacTheories
    ChronicNinjaz
    I still need help with this :(

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

    ZodiacTheories

    Do you know Java? You could make a million basic plugins without knowing Java. It is when you start advancing as a programmer that it is important to know Java.

    Code?
     
  7. Offline

    ChronicNinjaz

    Benpat9

    Sorry, I logged off.

    Ok so your problem is that "player" is not being defined, so you would do
    Code:java
    1. Player player = event.getPlayer();


    And the reason why
    Code:java
    1. event.setCancelled(true);
    is not working is because you
    put
    Code:java
    1. AsyncPlayerChatEvent e
    so you will need to change the (e) to (event) :) Hope that helps! also ZodiacTheories , People come here because they don't understand/know java so cut them some slack, would be much appreciated. Thanks.
     
  8. Offline

    ZodiacTheories

    ChronicNinjaz

    People that come here that don't understand/know Java should learn Java then come back here. Your comment is invalid because I have seen SO many posts that go like:

    IDontKnowJava: Hi, I am making a plugin so when u right click something a player will get a message. plz give me some code.

    IKnowJava: No, we will not give you code, we will help you learn though.

    I respect Benpat9 for actually attempting his plugin first rather than asking for code. However, I do not respect him because he should learn Java first before attempting Bukkit. It is a bit like Latin and Italian, it is easier to learn Italian if you know Latin.

    If you learn Java before Bukkit, it saves so much time.
    .
     
    es359 and Acer_Mortem like this.
  9. Offline

    ChronicNinjaz

    ZodiacTheories I agree with you to an extent, but some comments you made were not necessary, the giving code thing... it depends... as he said he is new to coding and Benpat9 he is right, you should start with Java and not Bukkit because it will be better in the long run but it is up to you, a massive majority of Bukkit programmers have learnt Java before they started working with Bukkit, I would recommend you go away and learn at least the basics of Java before you start working with anything else because it will be so much easier to understand and you will have a better understanding on what you are working with, but that is up to completely up to you.

    Back to Zodiac, I know where your coming from but sometimes it is good to give the code to them to refer to, It will help them understand how things work, and tbh... when you have a problem, do you not google it ? if NO ONE at all help each other with bits of code, would you have been able to make the plugin as fast ? and would you understand as much as you do right now ? the truth is... no because everyone knows one thing that someone else doesn't.

    Once again, this is the forums were we as better developers help the new people, and really... if your going to give people a hard time then I would say just keep away form here because %90 of people who post here ask for code examples, hope I didn't offend you, wasn't my intention, just trying to make sure everyone on here goes away with something good :) Have a grate day!

    Hope you understood the code Benpat9 , try take my advise and learn Java before starting Bukkit!
     
  10. Offline

    ZodiacTheories

    ChronicNinjaz

    You see, that is my problem,
    I am trying to stop that by eliminating (not physically) the users that do not know Java and welcoming them when they learn it :p
     
  11. Offline

    ChronicNinjaz

    ZodiacTheories Why not put them on to the right path... don't eliminate them and put them down, help them and get them started!

    Also, sorry to say but this conversation has to stop here, I need to get back to work :)
     
  12. Offline

    ZodiacTheories

    ChronicNinjaz

    Well, by eliminate them I mean put them on the right path, I'm not that mean
     
  13. Offline

    Benpat9

    ChronicNinjaz
    ChronicNinjaz ZodiacTheories
    i agree with ChronicNinjaz cos people who come here are new and they come here MOSTLY because they have an error or dont know how to do a specific bit of code so they ask the people that do know. but i appreciate that SOME people come here and just ask for the entire plugin (I also did watch a basic java tutorial i just simply missed the error by human error xD) and by the way the plugin doesnt work still D: heres the whole plugin again D; (When in staff chat and i talk it just goes to public still...)



    Code:java
    1. package me.benpat9;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13. import org.bukkit.permissions.Permission;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Legacy extends JavaPlugin{
    18.  
    19. ArrayList<String> list = new ArrayList<String> ();
    20.  
    21. public Permission legacycreative = new Permission("Legacy.creative");
    22. public Permission legacysurvival = new Permission("Legacy.survival");
    23. public Permission legacychallenge = new Permission("Legacy.damageon");
    24. public Permission legacychallengeaccept = new Permission("Legacy.damageoff");
    25. public Permission legacyactoggle = new Permission("Legacy.actoggle");
    26. public Permission legacyacadd = new Permission("Legacy.acadd");
    27. public Permission legacyackick = new Permission("Legacy.acadd");
    28. public Permission legacyvitalog = new Permission("Legacy.vitalog");
    29. public Permission legacyaclisten = new Permission("Legacy.aclisten");
    30.  
    31. @Override
    32. public void onEnable() {
    33. new JoinListener(this);
    34. new QuitListener(this);
    35. new NewPlayer(this);
    36. PluginManager pm = getServer().getPluginManager();
    37. pm.addPermission(legacycreative);
    38. pm.addPermission(legacysurvival);
    39. pm.addPermission(legacychallengeaccept);
    40. pm.addPermission(legacychallenge);
    41. pm.addPermission(legacyactoggle);
    42. pm.addPermission(legacyacadd);
    43. pm.addPermission(legacyackick);
    44. pm.addPermission(legacyvitalog);
    45. pm.addPermission(legacyaclisten);
    46.  
    47.  
    48. }
    49. @Override
    50. public void onDisable() {
    51.  
    52. }
    53.  
    54. @SuppressWarnings("deprecation")
    55. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    56. Player player = (Player) sender;
    57. Player vitalog = Bukkit.getPlayerExact("Vitalog");
    58.  
    59. if(commandLabel.equalsIgnoreCase("c")){
    60. if(player.hasPermission("Legacy.creative")) {
    61. if(args.length == 0){
    62. //c = 0 args /c NAME = 1 args
    63. player.setGameMode(GameMode.CREATIVE);
    64. player.sendMessage(ChatColor.GREEN + "GameMode Set To Creative!");
    65. }else if(args.length == 1){
    66. if(player.getServer().getPlayer(args[0]) !=null){
    67. Player targetPlayer = player.getServer().getPlayer(args[0]);
    68. targetPlayer.setGameMode(GameMode.CREATIVE);
    69. targetPlayer.sendMessage(ChatColor.GREEN + "GameMode Set To Creative!");
    70. player.sendMessage(ChatColor.GRAY + targetPlayer.getName() + "'s" + ChatColor.GREEN + " GameMode Set To Creative!");
    71. }
    72. }else{
    73. player.sendMessage(ChatColor.RED + "Player Not Online");
    74. }
    75. }
    76. if(!player.hasPermission("Legacy.creative")) {
    77. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    78. }
    79.  
    80.  
    81.  
    82. return false;
    83.  
    84. }
    85. else if(commandLabel.equalsIgnoreCase("s")){
    86. if(player.hasPermission("Legacy.survival")) {
    87. if(args.length == 0){
    88. //c = 0 args /c NAME = 1 args
    89. player.setGameMode(GameMode.SURVIVAL);
    90. player.sendMessage(ChatColor.GREEN + "GameMode Set To Survival!");
    91. }else if(args.length == 1){
    92. if(player.getServer().getPlayer(args[0]) !=null){
    93. Player targetPlayer = player.getServer().getPlayer(args[0]);
    94. targetPlayer.setGameMode(GameMode.SURVIVAL);
    95. targetPlayer.sendMessage(ChatColor.GREEN + "GameMode Set To Survival!");
    96. player.sendMessage(ChatColor.GRAY + targetPlayer.getName() + "'s" + ChatColor.GREEN + " GameMode Set To Survival!");
    97. }
    98. }else{
    99. player.sendMessage(ChatColor.RED + "Player Not Online");
    100. }
    101. }
    102. if(!player.hasPermission("Legacy.creative")) {
    103. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    104. }
    105. return false;
    106. }
    107.  
    108. else if(commandLabel.equalsIgnoreCase("challenge")){
    109. if(player.hasPermission("Legacy.challenge")) {
    110. if(args.length == 1){
    111. if(player.getServer().getPlayer(args[0]) !=null){
    112. Player targetPlayer = player.getServer().getPlayer(args[0]);
    113. targetPlayer.sendMessage(ChatColor.GRAY + player.getName() + " Has Challenged You To A Fight type " + ChatColor.DARK_GRAY + "/challengeaccept" + ChatColor.GRAY + "To accept");
    114. player.sendMessage(ChatColor.GREEN + "You Have Challenged " + targetPlayer.getName() + " And Have Been Warped To PVP");
    115. player.chat("/pvp");
    116. }
    117. }
    118. else{
    119. player.sendMessage(ChatColor.RED + "Player Not Online");
    120. }
    121. }
    122. if(!player.hasPermission("Legacy.challenge")) {
    123. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    124. }
    125. if(args.length == 0){
    126. player.sendMessage(ChatColor.RED + "Usage /challenge PLAYER");
    127. }
    128. return false;
    129. }
    130. else if(commandLabel.equalsIgnoreCase("challengeAccept")){
    131. if(player.hasPermission("Legacy.challengeaccept")) {
    132. player.sendMessage(ChatColor.GREEN + "You Have Accepted Your Challenge");
    133. player.chat("/pvp");
    134. }
    135.  
    136. if(!player.hasPermission("Legacy.challengeaccept")) {
    137. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    138. }
    139.  
    140.  
    141.  
    142.  
    143.  
    144.  
    145. return true;
    146. }
    147. else if(commandLabel.equalsIgnoreCase("pepper")){
    148. if(player.hasPermission("Legacy.pepper")) {
    149. player.sendMessage(ChatColor.GREEN + "77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
    150. }
    151.  
    152. if(!player.hasPermission("Legacy.challengeaccept")) {
    153. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    154. }
    155. return true;
    156. }
    157. else if(commandLabel.equalsIgnoreCase("isvitalogonline")){
    158. if(player.hasPermission("Legacy.vitalog")) {
    159. if(vitalog.isOnline())
    160. player.sendMessage(ChatColor.GREEN + "Vitalog Is Online");
    161.  
    162. }
    163. if(!player.hasPermission("Legacy.vitalog")) {
    164. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    165. }
    166.  
    167. if(!vitalog.isOnline()){
    168. player.sendMessage(ChatColor.RED + "Vitalog Is Offline");
    169.  
    170. }
    171. return true;
    172. }else {
    173. if(cmd.getName().equalsIgnoreCase("ac")){
    174. if(args.length == 0){
    175. if(player.hasPermission("legacy.actoggle")){;
    176. if(!list.contains(player.getName())){
    177. list.add(player.getName());
    178. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Staff Chat");
    179.  
    180. } else {
    181. list.remove(player.getName());
    182. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Public Chat");
    183. }
    184. }
    185. }
    186.  
    187. if(args.length == 1)
    188. {
    189. if(args[0].equalsIgnoreCase("add"))
    190. {
    191. if(player.hasPermission("legacy.acadd"))
    192. {
    193. player.sendMessage(ChatColor.GRAY+ "/ac add <player>");
    194. }
    195. }
    196.  
    197. if(args[0].equalsIgnoreCase("kick"))
    198. {
    199. if(player.hasPermission("legacy.ackick"))
    200. {
    201. player.sendMessage(ChatColor.GRAY+ "/ac kick <player>");
    202. }
    203. }
    204. }
    205.  
    206. if(args.length == 2)
    207. {
    208. if(args[0].equalsIgnoreCase("add"))
    209. {
    210. if(player.hasPermission("legacy.acadd"))
    211. {
    212. Player target = Bukkit.getServer().getPlayer(args[1]);
    213. if(target != null)
    214. {
    215. if(!list.contains(target.getName()))
    216. {
    217. list.add(target.getName());
    218. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Staff Chat");
    219.  
    220. } else {
    221. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is already in the staff channel.");
    222. }
    223. } else {
    224. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    225. }
    226. }
    227. }
    228.  
    229. if(args[0].equalsIgnoreCase("kick"))
    230. {
    231. if(player.hasPermission("legacy.ackick"))
    232. {
    233. Player target = Bukkit.getServer().getPlayer(args[1]);
    234. if(target != null)
    235. {
    236. if(list.contains(target.getName()))
    237. {
    238. list.remove(target.getName());
    239. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Public Chat");
    240.  
    241. } else {
    242. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is not in the staff channel.");
    243. }
    244. } else {
    245. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    246. }
    247. }
    248. }
    249. }
    250. }
    251. return false;
    252. }
    253. }
    254.  
    255.  
    256. @SuppressWarnings("deprecation")
    257. @EventHandler
    258. public void onEvent(AsyncPlayerChatEvent event){
    259. Player player = event.getPlayer();
    260. if(player.hasPermission("legacy.aclisten")){
    261. event.setCancelled(true);
    262. for(String s: list){
    263. Player p = Bukkit.getPlayer(s);
    264. if(p.hasPermission("legacy.aclisten")){
    265. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "StaffChat" + ChatColor.GOLD + "] " + p.getDisplayName() + ChatColor.GRAY+ ": " + ChatColor.GRAY + event.getMessage().replaceAll("&([l-ok0-8k9a-f])", "§$1"));
    266. }
    267. }
    268. }
    269. }
    270. }
    271.  
    272.  


    Im sorry for asking again and again but im stumped D:
     
  14. Offline

    ZodiacTheories

  15. Offline

    theguynextdoor

    Maybe you should let your main class implement Listener and then register it. I feel that could help here.
     
  16. Offline

    ZodiacTheories

    Benpat9

    Read through your code carefully and look at your logic
     
  17. Offline

    Benpat9

  18. Offline

    ZodiacTheories

    Benpat9

    Because I am trying to get you to think by yourself here.
    Look at lines 66 and 67
     
  19. Offline

    Benpat9

  20. Offline

    theguynextdoor

    I'm sorry but I gave you a response, I don't see why you can't read it, follow it and everything is just that little bit better.
     
    es359 likes this.
  21. Offline

    ZodiacTheories

  22. Offline

    Benpat9

    ZodiacTheories theguynextdoor
    im not quite sure how to do that in this circumstance, i have done a join listener, but would this mean i would have to move the all the "ac" into another class?
     
  23. Offline

    theguynextdoor

    Well, what did you do with your 'join listener' that made it a listener? did your class implement a specific interface?(I think it might have). Could your main class do something similar? (I think it can). Did you then have to use a method in your onEnable (or by the looks of things in the constructor for your listeners) which resembled 'registerEvents' from the PluginManager class?

    If I told you any more than what I have, then I would be just giving you code, and I don't plan to do that.
     
    xTigerRebornx likes this.
  24. Offline

    Benpat9

    Ok i have put the entire thing into a new class (Thats The Only way i know how to import a listener) but i fear i have broken it even more all the plugin works except the staff chat but in game i can no longer talk in chat and the commands dont send a message anymore, ive had a look through both classes but i dont see any obvious mistake although i think i may have copied it in wrong or should have implemented the listener into my main.

    Main Class
    Code:java
    1. package me.benpat9;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.permissions.Permission;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Legacy extends JavaPlugin{
    16.  
    17. ArrayList<String> list = new ArrayList<String> ();
    18.  
    19. public Permission legacycreative = new Permission("Legacy.creative");
    20. public Permission legacysurvival = new Permission("Legacy.survival");
    21. public Permission legacychallenge = new Permission("Legacy.damageon");
    22. public Permission legacychallengeaccept = new Permission("Legacy.damageoff");
    23. public Permission legacyactoggle = new Permission("Legacy.actoggle");
    24. public Permission legacyacadd = new Permission("Legacy.acadd");
    25. public Permission legacyackick = new Permission("Legacy.acadd");
    26. public Permission legacyvitalog = new Permission("Legacy.vitalog");
    27. public Permission legacyaclisten = new Permission("Legacy.aclisten");
    28.  
    29.  
    30.  
    31. @Override
    32. public void onEnable() {
    33. new JoinListener(this);
    34. new QuitListener(this);
    35. new NewPlayer(this);
    36. new StaffChat(this);
    37. PluginManager pm = getServer().getPluginManager();
    38. pm.addPermission(legacycreative);
    39. pm.addPermission(legacysurvival);
    40. pm.addPermission(legacychallengeaccept);
    41. pm.addPermission(legacychallenge);
    42. pm.addPermission(legacyactoggle);
    43. pm.addPermission(legacyacadd);
    44. pm.addPermission(legacyackick);
    45. pm.addPermission(legacyvitalog);
    46. pm.addPermission(legacyaclisten);
    47.  
    48.  
    49. }
    50. @Override
    51. public void onDisable() {
    52.  
    53. }
    54.  
    55. @SuppressWarnings("deprecation")
    56. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    57. Player player = (Player) sender;
    58. Player vitalog = Bukkit.getPlayerExact("Vitalog");
    59.  
    60. if(commandLabel.equalsIgnoreCase("c")){
    61. if(player.hasPermission("Legacy.creative")) {
    62. if(args.length == 0){
    63. //c = 0 args /c NAME = 1 args
    64. player.setGameMode(GameMode.CREATIVE);
    65. player.sendMessage(ChatColor.GREEN + "GameMode Set To Creative!");
    66. }else if(args.length == 1){
    67. if(player.getServer().getPlayer(args[0]) !=null){
    68. Player targetPlayer = player.getServer().getPlayer(args[0]);
    69. targetPlayer.setGameMode(GameMode.CREATIVE);
    70. targetPlayer.sendMessage(ChatColor.GREEN + "GameMode Set To Creative!");
    71. player.sendMessage(ChatColor.GRAY + targetPlayer.getName() + "'s" + ChatColor.GREEN + " GameMode Set To Creative!");
    72. }
    73. }else{
    74. player.sendMessage(ChatColor.RED + "Player Not Online");
    75. }
    76. }
    77. if(!player.hasPermission("Legacy.creative")) {
    78. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    79. }
    80.  
    81.  
    82.  
    83. return false;
    84.  
    85. }
    86. else if(commandLabel.equalsIgnoreCase("s")){
    87. if(player.hasPermission("Legacy.survival")) {
    88. if(args.length == 0){
    89. //c = 0 args /c NAME = 1 args
    90. player.setGameMode(GameMode.SURVIVAL);
    91. player.sendMessage(ChatColor.GREEN + "GameMode Set To Survival!");
    92. }else if(args.length == 1){
    93. if(player.getServer().getPlayer(args[0]) !=null){
    94. Player targetPlayer = player.getServer().getPlayer(args[0]);
    95. targetPlayer.setGameMode(GameMode.SURVIVAL);
    96. targetPlayer.sendMessage(ChatColor.GREEN + "GameMode Set To Survival!");
    97. player.sendMessage(ChatColor.GRAY + targetPlayer.getName() + "'s" + ChatColor.GREEN + " GameMode Set To Survival!");
    98. }
    99. }else{
    100. player.sendMessage(ChatColor.RED + "Player Not Online");
    101. }
    102. }
    103. if(!player.hasPermission("Legacy.creative")) {
    104. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    105. }
    106. return false;
    107. }
    108.  
    109. else if(commandLabel.equalsIgnoreCase("challenge")){
    110. if(player.hasPermission("Legacy.challenge")) {
    111. if(args.length == 1){
    112. if(player.getServer().getPlayer(args[0]) !=null){
    113. Player targetPlayer = player.getServer().getPlayer(args[0]);
    114. targetPlayer.sendMessage(ChatColor.GRAY + player.getName() + " Has Challenged You To A Fight type " + ChatColor.DARK_GRAY + "/challengeaccept" + ChatColor.GRAY + "To accept");
    115. player.sendMessage(ChatColor.GREEN + "You Have Challenged " + targetPlayer.getName() + " And Have Been Warped To PVP");
    116. player.chat("/pvp");
    117. }
    118. }
    119. else{
    120. player.sendMessage(ChatColor.RED + "Player Not Online");
    121. }
    122. }
    123. if(!player.hasPermission("Legacy.challenge")) {
    124. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    125. }
    126. if(args.length == 0){
    127. player.sendMessage(ChatColor.RED + "Usage /challenge PLAYER");
    128. }
    129. return false;
    130. }
    131. else if(commandLabel.equalsIgnoreCase("challengeAccept")){
    132. if(player.hasPermission("Legacy.challengeaccept")) {
    133. player.sendMessage(ChatColor.GREEN + "You Have Accepted Your Challenge");
    134. player.chat("/pvp");
    135. }
    136.  
    137. if(!player.hasPermission("Legacy.challengeaccept")) {
    138. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    139. }
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146. return true;
    147. }
    148. else if(commandLabel.equalsIgnoreCase("pepper")){
    149. if(player.hasPermission("Legacy.pepper")) {
    150. player.sendMessage(ChatColor.GREEN + "77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
    151. }
    152.  
    153. if(!player.hasPermission("Legacy.challengeaccept")) {
    154. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    155. }
    156. return true;
    157. }
    158. else if(commandLabel.equalsIgnoreCase("isvitalogonline")){
    159. if(player.hasPermission("Legacy.vitalog")) {
    160. if(vitalog.isOnline())
    161. player.sendMessage(ChatColor.GREEN + "Vitalog Is Online");
    162.  
    163. }
    164. if(!player.hasPermission("Legacy.vitalog")) {
    165. player.sendMessage(ChatColor.RED + "You Do Not Have Permission");
    166. }
    167.  
    168. if(!vitalog.isOnline()){
    169. player.sendMessage(ChatColor.RED + "Vitalog Is Offline");
    170.  
    171. }
    172. return false;
    173. }
    174. return false;
    175. }
    176. }
    177.  
    178.  
    179.  



    Staff Chat
    Code:java
    1. package me.benpat9;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13.  
    14. public class StaffChat implements Listener {
    15. ArrayList<String> list = new ArrayList<String> ();
    16.  
    17. public StaffChat(Legacy plugin) {
    18. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    19. }
    20.  
    21. @SuppressWarnings("deprecation")
    22. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    23. Player player = (Player) sender;
    24.  
    25. if(cmd.getName().equalsIgnoreCase("ac")){
    26. if(args.length == 0){
    27. if(player.hasPermission("legacy.actoggle")){;
    28. if(!list.contains(player.getName())){
    29. list.add(player.getName());
    30. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Staff Chat");
    31.  
    32. } else {
    33. list.remove(player.getName());
    34. player.sendMessage(ChatColor.GRAY + "You Are Now Talking In " + ChatColor.RED + "Public Chat");
    35. }
    36. }
    37. }
    38.  
    39. if(args.length == 1)
    40. {
    41. if(args[0].equalsIgnoreCase("add"))
    42. {
    43. if(player.hasPermission("legacy.acadd"))
    44. {
    45. player.sendMessage(ChatColor.GRAY+ "/ac add <player>");
    46. }
    47. }
    48.  
    49. if(args[0].equalsIgnoreCase("kick"))
    50. {
    51. if(player.hasPermission("legacy.ackick"))
    52. {
    53. player.sendMessage(ChatColor.GRAY+ "/ac kick <player>");
    54. }
    55. }
    56. }
    57.  
    58. if(args.length == 2)
    59. {
    60. if(args[0].equalsIgnoreCase("add"))
    61. {
    62. if(player.hasPermission("legacy.acadd"))
    63. {
    64. Player target = Bukkit.getServer().getPlayer(args[1]);
    65. if(target != null)
    66. {
    67. if(!list.contains(target.getName()))
    68. {
    69. list.add(target.getName());
    70. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Staff Chat");
    71.  
    72. } else {
    73. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is already in the staff channel.");
    74. }
    75. } else {
    76. player.sendMessage(ChatColor.GRAY+ "[StaffChat] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    77. }
    78. }
    79. }
    80.  
    81. if(args[0].equalsIgnoreCase("kick"))
    82. {
    83. if(player.hasPermission("legacy.ackick"))
    84. {
    85. Player target = Bukkit.getServer().getPlayer(args[1]);
    86. if(target != null)
    87. {
    88. if(list.contains(target.getName()))
    89. {
    90. list.remove(target.getName());
    91. player.sendMessage(ChatColor.GRAY + target.getName() + " Is Now Talking In " + ChatColor.RED + "Public Chat");
    92.  
    93. } else {
    94. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is not in the staff channel.");
    95. }
    96. } else {
    97. player.sendMessage(ChatColor.GRAY+ "[Legacy] " + ChatColor.RED+ "This player is either not online, or does not exist.");
    98. }
    99. }
    100. }
    101. }
    102.  
    103. return true;
    104. }
    105. return false;
    106. }
    107.  
    108.  
    109. @SuppressWarnings("deprecation")
    110. @EventHandler
    111. public void onEvent(AsyncPlayerChatEvent event){
    112. Player player = event.getPlayer();
    113. if(player.hasPermission("legacy.aclisten")){
    114. event.setCancelled(true);
    115. for(String s: list){
    116. Player p = Bukkit.getPlayer(s);
    117. if(p.hasPermission("legacy.aclisten")){
    118. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "StaffChat" + ChatColor.GOLD + "] " + p.getDisplayName() + ChatColor.GRAY+ ": " + ChatColor.GRAY + event.getMessage().replaceAll("&([l-ok0-8k9a-f])", "§$1"));
    119. }
    120. }
    121. }
    122. }
    123. }
    124.  
    125.  
     
  25. Offline

    theguynextdoor

    Because that totally isn't what I was saying to do this whole time .... To solve your initial problem, all you had to do what let your main class implement Listener and then register the main class. That is what I was telling you.
     
    ZodiacTheories and xTigerRebornx like this.
  26. Offline

    Benpat9

    First Off SORRY i dont know much about coding java... this is only my second plugin and im still new to it.
    but if i add public class Legacy implements Listener{ i get the error that it is already defined (i think by public class Legacy extends JavaPlugin{) ... yet again im probaly missing something obvious...
     
  27. Offline

    CraftCreeper6

    Benpat9
    You simply check if the player is in some sort of an arraylist like this:
    Code:java
    1. for(Player all : Bukkit.getOnlinePlayers())
    2. {
    3. if(YOUR-ARRAY-LIST.contains(all.getName()))
    4. {
    5. all.sendMessage(e.getMessage());
    6.  
    7. }
    8. }


    That will send everyone that is in an ArrayList instead of the whole server :)

    EDIT: Sorry, misread. :p
     
  28. Offline

    theguynextdoor

    To allow your main class to implement Listener all you need to do is
    Code:java
    1. public class Legacy extends JavaPlugin implements Listener {


    I don't really care how many plugins you have made. You could have made 1, 5 or 50, yet I would answer it the same. If you don't know much about coding Java, then you should probably work on that. If you didn't understand what I was saying, then I would have probably replied with more questions until you led yourself to the answer. Although most people on these forums are assholes (including myself) when it comes to new people coding with little java experience, it is because 95% of the problems on these forums are not bukkit related and could be fixed by people having a sound foundation in Java.

    In your case, It was more of a bukkit issue, hence why I didn't just flat out tell you to learn java.

    I shall also give you a few comments on your code for you to think about...
    1) You don't need to define all those permissions at the top of you plugin, and you don't need to add them in your onEnable()

    2) You should add an access modifier to your ArrayList (It should probably be private)

    3) Instead of using commandLabel, you should use cmd.getName() The use of commandLabel is an old practice and is one just continued by newbies who have watched tutorials done by other newbies who half of which don't deserve to teach (But that is besides the point).

    4) permissions tend to be lower case

    5) In your code for your command, you do something along the lines of
    Code:
    if (player.hasPermission("blablabla") {
    // Do stuff here
    }
     
    if (!player.hasPermission("blablabla") {
    // send them message
    }
    Instead, it would be much easier to do this...
    Code:
    if (player.hasPermission("blablabla") {
    // Do stuff
    } else {
    // send them message
    }
    Because this way, if they don't have the perm, then it is perfectly safe to say they don't, so just send them the message saying they dont have the permission.

    6) You cast the sender to a player, but you don't actually check if the sender is an instanceof a Player

    7) (This is more a java issue than bukkit) In your event, you are just checking if the player sending the message has the permission to 'listen' to staff chat. You never check if they are in the list and have it toggled. Therefore, if someone has the permission to listen to the chat, you are making every message they send a message in staff chat. So in your event, you need to replace the first permission check, with a check that the list (in your plugins main class!!) contains that players name (Although you should switch to storing the player's UUID instead of their name, in response to recent (planned) changes regarding the ability for a player to change their name.
     
  29. Offline

    hofma100

  30. Offline

    ChronicNinjaz

    hofma100 He really shouldn't... He needs to learn to think for him self, these guys have given him a VERY clear understanding of what needs to be done and if he cant follow there steps then he should think about going to the basics and starting again because copying code will not help at all, it will only make lazy...
     
Thread Status:
Not open for further replies.

Share This Page