Import CraftPlayer and netServerHandler problem

Discussion in 'Plugin Development' started by Hester, Mar 18, 2013.

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

    Hester

    Hey I have problem with implements CraftPlayer when i chose "organize imports" this problem disappears but then i have problem with netServerHandler (netServerHandler cannot be resolved or is not a field)

    I use craftbukkit 1.4.7 - R1

    import net.minecraft.server.v1_4_R1.EntityPlayer;
    import net.minecraft.server.v1_4_R1.Packet20NamedEntitySpawn;
    import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer;

    ((CraftPlayer) tempPlayer).getHandle().netServerHandler.sendPacket(new Packet20NamedEntitySpawn(changingName));

    Anyone know how to fix it ?
     
  2. Offline

    macguy8

    Use .playerConnection instead, it was changed... I think in 1.4
     
  3. Offline

    Hester


    Ok, supposedly work, but none of the players haven't color nick above head
    This code worked in earlier versions of craftbukkit and now not: (

    Code:
    Player[] players = Bukkit.getOnlinePlayers();
    for(int i = 0; i < players.length; i++){
                Player player = players[i];
                String oldName = player.getName();
                EntityPlayer changingName = ((CraftPlayer) player).getHandle();
                     
                changingName.name = ChatColor.RED.toString()+player.getName();
                for(Player pInWorld : Bukkit.getServer().getOnlinePlayers()){
                    if(pInWorld != player){
                        ((CraftPlayer) pInWorld).getHandle().playerConnection.sendPacket(new Packet20NamedEntitySpawn(changingName));
                    }
                }
               
                changingName.name = oldName;
            }
    any ideas?
     
  4. Offline

    fireblast709

    Why not just use the TagAPI?
     
  5. Offline

    Hester

    in TAGAPI I can not give a different color for each player. for example, Player A sees Player B and C in red. Player D is green but player B sees player A and D in red. and player C in white. Fact or I'm wrong?
     
  6. Offline

    Comphenix

    You can do that in TagAPI - use getPlayer() to retrieve who will recieve the name plate, and getNamedPlayer() to retrieve who owns the particular name plate.

    So, if the color depends on who receives the name plate instead of who is on it, the situation you described is possible.
    Code:java
    1. public class ExampleMod extends JavaPlugin implements Listener {
    2. @Override
    3. public void onEnable() {
    4. getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. @EventHandler
    8. public void onNameTag(PlayerReceiveNameTagEvent event) {
    9. // This is the player that will recieve the tag information!
    10. Player observer = event.getPlayer();
    11.  
    12. event.setTag(getColor(observer.getName().hashCode()) + event.getTag());
    13. }
    14.  
    15. private ChatColor getColor(int code) {
    16. return ChatColor.values()[Math.abs(code) % ChatColor.values().length];
    17. }
    18. }
     
  7. Offline

    Hester

    how to use this "event" to make the player has seen the player "A" in blue, player "B" on the green and player "C" in blue?

    btw when i use
    Code:
    Player[] players = Bukkit.getOnlinePlayers();
    for(int i = 0; i < players.length; i++){
                Player player = players[i];
                String oldName = player.getName();
                EntityPlayer changingName = ((CraftPlayer) player).getHandle();
                   
                changingName.name = ChatColor.RED.toString()+player.getName();
                for(Player pInWorld : Bukkit.getServer().getOnlinePlayers()){
                    if(pInWorld != player){
                        ((CraftPlayer) pInWorld).getHandle().playerConnection.sendPacket(new Packet20NamedEntitySpawn(changingName));
                    }
                }
             
                changingName.name = oldName;
            }
    player see colors when do relog - how to update the packages without relog
     
  8. Offline

    Comphenix

    You probably have to delete the original entity ID on the client side before you can send the Packet20NamedEntitySpawn packet.

    But I would strongly recommend you use TagAPI instead - it ensures your plugin is compatible with other plugins (VanishNoPacket, NameTags, etc.), and doesn't suffer from the very common downsides of the method you're using - players often appear to lose their armor and visible potion effects when you resend the spawn packet.

    Just to make things a bit clearer, I'll translate your example into a table - each row is the view of a player observing the players in each column:

    ABCD
    AxRedRedGreen
    BRedx WhiteRed
    C--x-
    D---x

    How you implement this is up to you, but you probably want to have a hash map for each player, containing a color (or perhaps an enum) that you send to the client with the setTag() method.

    Note that you have to call TagAPI.refreshPlayer() in order to "invoke" a new PlayerReceiveNameTagEvent.
     
  9. Offline

    Hester

    I still do not understand how this is working in my example. If that's not a problem you can send me an example in which you change for the player "A" nick color player "B" in red, and the player "C" on the green.?
     
  10. Offline

    Comphenix

    Just to give you an idea, here's how you would hard-code it with simple if-statements:
    Code:java
    1. @EventHandler
    2. public void onNameTag(PlayerReceiveNameTagEvent event) {
    3. // This is the player that will recieve the tag information!
    4. Player observer = event.getPlayer();
    5. Player named = event.getNamedPlayer();
    6.  
    7. if (observer.getName().equals("A")) {
    8. if (named.getName().equals("B"))
    9. event.setTag(ChatColor.RED + event.getTag());
    10. if (named.getName().equals("C"))
    11. event.setTag(ChatColor.GREEN + event.getTag());
    12. }
    13. }

    Obviously, you might want to make a more flexible version, but I leave that to you.
     
  11. Offline

    Hester

    that's not enough, I do guilds where are alliances etc. So I have to set player to that person other colors and I can not set the color also for all. Player 'X' is in alliance with the player "Y" and "Z" but the player "Y" is the only in alliance with player "X" so therefore he can not see the player "Z" in green
     
  12. Offline

    Postkutsche

    That's why Comphenix told you not to hardcode but make a more flexible version of it.
    I don't want to sound rude but you have to think about this yourself.
     
    Comphenix likes this.
  13. Offline

    Comphenix

    Of course it's enough - you just have to check if each player is in an alliance or the same guild, instead of checking player names.

    I don't know how you store alliances or guilds, but I'll assume a simple structure where alliances are collections of guilds and guilds are collections of players, where a player can only be in a single guild at any time, and a guild can only be allied with a single alliance.

    Internally, this could be represented with a simple Map<Player, Guild> (for looking up a player's guild) and Map<Guild, Alliance>:
    Code:java
    1. public interface Guild {
    2. public String getName();
    3. }
    4.  
    5. public interface Alliance {
    6. public String getName();
    7. }
    8.  
    9. private Map<Player, Guild> guilds = new WeakHashMap<Player, Guild>();
    10. private Map<Guild, Alliance> alliances = new HashMap<Guild, Alliance>();
    11.  
    12. @EventHandler
    13. public void onNameTag(PlayerReceiveNameTagEvent event) {
    14. Guild observerGuild = getGuild(event.getPlayer());
    15. Guild namedGuild = getGuild(event.getNamedPlayer());
    16.  
    17. // Are they in the same guild?
    18. if (Objects.equal(observerGuild, namedGuild)) {
    19. event.setTag(ChatColor.GREEN + event.getTag());
    20.  
    21. } else if (Objects.equal(getAlliance(observerGuild), getAlliance(namedGuild))) {
    22. event.setTag(ChatColor.WHITE + event.getTag());
    23.  
    24. // Must be an enemy
    25. } else {
    26. event.setTag(ChatColor.RED + event.getTag());
    27. }
    28. }
    29.  
    30. private Guild getGuild(Player player) {
    31. return guilds.get(player);
    32. }
    33.  
    34. private Alliance getAlliance(Guild guild) {
    35. return alliances.get(guild);
    36. }
    37.  
    38. @Override
    39. public void onEnable() {
    40. getServer().getPluginManager().registerEvents(this, this);
    41. }

    I can't be more specific than your question, so I have to assume a lot. If you want better answers, post better questions!
     
Thread Status:
Not open for further replies.

Share This Page