Skip to content

Commit 5fb4c11

Browse files
authored
Use UUID to get POS world and Server instead of Bukkit static methods (#94)
Use UUID to get POS world and `Server` instead of `Bukkit` static methods
2 parents 7ba6539 + 3a771ad commit 5fb4c11

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.gson.JsonParser;
55
import com.zaxxer.hikari.HikariConfig;
66
import com.zaxxer.hikari.HikariDataSource;
7-
import org.bukkit.Bukkit;
87
import org.bukkit.NamespacedKey;
98
import org.bukkit.OfflinePlayer;
109
import org.bukkit.command.CommandExecutor;
@@ -90,7 +89,7 @@ public void onEnable() {
9089
for (final @NotNull Listener event : events) getServer().getPluginManager().registerEvents(event, this);
9190

9291
// Setup PlaceholderAPI Integration
93-
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
92+
if(BankAccounts.getInstance().getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
9493
new PAPIIntegration().register();
9594
} else {
9695
getLogger().log(Level.INFO, "PlaceholderAPI not found. Skipping integration.");

src/main/java/pro/cloudnode/smp/bankaccounts/POS.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package pro.cloudnode.smp.bankaccounts;
22

3-
import org.bukkit.Bukkit;
43
import org.bukkit.Location;
54
import org.bukkit.World;
65
import org.bukkit.block.Block;
@@ -29,6 +28,7 @@
2928
import java.util.Date;
3029
import java.util.Objects;
3130
import java.util.Optional;
31+
import java.util.UUID;
3232
import java.util.logging.Level;
3333
import java.util.stream.IntStream;
3434
import java.util.zip.CRC32;
@@ -96,7 +96,7 @@ public POS(final @NotNull ResultSet rs) throws @NotNull SQLException, @NotNull I
9696
this.x = rs.getInt("x");
9797
this.y = rs.getInt("y");
9898
this.z = rs.getInt("z");
99-
final @NotNull Optional<@NotNull World> world = Optional.ofNullable(BankAccounts.getInstance().getServer().getWorld(rs.getString("world")));
99+
final @NotNull Optional<@NotNull World> world = Optional.ofNullable(BankAccounts.getInstance().getServer().getWorld(UUID.fromString(rs.getString("world"))));
100100
if (world.isEmpty()) throw new IllegalStateException("World not found: " + rs.getString("world"));
101101
this.world = world.get();
102102
this.price = rs.getBigDecimal("price");
@@ -123,7 +123,7 @@ public POS(final @NotNull ResultSet rs) throws @NotNull SQLException, @NotNull I
123123
* Create POS id
124124
*/
125125
public @NotNull String id() {
126-
return world.getName() + ":" + x + ":" + y + ":" + z;
126+
return world.getUID() + ":" + x + ":" + y + ":" + z;
127127
}
128128

129129
/**
@@ -135,7 +135,7 @@ public void save() {
135135
stmt.setInt(1, x);
136136
stmt.setInt(2, y);
137137
stmt.setInt(3, z);
138-
stmt.setString(4, world.getName());
138+
stmt.setString(4, world.getUID().toString());
139139
stmt.setBigDecimal(5, price);
140140
if (description == null) stmt.setNull(6, Types.VARCHAR);
141141
else stmt.setString(6, description);
@@ -157,7 +157,7 @@ public void delete() {
157157
stmt.setInt(1, x);
158158
stmt.setInt(2, y);
159159
stmt.setInt(3, z);
160-
stmt.setString(4, world.getName());
160+
stmt.setString(4, world.getUID().toString());
161161
stmt.executeUpdate();
162162
} catch (final @NotNull SQLException e) {
163163
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not delete POS in " + world.getName() + " at X: " + x + " Y: " + y + " Z: " + z, e);
@@ -178,7 +178,7 @@ public void delete() {
178178
stmt.setInt(1, x);
179179
stmt.setInt(2, y);
180180
stmt.setInt(3, z);
181-
stmt.setString(4, world.getName());
181+
stmt.setString(4, world.getUID().toString());
182182
final @NotNull ResultSet rs = stmt.executeQuery();
183183
return rs.next() ? Optional.of(new POS(rs)) : Optional.empty();
184184
} catch (final @NotNull SQLException e) {
@@ -236,7 +236,7 @@ public void delete() {
236236
public static @NotNull Optional<@NotNull POS> get(final @NotNull String id) {
237237
final @NotNull String @NotNull [] split = id.split(":");
238238
if (split.length != 4) return Optional.empty();
239-
final @NotNull Optional<@NotNull World> world = Optional.ofNullable(Bukkit.getWorld(split[0]));
239+
final @NotNull Optional<@NotNull World> world = Optional.ofNullable(BankAccounts.getInstance().getServer().getWorld(UUID.fromString(split[0])));
240240
if (world.isEmpty()) return Optional.empty();
241241
try {
242242
final int x = Integer.parseInt(split[1]);
@@ -263,7 +263,7 @@ public static void openOwnerGui(final @NotNull Player player, final @NotNull Che
263263
final @NotNull ItemStack @NotNull [] items = Arrays.stream(chest.getInventory().getStorageContents()).filter(Objects::nonNull).toArray(ItemStack[]::new);
264264
final int extraRows = 1;
265265
final int size = extraRows * 9 + items.length + 9 - items.length % 9;
266-
final @NotNull Inventory gui = Bukkit.createInventory(null, size, BankAccounts.getInstance().config().posTitle(pos));
266+
final @NotNull Inventory gui = BankAccounts.getInstance().getServer().createInventory(null, size, BankAccounts.getInstance().config().posTitle(pos));
267267
gui.addItem(items);
268268

269269
final @NotNull ItemStack overview = new ItemStack(BankAccounts.getInstance().config().posInfoMaterial(), 1);
@@ -308,7 +308,7 @@ public static void openBuyGui(final @NotNull Player player, final @NotNull Chest
308308
final @NotNull ItemStack @NotNull [] items = Arrays.stream(chest.getInventory().getStorageContents()).filter(Objects::nonNull).toArray(ItemStack[]::new);
309309
final int extraRows = 1;
310310
final int size = extraRows * 9 + items.length + 9 - items.length % 9;
311-
final @NotNull Inventory gui = Bukkit.createInventory(null, size, BankAccounts.getInstance().config().posTitle(pos));
311+
final @NotNull Inventory gui = BankAccounts.getInstance().getServer().createInventory(null, size, BankAccounts.getInstance().config().posTitle(pos));
312312
gui.addItem(items);
313313

314314
final @NotNull ItemStack overview = new ItemStack(BankAccounts.getInstance().config().posInfoMaterial(), 1);

src/main/java/pro/cloudnode/smp/bankaccounts/events/BlockBreak.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package pro.cloudnode.smp.bankaccounts.events;
22

3-
import org.bukkit.Bukkit;
43
import org.bukkit.block.Block;
54
import org.bukkit.block.Chest;
65
import org.bukkit.event.EventHandler;
@@ -20,7 +19,7 @@ public void onBlockBreak(final @NotNull BlockBreakEvent event) {
2019
if (block.getState() instanceof final @NotNull Chest chest) {
2120
final @NotNull Inventory inventory = chest.getInventory();
2221
if (!inventory.isEmpty()) {
23-
Bukkit.getScheduler().runTaskAsynchronously(BankAccounts.getInstance(), () -> {
22+
BankAccounts.getInstance().getServer().getScheduler().runTaskAsynchronously(BankAccounts.getInstance(), () -> {
2423
final @NotNull Optional<POS> pos = POS.get(chest);
2524
if (pos.isPresent()) {
2625
pos.get().delete();

src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package pro.cloudnode.smp.bankaccounts.events;
22

3-
import org.bukkit.Bukkit;
43
import org.bukkit.entity.Player;
54
import org.bukkit.event.EventHandler;
65
import org.bukkit.event.Listener;
@@ -19,7 +18,7 @@ public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
1918
final Player player = event.getPlayer();
2019
final @NotNull Optional<@NotNull Double> startingBalance = BankAccounts.getInstance().config()
2120
.startingBalance();
22-
startingBalance.ifPresent(aDouble -> Bukkit.getScheduler()
21+
startingBalance.ifPresent(aDouble -> BankAccounts.getInstance().getServer().getScheduler()
2322
.runTaskAsynchronously(BankAccounts.getInstance(), () -> {
2423
final @NotNull Account[] accounts = Account.get(player, Account.Type.PERSONAL);
2524
if (accounts.length == 0) {

src/main/resources/db-init/mysql.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ CREATE TABLE IF NOT EXISTS `bank_invoices`
4747
`created` DATETIME NOT NULL DEFAULT UTC_TIMESTAMP(),
4848
`transaction` INT DEFAULT NULL
4949
);
50+
51+
DELETE
52+
from `pos`
53+
WHERE `world` NOT LIKE '%-%-%-%-%';
54+
55+
ALTER TABLE `pos`
56+
CHANGE COLUMN `world` `world` CHAR(36) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL;

src/main/resources/db-init/sql.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,31 @@ CREATE TABLE IF NOT EXISTS `bank_invoices`
6464
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
6565
`transaction` INTEGER DEFAULT NULL
6666
);
67+
68+
-- Modify `pos`.`world` to be `CHAR(36)` (UUID)
69+
DELETE
70+
from `pos`
71+
WHERE `world` NOT LIKE '%-%-%-%-%';
72+
73+
CREATE TABLE `new_pos`
74+
(
75+
`x` INTEGER NOT NULL,
76+
`y` INTEGER NOT NULL,
77+
`z` INTEGER NOT NULL,
78+
`world` CHAR(36) NOT NULL COLLATE NOCASE,
79+
`price` NUMERIC NOT NULL,
80+
`description` TEXT DEFAULT NULL,
81+
`seller` TEXT NOT NULL,
82+
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
83+
PRIMARY KEY (`x`, `y`, `z`, `world`)
84+
);
85+
86+
INSERT INTO `new_pos`
87+
SELECT *
88+
FROM `pos`;
89+
90+
DROP TABLE `pos`;
91+
92+
ALTER TABLE `new_pos`
93+
RENAME TO `pos`;
94+
-- END OF `pos` MODIFICATION

0 commit comments

Comments
 (0)