Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. package cf.striking.strikonomy
  2.  
  3. import cf.striking.strikonomy.Main.Companion.db
  4. import cf.striking.strikonomy.Main.Companion.log
  5. import org.bukkit.Material
  6. import org.bukkit.entity.EntityType
  7. import org.bukkit.entity.Player
  8. import org.bukkit.event.EventHandler
  9. import org.bukkit.event.Listener
  10. import org.bukkit.event.block.BlockBreakEvent
  11. import org.bukkit.event.entity.EntityDeathEvent
  12. import java.sql.SQLException
  13.  
  14. class GrowthListener : Listener {
  15.  
  16. val resourceTypes = mapOf(
  17. Pair("crops", listOf(
  18. Material.WHEAT, Material.POTATO, Material.CARROT, Material.SUGAR_CANE_BLOCK, Material.PUMPKIN,
  19. Material.MELON_BLOCK, Material.CACTUS
  20. )),
  21. Pair("blocks",listOf(
  22. Material.DIAMOND_ORE, Material.GOLD_ORE, Material.STONE, Material.IRON_ORE, Material.REDSTONE_ORE,
  23. Material.GLOWING_REDSTONE_ORE, Material.QUARTZ_ORE, Material.LAPIS_ORE, Material.EMERALD_ORE,
  24. Material.COAL_ORE, Material.SAND, Material.GRAVEL, Material.OBSIDIAN, Material.CLAY, Material.LOG, Material.LOG_2, Material.DIRT, Material.SOIL, Material.GRASS, Material.LONG_GRASS,
  25. Material.GRASS_PATH, Material.DOUBLE_PLANT, Material.YELLOW_FLOWER, Material.RED_ROSE
  26. )),
  27. Pair("hostile_mobs", listOf(
  28. EntityType.WITHER, EntityType.WITHER_SKELETON, EntityType.CAVE_SPIDER, EntityType.BLAZE,
  29. EntityType.CREEPER, EntityType.ELDER_GUARDIAN, EntityType.ENDERMAN, EntityType.ENDERMITE,
  30. EntityType.EVOKER, EntityType.GIANT, EntityType.GUARDIAN, EntityType.HUSK, EntityType.ZOMBIE_VILLAGER,
  31. EntityType.ILLUSIONER, EntityType.PIG_ZOMBIE, EntityType.SILVERFISH, EntityType.SKELETON, EntityType.SPIDER,
  32. EntityType.STRAY, EntityType.VEX, EntityType.VINDICATOR, EntityType.WITCH, EntityType.GHAST, EntityType.POLAR_BEAR,
  33. EntityType.SHULKER, EntityType.SLIME, EntityType.MAGMA_CUBE
  34. )),
  35. Pair("peaceful_mobs", listOf(
  36. EntityType.PIG, EntityType.COW, EntityType.HORSE, EntityType.DONKEY, EntityType.MULE, EntityType.MUSHROOM_COW,
  37. EntityType.BAT, EntityType.SHEEP, EntityType.LLAMA, EntityType.OCELOT, EntityType.RABBIT, EntityType.SQUID
  38. ))
  39. )
  40.  
  41.  
  42. //private val resourceTypes = mapOf(Pair(crops, 1), Pair(naturalResources, 2), Pair(overworldResources, 3))
  43.  
  44. @EventHandler
  45. fun onBlockBreak(event: BlockBreakEvent) {
  46. try {
  47.  
  48. val type = when (event.block.type) {
  49. in resourceTypes["crops"]!! -> 1
  50. in resourceTypes["blocks"]!! -> 2
  51. else -> 0
  52. }
  53.  
  54. val addNewResource = db.connection.prepareStatement("INSERT OR IGNORE INTO harvest (resource, harvests, type) VALUES (?, 1, ?)")
  55. addNewResource.apply {
  56. setString(1, event.block.type.name)
  57. setInt(2, type)
  58. executeUpdate()
  59. }
  60.  
  61. val response = db.connection.createStatement().executeQuery("SELECT * FROM harvest WHERE (resource, type) = ('${event.block.type.name}', $type)")
  62. while (response.next()) {
  63. val currentHarvests = response.getInt("harvests")
  64.  
  65. val addHarvest = db.connection.prepareStatement("UPDATE harvest SET harvests = ? WHERE (resource, type) = (?, ?)")
  66. addHarvest.apply{
  67. setInt(1, currentHarvests+1)
  68. setString(2, event.block.type.name)
  69. setInt(3, type)
  70. executeUpdate()
  71. }
  72. }
  73.  
  74.  
  75. var result = db.connection.createStatement().executeQuery("SELECT SUM(harvests) AS sm FROM harvest WHERE type = $type")
  76. val total_harvests = if (result.next()) result.getInt("sm") else 0
  77.  
  78. result = db.connection.createStatement().executeQuery("SELECT harvests FROM harvest WHERE resource = '${event.block.type.name}'")
  79. val harvests = if (result.next()) result.getInt("harvests") else 0
  80.  
  81. result = db.connection.createStatement().executeQuery("SELECT COUNT(resource) AS cunt FROM harvest WHERE type = $type")
  82. val unique_harvests = if (result.next()) result.getInt("cunt") else 0
  83.  
  84. val growth = calculateGrowth(total_harvests, harvests, unique_harvests)
  85.  
  86. result = db.connection.createStatement().executeQuery("SELECT net_worth FROM corporations WHERE id = 0")
  87.  
  88. val currentWorth = if (result.next()) result.getDouble("net_worth") else 0.0
  89.  
  90. result = db.connection.createStatement().executeQuery("SELECT * FROM corporations WHERE id = 0") //The reserve has an id of 0
  91. while (result.next()) {
  92. val updateNetWorth = db.connection.prepareStatement("UPDATE corporations SET net_worth = ? WHERE id = 0").apply {
  93. setDouble(1, currentWorth + growth)
  94. executeUpdate()
  95. }
  96.  
  97. }
  98.  
  99. result = db.connection.createStatement().executeQuery("SELECT shares, share_value FROM corporations WHERE id = 0")
  100. while (result.next()) {
  101. val shares = result.getInt("shares")
  102.  
  103. val updateShareValue = db.connection.prepareStatement("UPDATE corporations SET share_value = ? WHERE id = 0").apply {
  104. setDouble(1, currentWorth / shares.toDouble())
  105. executeUpdate()
  106. }
  107.  
  108. }
  109.  
  110. result = db.connection.createStatement().executeQuery("SELECT net_worth FROM corporations WHERE id = 0")
  111. while (result.next()) {
  112. }
  113. }
  114. catch (exception: SQLException) {
  115. log.severe(exception.message)
  116. }
  117. }
  118.  
  119. @EventHandler
  120. fun onMobKill(event: EntityDeathEvent) {
  121. val player: Player?
  122.  
  123. val victim = event.entity
  124. when (victim.killer) {
  125. null -> return
  126. !is Player -> return
  127. else -> player = victim.killer
  128. }
  129.  
  130. val type = when (victim.type) {
  131. in resourceTypes["hostile_mobs"]!! -> 3
  132. in resourceTypes["peaceful_mobs"]!! -> 4
  133. else -> 0
  134. }
  135.  
  136. try {
  137.  
  138. val insertNewHarvest = db.connection.prepareStatement("INSERT OR IGNORE INTO harvest (resource, harvests, type) VALUES (?, 1, ?)").apply {
  139. setString(1, victim.type.name)
  140. setInt(2, type)
  141. executeUpdate()
  142. }
  143.  
  144. val response = db.connection.createStatement().executeQuery("SELECT * FROM harvest WHERE (resource, type) = ('${victim.type.name}', $type)")
  145. while (response.next()) {
  146. val currentHarvests = response.getInt("harvests")
  147.  
  148. val addHarvest = db.connection.prepareStatement("UPDATE harvest SET harvests = ? WHERE (resource, type) = (?, ?)").apply {
  149. setInt(1, currentHarvests + 1)
  150. setString(2, victim.type.name)
  151. setInt(3, type)
  152. executeUpdate()
  153. }
  154.  
  155. }
  156.  
  157.  
  158. var result = db.connection.createStatement().executeQuery("SELECT SUM(harvests) AS sm FROM harvest WHERE type = $type")
  159. val total_harvests = if (result.next()) result.getInt("sm") else 0
  160.  
  161. result = db.connection.createStatement().executeQuery("SELECT harvests FROM harvest WHERE resource = '${victim.type.name}'")
  162. val harvests = if (result.next()) result.getInt("harvests") else 0
  163.  
  164. result = db.connection.createStatement().executeQuery("SELECT COUNT(resource) AS cunt FROM harvest WHERE type = $type")
  165. val unique_harvests = if (result.next()) result.getInt("cunt") else 0
  166.  
  167. val growth = calculateGrowth(total_harvests, harvests, unique_harvests)
  168.  
  169. result = db.connection.createStatement().executeQuery("SELECT * FROM corporations WHERE id = 0") //The reserve has an id of 0
  170. while (result.next()) {
  171. val currentWorth = result.getDouble("net_worth")
  172. val remainingShares = result.getInt("remaining_shares")
  173.  
  174. val updateNetWorth = db.connection.prepareStatement("UPDATE corporations SET net_worth = ? WHERE id = 0").apply {
  175. setDouble(1, currentWorth + growth)
  176. executeUpdate()
  177. }
  178.  
  179. val updateShareValue = db.connection.prepareStatement("UPDATE corporations SET share_value = ? WHERE id = 0").apply {
  180. setDouble(1, currentWorth / remainingShares.toDouble())
  181. executeUpdate()
  182. }
  183. }
  184.  
  185. } catch (exception: SQLException) {
  186. log.severe(exception.message)
  187. }
  188.  
  189.  
  190. }
  191.  
  192. private fun calculateGrowth(total_harvests: Int, harvests: Int, unique_harvests: Int): Double = total_harvests.toDouble() / (harvests * unique_harvests + 1).toDouble()
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement