Skip to content

Commit bfac36a

Browse files
committed
Fix database creation on first startup
1 parent 16c5567 commit bfac36a

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

API/src/main/java/me/innectic/permissify/api/PermissifyAPI.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ public void initialize(HandlerType type, Optional<ConnectionInformation> connect
5858
e.printStackTrace();
5959
}
6060
if (!databaseHandler.isPresent()) throw new Exception("No data handler present.");
61-
boolean connected = databaseHandler.get().connect();
62-
if (connected) System.out.println("Connected to the database.");
63-
else System.out.println("Unable to connect to the database.");
64-
databaseHandler.ifPresent(handler -> handler.clear(new ArrayList<>()));
61+
databaseHandler.ifPresent(handler -> {
62+
handler.initialize();
63+
handler.clear(new ArrayList<>());
64+
boolean connected = handler.connect();
65+
if (connected) System.out.println("Connected to the database.");
66+
else System.out.println("Unable to connect to the database.");
67+
});
6568
}
6669

6770
public static Optional<PermissifyAPI> get() {

API/src/main/java/me/innectic/permissify/api/database/handlers/MySQLHandler.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,59 @@ private Optional<Connection> getConnection() {
6666
public void initialize() {
6767
// Make sure that the cache is empty
6868
this.cachedPermissions = new HashMap<>();
69+
// Make sure the database needed actually exists.
70+
if (!connectionInformation.isPresent()) return;
71+
72+
try {
73+
String connectionURL = "jdbc:mysql://" + connectionInformation.get().getUrl() + ":" + connectionInformation.get().getPort();
74+
Optional<Connection> connection = Optional.ofNullable(DriverManager.getConnection(connectionURL, connectionInformation.get().getUsername(), connectionInformation.get().getPassword()));
75+
if (!connection.isPresent()) return;
76+
String database = connectionInformation.get().getDatabase();
77+
// TODO: This should all be prepared statements, but that breaks for some reason
78+
PreparedStatement databaseStatement = connection.get().prepareStatement("CREATE DATABASE IF NOT EXISTS " + database);
79+
databaseStatement.execute();
80+
databaseStatement.close();
81+
82+
PreparedStatement formattingStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".formatting (`format` VARCHAR(400) NOT NULL, formatter VARCHAR(200) NOT NULL)");
83+
formattingStatement.execute();
84+
formattingStatement.close();
85+
86+
PreparedStatement groupMembersStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".groupMembers (uuid VARCHAR(767) NOT NULL, `group` VARCHAR(700) NOT NULL, `primary` TINYINT NOT NULL)");
87+
groupMembersStatement.execute();
88+
groupMembersStatement.close();
89+
90+
PreparedStatement groupPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database +".groupPermissions (groupName VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL)");
91+
groupPermissionsStatement.execute();
92+
groupPermissionsStatement.close();
93+
94+
PreparedStatement groupsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".groups (name VARCHAR(100) NOT NULL UNIQUE, prefix VARCHAR(100) NOT NULL, suffix VARCHAR(100) NOT NULL, chatcolor VARCHAR(4) NOT NULL, defaultGroup TINYINT NOT NULL)");
95+
groupsStatement.execute();
96+
groupsStatement.close();
97+
98+
PreparedStatement playerPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".playerPermissions (uuid VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL, granted TINYINT NOT NULL)");
99+
playerPermissionsStatement.execute();
100+
playerPermissionsStatement.close();
101+
102+
PreparedStatement superAdminStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".superAdmin (uuid VARCHAR(767) NOT NULL)");
103+
superAdminStatement.execute();
104+
superAdminStatement.close();
105+
106+
PreparedStatement defaultChatFormat = connection.get().prepareStatement("INSERT INTO " + database + ".formatting (`format`, formatter) VALUES (?,?)");
107+
defaultChatFormat.setString(1, "{group} {username}: {message}");
108+
defaultChatFormat.setString(2, "chat");
109+
defaultChatFormat.execute();
110+
defaultChatFormat.close();
111+
112+
PreparedStatement defaultWhisperFormat = connection.get().prepareStatement("INSERT INTO " + database + ".formatting (`format`, formatter) VALUES (?,?)");
113+
defaultWhisperFormat.setString(1, "{senderGroup} {username} > {receiverGroup} {to}: {message}");
114+
defaultWhisperFormat.setString(2, "whisper");
115+
defaultWhisperFormat.execute();
116+
defaultWhisperFormat.close();
117+
118+
connection.get().close();
119+
} catch (SQLException e) {
120+
e.printStackTrace();
121+
}
69122
}
70123

71124
@Override

Spigot/src/main/java/me/innectic/permissify/spigot/commands/permissify/CacheCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import me.innectic.permissify.api.PermissifyConstants;
2828
import me.innectic.permissify.api.database.DatabaseHandler;
29+
import me.innectic.permissify.api.permission.PermissionGroup;
2930
import me.innectic.permissify.api.util.ArgumentUtil;
3031
import me.innectic.permissify.spigot.PermissifyMain;
3132
import me.innectic.permissify.spigot.commands.CommandResponse;
@@ -51,8 +52,8 @@ public CommandResponse handleCache(CommandSender sender, String[] args) {
5152
return new CommandResponse(PermissifyConstants.UNABLE_OTHER.replace("<REASON>", "No database handler"), false);
5253
// Show information about the current cache
5354
DatabaseHandler handler = PermissifyMain.getInstance().getPermissifyAPI().getDatabaseHandler().get();
54-
String cacheInformation = String.format("Cached groups: %d, cached permissions: %d", handler.getCachedGroups().size(),
55-
handler.getCachedPermissions().size());
55+
String cacheInformation = String.format("Cached groups: %d, cached permissions: %d, default group: %s", handler.getCachedGroups().size(),
56+
handler.getCachedPermissions().size(), handler.getDefaultGroup().map(PermissionGroup::getName).orElse("NONE"));
5657
return new CommandResponse(cacheInformation, false);
5758
}
5859

Spigot/src/main/java/me/innectic/permissify/spigot/commands/permissify/FormatCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ private CommandResponse handleWhisperFormat(CommandSender sender, String[] args)
6969

7070
private CommandResponse handleDisableFormat(CommandSender sender, String[] args) {
7171
PermissifyMain.getInstance().getConfig().set("handleChat", false);
72+
PermissifyMain.getInstance().saveConfig();
7273
PermissifyMain.getInstance().setHandleChat(false);
7374
return new CommandResponse(PermissifyConstants.TOGGLED_CHAT_HANDLE.replace("<STATE>", "Disabled"), true);
7475
}
7576

7677
private CommandResponse handleEnableFormat(CommandSender sender, String[] args) {
7778
PermissifyMain.getInstance().getConfig().set("handleChat", true);
79+
PermissifyMain.getInstance().saveConfig();
7880
PermissifyMain.getInstance().setHandleChat(true);
7981
return new CommandResponse(PermissifyConstants.TOGGLED_CHAT_HANDLE.replace("<STATE>", "Enabled"), true);
8082
}

0 commit comments

Comments
 (0)