Skip to content

Commit

Permalink
🍎 shop content migrations and sync stuff
Browse files Browse the repository at this point in the history
Took 36 minutes
  • Loading branch information
kiranhart committed Jul 29, 2023
1 parent 42ee377 commit 7e9955c
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 17 deletions.
10 changes: 9 additions & 1 deletion src/main/java/ca/tweetzy/shops/Shops.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import ca.tweetzy.shops.commands.ShopsCommand;
import ca.tweetzy.shops.database.DataManager;
import ca.tweetzy.shops.database.migrations._1_InitialMigration;
import ca.tweetzy.shops.database.migrations._2_ShopItemMigration;
import ca.tweetzy.shops.impl.manager.CurrencyManager;
import ca.tweetzy.shops.impl.manager.ShopContentManager;
import ca.tweetzy.shops.impl.manager.ShopManager;
import ca.tweetzy.shops.settings.Settings;
import ca.tweetzy.shops.settings.Translations;
Expand All @@ -28,6 +30,7 @@ public final class Shops extends FlightPlugin {
private final GuiManager guiManager = new GuiManager(this);

private final ShopManager shopManager = new ShopManager();
private final ShopContentManager shopContentManager = new ShopContentManager();
private final CurrencyManager currencyManager = new CurrencyManager();

// default vault economy
Expand All @@ -45,7 +48,8 @@ protected void onFlight() {
this.dataManager = new DataManager(this.databaseConnector, this);

final DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
new _1_InitialMigration()
new _1_InitialMigration(),
new _2_ShopItemMigration()
);

// run migrations for tables
Expand Down Expand Up @@ -93,6 +97,10 @@ public static ShopManager getShopManager() {
return getInstance().shopManager;
}

public static ShopContentManager getShopContentManager() {
return getInstance().shopContentManager;
}

public static CurrencyManager getCurrencyManager() {
return getInstance().currencyManager;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ca/tweetzy/shops/api/shop/AbstractShopContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
public abstract class AbstractShopContent implements ShopContent {

protected final UUID id;
protected final ShopContentType type;
protected final String shopId;
protected int minPurchaseQty;
protected double buyPrice;
protected double sellPrice;

Expand All @@ -19,9 +21,25 @@ public UUID getId() {
return this.id;
}

@NotNull
@Override
public ShopContentType getType() {
return this.type;
}

@NotNull
@Override
public String getShopId() {
return this.shopId;
}

@Override
public int getMinimumPurchaseQty() {
return this.minPurchaseQty;
}

@Override
public void setMinimumPurchaseQty(int qty) {
this.minPurchaseQty = qty;
}
}
6 changes: 6 additions & 0 deletions src/main/java/ca/tweetzy/shops/api/shop/ShopContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public interface ShopContent extends Identifiable<UUID>, Synchronize, Storeable<

@NonNull String getShopId();

@NonNull ShopContentType getType();

int getMinimumPurchaseQty();

void setMinimumPurchaseQty(final int qty);

double getBuyPrice();

void setBuyPrice(final double price);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ca/tweetzy/shops/api/shop/ShopContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ca.tweetzy.shops.api.shop;

public enum ShopContentType {

COMMAND,
ITEM
}
117 changes: 114 additions & 3 deletions src/main/java/ca/tweetzy/shops/database/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import ca.tweetzy.flight.database.UpdateCallback;
import ca.tweetzy.flight.utils.SerializeUtil;
import ca.tweetzy.shops.api.shop.Shop;
import ca.tweetzy.shops.impl.shop.ServerShop;
import ca.tweetzy.shops.impl.shop.ShopLayout;
import ca.tweetzy.shops.impl.shop.ShopSettings;
import ca.tweetzy.shops.api.shop.ShopContent;
import ca.tweetzy.shops.api.shop.ShopContentType;
import ca.tweetzy.shops.impl.shop.*;
import lombok.NonNull;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,6 +19,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public final class DataManager extends DataManagerAbstract {

Expand Down Expand Up @@ -129,6 +130,116 @@ private Shop extractServerShop(final ResultSet resultSet) throws SQLException {
);
}

public void insertServerShopContent(@NonNull final ShopContent content, final Callback<ShopContent> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
final String query = "INSERT INTO " + this.getTablePrefix() + "shop_content (id, shop_id, type, buy_price, sell_price, purchase_qty, item, command) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
final String fetchQuery = "SELECT * FROM " + this.getTablePrefix() + "shop_content WHERE id = ?";

try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
final PreparedStatement fetch = connection.prepareStatement(fetchQuery);

fetch.setString(1, content.getId().toString());

preparedStatement.setString(1, content.getId().toString());
preparedStatement.setString(2, content.getShopId().toLowerCase());
preparedStatement.setString(3, content.getType().name());
preparedStatement.setDouble(4, content.getBuyPrice());
preparedStatement.setDouble(5, content.getSellPrice());
preparedStatement.setInt(6, content.getMinimumPurchaseQty());

if (content instanceof final ItemShopContent itemShopContent)
preparedStatement.setString(7, SerializeUtil.encodeItem(itemShopContent.getItem()));
else
preparedStatement.setString(7, null);

if (content instanceof final CommandShopContent commandShopContent)
preparedStatement.setString(8, commandShopContent.getCommand());
else
preparedStatement.setString(8, null);

preparedStatement.executeUpdate();

if (callback != null) {
final ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractServerShopContent(res));
}

} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}

