Warning Very Noob Question! Commands

Discussion in 'Plugin Development' started by AKS JEFFREY, Apr 28, 2014.

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

    AKS JEFFREY

    So i want it if someone does /blacklist {The Player Name} the player gets ip banned the code i have so far

    Main Class
    Code:java
    1. package com.weebly.aksjeffreydonation;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.AsyncPlayerChatEvent;
    8. import org.bukkit.plugin.Plugin;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. import java.util.Calendar;
    12. import java.util.Date;
    13. import java.util.HashMap;
    14.  
    15. public class MainClass extends JavaPlugin implements Listener{
    16.  
    17. private static Plugin instance;
    18. int n = (int)(Math.random() * 8 + 1);
    19. Player sender;
    20. private HashMap<String, Date> lastMsg = new HashMap<String, Date>();
    21. private final float timeDelay = 3;
    22. @Override
    23. public void onEnable() {
    24. getServer().getPluginManager().registerEvents(this, this);
    25. getLogger().info(ChatColor.BLUE +"ChatBot Has Started!");
    26. sender.sendMessage(ChatColor.GREEN +"N Server Is Controling This Server!");
    27. getLogger().info(ChatColor.RED +"Warning: Server should have 2gb or 1gb min!");
    28. getCommand("basic").setExecutor(new Commands());
    29. instance = this;
    30. }
    31.  
    32.  
    33.  
    34.  
    35. @EventHandler
    36. public void onChat(AsyncPlayerChatEvent event){
    37. Player p = event.getPlayer();
    38. if(lastMsg.get(p.getName()) != null){
    39. Date last = lastMsg.remove(p.getName());
    40. Date now = Calendar.getInstance().getTime();
    41. long diffMillis = now.getTime() - last.getTime();
    42. long diffSecs = (diffMillis) / 1000L;
    43. if(diffSecs < timeDelay){
    44. onChatTooSoon(event.getPlayer());
    45. event.setCancelled(true);
    46. }
    47. } else {
    48. lastMsg.put(p.getName(), Calendar.getInstance().getTime());
    49. }
    50. }
    51.  
    52. private static void onChatTooSoon(Player player){
    53. player.getPlayer().kickPlayer("Why Would You Spam! There's A Bot On This Server now don't spam agian!");
    54. }
    55. public static Plugin getInstance() {
    56. return instance;
    57. }
    58. }


    My Commands Class
    Code:java
    1. package com.weebly.aksjeffreydonation;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.plugin.Plugin;
    7.  
    8. public class Commands implements CommandExecutor {
    9.  
    10. @SuppressWarnings("unused")
    11. private Plugin instanxce = MainClass.getInstance(); // pointer to your main class, not required if you don't need methods from the main class
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    15. if (cmd.getName().equalsIgnoreCase("blacklist")) { // If the player typed /basic then do the following...
    16. // doSomething
    17. return true;
    18. } //If this has happened the function will return true.
    19. // If this hasn't happened the value of false will be returned.
    20. return false;
    21. }
    22. }
    23.  

    So what do i do for the commands class?
     
  2. Offline

    TGRHavoc

    AKS JEFFREY
    You need to set the command "blacklist" to be executed by the command executor class. In the example you have given you are setting the command "basic" to the executor..
     
  3. Offline

    AKS JEFFREY

    TGRHavoc K i will fix that

    TGRHavoc So for example a mod come on the server a guy does something really really bad that he wants to IP ban him so he would typed /blacklist (Player Name) But for the world what would show your be banning the (Player name) and not the mod

    TGRHavoc are u here?

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

    TGRHavoc

    AKS JEFFREY
    Bukkit.getPlayer(String playerName); .. I think that's what you're asking.. Right?
     
  5. Offline

    AKS JEFFREY

    TGRHavoc so for that Bukkit.getPlayer(String playerName); were would i put that e.g
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("blacklist" + Bukkit.getPlayer(String playerName);)) { // If the player typed /blacklist then do the following...
    4.  
    5. return true;
    6. } //If this has happened the function will return true.
    7. // If this hasn't happened the value of false will be returned.
    8. return false;
    9. }
    10. }
    11.  


    TGRHavoc Bukkit.getPlayer(String playerName); would get the player name but if i do it like that ^ wouldn't it get the player name off who typed the command?

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

    TGRHavoc

    AKS JEFFREY
    Sorry for the delayed responses (At college). The code that I gave you was just a pseudo-code, replace "String playerName" with the argument from the command (Probably arg[0]).
     
  7. Offline

    MOMOTHEREAL

    The String[] args is an Array of String containing everything after the command, so the arguments. You can get the first argument using args[0]
     
  8. Offline

    AKS JEFFREY

    MOMOTHEREAL TGRHavoc so heres a code my teacher game me
    Code:java
    1. @SuppressWarnings("unused")
    2. private Plugin instanxce = MainClass.getInstance(); // pointer to your main class, not required if you don't need methods from the main class
    3.  
    4. @SuppressWarnings("deprecation")
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String label,String[] args){
    7.  
    8. Player t = Bukkit.getServer().getPlayer(args[0]);
    9.  
    10. if(cmd.getName().equalsIgnoreCase("ban")){
    11. if(args.length == 0){
    12. sender.sendMessage(ChatColor.RED + "Please tell me the player name");
    13. return true;
    14.  
    15. }
    16. if(t == null){
    17. sender.sendMessage(ChatColor.RED + "That player is not online!");
    18. return true;
    19. }
    20. String msg = "";
    21. for(int i = 1; i < args.length; i++){
    22. msg += args[i] + " ";
    23. }
    24. t.kickPlayer(ChatColor.GRAY + "You have been banned for " + msg);
    25. t.setBanned(true);
    26. return true;
    27.  
    28.  
    29. }
    30.  
    31.  
    32.  
    33.  
    34.  
    35. return true;
    36. }[/i]


    however it may not be right since he doesn't know Bukkit API
     
  9. Offline

    foldagerdk

    AKS JEFFREY
    I don't have time to look over the code, but are you using an IDE? Because you should notice errors like "instanxce" (which should obviously be "instance")..
     
    AKS JEFFREY likes this.
  10. Offline

    AKS JEFFREY

    foldagerdk no sure why but my eclipse doesn't show that maybe its a variable
     
  11. Offline

    foldagerdk

    Why on earth would you use "instanxce" as a variable? :p
    I can check the code soon if no one else does.
     
  12. Offline

    AoH_Ruthless

  13. Offline

    AKS JEFFREY

  14. Offline

    AoH_Ruthless

    AKS JEFFREY
    Do another if check - if (sender.hasPermission("your.permission") { // command }
     
  15. Offline

    foldagerdk


    AKS JEFFREY And remember to include the perm in your plugin.yml
     
  16. Offline

    AoH_Ruthless

    foldagerdk

    You dont need to add it in the plugin.yml
     
  17. Offline

    foldagerdk

    I am aware of that. :)
     
Thread Status:
Not open for further replies.

Share This Page