Mise à jour du code sur git

This commit is contained in:
Maxlego08 2021-05-14 13:30:00 +02:00
parent 08a760e5c2
commit a998cffbe0
16 changed files with 653 additions and 70 deletions

View File

@ -1,7 +1,7 @@
name: zKoth
author: Maxlego08
main: fr.maxlego08.zkoth.ZKothPlugin
version: 2.0.1.1
version: 2.0.1.3
softdepend: [PlaceholderAPI, Guilds, Factions, FactionsX, SuperiorSkyblock2, LegacyFactions, FeatherBoard, TAB, TitleManager, UltimateFactions]
commands:
api-version: 1.13

View File

@ -153,6 +153,24 @@ public class ZKoth extends ZUtils implements Koth {
}
@Override
public void spawn(boolean now) {
if (this.minLocation == null || this.maxLocation == null) {
return;
} else if (this.isCooldown) {
return;
} else if (this.isEnable) {
return;
} else {
if (now)
spawnNow();
else
spawn();
}
}
private void spawn() {
this.isCooldown = true;
@ -404,7 +422,7 @@ public class ZKoth extends ZUtils implements Koth {
this.commands.forEach(command -> {
command = replaceMessage(command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(command, player));
});
Location center = cuboid.getCenter();

View File

@ -2,6 +2,7 @@ package fr.maxlego08.zkoth;
import org.bukkit.plugin.ServicePriority;
import be.maximvdw.featherboard.ch;
import fr.maxlego08.zkoth.api.KothManager;
import fr.maxlego08.zkoth.command.CommandManager;
import fr.maxlego08.zkoth.command.commands.CommandKoth;
@ -9,6 +10,7 @@ import fr.maxlego08.zkoth.inventory.InventoryManager;
import fr.maxlego08.zkoth.listener.AdapterListener;
import fr.maxlego08.zkoth.save.Config;
import fr.maxlego08.zkoth.save.MessageLoader;
import fr.maxlego08.zkoth.scheduler.SchedulerManager;
import fr.maxlego08.zkoth.scoreboard.ScoreBoardManager;
import fr.maxlego08.zkoth.scoreboard.implementations.FeatherBoardHook;
import fr.maxlego08.zkoth.scoreboard.implementations.TabPremiumHook;
@ -18,6 +20,7 @@ import fr.maxlego08.zkoth.zcore.logger.Logger;
import fr.maxlego08.zkoth.zcore.logger.Logger.LogType;
import fr.maxlego08.zkoth.zcore.utils.plugins.Metrics;
import fr.maxlego08.zkoth.zcore.utils.plugins.Plugins;
import fr.maxlego08.zkoth.zcore.utils.plugins.VersionChecker;
/**
* System to create your plugins very simply Projet:
@ -29,6 +32,7 @@ import fr.maxlego08.zkoth.zcore.utils.plugins.Plugins;
public class ZKothPlugin extends ZPlugin {
private KothManager kothManager;
private SchedulerManager scheduler;
private final MessageLoader messageLoader = new MessageLoader(this);
@Override
@ -38,6 +42,7 @@ public class ZKothPlugin extends ZPlugin {
this.scoreboardManager = new ScoreBoardManager();
this.kothManager = new ZKothManager(this.scoreboardManager);
this.scheduler = new SchedulerManager(this.kothManager);
this.getServer().getServicesManager().register(KothManager.class, kothManager, this, ServicePriority.High);
@ -59,34 +64,38 @@ public class ZKothPlugin extends ZPlugin {
addSave(Config.getInstance());
addSave((ZKothManager) kothManager);
addSave(messageLoader);
addSave(scheduler);
if (this.isEnable(Plugins.FEATHERBOARD)) {
this.scoreboardManager.setScoreboard(new FeatherBoardHook());
}
try {
if (this.isEnable(Plugins.TAB) && Class.forName("me/neznamy/tab/api/TABAPI") != null) {
this.scoreboardManager.setScoreboard(new TabPremiumHook());
}
} catch (ClassNotFoundException e) {
}
if (this.isEnable(Plugins.TITLEMANAGER)) {
this.scoreboardManager.setScoreboard(new TitleManagerHook());
}
this.scoreboardManager.setDefaultScoreboard();
Logger.info("Load " + this.scoreboardManager.getScoreboard().getClass().getName() + " scoreboard manager");
if (this.isEnable(Plugins.PLACEHOLDERAPI)){
if (this.isEnable(Plugins.PLACEHOLDERAPI)) {
Logger.info("Load PlaceHolderAPI", LogType.INFO);
KothExpension expension = new KothExpension(this, kothManager);
expension.register();
}
getSavers().forEach(saver -> saver.load(getPersist()));
new Metrics(this, 6924);
VersionChecker checker = new VersionChecker(this, 9);
checker.useLastVersion();
postEnable();
}
@ -110,4 +119,8 @@ public class ZKothPlugin extends ZPlugin {
return kothManager;
}
public SchedulerManager getSchedulerManager() {
return scheduler;
}
}

View File

@ -101,6 +101,12 @@ public interface Koth {
* @param now
*/
public void spawn(CommandSender sender, boolean now);
/**
*
* @param now
*/
public void spawn(boolean now);
/**
*

View File

@ -2,6 +2,7 @@ package fr.maxlego08.zkoth.command.commands;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.command.commands.scheduler.CommandKothScheduler;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
@ -27,6 +28,7 @@ public class CommandKoth extends VCommand {
this.addSubCommand(new CommandKothSetCapture());
this.addSubCommand(new CommandKothAxe());
this.addSubCommand(new CommandKothVersion());
this.addSubCommand(new CommandKothScheduler());
}
@Override

View File

@ -0,0 +1,33 @@
package fr.maxlego08.zkoth.command.commands.scheduler;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
public class CommandKothScheduler extends VCommand {
public CommandKothScheduler() {
this.setPermission(Permission.ZKOTH_SCHEDULER);
this.setDescription(Message.DESCRIPTION_SCHEDULER);
this.addSubCommand("scheduler", "s");
this.addSubCommand(new CommandKothSchedulerList());
this.addSubCommand(new CommandKothSchedulerAdd());
this.addSubCommand(new CommandKothSchedulerRemove());
}
@Override
protected CommandType perform(ZKothPlugin main) {
this.subVCommands.forEach(command -> {
String message = Message.COMMAND_SYNTAXE_HELP.getMessage();
message = message.replace("%command%", command.getSyntaxe());
message = message.replace("%description%", command.getDescription());
messageWO(sender, message);
});
return CommandType.SUCCESS;
}
}

View File

@ -0,0 +1,71 @@
package fr.maxlego08.zkoth.command.commands.scheduler;
import java.util.Optional;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.api.Koth;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.scheduler.Scheduler;
import fr.maxlego08.zkoth.scheduler.SchedulerType;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
public class CommandKothSchedulerAdd extends VCommand {
public CommandKothSchedulerAdd() {
this.setPermission(Permission.ZKOTH_SCHEDULER);
this.setDescription(Message.DESCRIPTION_SCHEDULER_ADD);
this.addSubCommand("add");
this.addRequireArg("delay/repeat");
this.addRequireArg("koth name");
this.addOptionalArg("day");
this.addOptionalArg("hour");
this.addOptionalArg("minute");
}
@Override
protected CommandType perform(ZKothPlugin main) {
SchedulerType type = SchedulerType.valueOf(argAsString(0).toUpperCase());
Optional<Koth> optional = main.getKothManager().getKoth(argAsString(1));
if (!optional.isPresent()) {
message(sender, Message.ZKOTH_DOESNT_EXIST);
return CommandType.DEFAULT;
}
Koth totem = optional.get();
Scheduler scheduler;
switch (type) {
case DELAY:
String day = argAsString(2);
if (!isDay(day)) {
message(sender, Message.KOTH_SCHEDULER_ERROR, day);
return CommandType.SUCCESS;
}
int hour = argAsInteger(3);
int minute = argAsInteger(4);
scheduler = new Scheduler(type, day, hour, minute, totem.getName());
break;
case REPEAT:
minute = argAsInteger(2);
scheduler = new Scheduler(type, minute, totem.getName());
break;
default:
return CommandType.SYNTAX_ERROR;
}
main.getSchedulerManager().addScheduler(sender, scheduler);
return CommandType.SUCCESS;
}
}

View File

@ -0,0 +1,25 @@
package fr.maxlego08.zkoth.command.commands.scheduler;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
public class CommandKothSchedulerList extends VCommand {
public CommandKothSchedulerList() {
this.setPermission(Permission.ZKOTH_SCHEDULER);
this.setDescription(Message.DESCRIPTION_SCHEDULER_LIST);
this.addSubCommand("list");
}
@Override
protected CommandType perform(ZKothPlugin main) {
main.getSchedulerManager().show(sender);
return CommandType.SUCCESS;
}
}

View File

@ -0,0 +1,29 @@
package fr.maxlego08.zkoth.command.commands.scheduler;
import java.util.UUID;
import fr.maxlego08.zkoth.ZKothPlugin;
import fr.maxlego08.zkoth.command.VCommand;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.commands.CommandType;
public class CommandKothSchedulerRemove extends VCommand {
public CommandKothSchedulerRemove() {
this.setPermission(Permission.ZKOTH_SCHEDULER);
this.setDescription(Message.DESCRIPTION_SCHEDULER_REMOVE);
this.addSubCommand("remove");
this.addRequireArg("UUID");
}
@Override
protected CommandType perform(ZKothPlugin main) {
UUID uuid = UUID.fromString(argAsString(0));
main.getSchedulerManager().remove(sender, uuid);
return CommandType.SUCCESS;
}
}

View File

@ -0,0 +1,108 @@
package fr.maxlego08.zkoth.scheduler;
import java.util.Calendar;
import fr.maxlego08.zkoth.zcore.utils.ZUtils;
public class Scheduler extends ZUtils{
private final SchedulerType type;
private final String day;
private final int hour;
private final int minutes;
private final String totemName;
private long lastCreate;
/**
*
* @param type
* @param day
* @param hour
* @param minutes
* @param totemName
*/
public Scheduler(SchedulerType type, String day, int hour, int minutes, String totemName) {
super();
this.type = type;
this.day = day;
this.hour = hour;
this.minutes = minutes;
this.totemName = totemName;
}
/**
*
* @param type
* @param hour
* @param totemName
*/
public Scheduler(SchedulerType type, int minute, String totemName) {
this(type, null, 0, minute, totemName);
lastCreate = System.currentTimeMillis() + (1000 * 60 * minute);
}
/**
* @return the lastCreate
*/
public long getLastCreate() {
return lastCreate;
}
/**
* @return the totemName
*/
public String getTotemName() {
return totemName;
}
/**
* @return the type
*/
public SchedulerType getType() {
return type;
}
/**
* @return the day
*/
public String getDay() {
return day;
}
/**
* @return the hour
*/
public int getHour() {
return hour;
}
/**
* @return the minutes
*/
public int getMinutes() {
return minutes;
}
/**
*
* @param calendar
* @return
*/
public boolean toggle(Calendar calendar) {
switch (type) {
case DELAY:
return (calendar.get(Calendar.HOUR_OF_DAY) == hour && calendar.get(Calendar.MINUTE) == minutes
&& super.getDay().equalsIgnoreCase(day));
case REPEAT:
if (lastCreate != 0 && System.currentTimeMillis() > lastCreate) {
lastCreate = System.currentTimeMillis() + (1000 * 60 * minutes);
return true;
}
return false;
default:
return false;
}
}
}

View File

@ -0,0 +1,204 @@
package fr.maxlego08.zkoth.scheduler;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import fr.maxlego08.zkoth.api.Koth;
import fr.maxlego08.zkoth.api.KothManager;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.logger.Logger;
import fr.maxlego08.zkoth.zcore.logger.Logger.LogType;
import fr.maxlego08.zkoth.zcore.utils.ZUtils;
import fr.maxlego08.zkoth.zcore.utils.builder.TimerBuilder;
import fr.maxlego08.zkoth.zcore.utils.storage.Persist;
import fr.maxlego08.zkoth.zcore.utils.storage.Saveable;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.HoverEvent.Action;
import net.md_5.bungee.api.chat.TextComponent;
public class SchedulerManager extends ZUtils implements Saveable {
private static List<Scheduler> schedulers = new ArrayList<Scheduler>();
private transient Map<UUID, Scheduler> map = new HashMap<>();
private transient boolean isRunning = false;
private final transient KothManager manager;
public SchedulerManager(KothManager manager) {
super();
this.manager = manager;
}
@Override
public void save(Persist persist) {
persist.save(this, "schedulers");
}
@Override
public void load(Persist persist) {
persist.loadOrSaveDefault(this, SchedulerManager.class, "schedulers");
run();
}
/**
* Ajout d'un scheduler
*
* @param sender
* @param scheduler
*/
public void addScheduler(CommandSender sender, Scheduler scheduler) {
schedulers.add(scheduler);
message(sender, Message.ZKOTH_SCHEDULER_CREATE, scheduler.getTotemName());
run();
}
public void run() {
if (isRunning)
return;
isRunning = true;
scheduleFix(1000, (task, canRun) -> {
if (!canRun || !isRunning || schedulers.size() == 0) {
task.cancel();
isRunning = false;
return;
}
Calendar calendar = Calendar.getInstance();
Iterator<Scheduler> iterator = schedulers.iterator();
while (iterator.hasNext()) {
Scheduler scheduler = iterator.next();
if (scheduler.toggle(calendar)) {
Optional<Koth> optional = manager.getKoth(scheduler.getTotemName());
if (!optional.isPresent()) {
Logger.info("Deleting a scheduler, unable to find the koth " + scheduler.getTotemName(),
LogType.ERROR);
iterator.remove();
continue;
}
Koth koth = optional.get();
koth.spawn(false);
}
}
});
}
/**
*
* @param sc
* @return
*/
private UUID getUUID(Scheduler sc) {
for (Entry<UUID, Scheduler> e : map.entrySet())
if (e.getValue().equals(sc))
return e.getKey();
return UUID.randomUUID();
}
/**
*
* @param sender
* @param uuid
*/
public void remove(CommandSender sender, UUID uuid) {
if (!map.containsKey(uuid)) {
message(sender, Message.ZKOTH_SCHEDULER_REMOVE_ERROR);
return;
}
Scheduler scheduler = map.get(uuid);
schedulers.remove(scheduler);
map.remove(uuid);
message(sender, Message.ZKOTH_SCHEDULER_REMOVE_SUCCESS);
}
/**
*
* @param sender
*/
public void show(CommandSender sender) {
if (schedulers.size() == 0) {
message(sender, Message.ZKOTH_SCHEDULER_EMPTY);
return;
}
if (sender instanceof Player) {
schedulers.forEach(sc -> {
String message = "";
if (sc.getType().equals(SchedulerType.DELAY)) {
message = Message.ZKOTH_SCHEDULER_LIST.getMessage().replace("%totemName%", sc.getTotemName())
.replace("%type%", sc.getType().name()).replace("%hour%", String.valueOf(sc.getHour()))
.replace("%minute%", String.valueOf(sc.getMinutes())).replace("%day%", sc.getDay());
} else {
message = Message.ZKOTH_SCHEDULER_LIST_REPEAT.getMessage().replace("%type%", sc.getType().name())
.replace("%totemName%", sc.getTotemName())
.replace("%timer%", TimerBuilder.getStringTime(sc.getMinutes() * 60));
}
UUID uuid = getUUID(sc);
map.putIfAbsent(uuid, sc);
TextComponent component = new TextComponent(message);
component.setHoverEvent(new HoverEvent(Action.SHOW_TEXT,
new TextComponent[] { new TextComponent(Message.ZKOTH_SCHEDULER_REMOVE_HOVER.getMessage()) }));
component.setClickEvent(new ClickEvent(net.md_5.bungee.api.chat.ClickEvent.Action.SUGGEST_COMMAND,
"/koth scheduler remove " + uuid));
((Player) sender).spigot().sendMessage(component);
});
} else {
schedulers.forEach(sc -> {
if (sc.getType().equals(SchedulerType.DELAY)) {
message(sender, Message.ZKOTH_SCHEDULER_LIST.getMessage().replace("%totemName%", sc.getTotemName())
.replace("%type%", sc.getType().name()).replace("%hour%", String.valueOf(sc.getHour()))
.replace("%minute%", String.valueOf(sc.getMinutes())).replace("%day%", sc.getDay()));
} else {
message(sender,
Message.ZKOTH_SCHEDULER_LIST_REPEAT.getMessage().replace("%type%", sc.getType().name())
.replace("%totemName%", sc.getTotemName())
.replace("%timer%", TimerBuilder.getStringTime(sc.getMinutes() * 60)));
}
});
}
}
}

View File

@ -0,0 +1,8 @@
package fr.maxlego08.zkoth.scheduler;
public enum SchedulerType {
REPEAT,
DELAY
}

View File

@ -91,6 +91,20 @@ public enum Message {
ZKOHT_EVENT_PLAYER("Person"),
ZKOTH_EVENT_DISABLE("§cThe event is not enable."),
ZKOTH_SCHEDULER_CREATE("§eYou have just created a scheduler for the koth §6%s§e."),
ZKOTH_SCHEDULER_EMPTY("§cNo scheduler available"),
ZKOTH_SCHEDULER_REMOVE_HOVER("§7Click to delete the scheduler"),
ZKOTH_SCHEDULER_LIST("§6%totemName%§7, §f%type%§7, §e%day%§7, §e%hour%§7, §e%minute%"),
ZKOTH_SCHEDULER_LIST_REPEAT("§6%totemName%§7, §f%type%§7, §e%timer%"),
ZKOTH_SCHEDULER_REMOVE_ERROR("§cYou must make /koth scheduler list and then click on one of the koths to be able to delete it"),
ZKOTH_SCHEDULER_REMOVE_SUCCESS("§eYou have just deleted the scheduler"),
DESCRIPTION_SCHEDULER("Show scheduler commands"),
DESCRIPTION_SCHEDULER_LIST("Show scheduler list"),
DESCRIPTION_SCHEDULER_ADD("Add a scheduler"),
DESCRIPTION_SCHEDULER_REMOVE("Remove a scheduler"),
KOTH_SCHEDULER_ERROR("§cImpossible to find the day §f%s§c."),
;
private List<String> messages;

View File

@ -18,6 +18,7 @@ public enum Permission {
ZKOTH_LOOT,
ZKOTH_TYPE,
ZKOTH_SETCAPTURE,
ZKOTH_SCHEDULER,
;

View File

@ -7,7 +7,10 @@ import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -304,7 +307,6 @@ public abstract class ZUtils extends MessageUtils {
return decimalFormat.format(decimal);
}
/**
*
* @param player
@ -494,7 +496,7 @@ public abstract class ZUtils extends MessageUtils {
protected boolean hasPermission(Permissible permissible, Permission permission) {
return permissible.hasPermission(permission.getPermission());
}
/**
*
* @param permissible
@ -540,7 +542,7 @@ public abstract class ZUtils extends MessageUtils {
}
}, start, delay);
}
/**
*
* @param element
@ -1167,7 +1169,7 @@ public abstract class ZUtils extends MessageUtils {
}
private final transient static int CENTER_PX = 154;
/**
*
* @param message
@ -1211,11 +1213,41 @@ public abstract class ZUtils extends MessageUtils {
}
return sb.toString() + message;
}
protected void broadcastCenterMessage(List<String> messages) {
messages.stream().map(e -> e = getCenteredMessage(e)).forEach(e -> {
Bukkit.broadcastMessage(e);
});
}
protected String getDay() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
int today = calendar.get(Calendar.DAY_OF_WEEK);
switch (today) {
case GregorianCalendar.MONDAY:
return "MONDAY";
case GregorianCalendar.TUESDAY:
return "TUESDAY";
case GregorianCalendar.WEDNESDAY:
return "WEDNESDAY";
case GregorianCalendar.THURSDAY:
return "THURSDAY";
case GregorianCalendar.FRIDAY:
return "FRIDAY";
case GregorianCalendar.SATURDAY:
return "SATURDAY";
case GregorianCalendar.SUNDAY:
return "SUNDAY";
default:
return null;
}
}
protected boolean isDay(String day) {
return day.equalsIgnoreCase("MONDAY") || day.equalsIgnoreCase("TUESDAY") || day.equalsIgnoreCase("WEDNESDAY")
|| day.equalsIgnoreCase("THURSDAY") || day.equalsIgnoreCase("FRIDAY")
|| day.equalsIgnoreCase("SATURDAY") || day.equalsIgnoreCase("SUNDAY");
}
}

View File

@ -1,94 +1,113 @@
package fr.maxlego08.zkoth.zcore.utils.plugins;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import fr.maxlego08.zkoth.listener.ListenerAdapter;
import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.logger.Logger;
public class VersionChecker extends ListenerAdapter {
/**
*
* @author Maxlego08
*
*/
public class VersionChecker implements Listener {
private final String URL_API = "https://groupez.dev/api/v1/resource/version/%s";
private final String URL_RESOURCE = "https://groupez.dev/resources/%s";
private final Plugin plugin;
/**
* @param plugin
* @param useLastVersion
*/
public VersionChecker(Plugin plugin) {
super();
this.plugin = plugin;
}
private final int pluginID;
private boolean useLastVersion = false;
public void useLastVersion(Plugin plugin) {
UpdateChecker checker = new UpdateChecker(plugin, 74073);
/**
* Class constructor
*
* @param plugin
* @param pluginID
*/
public VersionChecker(Plugin plugin, int pluginID) {
super();
this.plugin = plugin;
this.pluginID = pluginID;
}
/**
* Allows to check if the plugin version is up to date.
*/
public void useLastVersion() {
Bukkit.getPluginManager().registerEvents(this, this.plugin); // Register
// event
String pluginVersion = plugin.getDescription().getVersion();
AtomicBoolean atomicBoolean = new AtomicBoolean();
checker.getVersion(version -> {
this.getVersion(version -> {
long ver = Long.valueOf(version.replace(".", ""));
long plVersion = Long.valueOf(plugin.getDescription().getVersion().replace(".", ""));
long plVersion = Long.valueOf(pluginVersion.replace(".", ""));
atomicBoolean.set(plVersion >= ver);
useLastVersion = atomicBoolean.get();
this.useLastVersion = atomicBoolean.get();
if (atomicBoolean.get())
Logger.info("There is not a new update available.");
else
Logger.info("There is a new update available. Your version: " + plugin.getDescription().getVersion()
+ ", Laste version: " + version);
Logger.info("No update available.");
else {
Logger.info("New update available. Your version: " + pluginVersion + ", latest version: " + version);
Logger.info("Download plugin here: " + String.format(URL_RESOURCE, this.pluginID));
}
});
}
@Override
protected void onConnect(PlayerJoinEvent event, Player player) {
new BukkitRunnable() {
@Override
public void run() {
if (!useLastVersion && event.getPlayer().hasPermission("zplugin.notifs")) {
message(player,
"§cYou do not use the latest version of the plugin! Thank you for taking the latest version to avoid any risk of problem!");
@EventHandler
public void onConnect(PlayerJoinEvent event) {
final Player player = event.getPlayer();
if (!useLastVersion && event.getPlayer().hasPermission("zplugin.notifs")) {
new BukkitRunnable() {
@Override
public void run() {
String prefix = Message.PREFIX.getMessage();
player.sendMessage(prefix
+ "§cYou do not use the latest version of the plugin! Thank you for taking the latest version to avoid any risk of problem!");
player.sendMessage(prefix + "§fDownload plugin here: §a" + String.format(URL_RESOURCE, pluginID));
}
}
}.runTaskLater(plugin, 20 * 2);
}.runTaskLater(plugin, 20 * 2);
}
}
public class UpdateChecker {
/**
* Get version by plugin id
*
* @param consumer
* - Do something after
*/
public void getVersion(Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
final String apiURL = String.format(URL_API, this.pluginID);
try {
URL url = new URL(apiURL);
URLConnection hc = url.openConnection();
hc.setRequestProperty("User-Agent",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
Scanner scanner = new Scanner(hc.getInputStream());
if (scanner.hasNext())
consumer.accept(scanner.next());
scanner.close();
private Plugin plugin;
private int resourceId;
public UpdateChecker(Plugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
/**
*
* @param consumer
*/
public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL(
"https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream();
Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
}
});
}
} catch (IOException exception) {
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
}
});
}
}