Solved I need help with my code

Discussion in 'Spigot Plugin Development' started by ZMarcGamerPvP, Aug 16, 2018.

Thread Status:
Not open for further replies.
  1. Hello guys!

    I'm actually coding a plugin "CommandProtect". I made an Update Checker that notifies the user when there is a new update through the console. Now I want that the new update message appears, but in-game when a player who has an specific permission joins the server.

    I hope you can help mee as soon as possible.

    Regards!
    ZMarc.
     
  2. You can use PlayerJoinEvent and check if the player has permission, and send a message to the player if he has the permission
     
  3. You haven't solved my doubt. What I want is when there is a new version, notifies the user through the game. Because I know to do what you said.
     
  4. A new version on your plugin with the spigot page ? (Sorry i m french)
     
  5. Yes
     
  6. On Plugin load - check update - Save the result.
    If player joins has permission -> send them a message if result states new version.
     
  7. You can use the Spigot Updater api
     
  8. Something like this
    Code (Java):
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        // Here you should change checkNewerVersion() to your boolean method
        if(checkNewerVersion()&&e.getPlayer().hasPermission("notify.update")) {
            e.getPlayer().sendMessage("Hey bro, there is a new update from KKPlugin.");
        }
    }
     
    • Like Like x 1
  9. I’m using this class for my plugins :

    Code (Java):
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import org.bukkit.plugin.Plugin;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    public class SpigotUpdater extends Thread
    {
    private final Plugin plugin;
    private final int id;
    private boolean enabled = true;
    private URL url;
    public SpigotUpdater(Plugin plugin, int resourceID) throws IOException
    {
    if(plugin == null)
    {
    throw new IllegalArgumentException("Plugin cannot be null");
    }
    if(resourceID == 0)
    {
    throw new IllegalArgumentException("Resource ID cannot be null (0)");
    }
    this.plugin = plugin;
    this.id = resourceID;
    this.url = new URL("https://api.inventivetalent.org/spigot/resource-simple/" + resourceID);
    super.start();
    }
    public synchronized void start()
    {}
    public void run()
    {
    if(!this.plugin.isEnabled())
    {
    return;
    }
    if(!this.enabled)
    {
    return;
    }
    HttpURLConnection connection = null;
    try
    {
    connection = (HttpURLConnection) this.url.openConnection();
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    connection.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String content = "";
    String line = null;
    while((line = in.readLine()) != null)
    {
    content = content + line;
    }
    in.close();
    JSONObject json = null;
    try
    {
    json = (JSONObject) new JSONParser().parse(content);
    }catch(ParseException e)
    {}
    String currentVersion = null;
    if((json != null) && (json.containsKey("version")))
    {
    String version = (String) json.get("version");
    if((version != null) && (!version.isEmpty()))
    {
    currentVersion = version;
    }
    }
    if(currentVersion == null)
    {
    return;
    }
    else if(!currentVersion.equals(this.plugin.getDescription().getVersion())) {
    this.plugin.getLogger().info("[Updater] New version : http://www.spigotmc.org/resources/" + this.id);
    }
    }catch(IOException e)
    {
    if(connection != null)
    {
    try
    {
    int code = connection.getResponseCode();
    this.plugin.getLogger().warning("[Updater] Error : " + code);
    }catch(IOException e1)
    {}
    }
    e.printStackTrace();
    }
    }
    }
    Sorry I’m on my phone, it’s not very useful
     
  10. Okay, can you help me to do that?
    Im using this method:

    Code (Java):
    package com.zmarcgm.commandprotect;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;

    public class UpdateChecker {

        private URL checkURL;
        private String newVersion;
        private JavaPlugin plugin;

        public UpdateChecker(JavaPlugin plugin, int projectID) {
            this.plugin = plugin;
            newVersion = plugin.getDescription().getVersion();
            plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.DARK_GRAY + "=+=+=+=+=+=+=+=+=+=+=+= " + ChatColor.GREEN + "CommandProtect" + ChatColor.DARK_GRAY + " =+=+=+=+=+=+=+=+=+=+=+=");
            plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.GREEN + "Checking for updates...");
            try {
                checkURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + projectID);
                try {
                    this.broadCastResult();
                } catch (Exception e) {
                    plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "Couldn't check for updates, don't you have internet connection?");
                }
            } catch (MalformedURLException e) {
                plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "Couldn't check for updates, don't you have internet connection?");
            }

        }

        public String getNewVersion() throws Exception {
            URLConnection con = checkURL.openConnection();
            newVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
            return newVersion;
        }

        public int versionCompare(String paramString1, String paramString2) {
            String[] arrayOfString1 = paramString1.split("_")[0].split("\\.");
            String[] arrayOfString2 = paramString2.split("_")[0].split("\\.");
            int i = 0;
            while ((i < arrayOfString1.length) && (i < arrayOfString2.length) && (arrayOfString1[i].equals(arrayOfString2[i]))) {
                i++;
            }
            if ((i < arrayOfString1.length) && (i < arrayOfString2.length)) {
                int j = Integer.valueOf(arrayOfString1[i]).compareTo(Integer.valueOf(arrayOfString2[i]));

                return Integer.signum(j);
            }
            return Integer.signum(arrayOfString1.length - arrayOfString2.length);
        }

        public void broadCastResult() throws Exception {
            String publishedvers = this.getNewVersion();
            try {
                String pluginvers = plugin.getDescription().getVersion();
                switch (versionCompare(pluginvers, publishedvers)) {
                    case 0:
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.GREEN + "You are using the last stable version of the plugin: v" + publishedvers);
                        break;
                    case 1:
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "You have a version which isn't published, your version: v" + pluginvers);
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "the published one: v" + publishedvers + " It is possible that you are the developer");
                        break;
                    case - 1:
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "Your version is outdated, download our new update version: v" + publishedvers);
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "https://www.spigotmc.org/resources/commandprotect.56316/");
                        break;
                    default:
                        plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "Couldn't check for updates, don't you have internet connection?");
                }
            } catch (Exception known) {
                plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + ChatColor.DARK_GREEN + "CommandProtect" + ChatColor.YELLOW + "] " + ChatColor.RED + "Couldn't check for updates, don't you have internet connection?");
            }
        }
    }
     
     
  11. And in the onEnable() :

    Code (Java):
    try
    {
    new SpigotUpdater(this, idOfTheSpigotPage).start();
    }catch(IOException e)
    {
    e.printStackTrace();
    }
     
  12. The update checker works correctly, what I want to add now is a message when entering the server. This message must somehow be linked to the Update Checker, in order to send the message only if there is a new version.
     
  13. So use the try_catch in the PlayerJoinEvent
     
  14. Can you help me to do that?
     
  15. With the class SpigotUpdater that I send above, you copy the try/catch and after in the « try » you can send a message to the player who has permission
     
  16. Can you send the code?
     
  17. Code (Java):
    @EventHandler
    public void onJoin(PlayerJoinEvent e){
    if(e.getPlayer().hasPermission(« permission »){
    try
    {
    new SpigotUpdater(Main.getInstance(), idOfTheSpigotPage).start();
    e.getPlayer().sendMessage(« message »);
    }catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }
     
  18. Is not running...
     
  19. Can you send me your code ?
     
  20. What part of the code?
     
Thread Status:
Not open for further replies.