Skip to content

Commit 3896d09

Browse files
committed
Designing structure.
1 parent ff1b292 commit 3896d09

File tree

9 files changed

+298
-4
lines changed

9 files changed

+298
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ group = 'io.izzel.netzach'
77
version = '1.0.0'
88

99
taboolib {
10+
loaderVersion = '2.2'
1011
classifier = null
1112
}
1213

src/main/java/io/izzel/netzach/Netzach.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import io.izzel.netzach.config.ConfigSpec;
44
import io.izzel.netzach.data.DataManager;
55
import io.izzel.taboolib.loader.Plugin;
6+
import io.izzel.taboolib.module.dependency.Dependency;
67
import io.izzel.taboolib.module.locale.TLocale;
78
import org.yaml.snakeyaml.Yaml;
89
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
910

11+
@Dependency(maven = "com.h2database:h2:1.4.200")
1012
public final class Netzach extends Plugin {
1113

1214
private ConfigSpec configSpec;

src/main/java/io/izzel/netzach/data/DataManager.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import com.zaxxer.hikari.HikariDataSource;
55
import com.zaxxer.hikari.util.PropertyElf;
66
import io.izzel.netzach.Netzach;
7+
import io.izzel.netzach.data.entity.ItemRecord;
8+
import io.izzel.netzach.data.entity.LogRecord;
79
import io.izzel.taboolib.module.locale.TLocale;
810

911
import java.sql.Connection;
12+
import java.sql.PreparedStatement;
1013
import java.sql.SQLException;
1114
import java.util.Properties;
1215

@@ -20,8 +23,8 @@ public void open() {
2023
hikariConfig.setJdbcUrl(jdbcUrl);
2124
hikariConfig.setPoolName("Netzach");
2225

23-
if (jdbcUrl.startsWith("jdbc:sqlite")) {
24-
hikariConfig.setDriverClassName("org.sqlite.JDBC");
26+
if (jdbcUrl.startsWith("jdbc:h2")) {
27+
hikariConfig.setDriverClassName("org.h2.Driver");
2528
} else if (jdbcUrl.startsWith("jdbc:mysql")) {
2629
hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
2730
}
@@ -34,7 +37,23 @@ public void open() {
3437
dataSourceProp.putAll(Netzach.config().database().dataSourceProperties());
3538
hikariConfig.setDataSourceProperties(dataSourceProp);
3639
this.dataSource = new HikariDataSource(hikariConfig);
37-
TLocale.sendToConsole("load.database");
40+
this.setup();
41+
}
42+
43+
private void setup() {
44+
try (Connection connection = connection()) {
45+
try (PreparedStatement statement = connection.prepareStatement(
46+
String.format(ItemRecord.CREATE_TABLE, Netzach.config().database().tablePrefix() + "items"))) {
47+
statement.execute();
48+
}
49+
try (PreparedStatement statement = connection.prepareStatement(
50+
String.format(LogRecord.CREATE_TABLE, Netzach.config().database().tablePrefix() + "logs"))) {
51+
statement.execute();
52+
}
53+
TLocale.sendToConsole("load.database");
54+
} catch (Throwable t) {
55+
TLocale.sendToConsole("load.database-error", t);
56+
}
3857
}
3958

4059
public void close() {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package io.izzel.netzach.data.entity;
2+
3+
import com.google.common.io.ByteStreams;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import java.sql.PreparedStatement;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.util.UUID;
11+
12+
public class ItemRecord {
13+
14+
public static final String CREATE_TABLE =
15+
"CREATE TABLE IF NOT EXISTS %s\n" +
16+
"(\n" +
17+
" id INT PRIMARY KEY AUTO_INCREMENT,\n" +
18+
" shop VARCHAR(32) NOT NULL,\n" +
19+
" item MEDIUMBLOB NOT NULL,\n" +
20+
" stock INT NOT NULL,\n" +
21+
" owner VARCHAR(36) NOT NULL,\n" +
22+
" create_time BIGINT(19) NOT NULL,\n" +
23+
" stock_time BIGINT(19) NOT NULL,\n" +
24+
" expire BIGINT(19) NOT NULL,\n" +
25+
" info TEXT NOT NULL,\n" +
26+
" INDEX (shop)\n" +
27+
");";
28+
29+
private int id;
30+
private String shop;
31+
private byte[] item;
32+
private int stock;
33+
private UUID owner;
34+
private long createTime;
35+
private long stockTime;
36+
private long expire;
37+
private String info;
38+
39+
public int id() {
40+
return id;
41+
}
42+
43+
public void setId(int id) {
44+
this.id = id;
45+
}
46+
47+
public String shop() {
48+
return shop;
49+
}
50+
51+
public void setShop(String shop) {
52+
this.shop = shop;
53+
}
54+
55+
public byte[] item() {
56+
return item;
57+
}
58+
59+
public void setItem(byte[] item) {
60+
this.item = item;
61+
}
62+
63+
public int stock() {
64+
return stock;
65+
}
66+
67+
public void setStock(int stock) {
68+
this.stock = stock;
69+
}
70+
71+
public UUID owner() {
72+
return owner;
73+
}
74+
75+
public void setOwner(UUID owner) {
76+
this.owner = owner;
77+
}
78+
79+
public long createTime() {
80+
return createTime;
81+
}
82+
83+
public void setCreateTime(long createTime) {
84+
this.createTime = createTime;
85+
}
86+
87+
public long stockTime() {
88+
return stockTime;
89+
}
90+
91+
public void setStockTime(long stockTime) {
92+
this.stockTime = stockTime;
93+
}
94+
95+
public long expire() {
96+
return expire;
97+
}
98+
99+
public void setExpire(long expire) {
100+
this.expire = expire;
101+
}
102+
103+
public String info() {
104+
return info;
105+
}
106+
107+
public void setInfo(String info) {
108+
this.info = info;
109+
}
110+
111+
public void read(ResultSet resultSet, int i) throws SQLException, IOException {
112+
this.id = resultSet.getInt(i++);
113+
this.shop = resultSet.getString(i++);
114+
this.item = ByteStreams.toByteArray(resultSet.getBlob(i++).getBinaryStream());
115+
this.stock = resultSet.getInt(i++);
116+
this.owner = UUID.fromString(resultSet.getString(i++));
117+
this.createTime = resultSet.getLong(i++);
118+
this.stockTime = resultSet.getLong(i++);
119+
this.expire = resultSet.getLong(i++);
120+
this.info = resultSet.getString(i);
121+
}
122+
123+
public void write(PreparedStatement statement, int i) throws SQLException {
124+
statement.setInt(i++, this.id);
125+
statement.setString(i++, this.shop);
126+
statement.setBlob(i++, new ByteArrayInputStream(this.item));
127+
statement.setInt(i++, this.stock);
128+
statement.setString(i++, this.owner.toString());
129+
statement.setLong(i++, this.createTime);
130+
statement.setLong(i++, this.stockTime);
131+
statement.setLong(i++, this.expire);
132+
statement.setString(i, this.info);
133+
}
134+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.izzel.netzach.data.entity;
2+
3+
import java.sql.PreparedStatement;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.util.UUID;
7+
8+
public class LogRecord {
9+
10+
public static final String CREATE_TABLE =
11+
"CREATE TABLE IF NOT EXISTS %s\n" +
12+
"(\n" +
13+
" action INT NOT NULL,\n" +
14+
" id INT NOT NULL,\n" +
15+
" time BIGINT(19) NOT NULL,\n" +
16+
" source VARCHAR(36),\n" +
17+
" info TEXT\n" +
18+
");";
19+
20+
private Action action;
21+
private int id;
22+
private long time;
23+
private UUID source;
24+
private String info;
25+
26+
public Action action() {
27+
return action;
28+
}
29+
30+
public void setAction(Action action) {
31+
this.action = action;
32+
}
33+
34+
public int id() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
public long time() {
43+
return time;
44+
}
45+
46+
public void setTime(long time) {
47+
this.time = time;
48+
}
49+
50+
public UUID source() {
51+
return source;
52+
}
53+
54+
public void setSource(UUID source) {
55+
this.source = source;
56+
}
57+
58+
public String info() {
59+
return info;
60+
}
61+
62+
public void setInfo(String info) {
63+
this.info = info;
64+
}
65+
66+
public void read(ResultSet resultSet, int i) throws SQLException {
67+
this.action = Action.values()[resultSet.getInt(i++)];
68+
this.id = resultSet.getInt(i++);
69+
this.time = resultSet.getLong(i++);
70+
String uuid = resultSet.getString(i++);
71+
if (uuid != null) {
72+
this.source = UUID.fromString(uuid);
73+
}
74+
this.info = resultSet.getString(i);
75+
}
76+
77+
public void write(PreparedStatement statement, int i) throws SQLException {
78+
statement.setInt(i++, this.action.ordinal());
79+
statement.setInt(i++, this.id);
80+
statement.setLong(i++, this.time);
81+
if (this.source != null) {
82+
statement.setString(i, this.source.toString());
83+
}
84+
i++;
85+
if (this.info != null) {
86+
statement.setString(i, this.info);
87+
}
88+
}
89+
90+
public enum Action {
91+
CREATE,
92+
DELETE,
93+
BUY,
94+
SELL,
95+
SETTING
96+
}
97+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.izzel.netzach.shop;
2+
3+
import io.izzel.netzach.Netzach;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.Iterator;
9+
10+
public class ShopManager {
11+
12+
public void load() {
13+
try {
14+
Path shops = Netzach.instance().getDataFolder().toPath().resolve("shops");
15+
Iterator<Path> iterator = Files.walk(shops).filter(it -> it.toString().endsWith(".yml")).iterator();
16+
while (iterator.hasNext()) {
17+
Path next = iterator.next();
18+
19+
}
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}

src/main/resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
database:
33
tablePrefix: 'netzach_'
4-
jdbcUrl: 'jdbc:sqlite:./plugins/Netzach/data.db'
4+
jdbcUrl: 'jdbc:h2:file:./plugins/Netzach/data.db'
55
#jdbcUrl: 'jdbc:mysql://localhost:3306/database'
66
properties:
77
#username: root

src/main/resources/lang/zh_CN.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
load:
33
database: '数据库初始化完成'
4+
database-error: '&c数据库初始化错误: {0}'
45
complete: 'Netzach 加载完成'
56
database:
67
unknown_protocol: '&未知的 jdbc 协议 {0}'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
layout:
2+
'XXXXXXXXX'
3+
'IIIIIIIII'
4+
'IIIIIIIII'
5+
'IIIIIIIII'
6+
'IIIIIIIII'
7+
'XXAXBXCXX'
8+
layout-button:
9+
X:
10+
type: item
11+
I:
12+
type: slot
13+
14+
default-setting:
15+
location: flow
16+
#permission: some

0 commit comments

Comments
 (0)