package de.anura.core; import de.anura.core.API.Core; import de.anura.core.API.Money; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.bukkit.ChatColor; import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Player; public class FlowerPots { private final HashMap> foundCache = new HashMap<>(); private final HashMap> foundTimestamp = new HashMap<>(); public boolean foundPot(Player P, Integer pot) { if (!foundCache.containsKey(P)) { refreshCache(P); } if (foundCache.get(P).containsKey(pot)) { if (foundCache.get(P).get(pot) && foundTimestamp.get(P).containsKey(pot)) { if ((System.currentTimeMillis() / 1000 - foundTimestamp.get(P).get(pot)) >= getPotWaitTime(pot)) { return false; } } return true; } return false; } private int getPotWaitTime(Integer pot) { if (AnuraCore.getInstance().flowerPots.containsKey(pot)) { if (AnuraCore.getInstance().flowerPotsWait.containsKey(pot)) { return AnuraCore.getInstance().flowerPotsWait.get(pot); } return -1; } return 0; } public void refreshCache(Player P) { try { ResultSet rs = AnuraCore.getSql().querySelect("SELECT id, timestamp, type FROM coreFoundPots WHERE player = (SELECT id FROM players WHERE uuid = '" + P.getUniqueId().toString() + "')"); HashMap list = new HashMap<>(); HashMap listTimes = new HashMap<>(); while (rs.next()) { list.put(rs.getInt("id"), rs.getBoolean("type")); if (rs.getBoolean("type")) { listTimes.put(rs.getInt("id"), rs.getInt("timestamp")); } } this.foundCache.put(P, list); this.foundTimestamp.put(P, listTimes); } catch (SQLException ex) { System.out.println("Error refreshCache(FlowerPots)"); } } public void refreshPot(Integer pot) { Location l = AnuraCore.getInstance().flowerPots.get(pot); for (Player P : l.getWorld().getPlayers()) { if (P.getLocation().distance(l) < 20) { refreshPlayerPot(P, pot, l); } } } public void refreshPlayerPot(Player p, Integer pot, Location l) { if (!this.foundPot(p, pot)) { p.sendBlockChange(l, Material.FLOWER_POT, (byte) 0); } else { p.sendBlockChange(l, Material.AIR, (byte) 0); } } public void refreshPlayer(Player p) { for (Map.Entry pots : AnuraCore.getInstance().flowerPots.entrySet()) { if (pots.getValue().getWorld().equals(p.getLocation().getWorld()) && pots.getValue().distance(p.getLocation()) < 20) { refreshPlayerPot(p, pots.getKey(), pots.getValue()); } } AnuraCore.getInstance().lastLoc.put(p, p.getLocation()); } public void playerFoundPot(Player P, Integer pot) { if (!foundPot(P, pot)) { Integer time = getPotWaitTime(pot); boolean alreadyFound = foundCache.get(P).containsKey(pot); int type = 0; if (time == -1) { foundCache.get(P).put(pot, false); } else { type = 1; foundCache.get(P).put(pot, true); foundTimestamp.get(P).put(pot, (int) (System.currentTimeMillis() / 1000)); } String sql; if (alreadyFound) { sql = "UPDATE coreFoundPots SET timestamp = '" + (System.currentTimeMillis() / 1000) + "', type = 1 WHERE player = (SELECT id FROM players WHERE uuid = '" + P.getUniqueId().toString() + "') AND id = '" + pot + "'"; } else { sql = "INSERT INTO coreFoundPots(id, player, timestamp, type) VALUES('" + pot + "', (SELECT id FROM players WHERE uuid = '" + P.getUniqueId().toString() + "'),'" + (int) (System.currentTimeMillis() / 1000) + "'," + type + ")"; } AnuraCore.getSql().queryUpdate(sql); Location loc = AnuraCore.getInstance().flowerPots.get(pot); for (int i = 0; i < 20; i++) { loc.getWorld().playEffect(loc, Effect.FIREWORKS_SPARK, 4); } P.playSound(loc, Sound.LEVEL_UP, 1, (float) 1.5); ResultSet rs = AnuraCore.getSql().querySelect("SELECT url, name, money FROM corePots WHERE id = '" + pot + "'"); try { rs.first(); Money.payMoney(P, rs.getInt("money")); P.sendMessage(ChatColor.RED + "---------- " + ChatColor.YELLOW + "Achievement" + ChatColor.RED + " ----------"); P.sendMessage(ChatColor.BLUE + rs.getString("name")); P.sendMessage("" + ChatColor.GOLD + ChatColor.UNDERLINE + rs.getString("url")); P.sendMessage(""); P.sendMessage(ChatColor.RED + "---------- " + ChatColor.YELLOW + "Achievement" + ChatColor.RED + " ----------"); } catch (SQLException ex) { System.out.println("Error playerFoundPot(FlowerPot)"); } } else if (getPotWaitTime(pot) != -1 && foundCache.containsKey(P) && foundCache.get(P).containsKey(pot) && foundTimestamp.containsKey(P) && foundTimestamp.get(P).containsKey(pot)) { int current = (int) (System.currentTimeMillis() / 1000); int found = foundTimestamp.get(P).get(pot); int waitTime = getPotWaitTime(pot); int toWait = (found + waitTime) - current; if (toWait <= 0) { return; } String text = ChatColor.RED + Core.getl("ach_wait_1", P); if (toWait < 60) { text += " " + ChatColor.GREEN + toWait + " " + ChatColor.RED + Core.getl("ach_wait_seconds", P); } else if (toWait < 60 * 60) { text += " " + ChatColor.GREEN + Math.round(toWait / 60) + " " + ChatColor.RED + Core.getl("ach_wait_minutes", P); } else if (toWait < 60 * 60 * 24) { text += " " + ChatColor.GREEN + Math.round(toWait / 60 / 60) + " " + ChatColor.RED + Core.getl("ach_wait_hours", P); } else { text += " " + ChatColor.GREEN + Math.round(toWait / 60 / 60 / 24) + " " + ChatColor.RED + Core.getl("ach_wait_days", P); } text += " " + Core.getl("ach_wait_2", P); P.sendMessage(text); } } }