Solved Not letting players use commands in water or lava

Discussion in 'Plugin Development' started by PimpDuck, Sep 15, 2013.

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

    PimpDuck

    Hello, I need some help.

    I coded a plugin to not let players use certain commands in lava or water or lava but it's doing that, but doing it in water and lava but also on land? any help.
    What it's doing on land and water and lava. I want it on water and lava, but not land: http://puu.sh/4sh7h.png
    My code:
    Code:java
    1. package me.PimpDuck.ACCommandDisable;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13.  
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16. @Override
    17. public void onEnable(){
    18. log.info("[ACCommandDisable] has been enabled!");
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. log.info("[ACCommandDisable] has been disabled!");
    24. }
    25.  
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    28. {
    29. Player player = (Player) sender;
    30. if(sender instanceof Player){
    31. Material m = player.getPlayer().getLocation().getBlock().getType();
    32. if(cmd.getName().equalsIgnoreCase("tpahere") || cmd.getName().equalsIgnoreCase("etpahere") || cmd.getName().equalsIgnoreCase("back") || cmd.getName().equalsIgnoreCase("eback") || cmd.getName().equalsIgnoreCase("tpa") || cmd.getName().equalsIgnoreCase("etpa") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    33. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You can't use this command here!");
    34. return false;
    35. }
    36. }
    37. return false;
    38. }
    39. }
    40.  


    UPDATE: The only command it seems to be blocking only in water is etpa. But when it blocks it in the water and they do it on land, nothing happens.

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

    tommycake50

    You need to learn how operators work.
    try this.
    Code:java
    1.  
    2. if((cmd.getName().equalsIgnoreCase("tpahere") || cmd.getName().equalsIgnoreCase("etpahere") || cmd.getName().equalsIgnoreCase("back") || cmd.getName().equalsIgnoreCase("eback") || cmd.getName().equalsIgnoreCase("tpa") || cmd.getName().equalsIgnoreCase("etpa")) && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    3.  
     
  3. Offline

    doubleboss00

    If I'm correct you have it where if your in lava or water "You can't use this command here!"
    Correct me if im wrong .-.
     
  4. Offline

    adam753

    Code:
    if(cmd.getName().equalsIgnoreCase("tpahere") || cmd.getName().equalsIgnoreCase("etpahere") || cmd.getName().equalsIgnoreCase("back") || cmd.getName().equalsIgnoreCase("eback") || cmd.getName().equalsIgnoreCase("tpa") || cmd.getName().equalsIgnoreCase("etpa") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    
    I believe the && operator has a higher precedence than ||, which means the material is only being taken into account when the command name is etpa. To fix this, you should put a single pair of brackets around all the commands in this statement, so that they will all be considered as one thing to the left of the &&, if you see what I mean. (what tommycake50 said)
    Code:
    if((cmd.getName().equalsIgnoreCase("tpahere") || cmd.getName().equalsIgnoreCase("etpahere") || cmd.getName().equalsIgnoreCase("back") || cmd.getName().equalsIgnoreCase("eback") || cmd.getName().equalsIgnoreCase("tpa") || cmd.getName().equalsIgnoreCase("etpa")) && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    
    On a sidenote, it seems like you're trying to block commands from other plugins from working here? This probably won't work. Your onCommand method will only get called if the command is registered in your plugin.yml, and even if you do that it will still be sent to all plugins with it registered, and you can't stop those from handling it.
     
    tommycake50 likes this.
  5. Offline

    PimpDuck

    tommycake50 adam753 doubleboss00
    I have changed my code to:
    Code:java
    1. package me.PimpDuck.ACCommandDisable;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13.  
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16. @Override
    17. public void onEnable(){
    18. log.info("[ACCommandDisable] has been enabled!");
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. log.info("[ACCommandDisable] has been disabled!");
    24. }
    25.  
    26. public void commandBlock(Player player){
    27. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You can't use this command here!");
    28. }
    29.  
    30. @Override
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    32. {
    33. Player player = (Player) sender;
    34. if(sender instanceof Player){
    35. Material m = player.getPlayer().getLocation().getBlock().getType();
    36. if(cmd.getName().equalsIgnoreCase("tpahere") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    37. commandBlock(player);
    38. }
    39. if(cmd.getName().equalsIgnoreCase("etpahere") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    40. commandBlock(player);
    41. }
    42. if(cmd.getName().equalsIgnoreCase("tpa") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    43. commandBlock(player);
    44. }
    45. if(cmd.getName().equalsIgnoreCase("etpa") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    46. commandBlock(player);
    47. }
    48. if(cmd.getName().equalsIgnoreCase("back") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    49. commandBlock(player);
    50. }
    51. if(cmd.getName().equalsIgnoreCase("eback") && (m == Material.STATIONARY_WATER || m == Material.WATER || m == Material.STATIONARY_LAVA || m == Material.LAVA)){
    52. commandBlock(player);
    53. }
    54. }
    55. return false;
    56. }
    57. }
    58.  


    and this seems to be fixing my problem but still, when I'm outside of the water or lava it still doesn't do anything?
     
  6. Offline

    adam753

    PimpDuck As I said above, the commands you're trying to stop the player from using all belong to other plugins, and you can't just intercept them and stop them from working (especially not by sending the player a message, which is all you're doing here.) The command will still be sent to the other plugin, and it will still handle it the same way.
     
  7. Offline

    doubleboss00

    What is your plugin doing anyway it seems to me that its doing nothing
     
  8. Offline

    PimpDuck

    adam753 So, is this impossible???
     
  9. Offline

    tommycake50

    That probably lies in the commandBlock method, Can we see it?
     
  10. Offline

    PimpDuck

    tommycake50 That's all my code right there???
     
  11. Offline

    tommycake50

    Oh im an idiot xD.
    Does it work to actually block the command?
     
  12. Offline

    PimpDuck

    tommycake50 Yes it blocks the command in lava and water now. But when I'm on land it does absolutely nothing? Is it because of what that one guy said about how these commands are apart of another plugin?
     
  13. Offline

    tommycake50

    It's possible, Can we see your plugin.yml?
     
  14. Offline

    PimpDuck

    Okay. and my plugin.yml:
    Code:
    name: ACCommandDisable
    main: me.PimpDuck.ACCommandDisable.Main
    version: 1.0
    commands:
      tpahere:
      etpahere:
      back:
      eback:
      tpa:
      etpa:
    tommycake50 ^

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

    tommycake50

    Yeah ok so use a nested if.
    pseudo
    Code:
    if(command = "command"){
        if(dissallowedmaterials.contains(material)){
            return false;
        }else{
            getServer().getPluginManager().getPlugin("plugin").onCommand(sender, command, label, args)
        }
    }
    
     
  16. Offline

    PimpDuck

    tommycake50 So mine would look like
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    3. {
    4. Player player = (Player) sender;
    5. if(sender instanceof Player){
    6. Material m = player.getPlayer().getLocation().getBlock().getType();
    7. if(command = "command"){
    8. if(dissallowedmaterials.contains(material)){
    9. return false;
    10. }else{
    11. getServer().getPluginManager().getPlugin("essentials").onCommand(sender, command, label, args)
    12. }
    13. }
    14. }
    15. return false;
    16. }
    17. }
    18.  

    I seem to be getting alot of errors? http://puu.sh/4skj2.png

    I have a feeling I'm doing this TERRIBLY wrong. Lol

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

    InflamedSebi

    your plugin will NOT block any command ... your "onCommand" section will only become active if a player executes a command that belongs to your plugin ... due to "tp" "tpa"and so on belong to other plugins, it will not trigger your section untill you'll list all of them in your plugin.yml ... even if you would, you just send the player a message "Error: You can't use this command here!"
    but this message will not prevent the sever from executing this command ... It will display your message and run the command anyway ...

    the only way to block the commands of other plugins is a "PlayerCommandPreprocessEvent"

    here is a quick example :

    Code:java
    1.  
    2. //list of all liquids
    3. private List<Material> liquid = Arrays.asList(new Material[] {
    4. Material.WATER,
    5. Material.STATIONARY_WATER,
    6. Material.LAVA,
    7. Material.STATIONARY_LAVA
    8. });
    9.  
    10. //list of all commands to block
    11. private List<String> block = Arrays.asList(new String[] {
    12. "tp",
    13. "tpa",
    14. "tphere",
    15. "tpahere",
    16. });
    17.  
    18. // the block event itself
    19. @EventHandler(priority = EventPriority.LOW)
    20. public void blockCommand(PlayerCommandPreprocessEvent evt) {
    21. Player player = evt.getPlayer();
    22. Material material = player.getPlayer().getLocation().getBlock().getType();
    23. String command = evt.getMessage();
    24. command = command.split(" ")[0];
    25. command = command.replace("/", "");
    26.  
    27. if(liquid.contains(material) && block.contains(command)){
    28. evt.setCancelled(true);
    29. player.sendMessage("Can't do this here!");
    30. }
     
  18. Offline

    PimpDuck

    InflamedSebi Okay, so here's what I got. What would go in the commandSender ?
    Code:java
    1. package me.PimpDuck.ACCommandDisable;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin {
    18.  
    19. Logger log = Logger.getLogger("Minecraft");
    20.  
    21. @Override
    22. public void onEnable(){
    23. log.info("[ACCommandDisable] has been enabled!");
    24. }
    25.  
    26. @Override
    27. public void onDisable(){
    28. log.info("[ACCommandDisable] has been disabled!");
    29. }
    30.  
    31. //list of all liquids
    32. private List<Material> liquid = Arrays.asList(new Material[] {
    33. Material.WATER,
    34. Material.STATIONARY_WATER,
    35. Material.LAVA,
    36. Material.STATIONARY_LAVA
    37. });
    38.  
    39. //list of all commands to block
    40. private List<String> block = Arrays.asList(new String[] {
    41. "tp",
    42. "tpa",
    43. "tphere",
    44. "tpahere",
    45. });
    46.  
    47. // the block event itself
    48. @EventHandler(priority = EventPriority.LOW)
    49. public void blockCommand(PlayerCommandPreprocessEvent evt) {
    50. Player player = evt.getPlayer();
    51. Material material = player.getPlayer().getLocation().getBlock().getType();
    52. String command = evt.getMessage();
    53. command = command.split(" ")[0];
    54. command = command.replace("/", "");
    55. }
    56.  
    57. @Override
    58. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    59. {
    60. Player player = (Player) sender;
    61. if(sender instanceof Player){
    62. // what would I put here ?
    63. }
    64. return false;
    65. }
    66. }
    67.  
     
  19. Offline

    InflamedSebi

    You dont need the whole
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    3. {
    4. Player player = (Player) sender;
    5. if(sender instanceof Player){
    6. // what would I put here ?
    7. }
    8. return false;
    9. }


    stuff ...

    You can use a listener ...
    the will hook up into the server and become active every time something special happens ... in your case, every time a player executes a command.

    there are a few steps to do, before you can use Listener ...
    first you have to enable/register them in the server
    than implement them correctly ...

    some examples can be found here

    btw you did not copy my whole code ... there are missing 3 lines ;) the code box has a scrollbar ;)
    and this was an example ... this might throw errors under some special circumstances ...but it should work if u implement it correctly ...

    just make sure u know how to use listener/the event API
     
    tommycake50 likes this.
  20. Offline

    PimpDuck

    InflamedSebi Ohh okay. I'm not sure how to do this but okay ._.
     
  21. Offline

    InflamedSebi

    just read the bukkit wiki about events ... this is pretty simple and powerfull! ... its worth every minute u spent to it ... btw i commented my code:

    Code:java
    1. //create a list of all liquid materials, so u can access the list instead of checking every single material
    2. private List<Material> liquid = Arrays.asList(new Material[] {
    3. Material.WATER,
    4. Material.STATIONARY_WATER,
    5. Material.LAVA,
    6. Material.STATIONARY_LAVA
    7. });
    8.  
    9. //also create a list of all command that should be blocked, which makes the code cleaner :)
    10. private List<String> block = Arrays.asList(new String[] {
    11. "tp",
    12. "tpa",
    13. "tphere",
    14. "tpahere",
    15. });
    16.  
    17. // this is the listener!
    18. // it will be executed, everytime a player tries to send a command
    19. @EventHandler(priority = EventPriority.LOW)
    20. public void blockCommand(PlayerCommandPreprocessEvent evt) {
    21. // first get the player
    22. Player player = evt.getPlayer();
    23.  
    24. // get the material
    25. Material material = player.getPlayer().getLocation().getBlock().getType();
    26.  
    27. // now get the command
    28. String command = evt.getMessage();
    29.  
    30. // due to the command can be something like "/tp Inflamedsebi evilGamer66"
    31. // so we have to clean it up
    32. // we split it up on each whitspace (you shuold know arrays to understand this!)
    33. String[] splitcommand = command.split(" ");
    34.  
    35. // the array now looks like"[/tp][Inflamedsebi][evilGamer66]"
    36. // and we just want to filter the basic command so we the the 1st segment (number 0)
    37. command = splitcommand[0];
    38.  
    39. // now we just have to replace the "/" with nothing :)
    40. command = command.replace("/", "");
    41.  
    42. // now check if the player material is a liquid AND (&&) the command is part of the blocked ones
    43. if(liquid.contains(material) && block.contains(command)){
    44.  
    45. // if true we will cancel this event, so the player can not call this command
    46. evt.setCancelled(true);
    47.  
    48. // just infor the player about the canceling
    49. player.sendMessage("Can't do this here!");
    50. }
    51.  
    52.  
    53.  
    54. }
    55.  
     
    PimpDuck likes this.
  22. Offline

    PimpDuck

    InflamedSebi The commands I wanted to block were "tpa, etpa, back, eback, tpahere, etpahere"

    InflamedSebi Oh I got it. I implemented listener and registered the events on the onEnable() thanks :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page