Skip to content

Commit 9565468

Browse files
committed
add new fields to sql table
1 parent b40157a commit 9565468

File tree

7 files changed

+221
-52
lines changed

7 files changed

+221
-52
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.maxgamer.quickshop.database;
2+
3+
import lombok.Getter;
4+
import org.apache.commons.lang.Validate;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
public class DataType {
9+
@Getter
10+
public DataTypeMapping datatype;
11+
@Getter
12+
public Integer length;
13+
@Getter
14+
public Object defaultValue;
15+
16+
public DataType(@NotNull DataTypeMapping type, @Nullable Integer length, @NotNull Object defaultValue){
17+
this.datatype = type;
18+
if(length != null)
19+
Validate.isTrue(length > 1, "Field length cannot be negative or zero.");
20+
this.length = length;
21+
this.defaultValue = defaultValue;
22+
}
23+
public DataType(@NotNull DataTypeMapping type, @Nullable Integer length){
24+
this.datatype = type;
25+
if(length != null)
26+
Validate.isTrue(length > 1, "Field length cannot be negative or zero.");
27+
this.length = length;
28+
this.defaultValue = null;
29+
}
30+
31+
public DataType(@NotNull DataTypeMapping type, @NotNull Object defaultValue){
32+
this.datatype = type;
33+
this.length = null;
34+
this.defaultValue = defaultValue;
35+
}
36+
37+
public DataType(@NotNull DataTypeMapping type){
38+
this.datatype = type;
39+
this.length = null;
40+
}
41+
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.maxgamer.quickshop.database;
2+
3+
import lombok.Getter;
4+
5+
public enum DataTypeMapping {
6+
TINYINT("tinyint","integer"),
7+
SMALLINT("smallint","integer"),
8+
MEDIUMINT("mediumint","integer"),
9+
INT("int","integer"),
10+
BIGINT("bigint","integer"),
11+
FLOAT("float","float"),
12+
DOUBLE("double","float"),
13+
VARCHAR("varchar","varchar"),
14+
TINYBLOB("tinyblob","blob"),
15+
TINYTEXT("tinytext","tinytext"),
16+
BLOB("blob","blob"),
17+
TEXT("text","text"),
18+
MEDIUMBLOB("mediumblob","blob"),
19+
MEDIUMTEXT("mediumtext","text"),
20+
LONGBLOB("longblob","blob"),
21+
LONGTEXT("longtext","text");
22+
23+
24+
25+
@Getter
26+
private final String mysql;
27+
@Getter
28+
private final String sqlite;
29+
DataTypeMapping( String mysql, String sqlite ){
30+
this.mysql = mysql;
31+
this.sqlite = sqlite;
32+
}
33+
}

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

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,32 +117,12 @@ public void onFailed(SQLException e) {
117117
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
118118
.getDbPrefix() + "messages MODIFY COLUMN time BIGINT(32) NOT NULL AFTER message", checkTask));
119119
//Extra column
120-
try {
121-
if (!manager.hasColumn(plugin.getDbPrefix() + "shops", "extra")) {
122-
String sqlString;
123-
if (manager.getDatabase() instanceof MySQLCore) {
124-
sqlString = "ALTER TABLE " + plugin
125-
.getDbPrefix() + "shops ADD extra LONGTEXT";
126-
} else {
127-
sqlString = "ALTER TABLE " + plugin
128-
.getDbPrefix() + "shops ADD COLUMN extra TEXT";
129-
}
130-
manager.runInstantTask(new DatabaseTask(sqlString, new DatabaseTask.Task() {
131-
@Override
132-
public void edit(PreparedStatement ps) throws SQLException {
133-
}
134-
135-
@Override
136-
public void onFailed(SQLException e) {
137-
Util.debugLog("Error to create EXTRA column: " + e.getMessage());
138-
}
139-
}));
140-
Util.debugLog("Setting up the column EXTRA...");
141-
}
142-
} catch (SQLException e) {
143-
Util.debugLog("Error to create EXTRA column: " + e.getMessage());
144-
//ignore
145-
}
120+
createColumn("shops","extra",new DataType(DataTypeMapping.LONGTEXT,null,""));
121+
createColumn("shops","currency",new DataType(DataTypeMapping.TEXT));
122+
createColumn("shops","disableDisplay",new DataType(DataTypeMapping.INT,null,-1));
123+
createColumn("shops","taxAccount",new DataType(DataTypeMapping.VARCHAR,255));
124+
125+
146126

