PlayerCommandPreprocessEvent?

Discussion in 'Plugin Development' started by Caprei, Feb 1, 2014.

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

    Caprei

    Hello,
    Wasn't really sure what to call this so I just called it one of the events in the plugin related to my question...
    Anyways, I'm trying to create a plugin with a configuration file, which allows you to customize banned commands, the message received when attempting to use one and a possible punishment.
    The command "bannedadd" isn't working for some reason, I'd appreciate any help. Thanks. Sorry if the indentation broke while copying and pasting...

    Code:java
    1. package me.Caprei.Restriction;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Restriction extends JavaPlugin implements Listener{
    13. public void onEnable(){
    14. getServer().getLogger().info("[Restriction] Restriction has been enabled. Made by Caprei.");
    15. getServer().getPluginManager().registerEvents(this, this);
    16. getConfig().options().copyDefaults(true);
    17. saveConfig();
    18. }
    19. public void onDisable(){
    20. getServer().getLogger().info("[Restriction] Restriction has been disabled. Made by Caprei.");
    21.  
    22. }
    23. @EventHandler(priority = EventPriority.HIGHEST)
    24. public void onPlayerCommand(PlayerCommandPreprocessEvent e){
    25. Player c = e.getPlayer();
    26. String msg = e.getMessage().toLowerCase();
    27. for(String banned:getConfig().getStringList("Banned Commands")){
    28. if(msg.startsWith(banned)){
    29. if(c.hasPermission("restriction.freechat")){
    30. return;
    31. }
    32. c.sendMessage(getConfig().getString("Restricted Word Message"));
    33. e.setCancelled(true);
    34. if(getConfig().getBoolean("Punish Player")){
    35. String p = getConfig().getString("Punishment").toLowerCase();
    36. if(p == "kill"){
    37. c.setHealth(0.0d);
    38. return;
    39. }
    40. if(p == "starve"){
    41. c.setFoodLevel(0);
    42. return;
    43. }
    44. if(p == "burn"){
    45. c.setFireTicks(1000);
    46. return;
    47. }
    48. if(p == "fall")
    49. {c.setFallDistance(50.0f); return;}
    50. if(p == "explode"){
    51. c.getWorld().createExplosion(c.getLocation(), 4.0f);
    52. c.setHealth(0);
    53. }
    54.  
    55. return;
    56. }
    57. }
    58. }
    59. }
    60. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    61. if(cmd.getName().equalsIgnoreCase("bannedadd")){
    62. if(sender.hasPermission("restriction.bannedadd")){
    63. if(args.length == 0){
    64. sender.sendMessage("" + ChatColor.RED + ChatColor.ITALIC + "Please specify a command to add to the list of banned commands.");
    65. return true;
    66. }
    67. for(String banned:args){
    68. getConfig().getStringList("Banned Commands").add(banned);
    69. saveConfig();
    70. }
    71. sender.sendMessage("" + ChatColor.GREEN + ChatColor.ITALIC + "The commands you specified were added to the banned list!");
    72. return true;
    73. }sender.sendMessage("" + ChatColor.RED + ChatColor.ITALIC + "You do not have permission to add to the list of banned commands.");
    74. return true;
    75. }
    76. if(cmd.getName().equalsIgnoreCase("banned"))
    77. if(sender.hasPermission("restriction.see")){
    78. sender.sendMessage("" + ChatColor.RED + ChatColor.BOLD + "These are the banned comands: " + getConfig().getStringList("Banned Commands"));
    79. return true;
    80. }sender.sendMessage("" + ChatColor.RED + ChatColor.ITALIC + "You do not have permission to see the banned commands.");
    81. return true;
    82.  
    83. }
    84. }
    85.  


    config.yml:
    Code:
    Banned Commands:
      - /op
      - /help
      - /pl
      - /give
      - /gamemode
     
    Restricted Word Message: Please do not use that command. It is restricted for you.
     
    Punish Player: false
    Punishment: 
    It's probably some really stupid mistake...
    Thanks!
     
  2. Offline

    Flybelette

    Why not put the "bannedadd" Command directly into PlayerCommandPrepocessEvent ?
     
  3. Offline

    Caprei

    Flybelette Well, is there any reason to do that? Will it fix the problem?
     
  4. Offline

    Flybelette

    Maybe, Why not try ? :p
     
  5. Offline

    Caprei

    Flybelette Because I'm pretty sure that won't do anything other then calling the onCommand method every time a player enters a command.
     
  6. Offline

    Flybelette

    Woot ? Everytime a player run a command it will throw PlayerCommandPreprocessEvent without need to add the Command in plugin.yml ...
     
  7. Offline

    Caprei

    Flybelette Maybe you should re-read my post, I wasn't saying anything about plugin.yml. I was simply saying, if I put the onCommand method in the onPlayerCommand method, everytime a player runs any command, it will call the onCommand method twice.
     
Thread Status:
Not open for further replies.

Share This Page