Skip to content

Commit

Permalink
Made purge command executable from console
Browse files Browse the repository at this point in the history
  • Loading branch information
ArkoSammy12 committed Jul 25, 2024
1 parent 3d55d06 commit 446fb39
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ object DatabaseCommands {
.literal("purge")
.requires { src -> src.hasPermissionLevel(4) }
.executes { ctx ->
val player: ServerPlayerEntity = ctx.source.playerOrThrow
player.sendMessage(Text.literal("Initiating purge...").formatted(Formatting.GRAY))
val deletedRows: Int = PublicEnderChest.DATABASE_MANAGER.purge(player.server, PublicEnderChest.CONFIG_MANAGER.getSettingValue<Int, NumberSetting<Int>>(ConfigSettings.PURGE_OLDER_THAN_X_DAYS.settingLocation))
val player: ServerPlayerEntity? = ctx.source.player
sendMessageToPlayerOrConsole(player, Text.literal("Initiating purge...").formatted(Formatting.GRAY), LogType.NORMAL)
val deletedRows: Int = PublicEnderChest.DATABASE_MANAGER.purge(ctx.source.server, PublicEnderChest.CONFIG_MANAGER.getSettingValue<Int, NumberSetting<Int>>(ConfigSettings.PURGE_OLDER_THAN_X_DAYS.settingLocation))
val deletedRowsText: MutableText = Text.literal("$deletedRows").formatted(Formatting.AQUA)
player.sendMessage(Text.empty().append(Text.literal("Purged ")).append(deletedRowsText).append(Text.literal(" entries from the Public Ender Chest inventory database.")))
sendMessageToPlayerOrConsole(player, Text.empty().append(Text.literal("Purged ")).append(deletedRowsText).append(Text.literal(" entries from the Public Ender Chest inventory database.")), LogType.NORMAL)
Command.SINGLE_SUCCESS
}
.build()
Expand All @@ -47,12 +47,12 @@ object DatabaseCommands {
.argument("entriesOlderThanDays", IntegerArgumentType.integer(1))
.requires { src -> src.hasPermissionLevel(4) }
.executes { ctx ->
val player: ServerPlayerEntity = ctx.source.playerOrThrow
val player: ServerPlayerEntity? = ctx.source.player
val olderThanDaysValue: Int = IntegerArgumentType.getInteger(ctx, "entriesOlderThanDays")
player.sendMessage(Text.literal("Initiating purge...").formatted(Formatting.GRAY))
val deletedRows: Int = PublicEnderChest.DATABASE_MANAGER.purge(player.server, olderThanDaysValue)
sendMessageToPlayerOrConsole(player, Text.literal("Initiating purge...").formatted(Formatting.GRAY), LogType.NORMAL)
val deletedRows: Int = PublicEnderChest.DATABASE_MANAGER.purge(ctx.source.server, olderThanDaysValue)
val deletedRowsText: MutableText = Text.literal("$deletedRows").formatted(Formatting.AQUA)
player.sendMessage(Text.empty().append(Text.literal("Purged ")).append(deletedRowsText).append(Text.literal(" entries from the Public Ender Chest inventory database.")))
sendMessageToPlayerOrConsole(player, Text.empty().append(Text.literal("Purged ")).append(deletedRowsText).append(Text.literal(" entries from the Public Ender Chest inventory database.")), LogType.NORMAL)
Command.SINGLE_SUCCESS
}
.build()
Expand Down Expand Up @@ -182,4 +182,23 @@ object DatabaseCommands {

}

private fun sendMessageToPlayerOrConsole(player: ServerPlayerEntity?, text: MutableText, logType: LogType) {
if (player != null) {
player.sendMessage(text)
} else {
when (logType) {
LogType.NORMAL -> PublicEnderChest.LOGGER.info(text.string)
LogType.WARNING -> PublicEnderChest.LOGGER.warn(text.string)
LogType.ERROR -> PublicEnderChest.LOGGER.error(text.string)

}
}
}

private enum class LogType {
NORMAL,
WARNING,
ERROR
}

}

0 comments on commit 446fb39

Please sign in to comment.