Help with a feedback command plugin

Discussion in 'Plugin Development' started by BagelMan123, Aug 8, 2020.

  1. Offline

    BagelMan123

    I am trying to program a plugin in which the player can type /feedback (feedback here) and then the feedback is compiled into a list that can be viewed by mods if they type /feedbackview or something like that. This is what I'm trying to do as my first plugin that Im trying to code, so don't assume I know any terminology (talk to me like I'm 5 lol). So far after watching a hello world tutorial for plugins, I have it so that if they say /feedback without anything after it, the system responds by saying something along the lines of "use /feedback (your feedback here) to give us your feedback!". But I do not know how to detect the actual feedback and I also do not know how to compile that feedback into a list. Here's a screenshot of what I have so far. If someone could also explain what the heck is actually going on in this code and how it works that would be great, the tutorial didn't explain it very well.

    Is this too complicated for a first plugin? I'm trying to learn how to code minigames and this seemed like a reasonable first step. Pls help, I'm very lost.
     

    Attached Files:

  2. Offline

    KarimAKL

    @BagelMan123 It seems like a good start for your first plugin. Minigames can be a lot more complicated.

    This is your current code:
    Code:Java
    1. public class FeedbackCommand implements CommandExecutor {
    2.  
    3. private Main plugin;
    4. public FeedbackCommand(Main plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. @Override
    9. public boolean onCommand(CommandSender sender, Command cmd, String label,
    10. String[] args) {
    11. if (!(sender instanceof Player)) {
    12. sender.sendMessage("Only players can execute this command!");
    13. return true;
    14. }
    15. Player p = (Player) sender;
    16. if (p.hasPermission("Feedback.Use")) {
    17. p.sendMessage("What feedback do you have about this server? Please type /feedback (Your Fee");
    18. return true;
    19. }
    20. else {
    21. p.sendMessage("You do not have permission to execute this command!");
    22. return true;
    23. }
    24. }
    25. }

    So you start by creating a new class that implements the interface CommandExecutor, you will then need to override the onCommand(CommandSender sender, Command cmd, String label, String[] args) to do whatever you want.

    Inside of the onCommand method, you're first checking if the CommandSender is a Player, if not, you send them a message and return true to stop the method early. After confirming that they're a Player, you then cast the CommandSender to a Player to use all of the Player methods.

    You then check whether or not the player has the permission "Feedback.Use" and if so, you send them a message asking what feedback they have, otherwise, you tell them that they don't have permission to execute the command.

    Now, you also have a constructor for the FeedbackCommand class that has Main as a parameter as well as a field of type Main that you pass the instance from the constructor to, that allows your onCommand method to access instance (non-static) fields and methods from your Main class. You don't need this as long as you don't use it for anything, which you're not in the code you provided.

    I hope i did well enough to explain that code. Let me know if you have any further questions.

    Now, for your feedback command to work as expected, you'll want to use StringBuilder for getting one String from all the arguments (the feedback).

    Code:Java
    1. StringBuilder builder = new StringBuilder();


    We'll then loop the arguments we consider part of the message (in this case, all of them) using a regular for-loop.

    Code:Java
    1. for (int i = 0; i < args.length; i++) {
    2.  
    3. }


    We then use this loop to add all of the arguments to the StringBuilder using the append(String) method. Now, since the arguments are in an array and doesn't have the spaces separating the arguments, we'll have to add those spaces ourselves.

    Code:Java
    1. builder.append(args{i}).append(' ');

    Note: I used {i} instead of [i] because of the Bukkit forums formatting italics being [i]

    That's everything for the loop, now you can get the feedback using toString() but, we have a small problem. Since we're adding a space after every argument, we'll have a string looking like this (for example) "first second third " but, this is an easy fix. We can remove that last space using trim().

    Code:Java
    1. String feedback = builder.toString().trim();


    You can then store this string inside of an ArrayList for getting later using the /feedbackview command.

    If you have any other questions, let me know. I'll be happy to answer them.
     
    BagelMan123 likes this.
  3. Offline

    BagelMan123

    @KarimAKL
    Thank you so much! I think I understand most of this now. I will try it out and let you know how it works, thank you for the help!

    Edit: I think I figured most of it out. I put in the code you added and I seem to have put it in the right spot. However, I'm not sure how to access it from a different class (for the FeedbackView command). And I'm not sure how to send the list to the sender
    This doesn't seem to work (I named the list feedback list):

    Code:
    sendMessage(FeedbackList);
     
    Last edited: Aug 9, 2020
    KarimAKL likes this.
  4. Offline

    KarimAKL

    You'll have to pass it to the class somehow, just like you're doing with your Main instance using the constructor.

    You'll have to loop the list.
     
  5. Offline

    Strahan

    Bukkit makes Apache Commons' StringUtils available so you could save a lot of effort by just doing String feedback = StringUtils.join(args, " ", 0, args.length);
     
  6. Offline

    The_Spaceman

    Can't you just use
    Code:java
    1. String.join(" ", args);
     
  7. Offline

    Strahan

    True, I default to the StringUtils out of habit because of my love for subcommands, lol. I almost never want all the arguments in the string but in this case since he does, yea, that'd be even simpler.
     

Share This Page