%something% to text

Discussion in 'Plugin Development' started by cdnyassuo34, Aug 8, 2019.

  1. Offline

    cdnyassuo34

    Hi, I am creating a plugin. and I saw that some plugin are using %something% to put data or text.
    for exemple:
    message: player team is %myplugin_something%
    and when you go in game it replace it by his team or something else
    If anyone could tell me how to do it I would be very happy ^^
     
  2. Offline

    CraftCreeper6

  3. Offline

    cdnyassuo34

    oh thanks ^^

    @CraftCreeper6 sorry to mention you but I tryied the replace method but I just didn't knew where I had to place it to make it relace %something% to some text in any message that contains it. if you could help me I would be really happy ^^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 9, 2019
  4. Offline

    Machine Maker

    You use the method on the String whatever string.replace("%something%, other thing);
     
  5. Offline

    cdnyassuo34

    I know this method is not correct but i don't know how I could use it :/ sorry to ask many question but I just want to make sure the plugin works correctly ^^

    Code:
    public String replace(CharSequence target,CharSequence replacement)
        {
            return (String) replacement;
        }
    
    @EventHandler
        public void onChat(PlayerChatEvent e)
        {
            if(e.getPlayer() instanceof Player) {
                String msg = e.getMessage().replace("%snow_group_name%","GroupName");
                e.setCancelled(true);
                Bukkit.broadcastMessage(e.getPlayer() + msg);
            }
        }
    if anyone could help me ^^
     
  6. Offline

    SnowPingerr

    I think you are talking about placeHolders
    you should use the PlaceHolderAPI some tutorials are explaining how to use PlaceHolderAPI
     
  7. Offline

    KarimAKL

    @cdnyassuo34
    1. Use AsyncPlayerChatEvent instead of PlayerChatEvent.
    2. You don't need to check if a Player is an instance of a Player.
    3. You can use the AsyncPlayerChatEvent#setMessage(String message) method, instead of cancelling the event.
     
  8. Offline

    cdnyassuo34

    I think it is a good solution but is there anything that can just replace %something% to text ? instead of replacing all the message (to make it works with other chat plugins ^^)
     
  9. Offline

    KarimAKL

    @cdnyassuo34 Try setting the priority on the event to HIGHEST.
     
  10. Offline

    cdnyassuo34

    last question , is this the right use ? if yes would it replace %something% to text even if the %something% is used in the prefix (ex: %something% Snow : message) ?

    Code (open)
    @EventHandler
    public void placeHolders(AsyncPlayerChatEvent e)
    {
    String msg = e.getMessage();
    msg.replaceAll("%snowclaim_groupname%", getGroupName(e.getPlayer()));
    msg.replaceAll("%snowclaim_chunkcredit%", cc.get(e.getPlayer().getName()) + "");
    msg.replaceAll("%snowclaim_snowdingerr%", "§0S§4n§co§6w§ed§ai§bn§9g§de§5r§1r");
    msg.replaceAll("%snowclaim_groupname%", getChunkOwner(getPlayerChunkXZ(e.getPlayer())));
    msg.replaceAll("%snowclaim_currentchunk%", e.getPlayer().getLocation().getChunk().getX() + "," + e.getPlayer().getLocation().getChunk().getZ());

    e.setMessage(msg);
    }
     
  11. Offline

    KarimAKL

    @cdnyassuo34 You are close, but not exactly.
    1. You can use String#replace() instead of String#replaceAll() because you aren't using regex.
    2. String#replace returns a new modified string, so you need to set 'msg' to that. Example:
    Code:Java
    1. msg = msg.replace(...);
     
  12. Offline

    cdnyassuo34

    oh ok ^^ thanks ^^
    @KarimAKL (sorry to mention you :/)
    Sorry to post many question and problems I have ^-^'
    but I am using this code:
    Code:
       @EventHandler (priority = EventPriority.HIGHEST)
        public void onChat(AsyncPlayerChatEvent e)
        {
            Player p = e.getPlayer();
            String currentc = e.getPlayer().getLocation().getChunk().getX() + "," + e.getPlayer().getLocation().getChunk().getZ();
            String ggn = getGroupName(e.getPlayer().getName());
    
            e.setFormat("%snowclaim_test% " + p.getName() + e.getMessage());
        
            e.getFormat().replace("%snowclaim_ggn%", ggn);
            e.getFormat().replace("%snowclaim_curc%", currentc);
            e.getFormat().replace("%snowclaim_cc%", cc.get(p.getName()) + "");
            e.getFormat().replace("%snowclaim_test%", "1234");
            e.getFormat().replace("%snowclaim_gco%", getChunkOwner(getPlayerChunkXZ(p)));
        }
    and it returns me this error everytime I send a message in the chat. I know it's a thing with AsyncPlayerChatEvent
    but I don't know how to fix it -_
    Error (open)

    [Netty Server IO #7/ERROR]: Could not pass event AsyncPlayerChatEvent to SnowClaim v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:66) ~[craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:506) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:488) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.PlayerConnection.chat(PlayerConnection.java:1495) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1444) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.NetworkManager.a(NetworkManager.java:132) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.NetworkManager.channelRead0(NetworkManager.java:121) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at net.minecraft.server.v1_13_R2.NetworkManager.channelRead0(NetworkManager.java:1) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    Caused by: java.util.UnknownFormatConversionException: Conversion = 'p'
    at org.bukkit.event.player.AsyncPlayerChatEvent.setFormat(AsyncPlayerChatEvent.java:103) ~[craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    at fr.snowdingerr.snowclaim.mainclass.onChat(mainclass.java:698) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
    ... 48 more


    EDIT: I tryied many things but only one things work.. i put %s it replace %s by lmy name... by I just didn't registered this... so why the placeholders I have created are not working but random placeholders that I didn't created works ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Aug 12, 2019
    KarimAKL likes this.

Share This Page