From f7593b07f4beb669e98fcad611901b1c9682a5b1 Mon Sep 17 00:00:00 2001 From: Clayderson Ferreira Date: Sun, 31 Mar 2019 19:14:27 -0300 Subject: [PATCH] =?UTF-8?q?Vers=C3=A3o=202.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mineshop.iml | 14 ++- .../com/mineshop/plugin/spigot/Commands.java | 47 ++++++++ src/br/com/mineshop/plugin/spigot/Main.java | 107 ++++++++++++++++++ .../mineshop/spigot/mineshop/Commands.java | 46 -------- src/br/com/mineshop/spigot/mineshop/Main.java | 88 -------------- src/config.yml | 9 +- src/plugin.yml | 9 +- 7 files changed, 173 insertions(+), 147 deletions(-) create mode 100644 src/br/com/mineshop/plugin/spigot/Commands.java create mode 100644 src/br/com/mineshop/plugin/spigot/Main.java delete mode 100644 src/br/com/mineshop/spigot/mineshop/Commands.java delete mode 100644 src/br/com/mineshop/spigot/mineshop/Main.java diff --git a/mineshop.iml b/mineshop.iml index 49300a1..24dd706 100644 --- a/mineshop.iml +++ b/mineshop.iml @@ -10,14 +10,22 @@ - + - + - + + + + + + + + + \ No newline at end of file diff --git a/src/br/com/mineshop/plugin/spigot/Commands.java b/src/br/com/mineshop/plugin/spigot/Commands.java new file mode 100644 index 0000000..f5af54a --- /dev/null +++ b/src/br/com/mineshop/plugin/spigot/Commands.java @@ -0,0 +1,47 @@ +package br.com.mineshop.plugin.spigot; + +import br.com.mineshop.msdk.MSDK; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +public class Commands implements Listener, CommandExecutor { + private MSDK msdk; + private JavaPlugin plugin; + + String cmd1 = "mineshop"; + + Commands(MSDK msdk, JavaPlugin plugin) { + this.msdk = msdk; + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (sender instanceof Player) { + sender.sendMessage(ChatColor.RED + "Este comando não pode ser executado fora do console do servidor"); + } else if (command.getName().equalsIgnoreCase(this.cmd1)) { + if (args.length > 0) { + String token = args[0].trim().toLowerCase(); + + this.msdk.setCredentials(token); + this.plugin.getConfig().set("token", token); + this.plugin.saveConfig(); + + sender.sendMessage(String.format( + "%sPronto! Se o token informado estiver correto, este servidor irá sincronizar com sua loja em " + + "alguns instantes.", + ChatColor.GREEN + )); + } else { + sender.sendMessage(String.format("%s ", this.cmd1)); + } + } + + return true; + } +} diff --git a/src/br/com/mineshop/plugin/spigot/Main.java b/src/br/com/mineshop/plugin/spigot/Main.java new file mode 100644 index 0000000..77da334 --- /dev/null +++ b/src/br/com/mineshop/plugin/spigot/Main.java @@ -0,0 +1,107 @@ +package br.com.mineshop.plugin.spigot; + +import br.com.mineshop.msdk.exceptions.MsdkException; +import br.com.mineshop.msdk.webservice.endpoints.v1.QueueItem; +import br.com.mineshop.msdk.exceptions.WebServiceException; +import br.com.mineshop.msdk.MSDK; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +public class Main extends JavaPlugin { + private MSDK msdk = new MSDK(); + private JavaPlugin plugin; + private Commands commands = new Commands(this.msdk, this); + + @Override + public void onEnable() { + this.saveDefaultConfig(); + + this.plugin = this; + this.msdk.setCredentials(this.getConfig().getString("token")); + this.getCommand(this.commands.cmd1).setExecutor(this.commands); + + int timerAfterRestart = this.getConfig().getInt("eventLoop.timer.afterRestart"); + int timerDelay = this.getConfig().getInt("eventLoop.timer.delay"); + + if (timerAfterRestart < 20) { + Bukkit.getLogger().warning(String.format( + "[%s] O event loop está configurado para ser executado em %s segundo(s) logo após a " + + "reinicialização do servidor ou do plugin! Recomendamos um delay entre 20 e 300 segundos neste campo.", + this.getDescription().getName(), + Integer.toString(timerAfterRestart) + )); + } + + if (timerDelay < 10) { + Bukkit.getLogger().warning(String.format( + "[%s] O event loop está configurado para ser executado a cada %s segundo(s)! Recomendamos um " + + "delay entre 10 e 60 segundos neste campo.", + this.getDescription().getName(), + Integer.toString(timerDelay) + )); + } + + new BukkitRunnable() { + @Override + public void run() { + QueueItem[] queueItems = null; + + try { + queueItems = msdk.getQueueItems(); + } catch (WebServiceException | MsdkException e) { + Bukkit.getLogger().warning(String.format("[%s] %s", getDescription().getName(), e.getMessage())); + } + + if (queueItems == null) { + return; + } + + for (QueueItem queueItem : queueItems) { + new BukkitRunnable() { + @Override + public void run() { + if (queueItem.getType().equalsIgnoreCase("online")) { + Player player = getServer().getPlayerExact(queueItem.getNickname()); + + if (player == null) { + return; + } + + int emptySlots = 0; + + for (ItemStack item : player.getInventory().getContents()) { + if (item == null) { + emptySlots++; + } + } + + if (queueItem.getSlotsNeeded() > emptySlots) { + player.sendMessage(String.format( + "%sNão pudemos entregar todos os itens que você comprou em nossa loja porque seu " + + "inventário não tem espaço suficiente. O restante dos itens serão entregues em %s segundo(s). " + + "Para recebê-los, por favor, esvazie seu inventário.", + ChatColor.LIGHT_PURPLE, + Integer.toString(timerDelay) + )); + + return; + } + } + + try { + msdk.hasBeenDelivered(queueItem.getNickname(), queueItem.getUuid()); + getServer().dispatchCommand(getServer().getConsoleSender(), queueItem.getCommand()); + } catch (WebServiceException | MsdkException e) { + Bukkit.getLogger().warning(String.format("[%s] %s", getDescription().getName(), e.getMessage())); + } + } + }.runTask(plugin); + } + } + }.runTaskTimerAsynchronously(this, timerAfterRestart * 20, timerDelay * 20); + } +} diff --git a/src/br/com/mineshop/spigot/mineshop/Commands.java b/src/br/com/mineshop/spigot/mineshop/Commands.java deleted file mode 100644 index 50c1dbc..0000000 --- a/src/br/com/mineshop/spigot/mineshop/Commands.java +++ /dev/null @@ -1,46 +0,0 @@ -package br.com.mineshop.spigot.mineshop; - -import br.com.mineshop.spigot.msdk.Exceptions.WSException; -import br.com.mineshop.spigot.msdk.MSDK; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.Listener; -import org.bukkit.plugin.java.JavaPlugin; - -public class Commands implements Listener, CommandExecutor { - private JavaPlugin plugin; - private MSDK msdk; - - String cmd1 = "mineshop"; - - Commands(JavaPlugin plugin, MSDK msdk) { - this.plugin = plugin; - this.msdk = msdk; - } - - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - if (commandSender instanceof Player) { - commandSender.sendMessage("Este comando só pode ser executado no console do servidor"); - } else { - if (command.getName().equalsIgnoreCase(this.cmd1)) { - if (strings.length != 0) { - String token = strings[0].trim().toLowerCase(); - try { - this.msdk.setAuthorization(token); - this.plugin.getConfig().set("authorization", token); - this.plugin.saveConfig(); - } catch (WSException ex) { - commandSender.sendMessage(ex.getMessage()); - } - } else { - commandSender.sendMessage(String.format("Use: %s ", this.cmd1)); - } - } - } - - return true; - } -} diff --git a/src/br/com/mineshop/spigot/mineshop/Main.java b/src/br/com/mineshop/spigot/mineshop/Main.java deleted file mode 100644 index 040f7af..0000000 --- a/src/br/com/mineshop/spigot/mineshop/Main.java +++ /dev/null @@ -1,88 +0,0 @@ -package br.com.mineshop.spigot.mineshop; - -import br.com.mineshop.spigot.msdk.Endpoints.Queue; -import br.com.mineshop.spigot.msdk.Exceptions.WSException; -import br.com.mineshop.spigot.msdk.MSDK; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitScheduler; - -public class Main extends JavaPlugin { - private JavaPlugin plugin; - private MSDK msdk = (MSDK) Bukkit.getServer().getPluginManager().getPlugin("msdk"); - private Commands commands = new Commands(this, this.msdk); - - @Override - public void onEnable() { - this.saveDefaultConfig(); - - this.plugin = this; - this.getCommand(this.commands.cmd1).setExecutor(this.commands); - - try { - this.msdk.setAuthorization(this.getConfig().getString("authorization")); - } catch (WSException ex) { - Bukkit.getLogger().warning(String.format("[%s] Falha ao tentar definir token de autorização", this.getDescription().getName())); - } - - long firstExecution = (this.getConfig().getLong("scheduler.firstExecution") * 20); - long repeat = (this.getConfig().getLong("scheduler.repeat") * 20); - - BukkitScheduler scheduler = this.getServer().getScheduler(); - scheduler.scheduleSyncRepeatingTask(this, new Runnable() { - @Override - public void run() { - Runnable runnable = new Runnable() { - @Override - public void run() { - Queue[] queue = null; - - try { - queue = msdk.getQueue(); - } catch (WSException ex) { - Bukkit.getLogger().warning(String.format("[%s] %s", getDescription().getName(), ex.getMessage())); - } - - if (queue != null) { - int requestsCount = 0; - - for (Queue currentQueue : queue) { - if (currentQueue.getType().equalsIgnoreCase("online")) { - Player target = getServer().getPlayerExact(currentQueue.getNickname()); - if (target == null) { - continue; - } - } - - try { - msdk.delivered(currentQueue); - for (int i = 0; i < currentQueue.getCommands().length; i++) { - int currentKey = i; - Bukkit.getScheduler().runTask(plugin, new Runnable() { - @Override - public void run() { - getServer().dispatchCommand(getServer().getConsoleSender(), currentQueue.getCommands()[currentKey]); - } - }); - } - } catch (WSException ex) { - Bukkit.getLogger().warning(String.format("[%s] %s", getDescription().getName(), ex.getMessage())); - } - - requestsCount++; - - if (requestsCount >= 5) { - break; - } - } - } - } - }; - - Thread thread = new Thread(runnable); - thread.start(); - } - }, firstExecution, repeat); - } -} diff --git a/src/config.yml b/src/config.yml index 04c9340..274abca 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,4 +1,5 @@ -authorization: -scheduler: - firstExecution: 300 - repeat: 90 \ No newline at end of file +eventLoop: + timer: + afterRestart: 60 + delay: 20 +token: \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 30b14ec..b337af2 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,13 +1,10 @@ name: mineshop -description: plugin oficial de integração entre loja e servidor para automatização da entrega de produtos vendidos -version: 1.0.0 -main: br.com.mineshop.spigot.mineshop.Main +description: plugin oficial de integração entre loja e servidor para automatizar a entrega de produtos vendidos +version: 2.0.1 +main: br.com.mineshop.plugin.spigot.Main load: POSTWORLD author: Sapiente website: https://github.com/clayderson/mineshop-spigot -depend: [msdk] -softdepend: [msdk] - commands: mineshop: usage: /