dev/rework
This commit is contained in:
@@ -3,6 +3,8 @@ package com.alttd.playershops.handler;
|
||||
import com.alttd.playershops.PlayerShops;
|
||||
import com.alttd.playershops.config.Config;
|
||||
import com.alttd.playershops.shop.PlayerShop;
|
||||
import com.alttd.playershops.storage.database.DatabaseHelper;
|
||||
import com.alttd.playershops.utils.Logger;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||
import lombok.Getter;
|
||||
@@ -12,6 +14,8 @@ import org.bukkit.Tag;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -34,6 +38,25 @@ public class ShopHandler {
|
||||
shopBuildLimits.defaultReturnValue(Config.shopLimit);
|
||||
shopMaterials = new ArrayList<>(); // TODO move into parent method where materials are loaded in.
|
||||
shopMaterials.add(Material.BARREL);
|
||||
|
||||
loadShops();
|
||||
}
|
||||
|
||||
void loadShops() {
|
||||
Logger.info("Loading all shops from database...");
|
||||
// TODO add a timer to test performance
|
||||
DatabaseHelper databaseHelper = plugin.getDatabaseHelper();
|
||||
try (ResultSet resultSet = databaseHelper.selectAllShops()) {
|
||||
while (resultSet.next()) {
|
||||
PlayerShop playerShop = databaseHelper.shopFromResultSet(resultSet);
|
||||
if (playerShop == null) continue;
|
||||
|
||||
addShop(playerShop);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Logger.error("Error loading shops\n" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerShop getShop(Location location) {
|
||||
@@ -50,6 +73,10 @@ public class ShopHandler {
|
||||
return Collections.unmodifiableCollection(shopLocation.values());
|
||||
}
|
||||
|
||||
public List<UUID> getShopOwners() {
|
||||
return shopLocation.values().stream().map(PlayerShop::getOwnerUUID).distinct().toList();
|
||||
}
|
||||
|
||||
public void addPlayerLimit(UUID uuid, int limit) {
|
||||
shopBuildLimits.put(uuid, limit);
|
||||
}
|
||||
@@ -58,7 +85,11 @@ public class ShopHandler {
|
||||
return shopBuildLimits.getInt(uuid);
|
||||
}
|
||||
|
||||
public void removeShops() {
|
||||
public void unloadShops() {
|
||||
for (PlayerShop shop : shopLocation.values()) {
|
||||
if (shop.isDirty())
|
||||
plugin.getDatabaseHelper().updateShop(shop, true);
|
||||
}
|
||||
shopLocation.clear();
|
||||
shopSignLocation.clear();
|
||||
}
|
||||
@@ -93,6 +124,7 @@ public class ShopHandler {
|
||||
public void removeShop(PlayerShop shop) {
|
||||
shopLocation.remove(shop.getShopLocation());
|
||||
shopSignLocation.remove(shop.getSignLocation());
|
||||
plugin.getDatabaseHelper().removeShop(shop);
|
||||
}
|
||||
|
||||
public boolean canPlayerBreakShop(Player player, PlayerShop shop) { // TODO move to util?
|
||||
|
||||
Reference in New Issue
Block a user