Skip to content

Commit

Permalink
Add donation and update notification and fix some small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMeinerLP committed Jul 24, 2024
1 parent 0604248 commit 98bd03b
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
// Utils
implementation(libs.serverlib)
implementation(libs.paperlib)
implementation(libs.semver)
// Stats
implementation(libs.bstats)
// Commands
Expand Down
3 changes: 3 additions & 0 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
- source: src/main/resources/bettergopaint.properties
translation: /src/main/resources/bettergopaint_%locale_with_underscore%.properties
59 changes: 54 additions & 5 deletions src/main/java/net/onelitefeather/bettergopaint/BetterGoPaint.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
package net.onelitefeather.bettergopaint;

import com.fastasyncworldedit.core.Fawe;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.TranslationRegistry;
import net.kyori.adventure.util.UTF8ResourceBundleControl;
import net.onelitefeather.bettergopaint.brush.PlayerBrushManager;
import net.onelitefeather.bettergopaint.command.GoPaintCommand;
import net.onelitefeather.bettergopaint.command.ReloadCommand;
import net.onelitefeather.bettergopaint.listeners.ConnectListener;
import net.onelitefeather.bettergopaint.listeners.InteractListener;
import net.onelitefeather.bettergopaint.listeners.InventoryListener;
import net.onelitefeather.bettergopaint.objects.other.Settings;
import net.onelitefeather.bettergopaint.service.UpdateService;
import net.onelitefeather.bettergopaint.translations.PluginTranslationRegistry;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
Expand All @@ -45,21 +52,23 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Level;