public void updateServerShopContent(@NonNull final ShopContent content, final Callback<Boolean> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
final String query = "UPDATE " + this.getTablePrefix() + "shop_content SET buy_price = ?, sell_price = ?, purchase_qty = ?, item = ?, command = ? WHERE id = ?";

try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {

preparedStatement.setDouble(1, content.getBuyPrice());
preparedStatement.setDouble(2, content.getSellPrice());
preparedStatement.setInt(3, content.getMinimumPurchaseQty());

if (content instanceof final ItemShopContent itemShopContent)
preparedStatement.setString(4, SerializeUtil.encodeItem(itemShopContent.getItem()));
else
preparedStatement.setString(4, null);

if (content instanceof final CommandShopContent commandShopContent)
preparedStatement.setString(5, commandShopContent.getCommand());
else
preparedStatement.setString(5, null);

preparedStatement.setString(6, content.getId().toString());

int result = preparedStatement.executeUpdate();

if (callback != null)
callback.accept(null, result > 0);

} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}

public void getServerShopContents(@NonNull final Callback<List<ShopContent>> callback) {
final List<ShopContent> contents = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "shop_content")) {
final ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
final ShopContent shopContent = extractServerShopContent(resultSet);
contents.add(shopContent);
}

callback.accept(null, contents);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}

private ShopContent extractServerShopContent(final ResultSet resultSet) throws SQLException {
return Enum.valueOf(ShopContentType.class, resultSet.getString("type").toUpperCase()) == ShopContentType.ITEM ? new ItemShopContent(
UUID.fromString(resultSet.getString("id")),
resultSet.getString("shop_id"),
SerializeUtil.decodeItem(resultSet.getString("item")),
resultSet.getInt("purchase_qty"),
resultSet.getDouble("buy_price"),
resultSet.getDouble("sell_price")
) : new CommandShopContent(
UUID.fromString(resultSet.getString("id")),
resultSet.getString("shop_id"),
resultSet.getString("command"),
resultSet.getInt("purchase_qty"),
resultSet.getDouble("buy_price")
);
}


