Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/craftaro/SongodaCore
Browse files Browse the repository at this point in the history
…into development
  • Loading branch information
ceze88 committed Jul 22, 2024
2 parents 4e4b152 + 4cd0c02 commit 91a92bc
Show file tree
Hide file tree
Showing 68 changed files with 900 additions and 255 deletions.
2 changes: 1 addition & 1 deletion Compatibility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.craftaro</groupId>
<artifactId>CraftaroCore-Modules</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>CraftaroCore-Compatibility</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.craftaro.core.compatibility;

import org.apache.commons.lang3.ArrayUtils;

public enum MajorServerVersion {
UNKNOWN, V1_7, V1_8, V1_9, V1_10, V1_11, V1_12, V1_13, V1_14, V1_15, V1_16, V1_17, V1_18, V1_19, V1_20, V1_21, V1_22, V1_23;

private static final MajorServerVersion SERVER_VERSION = getVersion();

public boolean isLessThan(MajorServerVersion other) {
if (SERVER_VERSION == UNKNOWN) {
return false;
}

return this.ordinal() < other.ordinal();
}

public boolean isAtOrBelow(MajorServerVersion other) {
if (SERVER_VERSION == UNKNOWN && other != UNKNOWN) {
return false;
}

return this.ordinal() <= other.ordinal();
}

public boolean isGreaterThan(MajorServerVersion other) {
if (SERVER_VERSION == UNKNOWN) {
return false;
}

return this.ordinal() > other.ordinal();
}

public boolean isAtLeast(MajorServerVersion other) {
if (SERVER_VERSION == UNKNOWN && other != UNKNOWN) {
return false;
}

return this.ordinal() >= other.ordinal();
}

public static MajorServerVersion getServerVersion() {
return SERVER_VERSION;
}

public static boolean isServerVersion(MajorServerVersion version) {
return SERVER_VERSION == version;
}

public static boolean isServerVersion(MajorServerVersion... versions) {
return ArrayUtils.contains(versions, SERVER_VERSION);
}

public static boolean isServerVersionAbove(MajorServerVersion version) {
if (SERVER_VERSION == UNKNOWN) {
return false;
}

return SERVER_VERSION.ordinal() > version.ordinal();
}

public static boolean isServerVersionAtLeast(MajorServerVersion version) {
if (SERVER_VERSION == UNKNOWN && version != UNKNOWN) {
return false;
}

return SERVER_VERSION.ordinal() >= version.ordinal();
}

public static boolean isServerVersionAtOrBelow(MajorServerVersion version) {
if (SERVER_VERSION == UNKNOWN && version != UNKNOWN) {
return false;
}

return SERVER_VERSION.ordinal() <= version.ordinal();
}

public static boolean isServerVersionBelow(MajorServerVersion version) {
if (SERVER_VERSION == UNKNOWN) {
return false;
}

return SERVER_VERSION.ordinal() < version.ordinal();
}

private static MajorServerVersion getVersion() {
for (MajorServerVersion version : values()) {
if (ServerVersion.serverPackageVersion.toUpperCase().startsWith(version.name())) {
return version;
}
}

return UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public enum ServerVersion {

;

private static final String serverPackageVersion;
static final String serverPackageVersion;
private static final String serverReleaseVersion;
private static final ServerVersion serverVersion;
private static final boolean isMocked;
Expand Down
2 changes: 1 addition & 1 deletion Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.craftaro</groupId>
<artifactId>CraftaroCore-Modules</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>CraftaroCore</artifactId>
Expand Down
92 changes: 4 additions & 88 deletions Core/src/main/java/com/craftaro/core/SongodaCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.craftaro.core.commands.CommandManager;
import com.craftaro.core.compatibility.ClientVersion;
import com.craftaro.core.core.LocaleModule;
import com.craftaro.core.core.PluginInfo;
import com.craftaro.core.core.PluginInfoModule;
import com.craftaro.core.core.SongodaCoreCommand;
import com.craftaro.core.core.SongodaCoreDiagCommand;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
Expand All @@ -20,24 +17,14 @@
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;

public class SongodaCore {
Expand Down Expand Up @@ -230,55 +217,11 @@ private void register(JavaPlugin plugin, int pluginID, String icon, String libra
getLogger().info(getPrefix() + "Hooked " + plugin.getName() + ".");
PluginInfo info = new PluginInfo(plugin, pluginID, icon, libraryVersion);

// don't forget to check for language pack updates ;)
info.addModule(new LocaleModule());
registeredPlugins.add(info);
this.tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> update(info), 60L));
}

/**
* @deprecated Seems useless and will probably be replaced in the near future
*/
@Deprecated
private void update(PluginInfo plugin) {
try {
URL url = new URL("https://update.songoda.com/index.php?plugin=" + plugin.getSongodaId()
+ "&version=" + plugin.getJavaPlugin().getDescription().getVersion()
+ "&updaterVersion=" + updaterVersion);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setConnectTimeout(5000);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

int numCharsRead;
char[] charArray = new char[1024];
StringBuilder sb = new StringBuilder();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
urlConnection.disconnect();

String jsonString = sb.toString();
JSONObject json = (JSONObject) new JSONParser().parse(jsonString);

plugin.setLatestVersion((String) json.get("latestVersion"));
plugin.setMarketplaceLink((String) json.get("link"));
plugin.setNotification((String) json.get("notification"));
plugin.setChangeLog((String) json.get("changeLog"));

plugin.setJson(json);

for (PluginInfoModule module : plugin.getModules()) {
module.run(plugin);
}
} catch (IOException ex) {
final String er = ex.getMessage();
getLogger().log(Level.FINE, "Connection with Songoda servers failed: " + (er.contains("URL") ? er.substring(0, er.indexOf("URL") + 3) : er));
} catch (ParseException ex) {
getLogger().log(Level.FINE, "Failed to parse json for " + plugin.getJavaPlugin().getName() + " update check");
if (plugin.getDescription().getWebsite() != null && plugin.getDescription().getWebsite().contains("songoda.com/")) {
info.setMarketplaceLink(plugin.getDescription().getWebsite());
}

registeredPlugins.add(info);
}

public static List<PluginInfo> getPlugins() {
Expand Down Expand Up @@ -391,33 +334,6 @@ void onEnable(PluginEnableEvent event) {
}

private class EventListener implements Listener {
final HashMap<UUID, Long> lastCheck = new HashMap<>();

@EventHandler
void onLogin(PlayerLoginEvent event) {
final Player player = event.getPlayer();

// don't spam players with update checks
long now = System.currentTimeMillis();
Long last = this.lastCheck.get(player.getUniqueId());

if (last != null && now - 10000 < last) {
return;
}

this.lastCheck.put(player.getUniqueId(), now);

// is this player good to revieve update notices?
if (!event.getPlayer().isOp() && !player.hasPermission("songoda.updatecheck")) return;

// check for updates! ;)
for (PluginInfo plugin : getPlugins()) {
if (plugin.getNotification() != null && plugin.getJavaPlugin().isEnabled())
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin.getJavaPlugin(), () ->
player.sendMessage("[" + plugin.getJavaPlugin().getName() + "] " + plugin.getNotification()), 10L);
}
}

@EventHandler
void onDisable(PluginDisableEvent event) {
// don't track disabled plugins
Expand Down
64 changes: 0 additions & 64 deletions Core/src/main/java/com/craftaro/core/core/LocaleModule.java

This file was deleted.

48 changes: 0 additions & 48 deletions Core/src/main/java/com/craftaro/core/core/PluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.craftaro.core.dependency.DependencyLoader;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -18,12 +17,7 @@ public final class PluginInfo {
private final String coreLibraryVersion;

private final List<PluginInfoModule> modules = new ArrayList<>();
private boolean hasUpdate = false;
private String latestVersion;
private String notification;
private String changeLog;
private String marketplaceLink;
private JSONObject json;

public PluginInfo(JavaPlugin javaPlugin, int songodaId, String icon, String coreLibraryVersion) {
this.javaPlugin = javaPlugin;
Expand All @@ -33,40 +27,6 @@ public PluginInfo(JavaPlugin javaPlugin, int songodaId, String icon, String core
this.coreLibraryVersion = coreLibraryVersion;
}

public String getLatestVersion() {
return this.latestVersion;
}

public void setLatestVersion(String latestVersion) {
this.latestVersion = latestVersion;

this.hasUpdate = latestVersion != null && !latestVersion.isEmpty() && !this.javaPlugin.getDescription().getVersion().equalsIgnoreCase(latestVersion);
}

public String getNotification() {
return this.notification;
}

public void setNotification(String notification) {
this.notification = notification;
}

public boolean hasUpdate() {
return this.hasUpdate;
}

public void setHasUpdate(boolean hasUpdate) {
this.hasUpdate = hasUpdate;
}

public String getChangeLog() {
return this.changeLog;
}

public void setChangeLog(String changeLog) {
this.changeLog = changeLog;
}

public String getMarketplaceLink() {
return this.marketplaceLink;
}
Expand All @@ -75,14 +35,6 @@ public void setMarketplaceLink(String marketplaceLink) {
this.marketplaceLink = marketplaceLink;
}

public JSONObject getJson() {
return this.json;
}

public void setJson(JSONObject json) {
this.json = json;
}

public PluginInfoModule addModule(PluginInfoModule module) {
this.modules.add(module);

Expand Down
Loading

0 comments on commit 91a92bc

Please sign in to comment.