Solved Setting player skin as server pic

Discussion in 'Plugin Help/Development/Requests' started by sgavster, Mar 4, 2015.

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

    sgavster

    That worked, I have a question.. I dont want to make a new post.. I want to make it so that the players avatar will be the server picture.. I want to do it so the players names arent in a hashmap so it will always be stored.. how would I do it with config? I have this:
    Code:
    package me.sgavster.qmc.func;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import me.sgavster.qmc.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    
    public class motdSet implements Listener {
    
        public static Main main;
    
        public motdSet(Main m) {
            main = m;
        }
    
        public Map<String, String> playerData = new HashMap<String, String>();
        public ArrayList<Player> joined = new ArrayList<Player>();
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            if (main.getConfig().contains(p.getName())) {
                if (!joined.contains(p)) {
                    joined.add(p);
                }
            } else {
                main.getConfig().set("Has Joined", p.getName());
                if (!joined.contains(p)) {
                    joined.add(p);
                }
                p.sendMessage("worked");
            }
            String playerIP = p.getAddress().getAddress().toString();
            playerIP = playerIP.replaceAll("/", "");
            playerIP = playerIP.replaceAll("\\.", "-");
            if (!(playerData.containsKey(playerIP))) {
                playerData.put(playerIP, p.getName());
            }
            if (!playerData.containsKey(playerIP)) {
                String imageUrl = "https://minotar.net/avatar/"
                        + playerData.get(playerIP) + "/64.png";
                String destinationFile = (main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
    
                try {
                    saveImage(imageUrl, destinationFile);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    
        public static void saveImage(String imageUrl, String destinationFile)
                throws IOException {
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(destinationFile);
    
            byte[] b = new byte[2048];
            int length;
    
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
    
            is.close();
            os.close();
        }
    
        @EventHandler
        public void motd(ServerListPingEvent e) {
            e.setMaxPlayers(e.getNumPlayers() + 1);
            String playerIP = e.getAddress().toString();
            playerIP = playerIP.replaceAll("/", "");
            playerIP = playerIP.replaceAll("\\.", "-");
            e.setMotd("§6Welcome back, §4" + playerData.get(playerIP) + "§6!");
            // if (playerData.containsKey(playerIP)) {
            if (main.getConfig().contains(playerData.get(playerIP))
                    || joined.contains(playerData.get(playerIP))) {
                e.setMotd("§6Welcome back, §4" + playerData.get(playerIP) + "§6!");
                String imageUrl = "https://minotar.net/avatar/"
                        + playerData.get(playerIP) + "/64.png";
                String destinationFile = (main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
                try {
                    saveImage(imageUrl, destinationFile);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                File icon;
                icon = new File(main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
                try {
                    e.setServerIcon(Bukkit.getServer().loadServerIcon(icon));
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (UnsupportedOperationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                e.setMotd("§6Welcome!");
    
            }
        }
    }
    
    With this error:

    [​IMG]

    Thanks for any help.. I can't figure it out :/
    I get that error when I have never joined, and it says "Welcome back, null"

    When I join it says "Welcome back, sgavster" and shows my picture.
     
    Last edited: Mar 4, 2015
  2. @sgavster What is line 48 of drugs.java?
     
  3. Offline

    Skionz

  4. Offline

    sgavster

  5. Offline

    SuchSwegMuchWow

    @sgavster Why do you have so much checks?

    Code:
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(event.getMaterial() == Material.SOMETHING && event.getItem() != null))){
                   
                    //Stuff
                   
                }   
            }
            
     
  6. Offline

    Skionz

    @sgavster It seems like you are checking if its type isn't null. Check if the ItemStack itself isn't null.
     
  7. @sgavster Sorry, didn't see that, try checking if i equals Material.AIR
     
  8. Offline

    sgavster

    That worked, I have a question.. I dont want to make a new post.. I want to make it so that the players avatar will be the server picture.. I want to do it so the players names arent in a hashmap so it will always be stored.. how would I do it with config? I have this:
    Code:
    package me.sgavster.qmc.func;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import me.sgavster.qmc.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    
    public class motdSet implements Listener {
    
        public static Main main;
    
        public motdSet(Main m) {
            main = m;
        }
    
        public Map<String, String> playerData = new HashMap<String, String>();
        public ArrayList<Player> joined = new ArrayList<Player>();
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            if (main.getConfig().contains(p.getName())) {
                if (!joined.contains(p)) {
                    joined.add(p);
                }
            } else {
                main.getConfig().set("Has Joined", p.getName());
                if (!joined.contains(p)) {
                    joined.add(p);
                }
                p.sendMessage("worked");
            }
            String playerIP = p.getAddress().getAddress().toString();
            playerIP = playerIP.replaceAll("/", "");
            playerIP = playerIP.replaceAll("\\.", "-");
            if (!(playerData.containsKey(playerIP))) {
                playerData.put(playerIP, p.getName());
            }
            if (!playerData.containsKey(playerIP)) {
                String imageUrl = "https://minotar.net/avatar/"
                        + playerData.get(playerIP) + "/64.png";
                String destinationFile = (main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
    
                try {
                    saveImage(imageUrl, destinationFile);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    
        public static void saveImage(String imageUrl, String destinationFile)
                throws IOException {
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(destinationFile);
    
            byte[] b = new byte[2048];
            int length;
    
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
    
            is.close();
            os.close();
        }
    
        @EventHandler
        public void motd(ServerListPingEvent e) {
            e.setMaxPlayers(e.getNumPlayers() + 1);
            String playerIP = e.getAddress().toString();
            playerIP = playerIP.replaceAll("/", "");
            playerIP = playerIP.replaceAll("\\.", "-");
            e.setMotd("§6Welcome back, §4" + playerData.get(playerIP) + "§6!");
            // if (playerData.containsKey(playerIP)) {
            if (main.getConfig().contains(playerData.get(playerIP))
                    || joined.contains(playerData.get(playerIP))) {
                e.setMotd("§6Welcome back, §4" + playerData.get(playerIP) + "§6!");
                String imageUrl = "https://minotar.net/avatar/"
                        + playerData.get(playerIP) + "/64.png";
                String destinationFile = (main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
                try {
                    saveImage(imageUrl, destinationFile);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                File icon;
                icon = new File(main.getDataFolder() + File.separator
                        + "PlayerFaces" + playerData.get(playerIP) + ".png");
                try {
                    e.setServerIcon(Bukkit.getServer().loadServerIcon(icon));
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (UnsupportedOperationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                e.setMotd("§6Welcome!");
    
            }
        }
    }
    
    With this error:

    [​IMG]

    Thanks for any help.. I can't figure it out :/
    I get that error when I have never joined, and it says "Welcome back, null"

    When I join it says "Welcome back, sgavster" and shows my picture.
     
    Last edited: Mar 4, 2015
  9. Offline

    sgavster

    bump, instead of making a new post I just changed the info and the title.
     
  10. @sgavster Just create a new thread instead and set this one to solved by going to Thread Tools > Edit Title > Prefix > Solved.
     
  11. Offline

    sgavster

    @CodePlaysMinecraft Okay. I had made 2 posts the day I post this so I didn't want people to think I was spamming. I will do that.
     
  12. @sgavster It's okay, as long as you don't post multiple threads of the same problem, you aren't spamming. :)
     
    sgavster likes this.
  13. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
Thread Status:
Not open for further replies.

Share This Page