private void resolveUpdateCallback(@Nullable UpdateCallback callback, @Nullable Exception ex) {
if (callback != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void migrate(Connection connection, String tablePrefix) throws SQLExcepti
"id VARCHAR(48) PRIMARY KEY, " +
"display_name VARCHAR(64) NOT NULL, " +
"description TEXT NOT NULL, " +
"icon VARCHAR(40) NOT NULL, " +
"icon TEXT NOT NULL, " +
"open BOOLEAN NOT NULL, " +
"requires_permission BOOLEAN NOT NULL, " +
"permission VARCHAR(64) NOT NULL, " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ca.tweetzy.shops.database.migrations;

import ca.tweetzy.flight.database.DataMigration;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public final class _2_ShopItemMigration extends DataMigration {

public _2_ShopItemMigration() {
super(2);
}

@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
try (Statement statement = connection.createStatement()) {
// shop item
statement.execute("CREATE TABLE " + tablePrefix + "shop_content (" +
"id VARCHAR(48) PRIMARY KEY, " +
"shop_id VARCHAR(48) NOT NULL, " +
"type VARCHAR(16) NOT NULL, " +
"buy_price DOUBLE NOT NULL, " +
"sell_price DOUBLE NOT NULL, " +
"purchase_qty INT NOT NULL, " +
"item TEXT NULL, " +
"command TEXT NULL " +
")");
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/ca/tweetzy/shops/gui/admin/ShopAddContentGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ca.tweetzy.shops.gui.admin;

import ca.tweetzy.shops.api.shop.Shop;
import ca.tweetzy.shops.api.shop.ShopContentType;
import ca.tweetzy.shops.gui.ShopsBaseGUI;
import lombok.NonNull;
import org.bukkit.entity.Player;

public final class ShopAddContentGUI extends ShopsBaseGUI {

private final Shop shop;
private final ShopContentType shopContentType;

public ShopAddContentGUI(@NonNull Player player, @NonNull final Shop shop, @NonNull final ShopContentType shopContentType) {
super(new ShopEditGUI(player, shop), player, "add content", 6);
this.shop = shop;
this.shopContentType = shopContentType;
draw();
}

@Override
protected void draw() {
applyBackExit();


}
}
3 changes: 3 additions & 0 deletions src/main/java/ca/tweetzy/shops/gui/admin/ShopEditGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ca.tweetzy.shops.settings.Translations;
import lombok.NonNull;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

public final class ShopEditGUI extends ShopsPagedGUI<ShopContent> {
Expand Down Expand Up @@ -42,6 +43,8 @@ protected void drawAdditional() {
))
.make(), click -> {

if (click.clickType == ClickType.LEFT)
click.manager.showGUI(click.player, new ShopSelectContentTypeGUI(click.player, this.shop, selectedType -> {}));
});

setButton(5, 7, QuickItem
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ca.tweetzy.shops.gui.admin;

import ca.tweetzy.flight.comp.enums.CompMaterial;
import ca.tweetzy.flight.settings.TranslationManager;
import ca.tweetzy.flight.utils.QuickItem;
import ca.tweetzy.shops.api.shop.Shop;
import ca.tweetzy.shops.api.shop.ShopContentType;
import ca.tweetzy.shops.gui.ShopsBaseGUI;
import ca.tweetzy.shops.settings.Translations;
import lombok.NonNull;
import org.bukkit.entity.Player;

import java.util.function.Consumer;

public final class ShopSelectContentTypeGUI extends ShopsBaseGUI {

private final Consumer<ShopContentType> contentTypeConsumer;

public ShopSelectContentTypeGUI(@NonNull Player player, @NonNull final Shop shop, @NonNull final Consumer<ShopContentType> contentTypeConsumer) {
super(new ShopEditGUI(player, shop), player, TranslationManager.string(Translations.GUI_SHOP_SELECT_CONTENT_TYPE_TITLE), 4);
this.contentTypeConsumer = contentTypeConsumer;
draw();
}

@Override
protected void draw() {
applyBackExit();

setButton(1, 2, QuickItem
.of(CompMaterial.DIAMOND_SWORD)
.hideTags(true)
.name(TranslationManager.string(Translations.GUI_SHOP_SELECT_CONTENT_TYPE_ITEMS_ITEM_NAME))
.lore(TranslationManager.list(Translations.GUI_SHOP_SELECT_CONTENT_TYPE_ITEMS_ITEM_LORE))
.make(), click -> contentTypeConsumer.accept(ShopContentType.ITEM));

setButton(1, 6, QuickItem
.of(CompMaterial.COMMAND_BLOCK)
.name(TranslationManager.string(Translations.GUI_SHOP_SELECT_CONTENT_TYPE_ITEMS_CMD_NAME))
.lore(TranslationManager.list(Translations.GUI_SHOP_SELECT_CONTENT_TYPE_ITEMS_CMD_LORE))
.make(), click -> contentTypeConsumer.accept(ShopContentType.COMMAND));
}
}
4 changes: 4 additions & 0 deletions src/main/java/ca/tweetzy/shops/gui/user/ShopContentsGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ca.tweetzy.shops.gui.user;

public final class ShopContentsGUI {
}
4 changes: 4 additions & 0 deletions src/main/java/ca/tweetzy/shops/gui/user/ShopsMainGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ca.tweetzy.shops.gui.user;

public final class ShopsMainGUI {
}
Loading

0 comments on commit 7e9955c

Please sign in to comment.