public class BetterGoPaint extends JavaPlugin implements Listener {

public static final String PAPER_DOCS = "https://jd.papermc.io/paper/1.20.6/org/bukkit/Material.html#enum-constant-summary";
public static final String USE_PERMISSION = "bettergopaint.use";
public static final String ADMIN_PERMISSION = "bettergopaint.admin";
public static final String RELOAD_PERMISSION = "bettergopaint.command.admin.reload";
public static final String WORLD_BYPASS_PERMISSION = "bettergopaint.world.bypass";

private final PlayerBrushManager brushManager = new PlayerBrushManager();
private final Metrics metrics = new Metrics(this, 18734);
private UpdateService updateService;

@Override
public void onLoad() {
Expand All @@ -84,6 +93,30 @@ public void onEnable() {

reloadConfig();

final TranslationRegistry translationRegistry = new PluginTranslationRegistry(TranslationRegistry.create(Key.key("bettergopaint", "translations")));
translationRegistry.defaultLocale(Locale.US);
Path langFolder = getDataFolder().toPath().resolve("lang");
var languages = new HashSet<>(Settings.settings().generic.LANGUAGES);
languages.add("en-US");
if (Files.exists(langFolder)) {
try (var urlClassLoader = new URLClassLoader(new URL[]{langFolder.toUri().toURL()})) {
languages.stream().map(Locale::forLanguageTag).forEach(r -> {
var bundle = ResourceBundle.getBundle("bettergopaint", r, urlClassLoader, UTF8ResourceBundleControl.get());
translationRegistry.registerAll(r, bundle, false);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
languages.stream().map(Locale::forLanguageTag).forEach(r -> {
var bundle = ResourceBundle.getBundle("bettergopaint", r, UTF8ResourceBundleControl.get());
translationRegistry.registerAll(r, bundle, false);
});
}
GlobalTranslator.translator().addSource(translationRegistry);
donationInformation();


Material brush = Settings.settings().generic.DEFAULT_BRUSH;
if (!brush.isItem()) {
getComponentLogger().error("{} is not a valid default brush, it has to be an item", brush.name());
Expand All @@ -103,6 +136,7 @@ public void onEnable() {
@Override
public void onDisable() {
metrics.shutdown();
this.updateService.shutdown();
}

public void reloadConfig() {
Expand All @@ -117,6 +151,13 @@ public void reloadConfig() {

}

private void updateService() {
this.updateService = new UpdateService(this);
this.updateService.run();
this.updateService.notifyConsole(getComponentLogger());
}


@SuppressWarnings("UnstableApiUsage")
private void registerCommands() {
Bukkit.getCommandMap().register("gopaint", getPluginMeta().getName(), new GoPaintCommand(this));
Expand All @@ -132,13 +173,17 @@ private void registerListeners() {
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new InventoryListener(getBrushManager()), this);
pm.registerEvents(new InteractListener(this), this);
pm.registerEvents(new ConnectListener(getBrushManager()), this);
pm.registerEvents(new ConnectListener(getBrushManager(), this), this);
}

private boolean hasOriginalGoPaint() {
return getServer().getPluginManager().getPlugin("goPaint") != this;
}

private void donationInformation() {
getComponentLogger().info(Component.translatable("bettergopaint.notify.donation.console"));
}

private @Nullable AnnotationParser<CommandSender> enableCommandSystem() {
try {
LegacyPaperCommandManager<CommandSender> commandManager = LegacyPaperCommandManager.createNative(
Expand All @@ -161,4 +206,8 @@ private boolean hasOriginalGoPaint() {
return brushManager;
}

public UpdateService getUpdateService() {
return updateService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.onelitefeather.bettergopaint.BetterGoPaint;
import net.onelitefeather.bettergopaint.objects.other.Settings;
import net.onelitefeather.bettergopaint.brush.PlayerBrush;
import net.onelitefeather.bettergopaint.utils.Constants;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
Expand Down Expand Up @@ -50,12 +51,12 @@ public boolean execute(
}
PlayerBrush pb = plugin.getBrushManager().getBrush(p);
String prefix = Settings.settings().generic.PREFIX;
if (!p.hasPermission(BetterGoPaint.USE_PERMISSION)) {
if (!p.hasPermission(Constants.USE_PERMISSION)) {
p.sendRichMessage(prefix + "<red>You are lacking the permission bettergopaint.use");
return true;
}
if (args.length == 0) {
if (p.hasPermission(BetterGoPaint.ADMIN_PERMISSION)) {
if (p.hasPermission(Constants.ADMIN_PERMISSION)) {
p.sendRichMessage(prefix + "<red>/gp size<gray>|<red>toggle<gray>|<red>info<gray>|<red>reload");
return true;
}
Expand All @@ -75,7 +76,7 @@ public boolean execute(
}
return true;
} else if ((args[0].equalsIgnoreCase("reload") || args[0].equalsIgnoreCase("r")) && p.hasPermission(
BetterGoPaint.ADMIN_PERMISSION)) {
Constants.ADMIN_PERMISSION)) {
plugin.reloadConfig();
p.sendRichMessage(prefix + "<green>Reloaded");
return true;
Expand All @@ -84,7 +85,7 @@ public boolean execute(
p.sendRichMessage(prefix + "<aqua>Links: <gold><click:open_url:https://twitter.com/themeinerlp'><u>Twitter</u></click>");
return true;
}
if (p.hasPermission(BetterGoPaint.ADMIN_PERMISSION)) {
if (p.hasPermission(Constants.ADMIN_PERMISSION)) {
p.sendRichMessage(prefix + "<red>/gp size<gray>|<red>toggle<gray>|<red>info<gray>|<red>reload");
return true;
}
Expand All @@ -102,7 +103,7 @@ public boolean execute(
return true;
}
}
if (p.hasPermission(BetterGoPaint.ADMIN_PERMISSION)) {
if (p.hasPermission(Constants.ADMIN_PERMISSION)) {
p.sendRichMessage(prefix + "<red>/gp size<gray>|<red>toggle<gray>|<red>info<gray>|<red>reload");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package net.onelitefeather.bettergopaint.command;

import net.onelitefeather.bettergopaint.BetterGoPaint;
import net.onelitefeather.bettergopaint.utils.Constants;
import org.bukkit.entity.Player;
import org.incendo.cloud.annotations.Command;
import org.incendo.cloud.annotations.Permission;
Expand All @@ -32,7 +33,7 @@ public ReloadCommand(final BetterGoPaint betterGoPaint) {
}

@Command("bgp|gp reload")
@Permission(BetterGoPaint.RELOAD_PERMISSION)
@Permission(Constants.RELOAD_PERMISSION)
public void onReload(Player player) {
betterGoPaint.reloadConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,43 @@
*/
package net.onelitefeather.bettergopaint.listeners;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.onelitefeather.bettergopaint.BetterGoPaint;
import net.onelitefeather.bettergopaint.brush.PlayerBrushManager;
import net.onelitefeather.bettergopaint.objects.other.Settings;
import net.onelitefeather.bettergopaint.utils.Constants;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;

public class ConnectListener implements Listener {

private final PlayerBrushManager brushManager;
private final BetterGoPaint betterGoPaint;

public ConnectListener(@NotNull PlayerBrushManager brushManager) {
public ConnectListener(@NotNull PlayerBrushManager brushManager, final BetterGoPaint paint) {
this.brushManager = brushManager;
this.betterGoPaint = paint;
}

@EventHandler(priority = EventPriority.LOWEST)
public void onQuit(@NotNull PlayerQuitEvent event) {
brushManager.removeBrush(event.getPlayer());
}

@EventHandler(priority = EventPriority.LOWEST)
public void onJoin(@NotNull PlayerJoinEvent event) {
var player = event.getPlayer();
if (player.isOp() || (player.hasPermission(Constants.ADMIN_PERMISSION) && !player.hasPermission(Constants.DISABLE_DONATION_NOTIFY))) {
player.sendMessage(Component.translatable("bettergopaint.notify.donation.player").arguments(MiniMessage.miniMessage()
.deserialize(Settings.settings().generic.PREFIX)));
}
if (player.isOp() || player.hasPermission(Constants.PERMISSION_NOTIFY_UPDATE)) {
this.betterGoPaint.getUpdateService().notifyPlayer(player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.onelitefeather.bettergopaint.brush.PlayerBrush;
import net.onelitefeather.bettergopaint.objects.brush.Brush;
import net.onelitefeather.bettergopaint.objects.other.Settings;
import net.onelitefeather.bettergopaint.utils.Constants;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
import org.bukkit.block.Block;
Expand All @@ -52,7 +53,7 @@ public InteractListener(BetterGoPaint plugin) {
public void onClick(PlayerInteractEvent event) {
Player player = event.getPlayer();

if (!player.hasPermission(BetterGoPaint.USE_PERMISSION)) {
if (!player.hasPermission(Constants.USE_PERMISSION)) {
return;
}

Expand Down Expand Up @@ -85,7 +86,7 @@ public void onClick(PlayerInteractEvent event) {
return;
}

final boolean hasNotWorldByePassPermission = !player.hasPermission(BetterGoPaint.WORLD_BYPASS_PERMISSION);
final boolean hasNotWorldByePassPermission = !player.hasPermission(Constants.WORLD_BYPASS_PERMISSION);

if (hasNotWorldByePassPermission && Settings.settings().generic.DISABLED_WORLDS
.contains(location.getWorld().getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public final class Settings extends Config {
@Comment("This is related to generic settings")
public static class Generic {

@Comment("Supported Languages")
public List<String> LANGUAGES = new ArrayList<>();

@Comment({
"Default brush item",
"Possible values: " + BetterGoPaint.PAPER_DOCS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package net.onelitefeather.bettergopaint.service;

import com.github.zafarkhaja.semver.Version;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.onelitefeather.bettergopaint.BetterGoPaint;
import net.onelitefeather.bettergopaint.objects.other.Settings;
import net.onelitefeather.bettergopaint.utils.Constants;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;

public final class UpdateService implements Runnable {
private final HttpClient hangarClient = HttpClient.newBuilder().build();
private static final Logger LOGGER = LoggerFactory.getLogger(UpdateService.class);
private final Version localVersion;
private Version remoteVersion;
private final BukkitTask scheduler;
private final String DOWNLOAD_URL = "https://hangar.papermc.io/OneLiteFeather/BetterGoPaint/versions/%s";

public UpdateService(BetterGoPaint betterGoPaint) {
this.localVersion = Version.parse(betterGoPaint.getPluginMeta().getVersion());
this.scheduler = Bukkit.getScheduler().runTaskTimerAsynchronously(betterGoPaint, this, 0, 20 * 60 * 60 * 3);
}


@Override
public void run() {
var remoteVersion = getNewerVersion();
if (remoteVersion != null) {
this.remoteVersion = remoteVersion;
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.isOp() || onlinePlayer.hasPermission(Constants.PERMISSION_NOTIFY_UPDATE)) {
notifyPlayer(localVersion, remoteVersion, onlinePlayer);
}
}
}
}

public void notifyConsole(ComponentLogger logger) {
if (this.remoteVersion != null && this.remoteVersion.isHigherThan(this.localVersion)) {
logger.warn(Component.translatable("bettergopaint.notify.update.console")
.arguments(Component.text(localVersion.toString()),
Component.text(remoteVersion.toString()),
Component.text(DOWNLOAD_URL.formatted(remoteVersion.toString()))
));
}
}


public void notifyPlayer(Player player) {
if (this.remoteVersion != null && this.remoteVersion.isHigherThan(this.localVersion)) {
notifyPlayer(this.localVersion, this.remoteVersion, player);
}
}


private void notifyPlayer(Version localVersion, Version remoteVersion, Player player) {
player.sendMessage(Component.translatable("bettergopaint.notify.update.player")
.arguments(
MiniMessage.miniMessage().deserialize(Settings.settings().generic.PREFIX),
Component.text(localVersion.toString()),
Component.text(remoteVersion.toString())
));
}


@Nullable
private Version getNewerVersion() {
try {
HttpResponse<String> httpResponse = hangarClient.send(Constants.LATEST_RELEASE_VERSION_REQUEST, HttpResponse.BodyHandlers.ofString());
Version remoteVersion = Version.parse(httpResponse.body());
if (remoteVersion.isHigherThan(this.localVersion)) {
return remoteVersion;
}
} catch (IOException | InterruptedException e) {
LOGGER.error("Something went wrong to check updates", e);
}
return null;
}

public void shutdown() {
this.hangarClient.shutdownNow();
this.scheduler.cancel();
}
}
Loading

0 comments on commit 98bd03b

Please sign in to comment.