Skip to content

Commit 82cd65c

Browse files
committed
tweaking out (fix upgrades later)
Took 2 hours 28 minutes
1 parent 622697f commit 82cd65c

12 files changed

Lines changed: 542 additions & 172 deletions

File tree

src/main/java/dev/oribuin/fishing/command/impl/GiveCommand.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,9 @@ public void giveTotem(CommandSender sender, Player target, Integer amount) {
110110
if (amount == null || amount < 0) amount = 1;
111111

112112
Totem totem = new Totem(null, null, target.getUniqueId());
113-
ItemStack itemStack = TotemConfig.get().getTotemItem().create(totem.getPlaceholders());
113+
ItemStack itemStack = TotemConfig.get().getTotemItem().create(totem.getPlaceholders()).clone();
114114
totem.saveTo(itemStack);
115115

116-
if (itemStack == null) {
117-
sender.sendMessage("An error occurred while creating the fish item."); // TODO: Plugin Message
118-
return;
119-
}
120-
121116
if (target.getInventory().firstEmpty() == -1) {
122117
PluginMessages.get().getFullInventory().send(sender); // TODO: Drop items on the ground
123118
return;

src/main/java/dev/oribuin/fishing/config/impl/PluginMessages.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static class TotemMessages {
7474
private TextMessage noSpace = new TextMessage(PREFIX + "There is not enough space available for the fishing totem");
7575

7676
@Comment("Player has activated the totem")
77-
private TextMessage activated = new TextMessage(PREFIX + "You have activated this fishing totem, It will last for <#93bc80><time> <white>minutes");
77+
private TextMessage activated = new TextMessage(PREFIX + "Your fishing totem will be active for <#93bc80><time>");
7878

7979
@Comment("Totem is currently on cooldown")
8080
private TextMessage onCooldown = new TextMessage(PREFIX + "You cannot activate this totem as it is on cooldown");
@@ -83,7 +83,7 @@ public static class TotemMessages {
8383
private TextMessage alreadyActive = new TextMessage(PREFIX + "This totem is already active");
8484

8585
@Comment("Player has levelled up an upgrade on the totem")
86-
private TextMessage upgradeLevelUp = new TextMessage(PREFIX + "You have increased the level of <#93bc80><upgrade> <white>to <#93bc80><level><white>/<#93bc8i0<max>");
86+
private TextMessage upgradeLevelUp = new TextMessage(PREFIX + "You have increased the level of <#93bc80><upgrade> <white>to <#93bc80><level><white>/<#93bc80><max>");
8787

8888
@Comment("Player cannot access the totems menu")
8989
private TextMessage cannotAccess = new TextMessage(PREFIX + "You cannot interact with this totem as it belongs to someone else.");
@@ -93,7 +93,9 @@ public static class TotemMessages {
9393

9494
@Comment("Other player activated totem")
9595
private TextMessage otherPlayerActivated = new TextMessage(PREFIX + "The player <#93bc80><activator> <white>has activated your totem");
96-
96+
97+
@Comment("Another totem is active in the nearby area")
98+
private TextMessage otherActiveNearby = new TextMessage(PREFIX + "There is already a totem active within this totem's range which may conflict, Activate this totem again to confirm");
9799

98100
public TextMessage getPlaced() {
99101
return placed;
@@ -134,6 +136,10 @@ public TextMessage getCannotActivate() {
134136
public TextMessage getOtherPlayerActivated() {
135137
return otherPlayerActivated;
136138
}
139+
140+
public TextMessage getOtherActiveNearby() {
141+
return otherActiveNearby;
142+
}
137143
}
138144

139145
public TotemMessages getTotem() {

src/main/java/dev/oribuin/fishing/gui/impl/totem/TotemMainMenu.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public TotemMainMenu(FishingPlugin plugin, Supplier<Totem> totemSupplier) {
3333
this.totemSupplier = totemSupplier;
3434

3535
Totem totem = this.totemSupplier.get();
36+
System.out.println("MAIN MENU - TOTEM DISPLAY ID: " + totem.getDisplayId());
3637
Fisher fisher = plugin.getDataManager().get(totem.getOwner());
3738
Placeholders placeholders = Placeholders.builder()
3839
.addAll(fisher.getPlaceholders())
@@ -127,7 +128,6 @@ public void tick() {
127128
}
128129

129130
totem.activate(activator); // Activate the totem // TODO: Play totem activate animation
130-
PluginMessages.get().getTotem().getActivated().send(activator); // Tell the player they activated the totem
131131
activator.closeInventory(InventoryCloseEvent.Reason.PLUGIN); // Close the player's inventory
132132
});
133133
}

src/main/java/dev/oribuin/fishing/gui/impl/totem/TotemUpgradeMenu.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public TotemUpgradeMenu(FishingPlugin plugin, Supplier<Totem> totemSupplier) {
3333
this.totemSupplier = totemSupplier;
3434

3535
Totem totem = this.totemSupplier.get();
36+
System.out.println("UPGRADE MENU - TOTEM DISPLAY ID: " + totem.getDisplayId());
3637
Fisher fisher = plugin.getDataManager().get(totem.getOwner());
3738
Placeholders placeholders = Placeholders.builder()
3839
.addAll(fisher.getPlaceholders())
@@ -43,7 +44,7 @@ public TotemUpgradeMenu(FishingPlugin plugin, Supplier<Totem> totemSupplier) {
4344
this.config.getPreviousPage().place(this.gui, placeholders, event -> gui.previous());
4445
this.config.getNextPage().place(this.gui, placeholders, event -> gui.next());
4546
this.config.getTotemMainMenu().place(this.gui, placeholders, event -> {
46-
TotemMainMenu mainMenu = new TotemMainMenu(plugin, totemSupplier);
47+
TotemMainMenu mainMenu = new TotemMainMenu(plugin, this.totemSupplier);
4748
mainMenu.open((Player) event.getWhoClicked());
4849
});
4950

src/main/java/dev/oribuin/fishing/listener/TotemListeners.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import dev.oribuin.fishing.config.impl.PluginMessages;
55
import dev.oribuin.fishing.config.impl.TotemConfig;
66
import dev.oribuin.fishing.gui.impl.totem.TotemMainMenu;
7-
import dev.oribuin.fishing.manager.MenuManager;
87
import dev.oribuin.fishing.manager.TotemManager;
98
import dev.oribuin.fishing.model.totem.Totem;
109
import dev.oribuin.fishing.storage.util.KeyRegistry;
@@ -24,7 +23,6 @@
2423
import org.bukkit.event.world.ChunkUnloadEvent;
2524
import org.bukkit.inventory.ItemStack;
2625
import org.bukkit.inventory.meta.ItemMeta;
27-
import org.checkerframework.checker.i18nformatter.qual.I18nUnknownFormat;
2826

2927
import java.util.ArrayList;
3028
import java.util.Arrays;
@@ -86,14 +84,14 @@ public void onInteract(PlayerInteractAtEntityEvent event) {
8684
if (!(event.getRightClicked() instanceof ArmorStand stand)) return;
8785

8886
TotemManager manager = this.plugin.getTotemManager();
89-
Totem totem = manager.getTotem(stand);
87+
Totem totem = manager.getAndLoadTotem(stand);
9088
Player player = event.getPlayer();
9189
if (totem == null) return;
9290

9391
event.setCancelled(true);
9492

9593
if (!player.isSneaking()) {
96-
new TotemMainMenu(this.plugin, () -> manager.getTotem(stand)).open(player);
94+
new TotemMainMenu(this.plugin, () -> manager.getTotem(stand.getUniqueId())).open(player);
9795
return;
9896
}
9997

@@ -108,53 +106,36 @@ public void onInteract(PlayerInteractAtEntityEvent event) {
108106
PluginMessages.get().getFullInventory().send(player);
109107
return;
110108
}
111-
109+
112110
ItemStack itemStack = TotemConfig.get().getTotemItem().create(totem.getPlaceholders());
113-
if (itemStack == null) return;
114111

115-
totem.getDisplay().remove(); // Remove the totem entity
116-
totem.setDisplay(null); // Set the totem entity to null
117112
manager.unregisterTotem(totem); // Unregister the totem
113+
totem.getDisplay().remove(); // Remove the totem entity
114+
totem.setDisplayId(null); // Set the totem entity to null
118115
PluginMessages.get().getTotem().getRemoved().send(player);
119-
120116

121117
totem.saveTo(itemStack);
122118
player.getInventory().addItem(itemStack);
123119
}
124120

125-
/**
126-
* Load all the totems in the chunk. This will register all the totems in the chunk.
127-
*
128-
* @param event The event to listen to.
129-
*/
130-
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
131-
public void onChunkLoad(PlayerChunkLoadEvent event) {
132-
TotemManager totemManager = this.plugin.getTotemManager();
133-
CompletableFuture.runAsync(() -> Arrays.stream(event.getChunk().getEntities()).forEach(entity -> {
134-
if (!(entity instanceof ArmorStand stand)) return;
135-
136-
totemManager.getTotem(stand); // Loads & Registers Totem
137-
}));
138-
}
139-
140-
/**
141-
* Unload all the totems in the chunk. This will unregister all the totems in the chunk.
142-
*
143-
* @param event The event to listen to.
144-
*/
145-
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
146-
public void onChunkUnload(ChunkUnloadEvent event) {
147-
TotemManager totemManager = this.plugin.getTotemManager();
148-
new ArrayList<>(totemManager.getTotems().values()).forEach(totem -> {
149-
if (!totem.getPosition().getWorld().getName().equalsIgnoreCase(event.getWorld().getName())) return;
150-
151-
int chunkX = totem.getPosition().getBlockX() >> 4;
152-
int chunkZ = totem.getPosition().getBlockZ() >> 4;
153-
154-
if (chunkX != event.getChunk().getX() || chunkZ != event.getChunk().getZ()) return;
155-
156-
totemManager.unregisterTotem(totem);
157-
});
158-
}
121+
// /**
122+
// * Unload all the totems in the chunk. This will unregister all the totems in the chunk.
123+
// *
124+
// * @param event The event to listen to.
125+
// */
126+
// @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
127+
// public void onChunkUnload(ChunkUnloadEvent event) {
128+
// TotemManager totemManager = this.plugin.getTotemManager();
129+
// new ArrayList<>(totemManager.getTotems().values()).forEach(totem -> {
130+
// if (!totem.getPosition().getWorld().getName().equalsIgnoreCase(event.getWorld().getName())) return;
131+
//
132+
// int chunkX = totem.getPosition().getBlockX() >> 4;
133+
// int chunkZ = totem.getPosition().getBlockZ() >> 4;
134+
//
135+
// if (chunkX != event.getChunk().getX() || chunkZ != event.getChunk().getZ()) return;
136+
//
137+
// totemManager.unregisterTotem(totem);
138+
// });
139+
// }
159140

160141
}

src/main/java/dev/oribuin/fishing/manager/DataManager.java

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66
import dev.oribuin.fishing.database.connector.DatabaseConnector;
77
import dev.oribuin.fishing.database.connector.MySQLConnector;
88
import dev.oribuin.fishing.database.connector.SQLiteConnector;
9+
import dev.oribuin.fishing.model.totem.Totem;
910
import dev.oribuin.fishing.scheduler.PluginScheduler;
1011
import dev.oribuin.fishing.storage.Fisher;
1112
import org.bukkit.Bukkit;
13+
import org.bukkit.entity.ArmorStand;
14+
import org.bukkit.entity.Entity;
1215
import org.bukkit.entity.Player;
1316

1417
import java.sql.Connection;
1518
import java.sql.PreparedStatement;
1619
import java.sql.ResultSet;
1720
import java.sql.SQLException;
21+
import java.sql.Statement;
1822
import java.util.Collection;
1923
import java.util.HashMap;
2024
import java.util.Map;
2125
import java.util.UUID;
26+
import java.util.concurrent.CompletableFuture;
2227

2328
public class DataManager implements Manager {
2429

2530
private static final String USERS_PREFIX = "fishingplugin_users";
31+
private static final String TOTEMS_PREFIX = "fishingplugin_totems";
2632
private static final Gson GSON = new Gson();
2733

2834
private final FishingPlugin plugin;
@@ -62,8 +68,10 @@ public void reload(FishingPlugin plugin) {
6268

6369
// Create the initial table for the plugin
6470
this.connector.connect(connection -> {
65-
try (PreparedStatement statement = connection.prepareStatement(CREATE_TABLE)) {
66-
statement.executeUpdate();
71+
try (Statement statement = connection.createStatement()) {
72+
statement.addBatch(CREATE_TABLE_USERS);
73+
statement.addBatch(CREATE_TABLE_TOTEMS);
74+
statement.executeBatch();
6775
}
6876
});
6977

@@ -73,6 +81,9 @@ public void reload(FishingPlugin plugin) {
7381
// Load all the users who are currently online
7482
Collection<UUID> uuids = Bukkit.getOnlinePlayers().stream().map(Player::getUniqueId).toList();
7583
this.loadBatch(uuids);
84+
85+
// Load all the totems in the plugin
86+
7687
}
7788

7889
/**
@@ -216,6 +227,71 @@ private void loadUser(UUID uuid, Connection connection) throws SQLException {
216227
}
217228
}
218229

230+
/**
231+
* Save a totem into the database
232+
*
233+
* @param totem The totem to save
234+
*/
235+
public void saveTotem(Totem totem) {
236+
ArmorStand display = totem.getDisplay();
237+
if (display == null) return;
238+
239+
this.async(() -> this.connector.connect(connection -> {
240+
try (PreparedStatement statement = connection.prepareStatement(SAVE_TOTEM)) {
241+
statement.setString(1, display.getUniqueId().toString());
242+
statement.setString(2, display.getWorld().getName());
243+
statement.executeUpdate();
244+
}
245+
}));
246+
}
247+
248+
/**
249+
* Remove a totem from the database
250+
*
251+
* @param totem The totem to remove
252+
*/
253+
public void removeTotem(UUID totem) {
254+
this.async(() -> this.connector.connect(connection -> {
255+
try (PreparedStatement statement = connection.prepareStatement(REMOVE_TOTEM)) {
256+
statement.setString(1, totem.toString());
257+
statement.executeUpdate();
258+
}
259+
}));
260+
}
261+
262+
/**
263+
* Load all the totems that are stored in the database
264+
*
265+
* @return The totem stored
266+
*/
267+
public CompletableFuture<Map<UUID, Totem>> loadTotems() {
268+
return CompletableFuture.supplyAsync(() -> {
269+
Map<UUID, Totem> results = new HashMap<>();
270+
try (
271+
Connection connection = this.connector.connect();
272+
PreparedStatement statement = connection.prepareStatement(SELECT_TOTEMS)
273+
) {
274+
ResultSet result = statement.executeQuery();
275+
while (result.next()) {
276+
String world = result.getString("world");
277+
String entityId = result.getString("entityId");
278+
if (entityId == null || world == null) continue;
279+
280+
UUID entityUUID = UUID.fromString(entityId);
281+
Entity entity = Bukkit.getEntity(entityUUID);
282+
if (entity == null || entity.isDead() || !(entity instanceof ArmorStand stand)) continue;
283+
if (!entity.getWorld().getName().equals(world)) continue;
284+
285+
results.put(entityUUID, new Totem(stand));
286+
}
287+
} catch (SQLException e) {
288+
this.plugin.getLogger().severe("An error occurred loading totems: " + e.getMessage());
289+
}
290+
291+
return results;
292+
});
293+
}
294+
219295
/**
220296
* Get all the current players from the cache
221297
*
@@ -238,17 +314,24 @@ private record PlayerSkills(Map<String, Integer> skills) {
238314
}
239315

240316
// SQL Queries
241-
private final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS `fishingplugin_users` (" +
242-
"`uuid` VARCHAR(36) NOT NULL PRIMARY KEY," +
243-
"`entropy` INT NOT NULL," +
244-
"`level` INT NOT NULL," +
245-
"`experience` INT NOT NULL," +
246-
"`skill_points` INT NOT NULL," +
247-
"`skills` TEXT NOT NULL" +
248-
");";
317+
private final String CREATE_TABLE_USERS = "CREATE TABLE IF NOT EXISTS `fishingplugin_users` (" +
318+
"`uuid` VARCHAR(36) NOT NULL PRIMARY KEY," +
319+
"`entropy` INT NOT NULL," +
320+
"`level` INT NOT NULL," +
321+
"`experience` INT NOT NULL," +
322+
"`skill_points` INT NOT NULL," +
323+
"`skills` TEXT NOT NULL" +
324+
")";
249325
private final String SAVE_USER = "REPLACE INTO `fishingplugin_users` " +
250326
"(`uuid`, `entropy`, `level`, `experience`, `skill_points`, `skills`) " +
251327
"VALUES(?, ?, ?, ?, ?, ?)";
252328

329+
private final String CREATE_TABLE_TOTEMS = "CREATE TABLE IF NOT EXISTS `fishingplugin_totems`(" +
330+
"`entityId` VARCHAR(36) NOT NULL PRIMARY KEY, " +
331+
"`world` VARCHAR(100) NOT NULL" +
332+
")";
253333

334+
private final String SELECT_TOTEMS = "SELECT * FROM `fishingplugin_totems`";
335+
private final String SAVE_TOTEM = "REPLACE INTO `fishingplugin_totems` (`entityId`, `world`) VALUES (?, ?)";
336+
private final String REMOVE_TOTEM = "DELETE FROM `fishingplugin_totems` WHERE `entityId` = ?";
254337
}

0 commit comments

Comments
 (0)