Skip to content

Commit

Permalink
1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BenceX100 committed Jun 23, 2024
1 parent b1f851d commit 47611aa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.artillexstudios</groupId>
<artifactId>AxSellwands</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>AxSellwands</name>
Expand Down Expand Up @@ -156,7 +156,7 @@
<dependency>
<groupId>com.artillexstudios.axapi</groupId>
<artifactId>axapi</artifactId>
<version>1.4.242</version>
<version>1.4.259</version>
<scope>compile</scope>
<classifier>all</classifier>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.loader.LoaderSettings;
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.updater.UpdaterSettings;
import com.artillexstudios.axapi.nms.NMSHandlers;
import com.artillexstudios.axapi.utils.FastFieldAccessor;
import com.artillexstudios.axapi.reflection.FastFieldAccessor;
import com.artillexstudios.axapi.utils.FeatureFlags;
import com.artillexstudios.axapi.utils.MessageUtils;
import com.artillexstudios.axapi.utils.StringUtils;
Expand All @@ -23,6 +23,7 @@
import com.artillexstudios.axsellwands.utils.CommandMessages;
import com.artillexstudios.axsellwands.utils.FileUtils;
import com.artillexstudios.axsellwands.utils.NumberUtils;
import com.artillexstudios.axsellwands.utils.UpdateNotifier;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -129,6 +130,8 @@ public void enable() {
getServer().getPluginManager().registerEvents(new InventoryClickListener(), this);

Bukkit.getConsoleSender().sendMessage(StringUtils.formatToString("&#FF5500[AxSellwands] Loaded plugin!"));

if (CONFIG.getBoolean("update-notifier.enabled", true)) new UpdateNotifier(this, 5725);
}

public void updateFlags() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.artillexstudios.axsellwands.utils;

import com.artillexstudios.axapi.AxPlugin;
import com.artillexstudios.axapi.scheduler.Scheduler;
import com.artillexstudios.axapi.utils.NumberUtils;
import com.artillexstudios.axapi.utils.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.HashMap;

import static com.artillexstudios.axsellwands.AxSellwands.CONFIG;
import static com.artillexstudios.axsellwands.AxSellwands.LANG;
import static java.time.temporal.ChronoUnit.SECONDS;

public class UpdateNotifier implements Listener {
private final int id;
private final String current;
private final AxPlugin instance;
private String latest = null;
private boolean newest = true;

public UpdateNotifier(AxPlugin instance, int id) {
this.id = id;
this.current = instance.getDescription().getVersion();
this.instance = instance;

instance.getServer().getPluginManager().registerEvents(this, instance);

long time = 30L * 60L * 20L;
Scheduler.get().runAsyncTimer(t -> {
this.latest = readVersion();
this.newest = isLatest(current);

if (latest == null || newest) return;
Bukkit.getConsoleSender().sendMessage(getMessage());
t.cancel();
}, 50L, time);
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
if (latest == null || newest) return;
if (!CONFIG.getBoolean("update-notifier.on-join", true)) return;
if (!event.getPlayer().hasPermission(instance.getName().toLowerCase() + ".update-notify")) return;
Scheduler.get().runLaterAsync(t -> {
event.getPlayer().sendMessage(getMessage());
}, 50L);
}

private String getMessage() {
HashMap<String, String> map = new HashMap<>();
map.put("%current%", current);
map.put("%latest%", latest);
return StringUtils.formatToString(CONFIG.getString("prefix") + LANG .getString("update-notifier"), map);
}

@Nullable
private String readVersion() {
try {
final HttpClient client = HttpClient.newHttpClient();
final HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://api.polymart.org/v1/getResourceInfoSimple/?resource_id=" + id + "&key=version"))
.timeout(Duration.of(10, SECONDS))
.GET()
.build();

final HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body().toString();
} catch (Exception ex) {
return null;
}
}

public String getLatest() {
return latest;
}

public boolean isLatest(String current) {
return getWeight(latest) <= getWeight(current);
}

private int getWeight(String version) {
if (version == null) return 0;
String[] s = version.split("\\.");
if (!NumberUtils.isInt(s[0]) || !NumberUtils.isInt(s[1]) || !NumberUtils.isInt(s[2])) return 0;
int res = 0;
res += Integer.parseInt(s[0]) * 1000000;
res += Integer.parseInt(s[1]) * 1000;
res += Integer.parseInt(s[2]);
return res;
}
}
6 changes: 4 additions & 2 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nothing-sold: "&#FFAA55Nothing in the container is sellable!"

cooldown: "&#FFAA55You must wait another &f%time% seconds &#FFAA55before using this sellwand again!"

no-permission: "&#FFAA55You don't have a permission to use a sellwant on this container!"
no-permission: "&#FFAA55You don't have a permission to use a sellwand on this container!"

disallowed-container: "&#FFAA55This sellwand can't be used on this type of container!"

Expand Down Expand Up @@ -73,5 +73,7 @@ commands:
invalid-player: "&#FF0000The player &#BB0000%player% &#FF0000can not be found!"
invalid-selector: "&#FF0000You can not use this selector in this command!"

update-notifier: "&#FFAA00There is a new version of the plugin available! &#DDDDDD(&#FFFFFFcurrent: &#FF0000%current% &#DDDDDD| &#FFFFFFlatest: &#00FF00%latest%&#DDDDDD)"

# do not change this
version: 2
version: 3

0 comments on commit 47611aa

Please sign in to comment.