Last active
March 25, 2024 12:41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Subcommand("fyr") | |
public void fireCommand(BukkitCommandActor sender, OfflinePlayer target) { | |
UUID targetUUID = target.getUniqueId(); | |
// If the target player is currently online, we can avoid the expensive database calls | |
if (target.isConnected()) { | |
Player targetOnline = Bukkit.getPlayer(targetUUID); | |
// Check if the player is actually online, but probably not necessary | |
if (targetOnline == null) { | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("The player is marked as online, but they aren't??").color(RED)) | |
.build()); | |
return; | |
} | |
// Attempt to remove the guard role using LuckPerms api | |
DataResult dataResult = guardRoleManager.fire(targetOnline); | |
// If removing the guard role was successful | |
if (dataResult.wasSuccessful()) { | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("You have now fired ").color(GREEN)) | |
.append(text().content(targetOnline.getName()).color(YELLOW)) | |
.build()); | |
targetOnline.sendMessage(text() | |
.content("You have been fired from your guard role!").color(RED).build()); | |
String location = configManager.getConfig().getString("fired-warp"); | |
locationManager.teleport(targetOnline, location); | |
clearPlayer(targetOnline); | |
setBalance(targetOnline); | |
} else { | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("An error occurred: ").color(GRAY)) | |
.append(text().content(dataResult.getMessage()).color(RED)) | |
.build()); | |
} | |
} else { | |
/*If the player is offline, then things are a bit more complicated. | |
We first need to check if the player has already been marked as fired in the database, | |
then based on that result we need to try and remove their guard role using Luckperms api, | |
then based on that result we need to push an update to the database marking the player as fired | |
so that they can be cleared the next time they join the server*/ | |
CompletableFuture<Boolean> isPlayerFiredFuture = firedPlayersDB.isPlayerFired(targetUUID); | |
CompletableFuture<DataResult> firePlayerFuture = guardRoleManager.fireAsync(targetUUID); | |
CompletableFuture<DataResult> insertFiredPlayer = firedPlayersDB.insertFiredGuard(targetUUID); | |
isPlayerFiredFuture.thenComposeAsync(isFired -> { | |
// If the player is already marked as fired in the database | |
if (isFired) { | |
return CompletableFuture.completedFuture(new DataResult(DataResult.ResultStatus.FAILED, "This player is already marked as fired.")); | |
} | |
// Otherwise, proceed with firing the player asynchronously | |
return firePlayerFuture; | |
}).thenComposeAsync(dataResult -> { | |
// If firing was successful, insert a record for the fired guard, marking the player as fired | |
if (dataResult.wasSuccessful()) { | |
return insertFiredPlayer; | |
} | |
// If firing failed, return a completed future indicating the failure to remove the guard role | |
return CompletableFuture.completedFuture(dataResult); | |
}).thenAcceptAsync(dataResult -> { | |
// If all operations were successful, notify the command sender | |
if (dataResult.wasSuccessful()) { | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("You have now fired ").color(GREEN)) | |
.append(text().content(target.getName()).color(YELLOW)) | |
.append(text().content(" as a guard, but since they were offline, they will be cleared the next time they log into the server.").color(GREEN)) | |
.build()); | |
} else { | |
// If any operation failed, notify the command sender | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("Could not fire the player: ").color(GRAY)) | |
.append(text().content(dataResult.getMessage()).color(RED)) | |
.build()); | |
} | |
}, Bukkit.getScheduler().getMainThreadExecutor(plugin)) // Ensure this runs on the main server thread | |
.exceptionally(e -> { | |
// Handle any exceptions that occurred during the asynchronous processing | |
sender.reply(text() | |
.content("[GuardPlugin] ").color(YELLOW) | |
.append(text().content("An error occurred: ").color(GRAY)) | |
.append(text().content(e.getMessage()).color(RED)) | |
.build()); | |
return null; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment