First Plugin - change textcolor if..

Discussion in 'Plugin Development' started by Script1996, Jul 8, 2014.

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

    Script1996

    Hey guys,

    I'm new in developing Bukkitplugins and want to create my own chatextensions plugin.

    including textcolor and so on...

    Now i want to ask you how to change the color of the chattext if(playername ="XXX").

    i got
    Code:java
    1. public void playerChat(AsyncPlayerChatEvent e){
    2. String mess= e.getMessage();
    3. Player p1= e.getPlayer();
    4.  
    5. if (p1.equals("XXX")){
    6. e.setFormat(ChatColor.BLUE + "<"+p1.getDisplayName()+">"+e.getMessage());
    7. }
    8. }


    but my text is just white like normal ..

    Hope you can help me starting to develope :)

    Greetings,
    Script
     
  2. Offline

    ItsLeoFTW

    Hey!

    Assuming you want to make the chat green if the player's name is, let's say, Jake, then you would do this:

    Code:java
    1. Player p = e.getPlayer();
    2. if (p.getName().equalsIgnoreCase("Jake")){
    3. e.setFormat(ChatColor.BLUE + "<"+p.getDisplayName()+">"+ChatColor.GREEN + e.getMessage());
    4. }

    Hope this helps!
     
  3. Offline

    TheHandfish

    In other words, the only problem was with the p.equals() line. When you do that, you're not checking his name, you're checking if the Player entity equals something else. What you want to so is check if p.getName().equals("XXX");.
     
  4. Offline

    Script1996

    Okay. Now i got:
    Code:java
    1. public void playerChat(AsyncPlayerChatEvent e){
    2. String mess= e.getMessage();
    3. Player p1= e.getPlayer();
    4.  
    5. if (p1.getName().equals("XXX")){
    6. e.setFormat(ChatColor.BLUE + "<"+p1.getDisplayName()+">"+ChatColor.GREEN+mess);
    7. }
    8. }


    but it's always white.

    Thanks for your help :)
     
  5. Offline

    indyetoile

    Script1996

    You don't need to convert the message to a string.
    Use the following code to edit the chat format:
    Code:java
    1. public void playerChat(AsyncPlayerChatEvent e){
    2. Player p1= e.getPlayer();
    3.  
    4. if (p1.getName().equals("XXX")){
    5. e.setFormat(ChatColor.BLUE + "<" +"%s" +">" + ChatColor.GREEN+ "%s");
    6. }
    7. }


    However, wouldn't it be less hassle to use permissions instead of checking the player name? You would end up with something like this.
    Code:java
    1. public void playerChat(AsyncPlayerChatEvent e){
    2. Player p1= e.getPlayer();
    3.  
    4. if (p1.hasPermission("chatformat.default")){
    5. e.setFormat(ChatColor.BLUE + "<"+ "%s" + ">" + ChatColor.GREEN + "%s");
    6. }
    7. }

    Change the 'chatformat.default' to whatever you'd like the required permission to be.
     
  6. Offline

    teej107

    Script1996
    You are checking to see if the player's name is XXX. If your player name isn't XXX, then your if statement will be false and any code inside of it will not execute.
     
  7. Offline

    mythbusterma

    Permission checks aren't thread safe, don't treat them as such.
     
  8. Offline

    indyetoile

  9. Offline

    teej107

    indyetoile
    Change the event to a PlayerChatEvent
     
  10. Offline

    TheHandfish

    teej107: PlayerChatEvent is deprecated... which means you should avoid using it unless completely necessary.
     
  11. Offline

    teej107

    TheHandfish Deprecated doesn't always mean you should avoid it. The JavaDocs for PlayerChatEvent states:
    Deprecated. This event will fire from the main thread and allows the use of all of the Bukkit API, unlike the AsyncPlayerChatEvent.
    Listening to this event forces chat to wait for the main thread which causes delays for chat. AsyncPlayerChatEvent is the encouraged alternative for thread safe implementations.
    If the OP was going to go with permission checks, PlayerChatEvent is the safest option.
     
    Zupsub likes this.
  12. Offline

    TheHandfish

    It sounds useful. But usually when something is deprecated, it means there's a better method. So why is it deprecated? :I

    Wouldn't it be better to save whether or not he has a permission in a metadata? I just don't want him to end up using an event that will be dropped later.
     
  13. Offline

    xTigerRebornx

    TheHandfish Its deprecated simply because it can cause delays in chat when listening to it, if the user wanted to use the Async event, they'd have to take precautions to do it in a thread safe manner.
     
  14. Offline

    Script1996

    okay guys. now I'm confused :eek:.

    This code won't work. My Ingamename is Script and i tried it that way:

    Code:
    public void playerChat(AsyncPlayerChatEvent e){
    Player p1= e.getPlayer();
     
    if (p1.getName().equals("Script")){
    e.setFormat(ChatColor.BLUE + "<" +"%s" +">" + ChatColor.GREEN+ "%s");
    }
    }
    i don't want to do this over permissions. mostly its just a test plugin but it would be great if i had success
     
  15. Offline

    maved145

    Script1996 Are you registering your events?
    Paste your whole class please.
     
  16. Offline

    Script1996

    Register ? :eek:

    okay thats everything.

    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6.  
    7. package ch.script.chatex;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.player.AsyncPlayerChatEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Java extends JavaPlugin {
    17. @Override
    18. public void onEnable(){
    19. System.out.println("[ChatEx] Plugin wurde aktiviert");
    20. }
    21.  
    22. @Override
    23. public void onDisable(){
    24. System.out.println("[ChatEx] Plugin wurde deaktiviert");
    25. }
    26. @Override
    27. public boolean onCommand(CommandSender verfasser, Command cmd, String commandLabel, String[] args){
    28.  
    29. if(cmd.getName().equalsIgnoreCase("farben")){
    30. verfasser.sendMessage("%blau% = Blau\n"+
    31. "%gruen% = Gruen\n"+
    32. "%rot% = Rot"
    33. );
    34. return true;
    35. }
    36. return false;
    37. }
    38. public void playerChat(AsyncPlayerChatEvent e){
    39. Player p1= e.getPlayer();
    40.  
    41. if (p1.getName().equals("Script")){
    42. e.setFormat(ChatColor.BLUE + "<" +"%s" +">" + ChatColor.GREEN+ "%s");
    43. }
    44. }
    45.  
    46. }
    47.  
     
  17. Offline

    maved145

    Script1996

    Well before:
    publicvoid playerChat(AsyncPlayerChatEvent e){

    add @EventHandler

    And in your on enable add:

    getServer().getPluginManager().registerEvents(this, this);

    It should work then.
     
    Script1996 likes this.
  18. Offline

    Script1996

    Okay. now i got this. and if i click on cast, my server crashes
    [​IMG]
     
  19. Offline

    indyetoile

    Script1996
    You don't need to add @Override above your onEnable().
     
  20. Offline

    Zupsub

  21. Offline

    TheHandfish

    ^^ You should, though.

    Script1996: make sure to implement Listener in your main class:

    Code:
    public class Java extends JavaPlugin implements Listener
     
    Script1996 likes this.
  22. Offline

    Script1996

    thank you guys. it works !! :))

    Zupsub
    lolz. i'm in an education in software developing and know how to deal with java. I just didn't know that I have to add listener for chat. :p :)
     
  23. Offline

    TheHandfish

    Script1996 likes this.
Thread Status:
Not open for further replies.

Share This Page