Basic plugin to block a command. Just a bit of help needed.

Discussion in 'Plugin Development' started by Jay23, Jul 28, 2014.

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

    Jay23

    Here's the main class:
    Code:java
    1.  
    2. package me.jay232323;
    3.  
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class PreventLookAtPlugins extends JavaPlugin{
    11.  
    12. @Override
    13. public void onEnable() {
    14. new PlayerListener(this);
    15. }
    16.  
    17. @Override
    18. public void onDisable() {
    19.  
    20. }
    21. }
    22.  
    23.  


    In onEnable, it references this class, where the majority of code is:

    Code:java
    1.  
    2. package me.jay232323;
    3.  
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9.  
    10. public class PlayerListener implements Listener {
    11.  
    12. @EventHandler
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    14.  
    15. if (cmd.getName().equalsIgnoreCase("plugins") && sender instanceof Player) {
    16.  
    17. Player player = (Player) sender;
    18.  
    19. event.setCancelled(true);
    20.  
    21. player.sendMessage("Only the console may use this command.");
    22. }
    23. return false;
    24. }
    25. }
    26.  



    In the second block of code, the line of code:
    Code:java
    1. event.setCancelled(true);

    is getting the error that event cannot be resolved. I'm extremely new to this, so I apologize in advance for being incompetent. I've Googled it up and down and can't find it for the life of me. I've also consulted three friends, and none of them can figure it out either, so I'm coming to you guys.
     
  2. Offline

    Monkey_Swag

    onCommand is not an event.... I recommend you learn more about the BukkitAPI before doing this. If you'll use an event to block a certain command use the CommandPreProccess event.
     
    Jay23 likes this.
  3. Offline

    Garris0n

    Commands aren't events.
     
    Monkey_Swag likes this.
  4. Offline

    Jay23

    Monkey_Swag
    Well, I don't know if I'm getting closer or farther, but this is what I've got.

    Code:java
    1.  
    2. public class PlayerListener implements Listener {
    3.  
    4. @SuppressWarnings("null")
    5. @EventHandler
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    7.  
    8. if (cmd.getName().equalsIgnoreCase("plugins") && sender instanceof Player) {
    9.  
    10. Player player = (Player) sender;
    11.  
    12. PlayerCommandPreprocessEvent event = null;
    13.  
    14. event.setCancelled(true);
    15.  
    16. player.sendMessage("Only the console may use this command.");
    17. }
    18. return false;
    19. }
    20.  
    21. }
    22.  
     
  5. Offline

    Monkey_Swag

    Jay23 don't do plugins like these if you're not skilled enough to do them. You should know how to make events from commands. You can't do event.setCancelled(); if it's not an event. Commands ARE NOT EVENTS.
     
    Jay23 likes this.
  6. Offline

    Goblom

    Jay23 Can't tell if serious or just Cory.

    Jay23 bare with me, I'm going to write this on my phone.

    Code:java
    1.  
    2. public class PlayerListener implements Listener {
    3. @EventHandler
    4. public void onCommandProcess(PlayerCommandPreprocessEvent event) {
    5. if (event.getMessage().startsWith("/") {
    6. String command = event.getMessage().split(" ")[0].replace("/", "");
    7.  
    8. if (command.equalsIgnoreCase("plugins") {
    9. event.setCancelled(true);
    10. event.getPlayer().sendMessage("You cannot do that");
    11. }
    12. }
    13. }
    14. }
    15.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    Jay23 and Monkey_Swag like this.
  7. Offline

    Monkey_Swag

    Goblom please remove that. Spoonfeeding him won't work, at all, especially in his state.
     
    Jay23 likes this.
  8. Offline

    Goblom

    Jay23 likes this.
  9. Offline

    Monkey_Swag

    Goblom It's just not right :/ He will always come and look for people to do plugins for him and not give them any credit once it's finished...
     
    Jay23 likes this.
  10. Offline

    Jay23

    Goblom
    Code:java
    1. @EventHandler
    2. public void onCommandProcess(PlayerCommandPreprocessEvent event) {
    3. if (event.getMessage().startsWith("/") {
    4. String command = event.getMessage().split(" ")[0].replace("/", "");


    What is this accomplishing?

    Monkey_Swag
    Where would you suggest I start? What type of plugins should I work on to develop my skills? I'm kinda using the sink-or-swim tactic on myself here.
     
  11. Offline

    Monkey_Swag

    Jay23 Read javadocs, look through the plugin development help forum and look at others' posts. That's what I would do. On my way to school and back I would just go on my phone and look at others' threads and learn new things from them. Another thing you could do is watch basic plugin tutorials on youtube. However, that is not completely recommended since it is spoonfeeding, but whatever suits you.
     
    Jay23 likes this.
  12. Offline

    Goblom

    Jay23 the if statement is checking if is a command because commands always start with a "/" otherwise it's a message.

    The next part is splitting the message by spaces because commands can have arguments, it's then getting the absolute first word in an array, then it is removing the "/" from the command.
     
    Jay23 likes this.
  13. Offline

    Monkey_Swag

    Wouldn't event#getMessage()#startsWith("/plugins") be easier? :p
     
    Jay23 likes this.
  14. Offline

    Jay23

    Goblom
    So it just makes them say "plugins" in chat by removing the slash?

    Monkey_Swag
    Wow, never thought of that before. That's a really good idea, thanks!

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

    Monkey_Swag

     
    Jay23 likes this.
  16. Offline

    Jay23

    But what about this?
    Code:java
    1. String command = event.getMessage().split(" ")[0].replace("/", "");


    Does it remove the slash, then run event.setCancelled(true); to prevent them from sending the message in chat?
     
  17. Offline

    Goblom

    Monkey_Swag I was thinking this was is simpler because you could then check equals ignore case multiple times instead of starts with... Just seemer simpler to me
     
    Jay23 likes this.
  18. Offline

    JBoss925

    Ok, instead of spoon-feeding you, I'll run you through this.
    First we want to use an @interface in order to say "Hey, this block of code needs to occur when this event occurs" so we add an
    Code:java
    1. @EventHandler
    above the method. Then we want to declare that this method is public and it doesn't return anything so we start with
    Code:java
    1. @EventHandler
    2. public void

    Now we want to name the method so when we look back at it we know what it does. Here we want the event to be called when a command is sent so we'll name it onCommandSend
    Code:java
    1. @EventHandler
    2. public void onCommandSend()

    Now we need to give the event we want the block of code called with. So we're going to use the PlayerCommandPreprocessEvent and call the instance event.
    Code:java
    1. @EventHandler
    2. public void onCommandSend(PlayerCommandPreprocessEvent event)

    Now we're going to give the actual code that is executed so we need to add some brackets.
    Code:java
    1. @EventHandler
    2. public void onCommandSend(PlayerCommandPreprocessEvent event){
    3.  
    4. }

    First, we want to check that the message in the chat(that will execute the command) starts with "/plugins" because that indicates it is the command we want to block so we do.
    Code:java
    1. @EventHandler
    2. public void onCommandSend(PlayerCommandPreprocessEvent event){
    3. if(event.getMessage().startsWith("/plugins"){
    4.  
    5. }
    6. }

    Then we want to cancel the event so it doesn't do anything. So we'll do:
    Code:java
    1. @EventHandler
    2. public void onCommandSend(PlayerCommandPreprocessEvent event){
    3. if(event.getMessage().startsWith("/plugins"){
    4. event.setCancelled(true);
    5. }
    6. }

    Next we'll send the player a message that says they can't use that command so we'll do:
    Code:java
    1. @EventHandler
    2. public void onCommandSend(PlayerCommandPreprocessEvent event){
    3. if(event.getMessage().startsWith("/plugins"){
    4. event.setCancelled(true);
    5. event.getPlayer().sendMessage("You can't use that!");
    6. }
    7. }

    Now we're done. Not hard, really.
     
    Jay23 and Goblom like this.
  19. Offline

    Goblom

    Jay23 it does not edit the message in any way, the command is still being sent, that is just getting what command is being ran.

    The event.setCancelled(true); is what is actually stopping the command from being ran in the first place
     
    Jay23 likes this.
  20. Offline

    Jay23

    JBoss925
    Wow, that's perfect, thank you so much!

    JBoss925
    Well, I entered the code and tested it, and it's still not working. It's not displaying the message or stopping the command from going through. Could it be that it's not as high of a priority as the other process? It's just ignoring it because the /plugins command it the higher priority of some sort? Just speculating here.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    JBoss925 likes this.
  21. Offline

    The_Coder

    You didn't register the event with bukkit...
     
    Jay23 likes this.
  22. Offline

    Jay23

    The_Coder
    Ah, got it! Thank all of you so much for putting up with my newbie crap! I really do appreciate it!

    Goblom
    Could I replace
    Code:java
    1. if(event.getMessage().startsWith("/plugins")


    with
    Code:java
    1. if(event.getMessage().equalsIgnoreCase("/plugins"))


    so they can't type /PLUGINS to bypass the plugin?

    Edit: Yes. Yes I can.

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

    _LB

  24. Offline

    Gnat008

    _LB
    You don't even have to do that. Just check if the sender is an instance of a Player. If it isn't, then its either Console, or a CommandBlock.
     
  25. Offline

    Goblom

    Jay23 techically you can, but they can easily get around that by doing "/plugins " (notice the space at the end)
     
    Jay23 likes this.
  26. Offline

    Jay23

    Goblom

    Then how could I implement both of them?
     
  27. Offline

    _LB

    You may want to reread my post - my solution involves writing 0 lines of code.
     
  28. Offline

    Gnat008

    _LB
    True. Good point.
     
  29. Offline

    Jay23

    Goblom
    Code:java
    1. if(event.getMessage().equalsIgnoreCase("/plugins") || event.getMessage().startsWith("/plugins"))
     
  30. Offline

    _LB

    Convert the string to lower case first, then use #startsWith. Or, you know, read my earlier post to find out how to do this in 0 lines of code.
     
Thread Status:
Not open for further replies.

Share This Page