Solved Syntax error on token "}", { expected after this token

Discussion in 'Plugin Development' started by LolG0D, Jun 19, 2017.

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

    LolG0D

    Hello everyone! So i was trying to do an report plugin but it gives me the error "Syntax error on token "}", { expected after this token " on line 40, and i dont know how to resolve it.


    Code:
    package Comandos;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    import Guis.GuiReport;
    
    
    public class Report implements CommandExecutor, Listener
    {
        @SuppressWarnings("deprecation")
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!(sender instanceof Player)) {
                return true;
            }
            final Player p = (Player)sender;
            Player t = Bukkit.getPlayer(args[0]);
          
            if (cmd.getName().equalsIgnoreCase("report")) {
                    GuiReport.GuiReports(p);
                    }
                                                       
                       final InventoryClickEvent e; {
                            final Player z = (Player)e.getWhoClicked();
                            if (e.getInventory().getTitle().equalsIgnoreCase("§cReport") && e.getCurrentItem() != null && e.getCurrentItem().getTypeId() != 0) {
                                e.setCancelled(true);
    
                                }
                            if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6Teste")) {              
                               e.setCancelled(true);
                               Bukkit.dispatchCommand((CommandSender)e, "Teste" + t.getDisplayName());
                               return true;
                            }
                         }
                    }  //  <-- Error Syntax error on token "}", { expected after this token             
                      return true;
                }
            }
     
    Last edited: Jun 19, 2017
  2. Offline

    MelonCola

    I believe you can remove the } on that line, however, I cannot see what your code should look like, as the formatting is incorrect(I believe). Please try fixing the formatting.
     
  3. Offline

    LolG0D

    If i take the } it gives an error on
    final InventoryClickEvent e;
     
  4. Offline

    MelonCola

    Like I said, please fix the formatting.
     
  5. Offline

    Zombie_Striker

    @LolG0D
    Remove the unneccesairy open and close brackets (the one after the event, and the one with the error message.)

    BTW: You cannot have methods nestled in methods; You cannot have an event in the onCommand. Move the event into its own method, and add a check if a player sent the command.
     
  6. Offline

    LolG0D

    @Zombie_Striker What i wanna do is like, the player do "/report (reportedplayer)" and then it opens an GUI whit the reasons (Killaura...) and when the player clicks in Killaura (for example) it sends a message ("The player (name of the player) reported (name of the reported player) for (reason)") to the players who have the permission "flame.staff". How i do that?
     
    Last edited: Jun 19, 2017
  7. Offline

    Zombie_Striker

    @LolG0D
    1. Create a hashmap. The keys will be UUIDs and the values will be Strings. This represents which player (the uuid) is trying to ban which player (the String).
    2. When a player sends a message, add the player's uuid and the arg to the hashmap.
    3. Then, in the event, if the player is in the hashmap, ban the target player from the hashmap.

    From the PM.

    Sort of. It should not be static, Player should be UUID, and you need to initialize it by setting it equal to a new hashmap.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 19, 2017
  8. Offline

    LolG0D

    like this?

    Code:
    package Comandos;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import Guis.GuiReport;
    
    
    public class Report implements CommandExecutor, Listener
    {
        public static HashMap<String, String> report; //key value
       
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!(sender instanceof Player)) {
                return true;
            }
            final Player p = (Player)sender;
            Player t = Bukkit.getPlayer(args[0]);
         
            if (cmd.getName().equalsIgnoreCase("report")) {
                    GuiReport.GuiReports(p);
                    report.put(t.getName(), p.getName());
                   
            }
            return true;
        }
    }
     
  9. Offline

    Zombie_Striker

    @LolG0D
    1. Again
      You have not done anything I mentioned in the previous post. Remove the static modifier, change the now String key to a UUID, and initialize the field.
    2. The Senders UUID should be first. The Targets name should be the second variable. Switch those two variables in the report.put line.
    3. If the player does not provide any args, this will throw an ArrayOutOFBoundsException and break the command. Make sure that there is a first arg before trying to get it.
    4. The target player can be null if the player is not online. Make sure t is not null before using it.
    5. Package names should follow the format "me.<your name>.<project name>".
     
    MelonCola likes this.
  10. Offline

    MelonCola

    I generally don't like spoonfeeding, but this is a problem I used to have, and seeing as you didn't already do this when Zombie_Striker said to, I am guessing you don't know how. When defining your hashmap, you should use code similar to this:
    Code:
    public HashMap<UUID, String> report = new HashMap<>();
     
  11. Offline

    LolG0D

    Thank you MelonCola.
    @Zombie_Striker
    How do i initialize the field?
    Now its giving an error on "put" line 34 (report.put), "The method put(UUID, String) in the type HashMap<UUID,String> is not applicable for the arguments (Player, String)"

    Code:
    package Comandos;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import Guis.GuiReport;
    
    
    public class Report implements CommandExecutor, Listener
    {
        public HashMap<UUID, String> report = new HashMap<>(); //key value
       
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!(sender instanceof Player)) {
                return true;
            }
            Player p = (Player)sender;
            Player t = Bukkit.getPlayer(args[0]);
         
            if (cmd.getName().equalsIgnoreCase("report")) {
                if (t == null){
                    p.sendMessage("§4The player " + t.getDisplayName() + "is not online!");
                }
                if (args.length == 0){
                    p.sendMessage("§cUse /report <player>");
                }
                    GuiReport.GuiReports(p);
                    report.put(p, t.getName());             
            }
            return true;
        }
    }
     
  12. Offline

    Zombie_Striker

    @LolG0D
    You initialize the field by adding the "= new HashMap" bit to that line.

    You are providing a player instance instead of their uuid. After p, put ".getUniqueId()" to get the player's UUID.
     
  13. Offline

    LolG0D

    So now what i need to is when a players click in an Item it sends a message, what do i put in the message?
    "The player" + ??? + "Reported" + ??? + "For Killaura"

    And whats the diferrence if i put static in the hashmap?

    Sorry for beeing so noob ;-;
     
  14. Offline

    Zombie_Striker

    @LolG0D
    The first question mark will be who reported the other player, and the second question mark should be who was reported. Since that hos nothing to do with the code above, I'm not sure where you want to put that line.

    As for static: The static modifier tells variables to be stored with the class object, not the instance. That means that there can only be one instance across all classes of that type. Not only that, but if the field is not handled properly, the static object will stay in memory across reloads (which can be a bad thing for some plugins) and will stay in memory even when the server shuts down. This is what is known as a memory leak.
     
  15. Offline

    LolG0D

    @Zombie_Striker
    You said to me that i could not have methods nestled in methods and then i separeted the command and the InvetoryClickEvent to diferent classes. The player who reported the other is the player who is doing the command /report (the player"p") and the reported is the player "t" but what i wanna to do is when a player clicks in a reason, it sends a message to the staff but the message is in another class and i needed the name of the "t" and the name of the "p" so how can i get the name of the "p" and the name of the "t" in another class
     
    Last edited by a moderator: Jun 20, 2017
  16. Offline

    Zombie_Striker

    @LolG0D
    it does not have to be different classes, it just cannot be nestled within another method.

    You would get 't' from the hashmap. If the player is in the hashmap, then that player is the 'p'.
     
  17. Offline

    LolG0D

  18. Offline

    Zombie_Striker

    @LolG0D
    report.put(p.getUniqueID(),t.getName());
     
  19. Offline

    LolG0D

  20. Offline

    Zombie_Striker

    @LolG0D
    report.get(PlayerWhoClicked.getUniqueId())
     
  21. Offline

    LolG0D

Thread Status:
Not open for further replies.

Share This Page