Skip to content

Commit

Permalink
Optimized and enable/disable update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathildeuh committed Jun 6, 2024
1 parent 43ebf2e commit 24f77cd
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 83 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {


group = 'fr.Mathildeuh'
version = '1.3.1'
version = '1.3.2'

repositories {
mavenCentral()
Expand Down Expand Up @@ -68,7 +68,8 @@ processResources {
compileJava.options.encoding("UTF-8")


libsDirectory = file("E:\\Plugins\\Spigot")
//libsDirectory = file("E:\\Plugins\\Spigot")
libsDirectory = file("E:\\Servers\\1.20.6\\plugins")
shadowJar {
archiveClassifier.set('')
relocate 'org.bstats', 'fr.mathildeuh.utils'
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/fr/mathildeuh/worldmanager/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
import fr.mathildeuh.worldmanager.util.UpdateChecker;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

public final class WorldManager extends JavaPlugin {

public static BukkitAudiences adventure;
public static File configFile;
public static FileConfiguration config;
public static FileConfiguration worldsConfig;
public static BackupConfig backupConfig;
private static SpiGUI spiGUI;
private boolean updated = true;
Expand Down Expand Up @@ -50,6 +52,8 @@ public void onEnable() {

spiGUI = new SpiGUI(this);

saveDefaultConfig();

new Metrics(this, 22073);
adventure = BukkitAudiences.create(this);

Expand All @@ -59,29 +63,30 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new JoinListener(), this);

configFile = new File("backups/WorldManager/backups.yml");
config = YamlConfiguration.loadConfiguration(configFile);
worldsConfig = YamlConfiguration.loadConfiguration(configFile);
try {
if (config.getString("backups") == null)
config.set("backups", "");
config.save(configFile);
backupConfig = new BackupConfig(configFile, config);
if (worldsConfig.getString("backups") == null)
worldsConfig.set("backups", "");
worldsConfig.save(configFile);
backupConfig = new BackupConfig(configFile, worldsConfig);
} catch (IOException e) {
throw new RuntimeException(e);
}

configFile = new File(getDataFolder(), "worlds.yml");
config = YamlConfiguration.loadConfiguration(configFile);
worldsConfig = YamlConfiguration.loadConfiguration(configFile);
try {
if (config.getString("worlds") == null)
config.set("worlds", "");
config.save(configFile);
if (worldsConfig.getString("worlds") == null)
worldsConfig.set("worlds", "");
worldsConfig.save(configFile);
} catch (IOException e) {
throw new RuntimeException(e);
}

WorldsConfig.loadWorlds();

update();
if (getConfig().getBoolean("update-checker"))
update();


}
Expand All @@ -90,12 +95,11 @@ private void update() {
new UpdateChecker(this, 117043).getVersion(version -> {
if (this.getDescription().getVersion().equals(version.replace("V", ""))) {
updated = true;
getLogger().info("No update available.");
} else {
updated = false;
System.out.println("An update is available for WorldManager.");
System.out.println("New version: " + version);
System.out.println("Download it here: " + UpdateChecker.RESOURCE_URL);
Bukkit.getLogger().log(Level.INFO, "An update is available for WorldManager.");
Bukkit.getLogger().log(Level.INFO, "New version: " + version.replace("V", "") + " | Current version: " + this.getDescription().getVersion());
Bukkit.getLogger().log(Level.INFO, "Download it here: " + UpdateChecker.RESOURCE_URL);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.Map;

public class Pregen {
private final JavaPlugin plugin = JavaPlugin.getPlugin(WorldManager.class);
public static final Map<String, WorldPreGenerator> generators = new HashMap<>();
private final JavaPlugin plugin = JavaPlugin.getPlugin(WorldManager.class);
private final MessageManager message;
private final CommandSender sender;
Player player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.mathildeuh.worldmanager.WorldManager;
import fr.mathildeuh.worldmanager.messages.MessageManager;
import fr.mathildeuh.worldmanager.util.UpdateChecker;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -17,6 +18,7 @@
import java.net.http.HttpResponse;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;

public class JoinListener implements Listener {
private static final String GITHUB_API_URL = "https://api.github.com/repos/%s/%s/releases/latest";
Expand Down Expand Up @@ -61,12 +63,12 @@ public String getLatestReleaseNote() {
}
return body;
} else {
System.out.println("Could not fetch the latest release note. Status code: " + response.statusCode());
Bukkit.getLogger().log(Level.INFO, "Could not fetch the latest release note. Status code: " + response.statusCode());
return "";
}

} catch (IOException | InterruptedException e) {
System.out.println("Could not fetch the latest release note.");
Bukkit.getLogger().log(Level.INFO, "Could not fetch the latest release note.");
return "";
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/fr/mathildeuh/worldmanager/gui/GUIBackups.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.util.Objects;
import java.util.stream.Collectors;

public class GUIBackups implements Listener {
public class GUIBackups implements Listener {
SGMenu menu;

public GUIBackups(WorldManager plugin) {
Bukkit.getPluginManager().registerEvents(this, plugin);
}
SGMenu menu ;


private List<String> getBackupFiles() {
File backupDirectory = new File(Bukkit.getServer().getWorldContainer(), "backups/WorldManager");
Expand All @@ -51,15 +51,14 @@ public void open(Player player) {
if (menuSize <= 9) {
menuSize = 18;
}
menu = WorldManager.getSpiGUI().create("&6Select world", menuSize/9);
menu = WorldManager.getSpiGUI().create("&6Select world", menuSize / 9);

int id = 0;


for (World world : sortedWorlds) {



menu.setButton(id, new SGButton(new ItemBuilder(Material.GRASS_BLOCK)
.name("§a" + world.getName())
.lore("§7Click to backup or restore this world")
Expand Down Expand Up @@ -100,8 +99,6 @@ private void openChoose(Player player, World world) {
new Backup(player).execute(world.getName());
}));

System.out.println(world.getName());
System.out.println(getBackupFiles());
boolean isBackup = getBackupFiles().contains(world.getName() + ".zip");
String name = isBackup ? "§aRestore" : "§cNo backup found";
String lore = isBackup ? "§7Backup found, you can restore this world" : "§cUse §e\"Backup\" §cbutton to create one";
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/fr/mathildeuh/worldmanager/gui/GUIDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import java.util.List;
import java.util.stream.Collectors;

public class GUIDelete implements Listener {
public GUIDelete() {
Bukkit.getPluginManager().registerEvents(this, WorldManager.getPlugin(WorldManager.class));
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/fr/mathildeuh/worldmanager/gui/GUILoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
import com.samjakob.spigui.item.ItemBuilder;
import com.samjakob.spigui.menu.SGMenu;
import fr.mathildeuh.worldmanager.WorldManager;
import fr.mathildeuh.worldmanager.commands.subcommands.Backup;
import fr.mathildeuh.worldmanager.commands.subcommands.Load;
import fr.mathildeuh.worldmanager.commands.subcommands.Restore;
import fr.mathildeuh.worldmanager.commands.subcommands.Unload;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedMainHandEvent;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

public class GUILoad implements Listener {
SGMenu menu;

public GUILoad(WorldManager plugin) {
Bukkit.getPluginManager().registerEvents(this, plugin);
}

SGMenu menu;

public void open(Player player) {


Expand All @@ -40,7 +37,7 @@ public void open(Player player) {
menuSize = 27;
}

menu = WorldManager.getSpiGUI().create("&aChoose a world", menuSize/9);
menu = WorldManager.getSpiGUI().create("&aChoose a world", menuSize / 9);

int id = 0;
for (File worldFolder : worldFolders) {
Expand Down Expand Up @@ -81,7 +78,7 @@ public void open(Player player) {
menu.setRowsPerPage(menuSize / 9);
}

menu.setButton(menuSize- 1, new SGButton(new ItemBuilder(Material.BARRIER)
menu.setButton(menuSize - 1, new SGButton(new ItemBuilder(Material.BARRIER)
.name("§cBack")
.lore("§7Click to open main menu")
.build()
Expand All @@ -90,7 +87,6 @@ public void open(Player player) {
}));



player.openInventory(menu.getInventory());
}
}
40 changes: 20 additions & 20 deletions src/main/java/fr/mathildeuh/worldmanager/gui/GUIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

public class GUIMain {

static SGMenu menu;
private final WorldManager plugin;
private final Player player;

Expand All @@ -25,33 +26,14 @@ public GUIMain(Player player) {

openMainMenu();
}
static SGMenu menu;

private void openMainMenu() {
menu = WorldManager.getSpiGUI().create("&9{------ World Manager -----}", 3);

menu.setButton(11, createButton(Material.TARGET, "§aCreate a world", "§7World creator options", event -> {
new GUICreate(plugin).open(player);
}));

menu.setButton(13, createButton(Material.GRASS_BLOCK, "§aManage your worlds", "§7World manager options", event -> {
openWorldChoose(player);
}));

menu.setButton(15, createButton(Material.DEBUG_STICK, "§aLoad a world", "§7World loader option", event -> {
new GUILoad(plugin).open(player);
}));

player.openInventory(menu.getInventory());
}

public static void openWorldChoose(Player player) {
List<String> worldNames = Bukkit.getWorlds().stream().map(org.bukkit.World::getName).collect(Collectors.toList());
int menuSize = Math.min((worldNames.size() + 8) / 9 * 9, 54);
if (menuSize <= 9) {
menuSize = 27;
}
menu = WorldManager.getSpiGUI().create("&aChoose a world to manage", menuSize/9);
menu = WorldManager.getSpiGUI().create("&aChoose a world to manage", menuSize / 9);

List<World> sortedWorlds = Bukkit.getWorlds().stream()
.filter(world -> !world.equals(Bukkit.getWorlds().get(0)))
Expand Down Expand Up @@ -87,6 +69,24 @@ public static void openWorldChoose(Player player) {

}

private void openMainMenu() {
menu = WorldManager.getSpiGUI().create("&9{------ World Manager -----}", 3);

menu.setButton(11, createButton(Material.TARGET, "§aCreate a world", "§7World creator options", event -> {
new GUICreate(plugin).open(player);
}));

menu.setButton(13, createButton(Material.GRASS_BLOCK, "§aManage your worlds", "§7World manager options", event -> {
openWorldChoose(player);
}));

menu.setButton(15, createButton(Material.DEBUG_STICK, "§aLoad a world", "§7World loader option", event -> {
new GUILoad(plugin).open(player);
}));

player.openInventory(menu.getInventory());
}

private SGButton createButton(Material material, String name, String lore, SGButtonListener listener) {
return new SGButton(new ItemBuilder(material)
.name(name)
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/fr/mathildeuh/worldmanager/gui/GUIManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.samjakob.spigui.menu.SGMenu;
import fr.mathildeuh.worldmanager.WorldManager;
import fr.mathildeuh.worldmanager.commands.subcommands.Backup;
import fr.mathildeuh.worldmanager.commands.subcommands.Delete;
import fr.mathildeuh.worldmanager.commands.subcommands.Restore;
import fr.mathildeuh.worldmanager.commands.subcommands.Unload;
import org.bukkit.Material;
Expand All @@ -17,13 +16,13 @@ public class GUIManage {

Player player;
World world;
SGMenu menu;

public GUIManage(Player player, World world) {
this.player = player;
this.world = world;
}

SGMenu menu;

public void open() {
menu = WorldManager.getSpiGUI().create("&cManage " + world.getName(), 3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static void restoreWorld(Player player, String name) {
List<Player> worldPlayers = new ArrayList<>();

File backupFile = new File(worldFolder.getParentFile(), "backups/WorldManager/" + name.toLowerCase() + ".zip");
System.out.println(backupFile.getAbsolutePath());

if (backupFile.exists()) {
World world = Bukkit.getWorld(name);
Expand All @@ -82,7 +81,6 @@ public static void restoreWorld(Player player, String name) {
for (Player players : worldPlayers) {
players.teleport(Bukkit.getWorlds().get(0).getSpawnLocation());
}
System.out.println("World unloading started");
Bukkit.unloadWorld(world, false);

}
Expand Down
Loading

0 comments on commit 24f77cd

Please sign in to comment.