2015-09-02 11:06:48 +00:00
|
|
|
package de.epiceric.shopchest.utils;
|
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
import de.epiceric.shopchest.ShopChest;
|
|
|
|
import de.epiceric.shopchest.config.Config;
|
|
|
|
import de.epiceric.shopchest.shop.Shop;
|
2016-06-03 15:58:53 +00:00
|
|
|
import de.epiceric.shopchest.sql.Database;
|
|
|
|
import org.bukkit.Bukkit;
|
2015-09-02 11:06:48 +00:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
2015-09-04 18:16:33 +00:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2015-09-02 11:06:48 +00:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.Chest;
|
|
|
|
import org.bukkit.block.DoubleChest;
|
2015-09-04 18:16:33 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2016-06-03 15:58:53 +00:00
|
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
2015-09-02 11:06:48 +00:00
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2015-09-02 11:06:48 +00:00
|
|
|
|
|
|
|
public class ShopUtils {
|
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
private static HashMap<Location, Shop> shopLocation = new HashMap<>();
|
|
|
|
|
|
|
|
public static Shop getShop(Location location) {
|
|
|
|
Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ());
|
|
|
|
|
|
|
|
if (shopLocation.containsKey(newLocation)) {
|
|
|
|
return shopLocation.get(newLocation);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isShop(Location location) {
|
|
|
|
Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ());
|
|
|
|
return shopLocation.containsKey(newLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Shop[] getShops() {
|
|
|
|
ArrayList<Shop> shops = new ArrayList<>();
|
|
|
|
|
|
|
|
for (Shop shop : shopLocation.values()) {
|
|
|
|
shops.add(shop);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shops.toArray(new Shop[shops.size()]);
|
|
|
|
}
|
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
public static void addShop(Shop shop, boolean addToDatabase) {
|
2016-05-28 13:47:57 +00:00
|
|
|
Location loc = shop.getLocation();
|
|
|
|
Block b = loc.getBlock();
|
2016-06-03 15:58:53 +00:00
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
|
|
|
|
Chest c = (Chest) b.getState();
|
|
|
|
InventoryHolder ih = c.getInventory().getHolder();
|
2016-06-03 15:58:53 +00:00
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
if (ih instanceof DoubleChest) {
|
|
|
|
DoubleChest dc = (DoubleChest) ih;
|
|
|
|
Chest r = (Chest) dc.getRightSide();
|
|
|
|
Chest l = (Chest) dc.getLeftSide();
|
|
|
|
|
|
|
|
shopLocation.put(r.getLocation(), shop);
|
|
|
|
shopLocation.put(l.getLocation(), shop);
|
2016-06-03 15:58:53 +00:00
|
|
|
} else {
|
|
|
|
shopLocation.put(shop.getLocation(), shop);
|
2016-05-28 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
if (addToDatabase)
|
|
|
|
ShopChest.database.addShop(shop);
|
|
|
|
}
|
2016-05-28 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
public static void removeShop(Shop shop, boolean removeFromDatabase) {
|
2016-05-28 13:47:57 +00:00
|
|
|
Location loc = shop.getLocation();
|
|
|
|
Block b = loc.getBlock();
|
2016-06-03 15:58:53 +00:00
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
|
|
|
|
Chest c = (Chest) b.getState();
|
|
|
|
InventoryHolder ih = c.getInventory().getHolder();
|
2016-06-03 15:58:53 +00:00
|
|
|
|
2016-05-28 13:47:57 +00:00
|
|
|
if (ih instanceof DoubleChest) {
|
|
|
|
DoubleChest dc = (DoubleChest) ih;
|
|
|
|
Chest r = (Chest) dc.getRightSide();
|
|
|
|
Chest l = (Chest) dc.getLeftSide();
|
|
|
|
|
|
|
|
shopLocation.remove(r.getLocation());
|
|
|
|
shopLocation.remove(l.getLocation());
|
2016-06-03 15:58:53 +00:00
|
|
|
} else {
|
|
|
|
shopLocation.remove(shop.getLocation());
|
2016-05-28 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
shop.removeItem();
|
|
|
|
shop.removeHologram();
|
2016-05-28 13:47:57 +00:00
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
if (removeFromDatabase)
|
|
|
|
ShopChest.database.removeShop(shop);
|
|
|
|
}
|
2016-05-28 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int getShopLimit(Player p) {
|
|
|
|
int limit = Config.default_limit();
|
|
|
|
|
|
|
|
if (ShopChest.perm.hasGroupSupport()) {
|
|
|
|
List<String> groups = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (String key : Config.shopLimits_group()) {
|
|
|
|
for (int i = 0; i < ShopChest.perm.getGroups().length; i++) {
|
|
|
|
if (ShopChest.perm.getGroups()[i].equals(key)) {
|
|
|
|
if (ShopChest.perm.playerInGroup(p, key)) {
|
|
|
|
groups.add(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groups.size() != 0) {
|
|
|
|
List<Integer> limits = new ArrayList<>();
|
|
|
|
for (String group : groups) {
|
|
|
|
int gLimit = ShopChest.getInstance().getConfig().getInt("shop-limits.group." + group);
|
|
|
|
limits.add(gLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
int highestLimit = 0;
|
|
|
|
for (int l : limits) {
|
|
|
|
if (l > highestLimit) {
|
|
|
|
highestLimit = l;
|
|
|
|
} else if (l == -1) {
|
|
|
|
highestLimit = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
limit = highestLimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String key : Config.shopLimits_player()) {
|
|
|
|
int pLimit = ShopChest.getInstance().getConfig().getInt("shop-limits.player." + key);
|
|
|
|
if (Utils.isUUID(key)) {
|
|
|
|
if (p.getUniqueId().equals(UUID.fromString(key))) {
|
|
|
|
limit = pLimit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (p.getName().equals(key)) {
|
|
|
|
limit = pLimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getShopAmount(OfflinePlayer p) {
|
|
|
|
int shopCount = 0;
|
|
|
|
|
|
|
|
for (Shop shop : ShopUtils.getShops()) {
|
|
|
|
if (shop.getVendor().equals(p)) shopCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shopCount;
|
|
|
|
}
|
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
public static void reloadShops(Player player) {
|
|
|
|
for (Shop shop : ShopUtils.getShops()) {
|
|
|
|
ShopUtils.removeShop(shop, false);
|
|
|
|
}
|
2016-05-28 13:47:57 +00:00
|
|
|
|
2016-06-03 15:58:53 +00:00
|
|
|
int count = 0;
|
|
|
|
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
|
|
|
ShopUtils.addShop(shop, false);
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player != null) player.sendMessage(Config.reloaded_shops(count));
|
|
|
|
|
|
|
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
|
|
|
Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation()));
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 11:06:48 +00:00
|
|
|
}
|