Solved Attempting to replace ":D" with "§a:D"

Discussion in 'Spigot Plugin Development' started by Caph, Jun 27, 2016.

Thread Status:
Not open for further replies.
  1. I am just starting my 'career' as a plugin developer, so I decided to create a simple plugin for my server. the plugin I made was supposed to replace with ":D" with ":D". I have been researching everywhere, but still no answer.
    Here is the code:

    Code (Text):
    public void onChat(AsyncPlayerChatEvent e){
                        String newMessage = e.getMessage().replace(":D", "§a:D");
                        e.setMessage(newMessage);

    It doesn't show up as a coloured smiley, just shows as white.

     
    #1 Caph, Jun 27, 2016
    Last edited: Jun 27, 2016
  2. use
    Code (Java):
    ChatColor#translateAlternateColorCode('&', "&a:D");
     
  3. It still didn't work for me.
     
  4. Just use
    String newMessage = e.getMessage().replace(":D", ChatColor.YOURDESIREDCOLORHERE + ":D")
     
  5. did you import the ChatColor ?
     
  6. Use
    Code (Text):
    ChatColor.translateAlternateColorCode('&', newMessage);
    This should perfectly work! :)
     
  7. we all know but he certainly looking for "&1234567890abcdef" color codes not for the chatcolor enum.
     
  8. When I do that, it send a whole lot of errors to console.

    Yes I did.
     
  9. yes it seems.
     
  10. No exceptions or such?
     
  11. tell us the error.
     
  12. Post the errors?
     
  13. Sorry XD when I was writing I didn't see your post cause my page didn't refresh :) sry all credits to : MadMaxCookie
     
  14. doesn't need the credits, I am just telling you there's already a feedback to what I've said, repeating the same message cause the same feedback of @OP.
     
    • Like Like x 1
  15. org.bukkit.command.CommandException: Unhandled exception executing command 'ab'
    in plugin AusSBroadcast v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServe
    r.java:646) ~[spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchServerCommand(Craf
    tServer.java:632) [spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.DedicatedServer.aL(DedicatedServer.java:
    437) [spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:6
    65) [spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java
    :564) [spigot-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R2.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at com.bilby.adminbroadcast.Main.onCommand(Main.java:14) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.9.4.jar:git-Spigot-8a048fe-d8637df]
    ... 8 more
     
  16. post your main class
    Code (Java):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R2.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at com.bilby.adminbroadcast.Main.onCommand(Main.java:14)
     
  17. package com.bilby.adminbroadcast;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("ab")){
    Player player = (Player) sender;
    if(player.isOp()){
    if(args[0].equalsIgnoreCase("restart")){
    Bukkit.broadcastMessage("§cAusSB " + "§8» " + "§4Server Restarting!");
    }else if(args[0].equalsIgnoreCase("reload")){
    Bukkit.broadcastMessage("§cAusSB " + "§8» " + "§4Server is reloading!");
    }
    }

    }
    return true;
    }

    public void onChat(AsyncPlayerChatEvent e){
    String newMessage = e.getMessage().replace(":D", ChatColor.BLUE + ":D");
    e.setMessage(newMessage);

    }

    }
     
  18. well
    Code (Java):
    if(sender instanceof Player) {
      Player player = (Player) sender;
    }
    and where is your onEnable() onDisable() and register the event.
     
  19. So would this be correct now?


    Code (Text):
    package com.bilby.adminbroadcast;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                    if(cmd.getName().equalsIgnoreCase("ab")){
                        if(sender instanceof Player) {
                              Player player = (Player) sender;
                              if(player.isOp()){
                            }
                            if(player.isOp()){
                                    if(args[0].equalsIgnoreCase("restart")){
                                            Bukkit.broadcastMessage("§cAusSB " + "§8» " + "§4Server Restarting!");
                                    }else if(args[0].equalsIgnoreCase("reload")){
                                            Bukkit.broadcastMessage("§cAusSB " + "§8» " + "§4Server is reloading!");
                                    }
                            }
                        }
                    }
                    return true;
            }
                   
                    public void onChat(AsyncPlayerChatEvent e){
                        String newMessage = e.getMessage().replace(":D", ChatColor.BLUE + ":D");
                        e.setMessage(newMessage);
                       
            }
                   
    }
     
  20. nope ?
    Code (Java):

    public void onEnable() {
    PluginManager pm = Bukkit.getServer().getPluginManager();
    pm.registerEvents(this, this);
    }

    public void onDisable() {
    }
     
Thread Status:
Not open for further replies.