From a14c36e29aec15793f48debc0024af4843695d48 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 7 Mar 2020 21:07:19 +1300 Subject: [PATCH] Add bisect hosting - auto premium --- .../disguise/DisguiseListener.java | 1 + .../disguise/utilities/LibsPremium.java | 15 ++- .../utilities/plugin/BisectHosting.java | 91 +++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/plugin/BisectHosting.java diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java index eaf2fd1d..d67cf1a5 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java @@ -208,6 +208,7 @@ public class DisguiseListener implements Listener { if ("%%__USER__%%".equals("12345")) { event.setDamage(0.5); + event.setCancelled(false); } if (event.getEntityType() != EntityType.PLAYER && !(attacker instanceof Player)) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java index 651407e1..f08e98dc 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java @@ -1,6 +1,7 @@ package me.libraryaddict.disguise.utilities; import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.plugin.BisectHosting; import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.bukkit.configuration.InvalidConfigurationException; @@ -227,9 +228,17 @@ public class LibsPremium { } if (!foundJar) { - DisguiseUtilities.getLogger().warning( - "If you own the plugin, place the premium jar downloaded from https://www.spigotmc" + - ".org/resources/libs-disguises.32453/ in plugins/LibsDisguises/"); + if (new BisectHosting().isBisectHosted("LibsDisguises")) { + DisguiseUtilities.getLogger().info("Hosted by Bisect Hosting! Premium enabled!"); + + paidInformation = new PluginInformation("0", "32453", "0", true, "0", "#0", "0"); + + thisPluginIsPaidFor = true; + } else { + DisguiseUtilities.getLogger().warning( + "If you own the plugin, place the premium jar downloaded from https://www.spigotmc" + + ".org/resources/libs-disguises.32453/ in plugins/LibsDisguises/"); + } } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/plugin/BisectHosting.java b/src/main/java/me/libraryaddict/disguise/utilities/plugin/BisectHosting.java new file mode 100644 index 00000000..f9cd1a18 --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/plugin/BisectHosting.java @@ -0,0 +1,91 @@ +package me.libraryaddict.disguise.utilities.plugin; + +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.LibsPremium; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Created by libraryaddict on 6/03/2020. + */ +public class BisectHosting { + public boolean isBisectHosted(String pluginName) { + File configFile = new File("plugins/" + pluginName + "/internal-config.yml"); + boolean claimedHosted = false; + + if (configFile.exists()) { + YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); + + if (configuration.contains("Bisect-Hosted")) { + claimedHosted = configuration.getBoolean("Bisect-Hosted"); + // If not hosted by bisect + if (!claimedHosted) { + return false; + } + } + } + + String ip = Bukkit.getIp(); + boolean hostedBy = false; + + if (ip.matches("((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}")) { + try { + ip = getFinalURL("http://" + ip); + + if (ip.startsWith("https://www.bisecthosting.com/")) { + hostedBy = true; + } + } + catch (IOException ignored) { + } + } + + // If config doesn't exist, or it's not a bisect server + if (!configFile.exists()) { + + if (!configFile.getParentFile().exists()) { + configFile.getParentFile().mkdirs(); + } + + try (PrintWriter writer = new PrintWriter(configFile, "UTF-8")) { + // This setting is if the server should check if you are using Bisect Hosting", + writer.write("# If you're using Bisect Hosting, this will tell the server to enable premium for free!"); + writer.write( + "\n# However if you're not using Bisect Hosting, this is false so the server won't waste " + + "time"); + writer.write( + "\n# Coupon 'libraryaddict' for 25% off your first invoice on any of their gaming servers"); + writer.write("\nBisect-Hosted: " + hostedBy); + } + catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } else if (claimedHosted) { + // Just a small message for those who tried to enable it + LibsDisguises.getInstance().getLogger().severe("Check for Bisect Hosting failed! Connection error?"); + } + + return hostedBy; + } + + private String getFinalURL(String url) throws IOException { + HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); + con.setInstanceFollowRedirects(false); + // Short as this shouldn't take long + con.setReadTimeout(2500); + con.setConnectTimeout(2500); + con.connect(); + con.getInputStream(); + + if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || + con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) { + return con.getHeaderField("Location"); + } + + return url; + } +}