From e52c4fa43b6d0bdf45b0526c75d26c73416bfa31 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Tue, 4 Dec 2018 18:16:55 +0100 Subject: [PATCH] Fixed the plugin not disabling - Fix some file creation problems - Fixed when the player has not joined a job, milk a cow and get paid, https://www.spigotmc.org/threads/jobs-reborn.50989/page-231#post-3240788 --- src/main/java/com/gamingmesh/jobs/Jobs.java | 6 +-- .../com/gamingmesh/jobs/config/YmlMaker.java | 52 ++++++++----------- .../jobs/listeners/JobsPaymentListener.java | 36 ++++++------- 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 2e7db966..7dc9f87f 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -841,10 +841,6 @@ public class Jobs extends JavaPlugin { @Override public void onDisable() { - // it will not run longer if it is a server error - if (!isEnabled()) - return; - GUIManager.CloseInventories(); shopManager.CloseInventories(); dao.saveExplore(); @@ -854,7 +850,7 @@ public class Jobs extends JavaPlugin { shutdown(); consoleMsg("&e[Jobs] &2Plugin has been disabled successfully."); - this.setEnabled(false); + setEnabled(false); } @SuppressWarnings("unused") diff --git a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java index 41ed6bda..b744f079 100644 --- a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java +++ b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java @@ -12,43 +12,34 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; -import com.gamingmesh.jobs.Jobs; - public class YmlMaker { - Jobs Plugin; public String fileName; private JavaPlugin plugin; public File ConfigFile; private FileConfiguration Configuration; - public YmlMaker(Jobs Plugin) { - this.Plugin = Plugin; - } - public YmlMaker(JavaPlugin plugin, String fileName) { if (plugin == null) { throw new IllegalArgumentException("plugin cannot be null"); } this.plugin = plugin; this.fileName = fileName; + File dataFolder = plugin.getDataFolder(); - if (dataFolder == null) { - throw new IllegalStateException(); - } - this.ConfigFile = new File(dataFolder.toString() + File.separatorChar + this.fileName); + if (!dataFolder.exists()) + dataFolder.mkdirs(); + ConfigFile = new File(dataFolder, fileName); } public void reloadConfig() { InputStreamReader f = null; try { - f = new InputStreamReader(new FileInputStream(this.ConfigFile), "UTF-8"); - } catch (UnsupportedEncodingException e1) { - e1.printStackTrace(); - } catch (FileNotFoundException e1) { + f = new InputStreamReader(new FileInputStream(ConfigFile), "UTF-8"); + } catch (UnsupportedEncodingException | FileNotFoundException e1) { e1.printStackTrace(); } - this.Configuration = YamlConfiguration.loadConfiguration(f); + Configuration = YamlConfiguration.loadConfiguration(f); if (f != null) try { f.close(); @@ -58,31 +49,35 @@ public class YmlMaker { } public FileConfiguration getConfig() { - if (this.Configuration == null) { + if (Configuration == null) reloadConfig(); - } - return this.Configuration; + return Configuration; + } + + public File getConfigFile() { + if (ConfigFile == null) + ConfigFile = new File(plugin.getDataFolder(), fileName); + return ConfigFile; } public void saveConfig() { - if ((this.Configuration == null) || (this.ConfigFile == null)) { + if ((Configuration == null) || (ConfigFile == null)) return; - } try { - getConfig().save(this.ConfigFile); + getConfig().save(ConfigFile); } catch (IOException ex) { - this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.ConfigFile, ex); + plugin.getLogger().log(Level.SEVERE, "Could not save config to " + ConfigFile, ex); } } public boolean exists() { - return this.ConfigFile.exists(); + return ConfigFile != null && ConfigFile.exists() ? false : ConfigFile.exists(); } public void createNewFile() { - if (!this.ConfigFile.exists()) { + if (ConfigFile != null && !ConfigFile.exists()) { try { - this.ConfigFile.createNewFile(); + ConfigFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } @@ -90,8 +85,7 @@ public class YmlMaker { } public void saveDefaultConfig() { - if (!this.ConfigFile.exists()) { - this.plugin.saveResource(this.fileName, false); - } + if (ConfigFile != null && !ConfigFile.exists()) + plugin.saveResource(fileName, false); } } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index c38b8ada..8b8b88c3 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -222,26 +222,9 @@ public class JobsPaymentListener implements Listener { if (!player.isOnline()) return; - if (Jobs.getGCManager().CowMilkingTimer > 0) - if (cow.hasMetadata(CowMetadata)) { - long time = cow.getMetadata(CowMetadata).get(0).asLong(); - if (System.currentTimeMillis() < time + Jobs.getGCManager().CowMilkingTimer) { - - long timer = ((Jobs.getGCManager().CowMilkingTimer - (System.currentTimeMillis() - time)) / 1000); - player.sendMessage(Jobs.getLanguage().getMessage("message.cowtimer", "%time%", timer)); - - if (Jobs.getGCManager().CancelCowMilking) - event.setCancelled(true); - return; - } - } - ItemStack itemInHand = Jobs.getNms().getItemInMainHand(player); - if (itemInHand == null) - return; - - if (itemInHand.getType() != Material.BUCKET) + if (itemInHand != null && itemInHand.getType() != Material.BUCKET) return; // check if in creative @@ -256,6 +239,23 @@ public class JobsPaymentListener implements Listener { if (jPlayer == null) return; + // Fix bug when the player has not joined a job, milk a cow and get paid + for (com.gamingmesh.jobs.container.Job jobs : Jobs.getJobs()) { + if (jPlayer.isInJob(jobs) && Jobs.getGCManager().CowMilkingTimer > 0) { + if (cow.hasMetadata(CowMetadata)) { + long time = cow.getMetadata(CowMetadata).get(0).asLong(); + if (System.currentTimeMillis() < time + Jobs.getGCManager().CowMilkingTimer) { + long timer = ((Jobs.getGCManager().CowMilkingTimer - (System.currentTimeMillis() - time)) / 1000); + jPlayer.getPlayer().sendMessage(Jobs.getLanguage().getMessage("message.cowtimer", "%time%", timer)); + + if (Jobs.getGCManager().CancelCowMilking) + event.setCancelled(true); + return; + } + } + } + } + Jobs.action(jPlayer, new EntityActionInfo(cow, ActionType.MILK)); Long Timer = System.currentTimeMillis();