2 issues with PlayerQuitEvent

Discussion in 'Plugin Development' started by RebzO1, Mar 12, 2014.

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

    RebzO1

    I have a simple plugin that i just test random stuff i learn in so i can refer to it later.

    Anyway i have this code:
    Code:java
    1. public void join(PlayerQuitEvent e) {
    2. Player p = e.getPlayer();
    3. e.setQuitMessage(ChatColor.GRAY + p.getName() + " " + ChatColor.AQUA + getConfig().getString("LEFT"));
    4. }


    Basically what i want is:
    1. to support colours in my config file (I have never done this before) so i can change it there.
    2. to be able to specify what colour the players name is in the same config file.

    My config file looks like:
    Code:
    # Here you can modify messages
    WELCOME: Welcome to the server!
    WELCOME BACK: Welcome back to the server!
    JOIN: Has joined the server!
    LEFT: Has left the server!
    Any help would be greatly appreciated
    Thanks
    Rebz

    I only figured out how to get a string from a config yesterday so if i seem like a noob i apologise.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. RebzO1 Code:
    PHP:
        @EventHandler
        
    public void quit(PlayerQuitEvent e){
            
    Player p e.getPlayer();
            
    String message ChatColor.translateAlternateColorCodes('&'getConfig().getString("LEFT"));
            
    String colour ChatColor.valueOf(getConfig().getString("COLOUR").toUpperCase());
            
    e.setQuitMessage(colour p.getName() + message);
        }
    Config:
    Code:
    LEFT: &4has left &3the server!
    COLOUR: gray
    Plus the rest of it. Out of interest, any particular reason you're using CAPS for the keys?

    EDIT: By the way, be careful with that. I didn't include any error handling for if you set the colour to something like bananas, you might want to do something with that.
     
  3. Offline

    RebzO1

    AdamQpzm
    That's not the way i tried it nemerous times lol
    I will try that now =]

    & No i can change that if its a bad idea.

    AdamQpzm
    i get the following error on the 'string colour' line
    Type mismatch: cannot convert from ChatColor to String

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. RebzO1 You don't necessarily need to change the keys, they'll work perfectly fine without the capitals, it's just generally not how it's done. :)

    Also, my bad! Change String colour to ChatColour colour
     
  5. Offline

    RebzO1

    AdamQpzm
    ok now it compiles with the following error on join event
    Show Spoiler
    [13:00:21 ERROR]: Could not pass event PlayerJoinEvent to CC-Games v1.0.8
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craft
    bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craft
    bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
    at me.rebz.games.Main.join(Main.java:87) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    ... 14 more
    >


    error is pointing to : ChatColor colour = ChatColor.valueOf(getConfig().getString("COLOUR").toUpperCase());
     
  6. Offline

    Assult

  7. Assult Is that not what I pointed out like 52 minutes ago?

    RebzO1 Something on that line is null - my immediate thought it that there isn't anything in the config as COLOUR, but it could be something else.
     
  8. Offline

    Assult

    Something in class Main on line 87 is null, can you please post that line?
     
  9. ...
     
  10. Offline

    RebzO1

    AdamQpzm

    there isnt anything in config.yml COLOUR
     
  11. Offline

    Assult

  12. RebzO1 You need to put something in there, then. :) Something like COLOUR: gray or COLOUR: aqua

    Assult We've all been there. :p
     
  13. Offline

    RebzO1

    AdamQpzm
    sorry for the delay in replying
    what your sayin makes sense lol, but i dont know where im putting it
     
  14. RebzO1 Can I see your config at the moment?
     
  15. Offline

    RebzO1

    AdamQpzm
    # Here you can modify messages
    WELCOME: Welcome to the server!
    WELCOME BACK: Welcome back to the server!
    JOIN: Has joined the server!
    LEFT: Has left the server!
     
  16. Offline

    Wizehh

    .replace('&', 'ยง');
     
  17. Offline

    RebzO1

    Wizehh
    ?
    again i am new so i kind of need things dumbed down
    this is the first time i have worked with a config.yml
     
  18. Offline

    Wizehh

    Get the string from the configuration, and replace '&' with the MC color code.
    It's just an alternative to the built-in Bukkit method.
     
  19. Offline

    acecheesecr14

  20. Offline

    RebzO1

    acecheesecr14
    if i wanted another persons plugin i would have asked for that instead of asking for help


    i had colours workin in the plugin which was easy, i jus want to be able to change them in the config
     
  21. Offline

    AoH_Ruthless

    RebzO1

    Let's say we have our config strings:
    Code:
    JOIN: '&eJoined the server.'
    LEFT: '&eLeft the server.'
    Remember, they are strings and since we aren't using the MC Section symbols we need our single quotations around them!

    Now, in our code, we want to get the config string and translate our alternate color code.
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4. e.setJoinMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("JOIN")));
    5. }

    This sets the join message to the config string "JOIN", and also translates the '&' to what minecraft uses (section symbols).

    Understand how the code works instead of blindly copy + paste.

     
Thread Status:
Not open for further replies.

Share This Page