147127
if (manager.getDatabase() instanceof MySQLCore) {
148128
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
@@ -153,6 +133,38 @@ public void onFailed(SQLException e) {
153133
plugin.getLogger().info("Finished!");
154134
}
155135

136+
public boolean createColumn(@NotNull String tableName, @NotNull String columnName, @NotNull DataType type) {
137+
138+
try {
139+
String table = plugin.getDbPrefix() + tableName;
140+
if (manager.hasColumn(table, columnName))
141+
return false;
142+
String sqlString;
143+
if (manager.getDatabase() instanceof MySQLCore) {
144+
sqlString = "alter table " + table + " add "+columnName+" " + type.getDatatype().getMysql();
145+
} else {
146+
sqlString = "alter table " + table + " add column "+columnName+" " + type.getDatatype().getSqlite();
147+
}
148+
if (type.getLength() != null)
149+
sqlString += "(" + type.getLength().toString()+") ";
150+
Util.debugLog("Append sql for creating column is "+ sqlString);
151+
manager.runInstantTask(new DatabaseTask(sqlString, new DatabaseTask.Task() {
152+
@Override
153+
public void edit(PreparedStatement ps) {
154+
}
155+
156+
@Override
157+
public void onFailed(SQLException e) {
158+
Util.debugLog("Cannot create column " + columnName + " casued by:" + e.getMessage());
159+
}
160+
}));
161+
return true;
162+
} catch (SQLException sqlException) {
163+
Util.debugLog("Cannot create column " + columnName + " casued by:" + sqlException.getMessage());
164+
return false;
165+
}
166+
}
167+
156168
public void cleanMessage(long weekAgo) {
157169
String sqlString = "DELETE FROM " + plugin
158170
.getDbPrefix() + "messages WHERE time < ?";
@@ -285,20 +297,26 @@ public void updateOwner2UUID(@NotNull String ownerUUID, int x, int y, int z, @No
285297
}
286298

287299
public void updateShop(@NotNull String owner, @NotNull ItemStack item, int unlimited, int shopType,
288-
double price, int x, int y, int z, String world, String extra) {
300+
double price, int x, int y, int z, @NotNull String world, @NotNull String extra,
301+
@NotNull String currency, boolean disableDisplay, @Nullable String taxAccount) {
289302
String sqlString = "UPDATE " + plugin
290-
.getDbPrefix() + "shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ?, extra = ? WHERE x = ? AND y = ? and z = ? and world = ?";
303+
.getDbPrefix() + "shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ?," +
304+
" extra = ?, currency = ?, disableDisplay = ?, taxAccount = ?" +
305+
" WHERE x = ? AND y = ? and z = ? and world = ?";
291306
manager.addDelayTask(new DatabaseTask(sqlString, ps -> {
292307
ps.setString(1, owner);
293308
ps.setString(2, Util.serialize(item));
294309
ps.setInt(3, unlimited);
295310
ps.setInt(4, shopType);
296311
ps.setDouble(5, price);
297312
ps.setString(6, extra);
298-
ps.setInt(7, x);
299-
ps.setInt(8, y);
300-
ps.setInt(9, z);
301-
ps.setString(10, world);
313+
ps.setString(7, currency);
314+
ps.setInt(8,((!disableDisplay) ? 0: 1));
315+
ps.setString(9,taxAccount);
316+
ps.setInt(10, x);
317+
ps.setInt(11, y);
318+
ps.setInt(12, z);
319+
ps.setString(13, world);
302320
}));
303321
//db.execute(q, owner, Util.serialize(item), unlimited, shopType, price, x, y, z, world);
304322

@@ -307,7 +325,9 @@ public void updateShop(@NotNull String owner, @NotNull ItemStack item, int unlim
307325
private void bakeTraceIfNeeded() {
308326
if (plugin.getConfig().getBoolean("debug.shop-deletion")) {
309327
for (StackTraceElement stackTraceElement : new Exception().getStackTrace()) {
310-
plugin.log("at [" + stackTraceElement.getClassName() + "] [" + stackTraceElement.getMethodName() + "] (" + stackTraceElement.getLineNumber() + ") - " + stackTraceElement.getFileName());
328+
plugin.log("at [" + stackTraceElement.getClassName() + "]" +
329+
"[" + stackTraceElement.getMethodName() + "] (" + stackTraceElement.getLineNumber() + ") - "
330+
+ stackTraceElement.getFileName());
311331
}
312332
}
313333
}

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

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public class ContainerShop implements Shop {
9191
@EqualsAndHashCode.Exclude
9292
private volatile boolean dirty;
9393
private volatile boolean updating = false;
94+
@Nullable
95+
private String currency;
96+
97+
private boolean disableDisplay;
98+
99+
private UUID taxAccount;
94100

95101
@SuppressWarnings("CopyConstructorMissesField")
96102
private ContainerShop(@NotNull ContainerShop s) {
@@ -108,6 +114,8 @@ private ContainerShop(@NotNull ContainerShop s) {
108114
this.extra = s.extra;
109115
this.dirty = true;
110116
this.inventoryPreview = null;
117+
this.currency = s.currency;
118+
this.disableDisplay = s.disableDisplay;
111119
initDisplayItem();
112120
}
113121

@@ -133,7 +141,9 @@ public ContainerShop(
133141
@NotNull ShopModerator moderator,
134142
boolean unlimited,
135143
@NotNull ShopType type,
136-
@NotNull YamlConfiguration extra) {
144+
@NotNull YamlConfiguration extra,
145+
@Nullable String currency,
146+
@NotNull boolean disableDisplay) {
137147
Util.ensureThread(false);
138148
this.location = location;
139149
this.price = price;
@@ -159,8 +169,36 @@ public ContainerShop(
159169
this.shopType = type;
160170
this.unlimited = unlimited;
161171
this.extra = extra;
172+
this.currency = currency;
173+
this.disableDisplay = disableDisplay;
162174
initDisplayItem();
163175
this.dirty = false;
176+
updateShopData();
177+
}
178+
179+
private void updateShopData() {
180+
ConfigurationSection section = getExtra(plugin);
181+
if (section.getString("currency") != null) {
182+
this.currency = section.getString("currency");
183+
section.set("currency", null);
184+
}
185+
setExtra(plugin, section);
186+
setDirty();
187+
this.update();
188+
}
189+
190+
public boolean isDisableDisplay() {
191+
return disableDisplay;
192+
}
193+
194+
@Override
195+
@Nullable
196+
public UUID getTaxAccount() {
197+
if (taxAccount != null)
198+
return taxAccount;
199+
if (plugin.getShopManager().getCacheTaxAccount() != null)
200+
return plugin.getShopManager().getCacheTaxAccount().getUniqueId();
201+
return null;
164202
}
165203

166204
private void initDisplayItem() {
@@ -487,7 +525,7 @@ public void onUnload() {
487525
OfflinePlayer player = plugin.getServer().getOfflinePlayer(this.getOwner());
488526
String name = player.getName();
489527
if (name == null || name.isEmpty()) {
490-
name = plugin.text().of( "unknown-owner").forLocale();
528+
name = plugin.text().of("unknown-owner").forLocale();
491529
}
492530
if (forceUsername) {
493531
return name;
@@ -643,7 +681,7 @@ public String[] getSignText() {
643681
break;
644682
//No remaining
645683
case 0:
646-
lines[1] =plugin.text().of(noRemainingStringKey).forLocale();
684+
lines[1] = plugin.text().of(noRemainingStringKey).forLocale();
647685
break;
648686
//Has remaining
649687
default:
@@ -655,11 +693,11 @@ public String[] getSignText() {
655693

656694
//line 4
657695
if (this.isStackingShop()) {
658-
lines[3] = plugin.text().of("signs.stack-price",
696+
lines[3] = plugin.text().of("signs.stack-price",
659697
Util.format(this.getPrice(), this), Integer.toString(item.getAmount()),
660698
Util.getItemStackName(item)).forLocale();
661699
} else {
662-
lines[3] = plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale();
700+
lines[3] = plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale();
663701
}
664702

665703
//New pattern for recognizing shop sign
@@ -736,7 +774,7 @@ public void update() {
736774
plugin.getDatabaseHelper()
737775
.updateShop(ShopModerator.serialize(this.moderator), this.getItem(),
738776
unlimited, shopType.toID(), this.getPrice(), x, y, z, world,
739-
this.saveExtraToYaml());
777+
this.saveExtraToYaml(), this.currency, this.disableDisplay, this.taxAccount == null ? null : this.taxAccount.toString());
740778
this.dirty = false;
741779
} catch (Exception e) {
742780
plugin.getLogger().log(Level.WARNING,
@@ -1032,15 +1070,15 @@ public void setShopType(@NotNull ShopType newShopType) {
10321070
signs.add(sign);
10331071
} else {
10341072
String header = lines[0];
1035-
String adminShopHeader = plugin.text().of("signs.header",plugin.text().of("admin-shop").forLocale()).forLocale();
1073+
String adminShopHeader = plugin.text().of("signs.header", plugin.text().of("admin-shop").forLocale()).forLocale();
10361074
String signHeaderUsername = plugin.text().of("signs.header", this.ownerName(true)).forLocale();
10371075
if (header.contains(adminShopHeader) || header.contains(signHeaderUsername)) {
10381076
signs.add(sign);
10391077
//TEXT SIGN
10401078
//continue
10411079
} else {
1042-
adminShopHeader = plugin.text().of("signs.header", plugin.text().of("admin-shop").forLocale(), "").forLocale();
1043-
signHeaderUsername = plugin.text().of("signs.header", this.ownerName(true), "").forLocale();
1080+
adminShopHeader = plugin.text().of("signs.header", plugin.text().of("admin-shop").forLocale(), "").forLocale();
1081+
signHeaderUsername = plugin.text().of("signs.header", this.ownerName(true), "").forLocale();
10441082
adminShopHeader = ChatColor.stripColor(adminShopHeader).trim();
10451083
signHeaderUsername = ChatColor.stripColor(signHeaderUsername).trim();
10461084
if (header.contains(adminShopHeader) || header.contains(signHeaderUsername)) {
@@ -1434,8 +1472,7 @@ public boolean isStackingShop() {
14341472
*/
14351473
@Override
14361474
public @Nullable String getCurrency() {
1437-
ConfigurationSection section = getExtra(plugin);
1438-
return section.getString("currency");
1475+
return this.currency;
14391476
}
14401477

14411478
/**
@@ -1445,9 +1482,7 @@ public boolean isStackingShop() {
14451482
*/
14461483
@Override
14471484
public void setCurrency(@Nullable String currency) {
1448-
ConfigurationSection section = getExtra(plugin);
1449-
section.set("currency", currency);
1450-
setExtra(plugin, section);
1485+
this.currency = currency;
14511486
setDirty();
14521487
this.update();
14531488
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
public enum DisplayType {
3030
/*
31-
* UNKNOWN = FALLBACK TO REALITEM
3231
* REALITEM = USE REAL DROPPED ITEM
3332
* ARMORSTAND = USE ARMORSTAND DISPLAY
3433
* VIRTUALITEM = USE VIRTUAL DROPPED ITEM (CLIENT SIDE)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,18 @@ default String[] getSignText() {
495495
*/
496496
ShopInfoStorage saveToInfoStorage();
497497

498+
/**
499+
* Getting if this shop has been disabled the display
500+
* @return Does display has been disabled
501+
*/
502+
boolean isDisableDisplay();
503+
504+
/**
505+
* Getting the shop tax account, it can be specific uuid or general tax account
506+
* @return Shop Tax Account, null if use general tax account
507+
*/
508+
@Nullable
509+
UUID getTaxAccount();
510+
498511

499512
}

0 commit comments

Comments
 (0)