Made it possible to save/load shops
Fixed defaults in db config
This commit is contained in:
parent
d33a0db545
commit
91c0c2ecd1
|
|
@ -1,7 +1,9 @@
|
||||||
package com.alttd.playershops;
|
package com.alttd.playershops;
|
||||||
|
|
||||||
import com.alttd.playershops.config.Config;
|
import com.alttd.playershops.config.Config;
|
||||||
|
import com.alttd.playershops.config.DatabaseConfig;
|
||||||
import com.alttd.playershops.config.MessageConfig;
|
import com.alttd.playershops.config.MessageConfig;
|
||||||
|
import com.alttd.playershops.database.Database;
|
||||||
import com.alttd.playershops.handler.ShopHandler;
|
import com.alttd.playershops.handler.ShopHandler;
|
||||||
import com.alttd.playershops.listener.PlayerListener;
|
import com.alttd.playershops.listener.PlayerListener;
|
||||||
import com.alttd.playershops.listener.ShopListener;
|
import com.alttd.playershops.listener.ShopListener;
|
||||||
|
|
@ -36,6 +38,8 @@ public class PlayerShops extends JavaPlugin {
|
||||||
registerListeners();
|
registerListeners();
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
|
||||||
|
Database.getDatabase(); //Start database
|
||||||
|
|
||||||
shopHandler = new ShopHandler(instance);
|
shopHandler = new ShopHandler(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,6 +80,7 @@ public class PlayerShops extends JavaPlugin {
|
||||||
public void reloadConfigs() {
|
public void reloadConfigs() {
|
||||||
Config.reload();
|
Config.reload();
|
||||||
MessageConfig.reload();
|
MessageConfig.reload();
|
||||||
|
DatabaseConfig.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ public class DatabaseConfig extends AbstractConfiguration {
|
||||||
public static String DRIVER = "mysql";
|
public static String DRIVER = "mysql";
|
||||||
public static String IP = "localhost";
|
public static String IP = "localhost";
|
||||||
public static String PORT = "3306";
|
public static String PORT = "3306";
|
||||||
public static String DATABASE_NAME = "AltitudeQuests";
|
public static String DATABASE_NAME = "playershops";
|
||||||
public static String USERNAME = "root";
|
public static String USERNAME = "root";
|
||||||
public static String PASSWORD = "root";
|
public static String PASSWORD = "root";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -148,4 +148,66 @@ public class ShopQueries {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int saveShopWithoutId(AbstractShop shop) {
|
||||||
|
String sql = "INSERT INTO shops " +
|
||||||
|
"(owner_name, owner_uuid, shop_type, server, container_location, sign_location, " +
|
||||||
|
"price, amount, balance, item_one, item_two, last_transaction)" +
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" +
|
||||||
|
"ON DUPLICATE KEY UPDATE owner_name = ?, owner_uuid = ?, shop_type = ?, server = ?, " +
|
||||||
|
"container_location = ?, sign_location = ?, price = ?, amount = ?, balance = ?, " +
|
||||||
|
"item_one = ?, item_two = ?, last_transaction = ?";
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
|
||||||
|
statement.setString(2, shop.getOwnerName());
|
||||||
|
statement.setString(3, shop.getOwnerUUID().toString());
|
||||||
|
statement.setString(4, shop.getServer());
|
||||||
|
statement.setString(5, shop.getType().toString());
|
||||||
|
statement.setString(6, locationToString(shop.getContainerLocation()));
|
||||||
|
statement.setString(7, locationToString(shop.getSignLocation()));
|
||||||
|
statement.setDouble(8, shop.getPrice());
|
||||||
|
statement.setInt(9, shop.getAmount());
|
||||||
|
statement.setDouble(10, shop.getBalance());
|
||||||
|
statement.setBytes(11, shop.getItemStack().serializeAsBytes());
|
||||||
|
statement.setBytes(12, shop.getSecondaryItem().serializeAsBytes());
|
||||||
|
statement.setLong(13, shop.getLastTransaction());
|
||||||
|
//repeat everything except id for update
|
||||||
|
statement.setString(14, shop.getOwnerName());
|
||||||
|
statement.setString(15, shop.getOwnerUUID().toString());
|
||||||
|
statement.setString(16, shop.getServer());
|
||||||
|
statement.setString(17, shop.getType().toString());
|
||||||
|
statement.setString(18, locationToString(shop.getContainerLocation()));
|
||||||
|
statement.setString(19, locationToString(shop.getSignLocation()));
|
||||||
|
statement.setDouble(20, shop.getPrice());
|
||||||
|
statement.setInt(21, shop.getAmount());
|
||||||
|
statement.setDouble(22, shop.getBalance());
|
||||||
|
statement.setBytes(23, shop.getItemStack().serializeAsBytes());
|
||||||
|
statement.setBytes(24, shop.getSecondaryItem().serializeAsBytes());
|
||||||
|
statement.setLong(25, shop.getLastTransaction());
|
||||||
|
|
||||||
|
if (statement.executeUpdate() != 1) {
|
||||||
|
//TODO error
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return loadShopWithoutId(shop);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int loadShopWithoutId(AbstractShop shop) {
|
||||||
|
String sql = "SELECT FROM shops WHERE server = ? AND container_location = ?";
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
statement.setString(1, shop.getServer());
|
||||||
|
statement.setString(2, locationToString(shop.getContainerLocation()));
|
||||||
|
|
||||||
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
if (resultSet.next())
|
||||||
|
return resultSet.getInt("id");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ package com.alttd.playershops.handler;
|
||||||
|
|
||||||
import com.alttd.playershops.PlayerShops;
|
import com.alttd.playershops.PlayerShops;
|
||||||
import com.alttd.playershops.config.Config;
|
import com.alttd.playershops.config.Config;
|
||||||
|
import com.alttd.playershops.database.ShopQueries;
|
||||||
import com.alttd.playershops.events.PlayerCreateShopEvent;
|
import com.alttd.playershops.events.PlayerCreateShopEvent;
|
||||||
import com.alttd.playershops.shop.AbstractShop;
|
import com.alttd.playershops.shop.AbstractShop;
|
||||||
import com.alttd.playershops.shop.ShopType;
|
import com.alttd.playershops.shop.ShopType;
|
||||||
|
import com.alttd.playershops.utils.Logger;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
@ -14,6 +16,7 @@ import org.bukkit.Tag;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
@ -34,6 +37,13 @@ public class ShopHandler {
|
||||||
shopBuildLimits = new Object2IntOpenHashMap<>();
|
shopBuildLimits = new Object2IntOpenHashMap<>();
|
||||||
shopBuildLimits.defaultReturnValue(Config.shopLimit);
|
shopBuildLimits.defaultReturnValue(Config.shopLimit);
|
||||||
shopMaterials = new ArrayList<>(); // TODO move into parent method where materials are loaded in.
|
shopMaterials = new ArrayList<>(); // TODO move into parent method where materials are loaded in.
|
||||||
|
loadShops();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadShops() {
|
||||||
|
for (AbstractShop shop : ShopQueries.loadShops()) {
|
||||||
|
shopLocation.put(shop.getContainerLocation(), shop);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractShop getShop(Location location) {
|
public AbstractShop getShop(Location location) {
|
||||||
|
|
@ -107,6 +117,19 @@ public class ShopHandler {
|
||||||
if(playerCreateShopEvent.isCancelled())
|
if(playerCreateShopEvent.isCancelled())
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
shopLocation.put(shop.getContainerLocation(), shop);
|
||||||
|
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int id = ShopQueries.saveShopWithoutId(shop);
|
||||||
|
if (id == -1)
|
||||||
|
Logger.warn("Tried to save a shop without id but couldn't get an id back [" + shop + "]");
|
||||||
|
else
|
||||||
|
shop.setId(id);
|
||||||
|
}
|
||||||
|
}.runTaskAsynchronously(PlayerShops.getInstance());
|
||||||
|
|
||||||
return shop;
|
return shop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@ import java.util.UUID;
|
||||||
|
|
||||||
public abstract class AbstractShop {
|
public abstract class AbstractShop {
|
||||||
|
|
||||||
@Getter
|
@Getter @Setter
|
||||||
private int id;
|
private int id = -1;
|
||||||
private String ownerName;
|
private String ownerName;
|
||||||
@Getter
|
@Getter
|
||||||
private UUID ownerUUID;
|
private UUID ownerUUID;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user