A claim plugin based on FTB Chunks
1package dev.keii.keiichunks;
2
3import net.kyori.adventure.text.Component;
4import net.kyori.adventure.text.format.NamedTextColor;
5import org.bukkit.Bukkit;
6
7import java.sql.Connection;
8import java.sql.DriverManager;
9import java.sql.SQLException;
10import java.sql.Statement;
11
12public class DatabaseConnector {
13 public static String createDatabaseSqlSqlLite = "PRAGMA foreign_keys = ON;\n" +
14 "CREATE TABLE IF NOT EXISTS `claim` (\n" +
15 " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
16 " `user_id` INTEGER NOT NULL,\n" +
17 " `chunk_x` INTEGER NOT NULL,\n" +
18 " `chunk_z` INTEGER NOT NULL,\n" +
19 " `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
20 " `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
21 " `allow_explosions` INTEGER NOT NULL DEFAULT 0\n" +
22 ");\n" +
23 "CREATE TABLE IF NOT EXISTS `claim_permission` (\n" +
24 " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
25 " `user_id` INTEGER,\n" +
26 " `claim_id` INTEGER NOT NULL,\n" +
27 " `block_break` INTEGER NOT NULL DEFAULT 0,\n" +
28 " `block_place` INTEGER NOT NULL DEFAULT 0,\n" +
29 " `bucket_empty` INTEGER NOT NULL DEFAULT 0,\n" +
30 " `bucket_fill` INTEGER NOT NULL DEFAULT 0,\n" +
31 " `interact` INTEGER NOT NULL DEFAULT 0,\n" +
32 " `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
33 " `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
34 " FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),\n" +
35 " FOREIGN KEY (`claim_id`) REFERENCES `claim`(`id`)\n" +
36 ");\n" +
37 "CREATE TABLE IF NOT EXISTS `user` (\n" +
38 " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
39 " `uuid` TEXT NOT NULL,\n" +
40 " `timestamp` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
41 " `claim_power` INTEGER NOT NULL DEFAULT 10\n" +
42 ");\n" +
43 "COMMIT;";
44
45 public static String createDatabaseSqlMySQL =
46 "START TRANSACTION;\n" +
47 "CREATE TABLE IF NOT EXISTS `claim` (\n" +
48 " `id` bigint(20) UNSIGNED NOT NULL,\n" +
49 " `user_id` bigint(20) UNSIGNED NOT NULL,\n" +
50 " `chunk_x` smallint(6) NOT NULL,\n" +
51 " `chunk_z` smallint(6) NOT NULL,\n" +
52 " `created_at` timestamp NOT NULL DEFAULT current_timestamp(),\n" +
53 " `updated_at` timestamp NOT NULL DEFAULT current_timestamp(),\n" +
54 " `allow_explosions` tinyint(1) NOT NULL DEFAULT 0\n" +
55 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n" +
56 "CREATE TABLE IF NOT EXISTS `claim_permission` (\n" +
57 " `id` bigint(20) UNSIGNED NOT NULL,\n" +
58 " `user_id` bigint(20) UNSIGNED DEFAULT NULL,\n" +
59 " `claim_id` bigint(20) UNSIGNED NOT NULL,\n" +
60 " `block_break` tinyint(1) NOT NULL DEFAULT 0,\n" +
61 " `block_place` tinyint(1) NOT NULL DEFAULT 0,\n" +
62 " `bucket_empty` tinyint(1) NOT NULL DEFAULT 0,\n" +
63 " `bucket_fill` tinyint(1) NOT NULL DEFAULT 0,\n" +
64 " `interact` tinyint(1) NOT NULL DEFAULT 0,\n" +
65 " `created_at` timestamp NOT NULL DEFAULT current_timestamp(),\n" +
66 " `updated_at` timestamp NOT NULL DEFAULT current_timestamp()\n" +
67 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n" +
68 "CREATE TABLE IF NOT EXISTS `user` (\n" +
69 " `id` bigint(20) UNSIGNED NOT NULL,\n" +
70 " `nickname` TEXT NOT NULL,\n" +
71 " `uuid` varchar(36) NOT NULL,\n" +
72 " `timestamp` timestamp NOT NULL DEFAULT current_timestamp(),\n" +
73 " `claim_power` int(10) UNSIGNED NOT NULL DEFAULT 10\n" +
74 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n" +
75
76 "ALTER TABLE `claim`\n" +
77 " ADD PRIMARY KEY (`id`),\n" +
78 " ADD KEY `claim_user_id_foreign` (`user_id`);\n" +
79 "ALTER TABLE `claim_permission`\n" +
80 " ADD PRIMARY KEY (`id`),\n" +
81 " ADD KEY `claim_permission_user_id_foreign` (`user_id`),\n" +
82 " ADD KEY `claim_permission_claim_id_foreign` (`claim_id`);\n" +
83 "ALTER TABLE `user`\n" +
84 " ADD PRIMARY KEY (`id`);\n" +
85 "ALTER TABLE `claim`\n" +
86 " MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=70;\n" +
87 "ALTER TABLE `claim_permission`\n" +
88 " MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=51;\n" +
89 "ALTER TABLE `user`\n" +
90 " MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;\n" +
91 "ALTER TABLE `claim`\n" +
92 " ADD CONSTRAINT `claim_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);\n" +
93 "ALTER TABLE `claim_permission`\n" +
94 " ADD CONSTRAINT `claim_permission_claim_id_foreign` FOREIGN KEY (`claim_id`) REFERENCES `claim` (`id`),\n" +
95 " ADD CONSTRAINT `claim_permission_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);\n" +
96 "COMMIT;";
97
98// public static boolean InitializeDatabase()
99// {
100// Connection connection = getConnection();
101//
102// try {
103// Statement statement = connection.createStatement();
104//
105// System.out.println("Database initialization succeeded!");
106//
107// statement.execute(createDatabaseSqlMySQL);
108//
109// return true;
110// } catch (SQLException e) {
111// Bukkit.getServer().sendMessage(Component.text("Fatal Database Error: " + e.getMessage()).color(NamedTextColor.RED));
112// return false;
113// }
114// };
115
116// public static Connection getConnection() {
117// try {
118// Class.forName("org.sqlite.JDBC");
119//
120// final String url = "jdbc:sqlite:./plugins/KeiiChunks/database.db";
121//
122// return DriverManager.getConnection(url);
123// } catch(Exception e)
124// {
125// Bukkit.getServer().sendMessage(Component.text("Fatal Database Error: " + e.getMessage()).color(NamedTextColor.RED));
126//
127// return null;
128// }
129// }
130
131 static String DB_URL = null;
132 static String DB_NAME = null;
133 static String DB_USER = null;
134 static String DB_PASSWORD = null;
135
136 public static boolean InitializeDatabase()
137 {
138 Connection connection = getConnection();
139
140 try {
141 Statement statement = connection.createStatement();
142
143 Bukkit.getServer().sendMessage(Component.text("Database initialization succeeded").color(NamedTextColor.GREEN));
144
145 statement.execute(createDatabaseSqlMySQL);
146
147 return true;
148 } catch (SQLException e) {
149 Bukkit.getServer().sendMessage(Component.text("Fatal Database Error: " + e.getMessage()).color(NamedTextColor.RED));
150 return false;
151 }
152 };
153
154 public static Connection getConnection() {
155 try {
156 final String url = DB_URL + DB_NAME;
157
158 return DriverManager.getConnection(url, DB_USER, DB_PASSWORD);
159 } catch (SQLException e) {
160 Bukkit.getServer().sendMessage(Component.text("Fatal Database Error: " + e.getMessage()).color(NamedTextColor.RED));
161 return null;
162 }
163 }
164}