Skip to content

Commit b612713

Browse files
author
uweb
committed
V1.0
0 parents  commit b612713

File tree

12 files changed

+713
-0
lines changed

12 files changed

+713
-0
lines changed

.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Package Files #
21+
*.jar
22+
*.war
23+
*.nar
24+
*.ear
25+
*.zip
26+
*.tar.gz
27+
*.rar
28+
29+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30+
hs_err_pid*
31+
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
# Windows thumbnail cache files
74+
Thumbs.db
75+
Thumbs.db:encryptable
76+
ehthumbs.db
77+
ehthumbs_vista.db
78+
79+
# Dump file
80+
*.stackdump
81+
82+
# Folder config file
83+
[Dd]esktop.ini
84+
85+
# Recycle Bin used on file shares
86+
$RECYCLE.BIN/
87+
88+
# Windows Installer files
89+
*.cab
90+
*.msi
91+
*.msix
92+
*.msm
93+
*.msp
94+
95+
# Windows shortcuts
96+
*.lnk
97+
98+
target/
99+
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
105+
release.properties
106+
dependency-reduced-pom.xml
107+
buildNumber.properties
108+
.mvn/timing.properties
109+
.mvn/wrapper/maven-wrapper.jar
110+
.flattened-pom.xml
111+
112+
# Common working directory
113+
run/

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Inventory Database
2+
3+
This plugin syncs players' inventory and armor using a database.
4+
Currently, the data is only saved when the user disconnects or gets kicked.
5+
6+
### Configuration
7+
8+
```
9+
# MySQL Database
10+
database:
11+
host: 127.0.0.1
12+
port: 3306
13+
# Database name (The database must exist)
14+
databaseName: 'minecraft'
15+
username: 'minecraft'
16+
# Use a strong password!
17+
password: 'password'
18+
19+
general:
20+
# Enable inventory sync
21+
syncInventory: true
22+
# Enable armor sync
23+
syncArmor: true
24+
25+
debug:
26+
# Inventory sync debug messages.
27+
syncMessages: false
28+
```

pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.lunarsoftware</groupId>
8+
<artifactId>inventorydatabase</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<name>InventoryDatabase</name>
13+
14+
<description>Syncs the Player inventory with a mysql database</description>
15+
<properties>
16+
<java.version>1.8</java.version>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<url>mc.lunar-software.eu</url>
20+
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.8.1</version>
27+
<configuration>
28+
<source>${java.version}</source>
29+
<target>${java.version}</target>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-shade-plugin</artifactId>
35+
<version>3.2.4</version>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>shade</goal>
41+
</goals>
42+
<configuration>
43+
<createDependencyReducedPom>false</createDependencyReducedPom>
44+
</configuration>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
</plugins>
49+
<resources>
50+
<resource>
51+
<directory>src/main/resources</directory>
52+
<filtering>true</filtering>
53+
</resource>
54+
</resources>
55+
</build>
56+
57+
<repositories>
58+
<repository>
59+
<id>papermc-repo</id>
60+
<url>https://repo.papermc.io/repository/maven-public/</url>
61+
</repository>
62+
<repository>
63+
<id>sonatype</id>
64+
<url>https://oss.sonatype.org/content/groups/public/</url>
65+
</repository>
66+
</repositories>
67+
68+
<dependencies>
69+
<dependency>
70+
<groupId>io.papermc.paper</groupId>
71+
<artifactId>paper-api</artifactId>
72+
<version>1.19.3-R0.1-SNAPSHOT</version>
73+
<scope>provided</scope>
74+
</dependency>
75+
</dependencies>
76+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.lunarsoftware.inventorydatabase;
2+
3+
import java.io.File;
4+
5+
public class Configuration {
6+
public static void loadConfig() {
7+
InventoryDatabase plugin = InventoryDatabase.getInstance();
8+
File dataFolder = plugin.getDataFolder();
9+
if (!dataFolder.exists()) dataFolder.mkdir();
10+
11+
File configurationFile = new File(dataFolder + System.getProperty("file.separator") + "config.yml");
12+
if (!configurationFile.exists()) plugin.saveDefaultConfig();
13+
14+
try {
15+
plugin.getConfig().load(configurationFile);
16+
} catch (Exception e) {
17+
plugin.getLogger().severe("Could not load the config file! Please check if the permissions are set! Error: " + e.getMessage());
18+
}
19+
}
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.lunarsoftware.inventorydatabase;
2+
3+
import de.lunarsoftware.inventorydatabase.database.MySQL;
4+
import de.lunarsoftware.inventorydatabase.events.PlayerJoin;
5+
import de.lunarsoftware.inventorydatabase.events.PlayerLeave;
6+
import de.lunarsoftware.inventorydatabase.sync.Sync;
7+
import org.bukkit.plugin.PluginManager;
8+
import org.bukkit.plugin.java.JavaPlugin;
9+
10+
import java.util.logging.Logger;
11+
12+
public final class InventoryDatabase extends JavaPlugin {
13+
private static Logger logger;
14+
private static InventoryDatabase instance;
15+
private MySQL database;
16+
private Sync sync;
17+
18+
@Override
19+
public void onEnable() {
20+
instance = this;
21+
logger = getLogger();
22+
Configuration.loadConfig();
23+
database = new MySQL();
24+
sync = new Sync();
25+
26+
//Register Listeners
27+
PluginManager pluginManager = getServer().getPluginManager();
28+
pluginManager.registerEvents(new PlayerJoin(), this);
29+
pluginManager.registerEvents(new PlayerLeave(), this);
30+
}
31+
32+
public static Logger logger() {
33+
return logger;
34+
}
35+
36+
public static InventoryDatabase getInstance() {
37+
return instance;
38+
}
39+
40+
public MySQL getDatabase() {
41+
return database;
42+
}
43+
44+
public Sync getSync() {
45+
return sync;
46+
}
47+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package de.lunarsoftware.inventorydatabase.database;
2+
3+
import de.lunarsoftware.inventorydatabase.InventoryDatabase;
4+
import org.bukkit.configuration.file.FileConfiguration;
5+
6+
import java.sql.Connection;
7+
import java.sql.DriverManager;
8+
import java.sql.PreparedStatement;
9+
import java.sql.SQLException;
10+
import java.util.Properties;
11+
12+
public class MySQL {
13+
private Connection conn = null;
14+
public static boolean databaseAvailable = false;
15+
16+
private final static String tableName = "player_inventories";
17+
18+
public MySQL() {
19+
conn = connect();
20+
21+
if (conn == null) {
22+
InventoryDatabase.logger().severe("Can't connect to database. Plugin will not work!");
23+
} else {
24+
databaseAvailable = true;
25+
initializeDatabase();
26+
}
27+
}
28+
29+
public void disconnect() {
30+
if (!databaseAvailable) return;
31+
32+
try {
33+
conn.close();
34+
} catch (SQLException e) {
35+
InventoryDatabase.logger().warning("Can't close database connection: " + e.getMessage());
36+
}
37+
38+
databaseAvailable = false;
39+
}
40+
41+
public static String getTableName() {
42+
return tableName;
43+
}
44+
45+
public Connection getConnection() {
46+
return conn;
47+
}
48+
49+
private Connection connect() {
50+
FileConfiguration config = InventoryDatabase.getInstance().getConfig();
51+
52+
String url = "jdbc:mysql://" +
53+
config.getString("database.host", "127.0.0.1") + ":" +
54+
config.getString("database.port", "3306") + "/" +
55+
config.getString("database.database", "minecraft") +
56+
"?user=" + config.getString("database.username", "minecraft") +
57+
"&password=" + config.getString("database.password", "password");
58+
59+
try {
60+
Class.forName("com.mysql.cj.jdbc.Driver");
61+
return DriverManager.getConnection(url);
62+
} catch (ClassNotFoundException e) {
63+
InventoryDatabase.logger().severe("Could not find mysql driver: " + e.getMessage());
64+
} catch (Exception e) {
65+
InventoryDatabase.logger().severe("Can't connect to database: " + e.getMessage());
66+
}
67+
68+
return null;
69+
}
70+
71+
private void initializeDatabase() {
72+
if (!databaseAvailable) return;
73+
PreparedStatement stmt = null;
74+
75+
try {
76+
String query = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
77+
"id int(10) AUTO_INCREMENT," +
78+
"player_uuid char(36) NOT NULL UNIQUE," +
79+
"player_name varchar(32) NOT NULL," +
80+
"player_last_seen char(13) NOT NULL," +
81+
"inventory LONGTEXT NOT NULL," +
82+
"armor LONGTEXT NOT NULL," +
83+
"PRIMARY KEY(id)" +
84+
") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";
85+
stmt = conn.prepareStatement(query);
86+
stmt.execute();
87+
} catch (SQLException e) {
88+
InventoryDatabase.logger().severe("Can't create table: " + e.getMessage());
89+
databaseAvailable = false;
90+
} finally {
91+
try {
92+
if (stmt != null) stmt.close();
93+
} catch (SQLException e) {
94+
InventoryDatabase.logger().warning("Can't close database connection: " + e.getMessage());
95+
}
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)