How to change the server icon?

Discussion in 'Plugin Development' started by DusRonald, Sep 9, 2014.

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

    DusRonald

    Hey Bukkit,

    I was wondering, how i can change the server icon, with a command or any trigger.

    I do not have any code, but i don't know you i must get started.

    Greeting,
    Ronald
     
    ShadowNight likes this.
  2. Offline

    Adriani6

    Just put a suitable image in the root directory of the server, I believe it must be a 64x64 px image in jpeg or png file format.
     
  3. Offline

    DusRonald

    That's not what i want to have..

    For example: I execute the command: /online, then changed the server icon to a 'Offline' icon

    Maybe the loadServerIcon method?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 19, 2018
    ShadowNight likes this.
  4. Offline

    Adriani6

    There's no other way I believe. It must have a unique name, making it impossible to trigger.
     
  5. Offline

    McMhz

    I believe you can do that with ProtocolLib, but you can try loadServerIcon.

    Edit:
    Do this in a ServerListPingEvent
    Code:java
    1. File fileObject = new File("pathToSomeImage.png");
    2. event.setServerIcon(Bukkit.loadServerIcon(fileObject));

    (Should work, not sure tho)
     
  6. Offline

    Nateb1121

  7. Offline

    DusRonald

    Can this with a command?
     
    ShadowNight likes this.
  8. Offline

    fireblast709

    DusRonald Yes.
    • Create a field of type CachedServerIcon
    • When you execute the command, it has to set that field to the icon you want to load (the CachedServerIcon loading has been posted in a previous post)
    • When the ServerListPingEvent fires, you check if the field is null. If it is not null, you set the icon in the event to the one loaded in the field.
     
  9. Offline

    DusRonald

    fireblast709 Maybe this?: (I don't have experience with ServerListPingEvent, and Server icons.
    Code:java
    1. File fileObject;
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command command,
    5. String label, String[] args) {
    6. if (command.getName().equalsIgnoreCase("online")) {
    7. // TODO: Permission Check.
    8.  
    9. File fileObject = new File("online.png");
    10. return true;
    11. }
    12. return false;
    13. }
    14.  
    15. @EventHandler
    16. public void onServerListPing(ServerListPingEvent e)
    17. if (fileObject != null) {
    18. e.setServerIcon(Bukkit.loadServerIcon(fileObject));
    19. }
    20.  
    21. }
     
    ShadowNight likes this.
  10. Offline

    fireblast709

    DusRonald Almost. fileObject on line one will always stay null because you never assign anything to it (line 9 will create a local File object instead of using the field). Just remove the 'File' from 'File fileObject' and you should be good to go.

    Also, to boost performance a bit, I recommend you do the Bukkit.loadServerIcon in the onCommand (since file IO can be slow).
     
  11. Offline

    DusRonald

    Yes, but you can i call the event?
    Bytheway: Sorry for my very bad english, i'm dutch, and it's hard for me to understand english..
     
    ShadowNight likes this.
  12. Offline

    fireblast709

    DusRonald why do you need to call the event?
     
  13. Offline

    DusRonald

    Oh i understand the code right now, At the Event althrough..
    fireblast709 Does this efficient work:
    Code:java
    1. File fileObject;
    2. public ServerState state;
    3.  
    4. private enum ServerState {
    5. ONLINE, OFFLINE;
    6. }
    7.  
    8. @Override
    9. public boolean onCommand(CommandSender sender, Command command,
    10. String label, String[] args) {
    11. if (command.getName().equalsIgnoreCase("online")) {
    12. // TODO: Permission Check.
    13.  
    14. fileObject = new File("online.png");
    15. this.state = ServerState.ONLINE;
    16.  
    17. return true;
    18. }
    19. return false;
    20. }
    21.  
    22. @EventHandler
    23. public void onServerListPing(ServerListPingEvent e)
    24. if (fileObject != null) {
    25. if(this.state == ServerState.ONLINE){
    26. e.setServerIcon((CachedServerIcon) new File("online.png"));
    27. setState(ServerState.ONLINE);
    28. } else {
    29. e.setServerIcon((CachedServerIcon) new File("offline.png"));
    30. setState(ServerState.OFFLINE);
    31. }
    32.  
    33. //e.setServerIcon(Bukkit.loadServerIcon(fileObject));
    34. }
    35.  
    36. }
    37.  
    38. public void setState(ServerState state){
    39. this.state = state;
    40. }
     
    ShadowNight likes this.
  14. Offline

    fireblast709

    DusRonald You seem to misunderstand what I meant.
    Code:java
    1. private CachedServerIcon icon;
    2. ...
    3. this.icon = Bukkit.loadServerIcon(new File("online.png")); // Could be "offline.png"
    4. ...
    5. if(this.icon != null)
    6. {
    7. e.setServerIcon(this.icon);
     
    ShadowNight likes this.
  15. Offline

    DusRonald

    Sorry for misunderstand, i try to understand, but sometimes and somethings......
     
    ShadowNight likes this.
Thread Status:
Not open for further replies.

Share This Page