diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index c7581c60..5197546b 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -96,6 +96,7 @@ import com.gamingmesh.jobs.listeners.PistonProtectionListener; import com.gamingmesh.jobs.selection.SelectionManager; import com.gamingmesh.jobs.stuff.ActionBar; import com.gamingmesh.jobs.stuff.CMIScoreboardManager; +import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; import com.gamingmesh.jobs.stuff.Loging; import com.gamingmesh.jobs.stuff.RawMessage; import com.gamingmesh.jobs.stuff.TabComplete; @@ -526,7 +527,6 @@ public class Jobs extends JavaPlugin { * @throws IOException */ public void startup() { - instance = this; try { reload(); } catch (IOException e1) { @@ -740,6 +740,8 @@ public class Jobs extends JavaPlugin { @Override public void onEnable() { + + instance = this; running = true; this.setEnabled(true); @@ -780,6 +782,8 @@ public class Jobs extends JavaPlugin { YmlMaker jobShopItems = new YmlMaker(this, "shopItems.yml"); jobShopItems.saveDefaultConfig(); + FurnaceBrewingHandling.load(); + setPermissionHandler(new PermissionHandler(this)); setJobsClassloader(); setPlayerManager(); @@ -849,6 +853,9 @@ public class Jobs extends JavaPlugin { shopManager.CloseInventories(); dao.saveExplore(); dao.saveBlockProtection(); + + FurnaceBrewingHandling.save(); + Jobs.shutdown(); String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] &2Plugin has been disabled succesfully."); ConsoleCommandSender console = Bukkit.getServer().getConsoleSender(); diff --git a/src/main/java/com/gamingmesh/jobs/PermissionManager.java b/src/main/java/com/gamingmesh/jobs/PermissionManager.java index 15521ef0..3d013d40 100644 --- a/src/main/java/com/gamingmesh/jobs/PermissionManager.java +++ b/src/main/java/com/gamingmesh/jobs/PermissionManager.java @@ -52,6 +52,8 @@ public class PermissionManager { jobs_boost_all_all_AMOUNT(remade("jobs.boost.all.all.%AMOUNT%"), 60 * 1000), jobs_spawner_AMOUNT(remade("jobs.nearspawner.%AMOUNT%"), 60 * 1000), jobs_petpay_AMOUNT(remade("jobs.petpay.%AMOUNT%"), 60 * 1000), + jobs_maxfurnaces_AMOUNT(remade("jobs.maxfurnaces.%AMOUNT%"), 2 * 1000), + jobs_maxbrewingstands_AMOUNT(remade("jobs.maxbrewingstands.%AMOUNT%"), 2 * 1000), // jobs_world_WORLDNAME(remade("jobs.world.%WORLDNAME%"), 60 * 1000) ; diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 52c5c5fe..e0fc7965 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -139,6 +139,8 @@ public class LanguageManager { c.get("general.error.ingame", "&cYou can use this command only in game!"); c.get("general.error.fromconsole", "&cYou can use this command only from console!"); c.get("general.error.worldisdisabled", "&cYou cant use command in this world!"); + c.get("general.error.noFurnaceRegistration", "&cYou reached max furnace count!"); + c.get("general.error.noBrewingRegistration", "&cYou reached max brewing stand count!"); c.get("command.help.output.info", "Type /jobs [cmd] ? for more information about a command."); c.get("command.help.output.cmdUsage", "&2Usage: &7[command]"); diff --git a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java index 51277adf..28889267 100644 --- a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java +++ b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java @@ -76,6 +76,20 @@ public class YmlMaker { } } + public boolean exists() { + return this.ConfigFile.exists(); + } + + public void createNewFile() { + if (!this.ConfigFile.exists()) { + try { + this.ConfigFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + public void saveDefaultConfig() { if (!this.ConfigFile.exists()) { this.plugin.saveResource(this.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 22f047a6..0634d1f0 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -20,6 +20,7 @@ package com.gamingmesh.jobs.listeners; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -95,12 +96,13 @@ import com.gamingmesh.jobs.container.FastPayment; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.Debug; +import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; import com.google.common.base.Objects; public class JobsPaymentListener implements Listener { private Jobs plugin; - private final String furnaceOwnerMetadata = "jobsFurnaceOwner"; - private final String brewingOwnerMetadata = "jobsBrewingOwner"; + public static final String furnaceOwnerMetadata = "jobsFurnaceOwner"; + public static final String brewingOwnerMetadata = "jobsBrewingOwner"; private final String BlockMetadata = "BlockOwner"; public static final String VegyMetadata = "VegyTimer"; private final String CowMetadata = "CowTimer"; @@ -178,7 +180,7 @@ public class JobsPaymentListener implements Listener { //disabling plugin in world if (event.getPlayer() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getPlayer().getWorld())) return; - + if (!(event.getEntity() instanceof Sheep)) return; Sheep sheep = (Sheep) event.getEntity(); @@ -233,7 +235,12 @@ public class JobsPaymentListener implements Listener { MetadataValue value = data.get(0); String playerName = value.asString(); - JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(playerName); + UUID uuid = UUID.fromString(playerName); + + if (uuid == null) + return; + + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(uuid); if (jPlayer == null || !jPlayer.isOnline()) return; @@ -259,8 +266,8 @@ public class JobsPaymentListener implements Listener { Block block = event.getBlock(); if (block == null) return; - if (block.getType() == Material.FURNACE && block.hasMetadata(this.furnaceOwnerMetadata)) - block.removeMetadata(this.furnaceOwnerMetadata, this.plugin); + if (block.getType() == Material.FURNACE && block.hasMetadata(furnaceOwnerMetadata)) + FurnaceBrewingHandling.removeFurnace(block); // make sure plugin is enabled if (!this.plugin.isEnabled()) @@ -750,8 +757,8 @@ public class JobsPaymentListener implements Listener { return; Block block = furnace.getBlock(); - if (block.hasMetadata(this.furnaceOwnerMetadata)) - block.removeMetadata(this.furnaceOwnerMetadata, this.plugin); + if (block.hasMetadata(furnaceOwnerMetadata)) + FurnaceBrewingHandling.removeFurnace(block); } catch (Exception e) { e.printStackTrace(); } @@ -775,8 +782,8 @@ public class JobsPaymentListener implements Listener { return; Block block = stand.getBlock(); - if (block.hasMetadata(this.brewingOwnerMetadata)) - block.removeMetadata(this.brewingOwnerMetadata, this.plugin); + if (block.hasMetadata(brewingOwnerMetadata)) + FurnaceBrewingHandling.removeBrewing(block); } catch (Exception e) { e.printStackTrace(); } @@ -793,16 +800,23 @@ public class JobsPaymentListener implements Listener { if (block == null) return; - if (!block.hasMetadata(this.furnaceOwnerMetadata)) + if (!block.hasMetadata(furnaceOwnerMetadata)) return; - List data = block.getMetadata(this.furnaceOwnerMetadata); + List data = block.getMetadata(furnaceOwnerMetadata); if (data.isEmpty()) return; // only care about first MetadataValue value = data.get(0); String playerName = value.asString(); - Player player = Bukkit.getServer().getPlayerExact(playerName); + + Player player = null; + + UUID uuid = UUID.fromString(playerName); + if (uuid == null) + return; + player = Bukkit.getPlayer(uuid); + if (player == null || !player.isOnline()) return; @@ -1283,8 +1297,8 @@ public class JobsPaymentListener implements Listener { if (block == null) continue; - if (block.getType() == Material.FURNACE && block.hasMetadata(this.furnaceOwnerMetadata)) - block.removeMetadata(this.furnaceOwnerMetadata, this.plugin); + if (block.getType() == Material.FURNACE && block.hasMetadata(furnaceOwnerMetadata)) + FurnaceBrewingHandling.removeFurnace(block); if (Jobs.getGCManager().useBlockProtection) if (block.getState().hasMetadata(BlockMetadata)) @@ -1311,14 +1325,49 @@ public class JobsPaymentListener implements Listener { return; if (block.getType() == Material.FURNACE || block.getType() == Material.BURNING_FURNACE) { - if (block.hasMetadata(this.furnaceOwnerMetadata)) - block.removeMetadata(this.furnaceOwnerMetadata, this.plugin); - block.setMetadata(this.furnaceOwnerMetadata, new FixedMetadataValue(this.plugin, event.getPlayer().getName())); - } else if (block.getType() == Material.BREWING_STAND) { - if (block.hasMetadata(brewingOwnerMetadata)) - block.removeMetadata(brewingOwnerMetadata, this.plugin); - block.setMetadata(brewingOwnerMetadata, new FixedMetadataValue(this.plugin, event.getPlayer().getName())); + boolean done = FurnaceBrewingHandling.registerFurnaces(event.getPlayer(), block); + + if (!done) { + boolean report = false; + if (block.hasMetadata(furnaceOwnerMetadata)) { + List data = block.getMetadata(furnaceOwnerMetadata); + if (data.isEmpty()) + return; + // only care about first + MetadataValue value = data.get(0); + String uuid = value.asString(); + + if (!uuid.equals(event.getPlayer().getUniqueId().toString())) + report = true; + } else + report = true; + + if (report) + Jobs.getActionBar().send(event.getPlayer(), Jobs.getLanguage().getMessage("general.error.noFurnaceRegistration")); + } + } else if (block.getType() == Material.BREWING_STAND) { + + boolean done = FurnaceBrewingHandling.registerBrewingStand(event.getPlayer(), block); + + if (!done) { + boolean report = false; + if (block.hasMetadata(brewingOwnerMetadata)) { + List data = block.getMetadata(brewingOwnerMetadata); + if (data.isEmpty()) + return; + // only care about first + MetadataValue value = data.get(0); + String uuid = value.asString(); + + if (!uuid.equals(event.getPlayer().getUniqueId().toString())) + report = true; + } else + report = true; + + if (report) + Jobs.getActionBar().send(event.getPlayer(), Jobs.getLanguage().getMessage("general.error.noBrewingRegistration")); + } } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java new file mode 100644 index 00000000..719fbf43 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java @@ -0,0 +1,342 @@ +package com.gamingmesh.jobs.stuff; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; + +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.config.YmlMaker; +import com.gamingmesh.jobs.listeners.JobsPaymentListener; + +public class FurnaceBrewingHandling { + + public FurnaceBrewingHandling() { + } + + static HashMap> furnaceMap = new HashMap>(); + static HashMap> brewingMap = new HashMap>(); + + public static void load() { + YmlMaker f = new YmlMaker(Jobs.getInstance(), "furnaceBrewingStands.yml"); + if (!f.exists()) + return; + + FileConfiguration config = f.getConfig(); + + if (config.contains("Furnace")) { + ConfigurationSection section = config.getConfigurationSection("Furnace"); + + try { + for (String one : section.getKeys(false)) { + String value = section.getString(one); + List ls = new ArrayList(); + if (value.contains(";")) + ls.addAll(Arrays.asList(value.split(";"))); + else + ls.add(value); + UUID uuid = UUID.fromString(one); + + if (uuid == null) + continue; + List blist = new ArrayList(); + for (String oneL : ls) { + blockLoc bl = new blockLoc(oneL); + Block block = bl.getBlock(); + if (block == null) + continue; + + block.removeMetadata(JobsPaymentListener.furnaceOwnerMetadata, Jobs.getInstance()); + block.setMetadata(JobsPaymentListener.furnaceOwnerMetadata, new FixedMetadataValue(Jobs.getInstance(), one)); + + Debug.D("set meta " + block.getLocation().toString() + " " + one); + + blist.add(bl); + } + if (!blist.isEmpty()) { + Debug.D("adding furnace " + uuid.toString() + " " + blist.size()); + furnaceMap.put(uuid, blist); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + if (config.contains("Brewing")) { + ConfigurationSection section = config.getConfigurationSection("Brewing"); + + try { + for (String one : section.getKeys(false)) { + String value = section.getString(one); + List ls = new ArrayList(); + if (value.contains(";")) + ls.addAll(Arrays.asList(value.split(";"))); + else + ls.add(value); + UUID uuid = UUID.fromString(one); + + if (uuid == null) + continue; + + List blist = new ArrayList(); + for (String oneL : ls) { + blockLoc bl = new blockLoc(oneL); + Block block = bl.getBlock(); + if (block == null) + continue; + + block.removeMetadata(JobsPaymentListener.brewingOwnerMetadata, Jobs.getInstance()); + block.setMetadata(JobsPaymentListener.brewingOwnerMetadata, new FixedMetadataValue(Jobs.getInstance(), one)); + + blist.add(bl); + } + if (!blist.isEmpty()) + brewingMap.put(uuid, blist); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public static void save() { + + YmlMaker f = new YmlMaker(Jobs.getInstance(), "furnaceBrewingStands.yml"); + + f.createNewFile(); + + f.saveDefaultConfig(); + FileConfiguration config = f.getConfig(); + + for (Entry> one : furnaceMap.entrySet()) { + + String full = ""; + + for (blockLoc oneL : one.getValue()) { + + if (!full.isEmpty()) + full += ";"; + + full += oneL.toString(); + } + if (!full.isEmpty()) { + config.set("Furnace." + one.getKey().toString(), full); + } + } + + for (Entry> one : brewingMap.entrySet()) { + + String full = ""; + + Debug.D("saving brewing stands " + one.getValue().size()); + + for (blockLoc oneL : one.getValue()) { + + if (!full.isEmpty()) + full += ";"; + + full += oneL.toString(); + } + if (!full.isEmpty()) + config.set("Brewing." + one.getKey().toString(), full); + } + + f.saveConfig(); + + } + + public static int getTotalFurnaces(Player player) { + List ls = furnaceMap.get(player.getUniqueId()); + if (ls == null) + return 0; + return ls.size(); + } + + public static int getTotalBrewingStands(Player player) { + List ls = brewingMap.get(player.getUniqueId()); + if (ls == null) + return 0; + return ls.size(); + } + + public static boolean removeFurnace(Block block) { + + UUID uuid = null; + + if (block.hasMetadata(JobsPaymentListener.furnaceOwnerMetadata)) { + List data = block.getMetadata(JobsPaymentListener.furnaceOwnerMetadata); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + String uuidS = value.asString(); + uuid = UUID.fromString(uuidS); + } + } + + List ls = furnaceMap.get(uuid); + if (ls == null) + return true; + + for (blockLoc one : ls) { + if (!one.getLocation().equals(block.getLocation())) + continue; + block.removeMetadata(JobsPaymentListener.furnaceOwnerMetadata, Jobs.getInstance()); + ls.remove(one); + return true; + } + return false; + + } + + public static boolean removeBrewing(Block block) { + + UUID uuid = null; + if (block.hasMetadata(JobsPaymentListener.furnaceOwnerMetadata)) { + List data = block.getMetadata(JobsPaymentListener.furnaceOwnerMetadata); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + String uuidS = value.asString(); + uuid = UUID.fromString(uuidS); + } + } + + List ls = brewingMap.get(uuid); + if (ls == null) + return true; + for (blockLoc one : ls) { + if (!one.getLocation().equals(block.getLocation())) + continue; + block.removeMetadata(JobsPaymentListener.brewingOwnerMetadata, Jobs.getInstance()); + ls.remove(one); + return true; + } + + return false; + + } + + public static boolean registerFurnaces(Player player, Block block) { + + if (block.getType() != Material.FURNACE && block.getType() != Material.BURNING_FURNACE) { + return false; + } + + Double maxV = Jobs.getPermissionManager().getMaxPermission(Jobs.getPlayerManager().getJobsPlayer(player), "jobs.maxfurnaces"); + + if (maxV == null) + maxV = 0D; + + int max = maxV.intValue(); + + int have = getTotalFurnaces(player); + + boolean owner = false; + if (block.hasMetadata(JobsPaymentListener.furnaceOwnerMetadata)) { + List data = block.getMetadata(JobsPaymentListener.furnaceOwnerMetadata); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + String uuid = value.asString(); + + if (uuid.equals(player.getUniqueId().toString())) { + if (have > max) + removeFurnace(block); + owner = true; + } + } + } + + if (owner) + return true; + + if (have >= max && max > 0) + return false; + + block.setMetadata(JobsPaymentListener.furnaceOwnerMetadata, new FixedMetadataValue(Jobs.getInstance(), player.getUniqueId().toString())); + +// if (max == 0) +// return true; + + List ls = furnaceMap.get(player.getUniqueId()); + if (ls == null) + ls = new ArrayList(); + ls.add(new blockLoc(block.getLocation())); + furnaceMap.put(player.getUniqueId(), ls); + + return true; + } + + public static boolean registerBrewingStand(Player player, Block block) { + + if (block.getType() != Material.BREWING_STAND) { + return false; + } + + Double maxV = Jobs.getPermissionManager().getMaxPermission(Jobs.getPlayerManager().getJobsPlayer(player), "jobs.maxbrewingstands"); + + if (maxV == null) + maxV = 0D; + + int max = maxV.intValue(); + + int have = getTotalFurnaces(player); + + boolean owner = false; + if (block.hasMetadata(JobsPaymentListener.brewingOwnerMetadata)) { + List data = block.getMetadata(JobsPaymentListener.brewingOwnerMetadata); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + String uuid = value.asString(); + + if (uuid.equals(player.getUniqueId().toString())) { + if (have > max) + removeBrewing(block); + owner = true; + } + } + } + + if (owner) + return true; + + if (have >= max && max > 0) + return false; + + block.setMetadata(JobsPaymentListener.brewingOwnerMetadata, new FixedMetadataValue(Jobs.getInstance(), player.getUniqueId().toString())); + +// if (max == 0) +// return true; + + List ls = brewingMap.get(player.getUniqueId()); + if (ls == null) + ls = new ArrayList(); + ls.add(new blockLoc(block.getLocation())); + brewingMap.put(player.getUniqueId(), ls); + + return true; + } + + public static boolean clearFurnaces(Player player) { + furnaceMap.remove(player.getUniqueId()); + return true; + } + + public static boolean clearBrewingStands(Player player) { + brewingMap.remove(player.getUniqueId()); + return true; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java new file mode 100644 index 00000000..041a5fc6 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java @@ -0,0 +1,103 @@ +package com.gamingmesh.jobs.stuff; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; + +public class blockLoc { + private int x; + private int y; + private int z; + private String worldName; + private World w; + + public blockLoc(String loc) { + this.fromString(loc); + } + + public blockLoc(Location loc) { + x = loc.getBlockX(); + y = loc.getBlockY(); + z = loc.getBlockZ(); + w = loc.getWorld(); + worldName = loc.getWorld().getName(); + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getZ() { + return z; + } + + public void setZ(int z) { + this.z = z; + } + + public String getWorldName() { + return worldName; + } + + public void setWorldName(String worldName) { + this.worldName = worldName; + } + + @Override + public String toString() { + return (w == null ? worldName : w.getName()) + ":" + x + ":" + y + ":" + z; + } + + public boolean fromString(String loc) { + if (!loc.contains(":")) + return false; + String[] split = loc.split(":"); + + World w = Bukkit.getWorld(split[0]); + if (w == null) + return false; + this.w = w; + + try { + x = Integer.parseInt(split[1]); + y = Integer.parseInt(split[2]); + z = Integer.parseInt(split[3]); + return true; + } catch (Exception e) { + return false; + } + } + + public Block getBlock() { + Location loc = this.getLocation(); + + if (loc == null) + return null; + + return loc.getBlock(); + } + + public Location getLocation() { + World w = this.w == null ? Bukkit.getWorld(worldName) : this.w; + if (w == null) + return null; + this.w = w; + + Location loc = new Location(w, x, y, z); + + return loc; + } +}