Skip to content

Commit 6c8c619

Browse files
committed
Ready for external cache to used for WebUI or something like that
1 parent 58dce2d commit 6c8c619

File tree

7 files changed

+46
-6
lines changed

7 files changed

+46
-6
lines changed

src/main/java/org/maxgamer/quickshop/database/DataTypeMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Getter;
44

55
public enum DataTypeMapping {
6-
INT("int", "integer"),
6+
INT("integer", "integer"),
77
BIGINT("bigint", "integer"),
88
FLOAT("float", "float"),
99
DOUBLE("double", "float"),

src/main/java/org/maxgamer/quickshop/database/DatabaseHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ private void init() throws SQLException {
6969
if(!manager.hasTable(plugin.getDbPrefix() + "logs")){
7070
createLogsTable();
7171
}
72+
if(!manager.hasTable(plugin.getDbPrefix()+"external_cache")){
73+
createExternalCacheTable();
74+
}
7275
checkColumns();
7376
}
7477

@@ -105,6 +108,14 @@ private void createLogsTable(){
105108
createColumn("logs", "data", new DataType(DataTypeMapping.LONGTEXT, null, ""));
106109
}
107110

111+
private void createExternalCacheTable(){
112+
String createTable = "CREATE TABLE " + plugin.getDbPrefix()
113+
+ "external_cache ( x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL );";
114+
manager.runInstantTask(new DatabaseTask(createTable));
115+
createColumn("external_cache", "space", new DataType(DataTypeMapping.INT, null, ""));
116+
createColumn("external_cache", "stock", new DataType(DataTypeMapping.INT, null, ""));
117+
}
118+
108119

109120
/**
110121
* Verifies that all required columns exist.
@@ -309,6 +320,10 @@ public void updateOwner2UUID(@NotNull String ownerUUID, int x, int y, int z, @No
309320
}));
310321
}
311322

323+
public void updateExternalInventoryProfileCache(@NotNull Shop shop, int space, int stock){
324+
325+
}
326+
312327
public void updateShop(@NotNull String owner, @NotNull ItemStack item, int unlimited, int shopType,
313328
double price, int x, int y, int z, @NotNull String world, @NotNull String extra,
314329
@NotNull String currency, boolean disableDisplay, @Nullable String taxAccount) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.maxgamer.quickshop.event;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import org.maxgamer.quickshop.shop.Shop;
7+
@EqualsAndHashCode(callSuper = true)
8+
@Data
9+
@AllArgsConstructor
10+
public class ShopInventoryCalculate extends QSEvent {
11+
private Shop shop;
12+
private int space;
13+
private int stock;
14+
}

src/main/java/org/maxgamer/quickshop/listener/InternalListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public void shopPurchase(ShopSuccessPurchaseEvent event) {
132132
}
133133
}
134134

135+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
136+
public void shopInventoryCalc(ShopInventoryCalculate event) {
137+
plugin.getDatabaseHelper().updateExternalInventoryProfileCache(event.getShop(),event.getSpace(), event.getStock());
138+
}
139+
140+
141+
135142
/**
136143
* Callback for reloading
137144
*

src/main/java/org/maxgamer/quickshop/listener/PlayerListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private void postTrade(PlayerInteractEvent e) {
194194
final double money = plugin.getEconomy().getBalance(p.getUniqueId(), shop.getLocation().getWorld(), shop.getCurrency());
195195

196196
if (shop.isSelling()) {
197-
int itemAmount = Math.min(Util.countSpace(p.getInventory(), shop.getItem()), (int) Math.floor(money / price));
197+
int itemAmount = Math.min(shop.getRemainingSpace(), (int) Math.floor(money / price));
198198
if (!shop.isUnlimited()) {
199199
itemAmount = Math.min(itemAmount, shop.getRemainingStock());
200200
}
@@ -208,7 +208,7 @@ private void postTrade(PlayerInteractEvent e) {
208208
}
209209
} else {
210210
final double ownerBalance = eco.getBalance(shop.getOwner(), shop.getLocation().getWorld(), shop.getCurrency());
211-
int items = Util.countItems(p.getInventory(), shop.getItem());
211+
int items = shop.getRemainingStock();
212212
final int ownerCanAfford = (int) (ownerBalance / shop.getPrice());
213213

214214
if (!shop.isUnlimited()) {

src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,9 @@ public int getRemainingSpace() {
10431043
if (this.unlimited) {
10441044
return -1;
10451045
}
1046-
return Util.countSpace(this.getInventory(), this.getItem());
1046+
int space = Util.countSpace(this.getInventory(), this.getItem());
1047+
new ShopInventoryCalculate(this,space,-1).callEvent();
1048+
return space;
10471049
}
10481050

10491051
/**
@@ -1057,7 +1059,9 @@ public int getRemainingStock() {
10571059
if (this.unlimited) {
10581060
return -1;
10591061
}
1060-
return Util.countItems(this.getInventory(), this.getItem());
1062+
int stock = Util.countItems(this.getInventory(), this.getItem());
1063+
new ShopInventoryCalculate(this,-1,stock).callEvent();
1064+
return stock;
10611065
}
10621066

10631067
@Override

src/main/java/org/maxgamer/quickshop/shop/ShopManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ private void actionTrade(@NotNull Player p, @NotNull Info info, @NotNull String
12561256
amount = Integer.parseInt(message);
12571257
} else {
12581258
if (message.equalsIgnoreCase(plugin.getConfig().getString("shop.word-for-trade-all-items", "all"))) {
1259-
int shopHaveItems = Util.countItems(((ContainerShop) shop).getInventory(), shop.getItem());
1259+
int shopHaveItems = shop.getRemainingStock();
12601260
int invHaveSpaces = Util.countSpace(p.getInventory(), shop.getItem());
12611261
if (!shop.isUnlimited()) {
12621262
amount = Math.min(shopHaveItems, invHaveSpaces);

0 commit comments

Comments
 (0)