Problem PEX Prefixes and ASyncPlayerChatEvent

Discussion in 'Plugin Development' started by firefwing24, Aug 9, 2014.

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

    firefwing24

    Hello,
    This is my first time posting on this forum (I think), so bear with me.

    This is simply a random plugin with a bunch of features randomly in it (mostly for my own practice)

    I've been trying to make it so that prefixes appear when chatting
    So it should look something like this

    %Prefix%DisplayName%Suffix: %Message

    Here's my code.

    This is my Event Handler
    Code:java
    1.  
    2. public class ChatEvent implements Listener{
    3. public ChatEvent(TestPlugin plugin) {
    4. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    5. }
    6.  
    7. @EventHandler (priority = EventPriority.HIGHEST)
    8. public void onMessage(AsyncPlayerChatEvent e){
    9.  
    10. if (e.isCancelled()) {
    11. return;
    12. }
    13.  
    14. Player player = e.getPlayer();
    15.  
    16. String WorldName = player.getWorld().getName();
    17. PermissionUser user = PermissionsEx.getPermissionManager().getUser(player);
    18.  
    19. if (user == null)
    20. return;
    21.  
    22. String message = e.getMessage();
    23. String format = "%prefix%name%suffix: %message";
    24. format.replace("%prefix",user.getPrefix(WorldName));
    25. format.replace("%suffix",user.getSuffix(WorldName));
    26. format.replace("%name", "%1$s");
    27. format.replace("%message", "%2$s");
    28.  
    29. e.setFormat(format);
    30. e.setMessage(message);
    31.  
    32.  
    33. }
    34. }


    Then i put this in my main class
    Code:java
    1. new ChatEvent(this);


    I've tried a lot of different ways to accomplish this and I'm still not able to do it
    I tried to grab the group of the user then grab the player. (I tried with Vault, didn't work, but i didnt' really know what i was doing)

    I've been also trying to integrate Prefixes/Suffixes into my MOTD EventHandle class.
    I wanted the prefix and the suffix to appear with the DisplayName
    So something like this

    Welcome to the Server [insert PrefixDisplayNameSuffix]!

    I wasn't able to do that either.
     
  2. Offline

    xTigerRebornx

    firefwing24 Strings are immutable, meaning that it will give you a new String when call replace(). You have to assign that to something, currently you are just calling it and doing nothing with the String it returns.
     
  3. Offline

    firefwing24

    xTigerRebornx

    So should it be...
    Code:
    format = format.replace(stuffhere,stuffhere);
    But how is it still displaying <name> message completely fine?
    Because if replace doesn't actually do anything to the string by itself, shouldn't the message be changed into

    %prefix%name%suffix : %message (literally this, not their counterparts)?
     
  4. Offline

    xTigerRebornx

    firefwing24 Do you understand what it means for a method to "return" something? If not, you may want to touch up on Java. If you do, then keep in mind that Strings are immutable and will return a new String everytime a change is made to one (assuming the method changing returns a String), meaning you have to assign it to a variable so that you have the newly created String.
     
  5. Offline

    firefwing24

    xTigerRebornx
    Sorry if i ever sound noobish to coding. I'm majoring in CS but it's still somewhat new to me (esp. java) I just thought Bukkit plugins would be a fun way to become more familiar with java (this isn't my first thing with java though lol).
    I know what it means to return. Wouldn't my answer still work? It is somewhat a recursive.
    Anyways, i made another method to return replaceFormat and
    Code:java
    1.  
    2. public String replaceFormat(Player player, String format) {
    3. PermissionUser user = PermissionsEx.getPermissionManager().getUser(player);
    4. String WorldName = player.getWorld().getName();
    5. return format.replace("%prefix",user.getPrefix(WorldName)).replace("%suffix",user.getSuffix(WorldName)).replace("%name", "%1$s").replace("%message", "%2$s");
    6. }


    then I would place
    String newFormat = this.replaceFormat(player, format); in that eventHandler above.
    and change e.setFormat(format); to e.setFormat(newFormat);

    Is this correct? or am i still doing it wrong.
     
  6. Offline

    xTigerRebornx

  7. Offline

    firefwing24

    So just like before I made these changes, it still leaves it as the default chat.
    <firefwing24> hi
    instead of
    [insertprefixhere]firefwing24: hi

    There's still something wrong with my code.
     
  8. Offline

    xTigerRebornx

    firefwing24 Post the code (full class would be preferred)
     
  9. Offline

    firefwing24

    Code:java
    1. package io.github.firefwing24.TestPlugin;
    2.  
    3.  
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.AsyncPlayerChatEvent;
    9.  
    10. import ru.tehkode.permissions.PermissionUser;
    11. import ru.tehkode.permissions.bukkit.PermissionsEx;
    12.  
    13. public class ChatEvent implements Listener{
    14. public ChatEvent(TestPlugin plugin) {
    15. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    16. }
    17.  
    18. @EventHandler (priority = EventPriority.HIGHEST)
    19. public void onMessage(AsyncPlayerChatEvent e){
    20.  
    21. if (e.isCancelled()) {
    22. return;
    23. }
    24.  
    25. Player player = e.getPlayer();
    26.  
    27. PermissionUser user = PermissionsEx.getPermissionManager().getUser(player);
    28.  
    29. if (user == null)
    30. return;
    31.  
    32. String message = e.getMessage();
    33. String format = "%prefix%name%suffix: %message";
    34.  
    35. this.replaceFormat(player, format);
    36.  
    37. e.setFormat(format);
    38. e.setMessage(message);
    39.  
    40.  
    41. }
    42.  
    43. public String replaceFormat(Player player, String format) {
    44. PermissionUser user = PermissionsEx.getPermissionManager().getUser(player);
    45. String WorldName = player.getWorld().getName();
    46. return format.replace("%prefix",user.getPrefix(WorldName)).replace("%suffix",user.getSuffix(WorldName)).replace("%name", "%1$s").replace("%message", "%2$s");
    47. }
    48. }


    Actually after removing all the plugins except for my plugin and PermissionsEx i ended up with this error.
    Sorry for double post.
    Code:
    [21:50:38] [Netty IO #2/ERROR]: Could not pass event AsyncPlayerChatEvent to TestPlugin v0.0.3
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:483) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.chat(PlayerConnection.java:881) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:831) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:84) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.channelRead0(NetworkManager.java:204) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:149) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:100) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:480) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:447) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:341) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.util.io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_65]
    Caused by: java.util.UnknownFormatConversionException: Conversion = 'p'
        at org.bukkit.event.player.AsyncPlayerChatEvent.setFormat(AsyncPlayerChatEvent.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at io.github.firefwing24.TestPlugin.ChatEvent.onMessage(ChatEvent.java:37) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        ... 28 more
    
    EDIT: I realized i didn't save my code completely. I'll try again
    EDIT2: I still get same Error, Except this time it says Netty IO #1

    I did end up finding the answer to this problem.
    It was a lot more complicated than i expected.
    Only problem is that i followed some code from another plugin, so I don't think it's fair to that developer to post the straight up code.

    Some Hints:
    I forgot to add
    String newFormat = this.replaceFormat(player, format);

    Make sure you translate color codes. (you'll need another method to do this)

    For prefix/suffix for PermissionsEx, I used the follow commands. Please note that "getGroups()[0]" is a deprecated command.
    Code:java
    1.  
    2. String prefix = PermissionsEx.getUser(player).getGroups()[0].getPrefix();
    3. String suffix = PermissionsEx.getUser(player).getGroups()[0].getSuffix();


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

Share This Page