Skip to content

Commit 3f666aa

Browse files
Fix Effects Extender DB features
1 parent fe86c58 commit 3f666aa

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/main/java/parallelmc/parallelutils/modules/effectextender/EffectExtender.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import parallelmc.parallelutils.Parallelutils;
99
import parallelmc.parallelutils.modules.effectextender.commands.ParallelEffectsCommand;
1010
import parallelmc.parallelutils.modules.effectextender.listeners.EffectListener;
11+
import parallelmc.parallelutils.modules.effectextender.listeners.JoinLeaveListener;
1112

1213
import java.sql.*;
13-
import java.util.UUID;
1414
import java.util.logging.Level;
1515

1616
/**
@@ -19,7 +19,7 @@
1919
public class EffectExtender implements ParallelModule {
2020

2121
// dont worry about it
22-
public static Connection dbConn;
22+
private Connection dbConn;
2323

2424
@Override
2525
public void onEnable() {
@@ -33,12 +33,6 @@ public void onEnable() {
3333

3434
Parallelutils puPlugin = (Parallelutils) plugin;
3535

36-
manager.registerEvents(new EffectListener(), plugin);
37-
38-
puPlugin.addCommand("effects", new ParallelEffectsCommand());
39-
40-
Parallelutils.log(Parallelutils.LOG_LEVEL, "EntityPotionEffectEvent registered successfully.");
41-
4236
dbConn = puPlugin.getDbConn();
4337

4438
// create effects table if it doesn't exist
@@ -58,6 +52,12 @@ EffectType varchar(20) not null,
5852
e.printStackTrace();
5953
}
6054

55+
manager.registerEvents(new EffectListener(), plugin);
56+
manager.registerEvents(new JoinLeaveListener(dbConn), plugin);
57+
58+
puPlugin.addCommand("effects", new ParallelEffectsCommand());
59+
60+
Parallelutils.log(Parallelutils.LOG_LEVEL, "EntityPotionEffectEvent registered successfully.");
6161
}
6262

6363
@Override
@@ -83,6 +83,9 @@ public void onDisable() {
8383
});
8484

8585
statement.executeBatch();
86+
87+
dbConn.commit();
88+
8689
statement.close();
8790
} catch (SQLException e) {
8891
e.printStackTrace();

src/main/java/parallelmc/parallelutils/modules/effectextender/listeners/EffectListener.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
public class EffectListener implements Listener {
2222

23-
// TODO: Make this save across logging off or on and reboots
2423
public static HashMap<LivingEntity, HashMap<PotionEffectType, Integer>> playerEffects = new HashMap<>();
2524

2625
// could put these in a hashmap but eh

src/main/java/parallelmc/parallelutils/modules/effectextender/listeners/JoinLeaveListener.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
package parallelmc.parallelutils.modules.effectextender.listeners;
22

3-
import org.bukkit.entity.EvokerFangs;
43
import org.bukkit.entity.LivingEntity;
54
import org.bukkit.event.EventHandler;
65
import org.bukkit.event.Listener;
76
import org.bukkit.event.player.PlayerJoinEvent;
87
import org.bukkit.event.player.PlayerQuitEvent;
98
import org.bukkit.potion.PotionEffectType;
10-
import parallelmc.parallelutils.modules.effectextender.EffectExtender;
119

1210
import java.sql.*;
1311
import java.util.HashMap;
1412

1513
public class JoinLeaveListener implements Listener {
1614

15+
private final Connection dbConn;
16+
17+
public JoinLeaveListener(Connection dbConn) {
18+
this.dbConn = dbConn;
19+
}
20+
1721
@EventHandler
1822
public void onPlayerJoin(PlayerJoinEvent event) {
1923
HashMap<PotionEffectType, Integer> effects = new HashMap<>();
2024
LivingEntity player = event.getPlayer();
2125
String uuid = player.getUniqueId().toString();
2226

2327
try {
24-
Statement statement = EffectExtender.dbConn.createStatement();
28+
Statement statement = dbConn.createStatement();
2529
statement.setQueryTimeout(10);
2630
ResultSet result = statement.executeQuery(
2731
"select * from PlayerEffects where UUID = '" + uuid + "'");
@@ -39,6 +43,8 @@ public void onPlayerJoin(PlayerJoinEvent event) {
3943
statement.execute("delete from PlayerEffects where UUID = '" + uuid + "'");
4044
}
4145

46+
dbConn.commit();
47+
4248
statement.close();
4349

4450
} catch (SQLException e) {
@@ -54,12 +60,11 @@ public void onPlayerLeave(PlayerQuitEvent event) {
5460
if (!EffectListener.playerEffects.containsKey(player))
5561
return;
5662

57-
5863
try {
5964
HashMap<PotionEffectType, Integer> effects = EffectListener.playerEffects.get(player);
6065
String uuid = player.getUniqueId().toString();
6166
// prepare a batch of sql statements for each effect
62-
PreparedStatement statement = EffectExtender.dbConn.prepareStatement("insert into PlayerEffects values (?, ?, ?)");
67+
PreparedStatement statement = dbConn.prepareStatement("insert into PlayerEffects values (?, ?, ?)");
6368
statement.setQueryTimeout(60);
6469
effects.forEach((effect, maxDuration) -> {
6570
// why do I HAVE to handle SQLExceptions
@@ -73,6 +78,7 @@ public void onPlayerLeave(PlayerQuitEvent event) {
7378
}
7479
});
7580
statement.executeBatch();
81+
dbConn.commit();
7682
statement.close();
7783
} catch (SQLException e) {
7884
e.printStackTrace();

0 commit comments

Comments
 (0)