Skip to content

Commit

Permalink
fixed reload bug + cleanup + formatting + new config messages
Browse files Browse the repository at this point in the history
  • Loading branch information
CodedRedGIT committed Oct 11, 2023
1 parent 5b3142a commit 3a4625d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
12 changes: 11 additions & 1 deletion src/main/java/dev/codedred/safedrop/SafeDrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import dev.codedred.safedrop.listeners.PlayerDropItem;
import dev.codedred.safedrop.listeners.PlayerJoinQuit;
import lombok.Getter;

import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Collection;
import java.util.Objects;

public final class SafeDrop extends JavaPlugin {
Expand All @@ -26,6 +29,14 @@ public void onEnable() {
registerListeners();

this.loadDatabase();

Collection<? extends Player> onlinePlayers = getServer().getOnlinePlayers();
if (!onlinePlayers.isEmpty()) {
for (Player p : onlinePlayers) {
PlayerJoinQuit.initalizePlayerDropStatus(p, this);
}
}

}

public void loadDatabase() {
Expand Down Expand Up @@ -62,7 +73,6 @@ private void registerListeners() {
pm.registerEvents(new PlayerJoinQuit(this), this);
}


private void checkForUpdate() {
getServer().getScheduler().runTaskAsynchronously(this, () -> {
UpdateChecker updater = new UpdateChecker(this, 72585);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/dev/codedred/safedrop/commands/Drop.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ private void handleDatabaseDisabledCommand(Player player, String[] args) {
}
}

private void updateDropStatus(DropManager dropManager, UUID playerId, boolean status, String messageKey, Player player) {
private void updateDropStatus(DropManager dropManager, UUID playerId, boolean status, String messageKey,
Player player) {
dropManager.addDropStatus(playerId, status);
sendMessageByConfigKey(player, messageKey);
}
Expand All @@ -93,7 +94,8 @@ private void handleCommonCommand(Player player, String[] args) {
}
case "REPORTBUG" -> {
if (player.hasPermission(PERMISSION_ADMIN))
player.sendMessage(ChatUtils.format("&9Report issues here: \nhttps://github.com/CodedRed-Spigot/SafeDrop/issues"));
player.sendMessage(ChatUtils
.format("&9Report issues here: \nhttps://github.com/CodedRed-Spigot/SafeDrop/issues"));
else
sendError(player);
}
Expand All @@ -105,7 +107,8 @@ private void sendUsage(Player player) {
for (String msg : DataManager.getInstance().getConfig().getStringList("messages.usage"))
player.sendMessage(ChatUtils.format(msg));
if (player.hasPermission(PERMISSION_ADMIN))
player.sendMessage(ChatUtils.format("&c&lAdmin Command:\n&c/&8sd reload &7- reloads plugin\n&c/&8sd reportbug &7- report a plugin bug"));
player.sendMessage(ChatUtils.format(
"&c&lAdmin Command:\n&c/&8sd reload &7- reloads plugin\n&c/&8sd reportbug &7- report a plugin bug"));
}

private void sendError(Player player) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/dev/codedred/safedrop/data/DataManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.codedred.safedrop.data;

import dev.codedred.safedrop.data.database.manager.DatabaseManager;
import org.bukkit.configuration.file.FileConfiguration;

import dev.codedred.safedrop.SafeDrop;
Expand Down Expand Up @@ -81,23 +80,23 @@ public void checkAndFixConfigKeys() {
modified = true;
}
if (!cfg.contains("messages.drop-text-message")) {
cfg.set("messages.drop-text-message", "&9Confirm drop by dropping item again!");
cfg.set("messages.drop-text-message", "&3&lConfirm drop &r&9by dropping item again!");
modified = true;
}
if (!cfg.contains("messages.drop-actionbar-message")) {
cfg.set("messages.drop-actionbar-message", "&9Confirm drop by dropping item again!");
cfg.set("messages.drop-actionbar-message", "&3&lConfirm drop &r&9by dropping item again!");
modified = true;
}
if (!cfg.contains("messages.safedrop-off")) {
cfg.set("messages.safedrop-off", "&cSafeDrop has been disabled!");
cfg.set("messages.safedrop-off", "&c&lSafeDrop has been disabled!");
modified = true;
}
if (!cfg.contains("messages.safedrop-on")) {
cfg.set("messages.safedrop-on", "&aSafeDrop has been enabled!");
cfg.set("messages.safedrop-on", "&a&lSafeDrop has been enabled!");
modified = true;
}
if (!cfg.contains("messages.usage")) {
List<String> usage = Arrays.asList("&9&lTry,", "&9/&8Safedrop &7- Turns on/off safedrop");
List<String> usage = Arrays.asList("&9&lTry,", "&9/&8Safedrop &7- &oTurns on/off safedrop");
cfg.set("messages.usage", usage);
modified = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

public class MySQL implements DataSource {


private Connection connection;

public MySQL(SafeDrop plugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private void handlePlayerDrop(Player player, UUID uuid, DataManager dataManager,
}

if (dataManager.getConfig().getBoolean("safe-drop.actionbar-message.enabled")) {
TextComponent message = new TextComponent(ChatUtils.format(dataManager.getConfig().getString("messages.drop-actionbar-message")));
TextComponent message = new TextComponent(
ChatUtils.format(dataManager.getConfig().getString("messages.drop-actionbar-message")));
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, message);
}
}
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/dev/codedred/safedrop/listeners/PlayerJoinQuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.codedred.safedrop.managers.DropManager;
import dev.codedred.safedrop.model.User;
import lombok.val;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
Expand All @@ -19,39 +21,48 @@ public PlayerJoinQuit(SafeDrop plugin) {
this.plugin = plugin;
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
public static void initalizePlayerDropStatus(Player player, SafeDrop plugin) {
DataManager dataManager = DataManager.getInstance();
DropManager dropManager = DropManager.getInstance();

if (dataManager.getConfig().getBoolean("database-settings.enabled") && plugin.getDatabaseManager().getDataSource().getConnection() != null) {
val uniqueId = event.getPlayer().getUniqueId();
if (dataManager.getConfig().getBoolean("database-settings.enabled")
&& plugin.getDatabaseManager().getDataSource().getConnection() != null) {
val uniqueId = player.getUniqueId();
val usersTable = plugin.getDatabaseManager().getUsersTable();
User user = usersTable.getByUuid(uniqueId);

if (user == null) {
user = new User(event.getPlayer().getUniqueId(), dataManager.getConfig().getBoolean("safe-drop.enabled"));
user = new User(player.getUniqueId(),
dataManager.getConfig().getBoolean("safe-drop.enabled"));
usersTable.insert(user);
}

dropManager.addDropStatus(event.getPlayer().getUniqueId(), user.isEnabled());
}
else {
boolean exists = dataManager.getSaves().contains(HEAD + event.getPlayer().getUniqueId());
dropManager.addDropStatus(player.getUniqueId(), user.isEnabled());
} else {
boolean exists = dataManager.getSaves().contains(HEAD + player.getUniqueId());
if (exists)
dropManager.addDropStatus(event.getPlayer().getUniqueId(), dataManager.getSaves().getBoolean(HEAD + event.getPlayer().getUniqueId()));
dropManager.addDropStatus(player.getUniqueId(),
dataManager.getSaves().getBoolean(HEAD + player.getUniqueId()));
else
dropManager.addDropStatus(event.getPlayer().getUniqueId(), dataManager.getConfig().getBoolean("safe-drop.enabled"));
dropManager.addDropStatus(player.getUniqueId(),
dataManager.getConfig().getBoolean("safe-drop.enabled"));
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
initalizePlayerDropStatus(event.getPlayer(), plugin);
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
DataManager dataManager = DataManager.getInstance();
DropManager dropManager = DropManager.getInstance();

if (!dataManager.getConfig().getBoolean("database-settings.enabled") || (dataManager.getConfig().getBoolean("database-settings.enabled") && plugin.getDatabaseManager().getDataSource().getConnection() == null )) {
dataManager.getSaves().set(HEAD + event.getPlayer().getUniqueId(), dropManager.getStatus(event.getPlayer().getUniqueId()));
if (!dataManager.getConfig().getBoolean("database-settings.enabled")
|| (dataManager.getConfig().getBoolean("database-settings.enabled")
&& plugin.getDatabaseManager().getDataSource().getConnection() == null)) {
dataManager.getSaves().set(HEAD + event.getPlayer().getUniqueId(),
dropManager.getStatus(event.getPlayer().getUniqueId()));
dataManager.saveSaves();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void run() {
removeRequest(uuid);
cancel();
}
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(SafeDrop.class),dataManager.getConfig().getLong("safe-drop.seconds") * 20L - 15L);
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(SafeDrop.class),
dataManager.getConfig().getLong("safe-drop.seconds") * 20L - 15L);
}
}
1 change: 0 additions & 1 deletion src/main/java/dev/codedred/safedrop/utils/async/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.Getter;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ForkJoinPool;
Expand Down

0 comments on commit 3a4625d

Please sign in to comment.