rasmonkey17

BalanceTopCommandBackup

Jul 15th, 2021
1,102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package otbmisc2commands
  2.  
  3. import com.earth2me.essentials.Essentials
  4. import com.earth2me.essentials.User
  5. import net.essentialsx.api.v2.services.BalanceTop
  6. import org.bukkit.command.Command
  7. import org.bukkit.command.CommandExecutor
  8. import org.bukkit.command.CommandSender
  9. import org.bukkit.configuration.ConfigurationSection
  10. import otbmisc2.OTBMisc2
  11. import otbmisc2.OTBMisc2Util.Util.toChatColor
  12. import java.math.RoundingMode
  13. import java.text.DecimalFormat
  14. import java.time.Instant
  15. import java.util.*
  16.  
  17. class BalanceTopCommandBackup(private val plugin: OTBMisc2, private val ess: Essentials) : CommandExecutor
  18. {
  19.     private val prefix: String = plugin.config.getString("prefix").toString()
  20.     private var timestamp: Long = 0
  21.     private val moneyFormat: DecimalFormat = DecimalFormat("#,##0.00")
  22.  
  23.     override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean
  24.     {
  25.         val baltopRefreshMilli: Long = plugin.config.getLong("baltopRefreshMilli")
  26.  
  27.         if (Instant.now().epochSecond - timestamp > baltopRefreshMilli ||
  28.             (args.isNotEmpty() && (args[0].equals("refresh", true) || args[0].equals("force", true))))
  29.         {
  30.             // refresh baltop
  31.             timestamp = Instant.now().epochSecond
  32.             ess.balanceTop.calculateBalanceTopMapAsync().thenRun {
  33.                 sendBaltop(sender, args)
  34.             }
  35.             return true
  36.         }
  37.  
  38.         // send baltop as is
  39.         sendBaltop(sender, args)
  40.         return true
  41.     }
  42.  
  43.     fun sendBaltop(sender: CommandSender, args: Array<out String>)
  44.     {
  45.         val baltopCache: Map<UUID, BalanceTop.Entry> = ess.balanceTop.balanceTopCache
  46.  
  47.         var page = 1
  48.         if (args.isNotEmpty() && args[0].toIntOrNull() != null)
  49.         {
  50.             page = args[0].toInt()
  51.  
  52.             if (page < 1 || (page - 1) * 10 + 10 > baltopCache.size)
  53.             {
  54.                 sender.sendMessage("$prefix &cThat is not a valid page number!".toChatColor())
  55.                 return
  56.             }
  57.         }
  58.  
  59.         var startValue: Int = (page - 1) * 10 + 1
  60.  
  61.         var baltopMsg = "\n&8-----------&b&lTOP BALANCES&8-----------\n"
  62.  
  63.         var totalCounter = 0
  64.         var counter = 0
  65.         for (entry: Map.Entry<UUID, BalanceTop.Entry> in baltopCache.entries)
  66.         {
  67.             ++totalCounter
  68.  
  69.             if (startValue > totalCounter) continue
  70.  
  71.             val number: Int = startValue + counter
  72.             val displayName: String = entry.value.displayName
  73.             val user: User = ess.getUser(entry.key)
  74.             val worldName: String = user.world.name
  75.             val formattedBalance: String = moneyFormat.format(entry.value.balance.setScale(2, RoundingMode.HALF_UP))
  76.  
  77.             var gameMode: String
  78.             val worldNameSection: ConfigurationSection = plugin.config.getConfigurationSection("baltopWorldNames") ?: continue
  79.  
  80.             if (worldNameSection.contains(worldName))
  81.                 gameMode = worldNameSection.get(worldName).toString()
  82.             else
  83.                 gameMode = worldName.uppercase()
  84.  
  85.             baltopMsg += "&3$number) &b$displayName &8- &7$gameMode &8- &a$$formattedBalance\n"
  86.             ++counter
  87.  
  88.             if (counter > 9) break
  89.         }
  90.  
  91.         baltopMsg += "&8----------------------------------\n\n "
  92.         sender.sendMessage(baltopMsg.toChatColor())
  93.     }
  94. }
  95.  
RAW Paste Data