Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package org.jubaroo.mods.requiem.misc.database.patreon;
  2.  
  3. import com.wurmonline.server.FailedException;
  4. import com.wurmonline.server.items.*;
  5. import com.wurmonline.server.players.Player;
  6. import com.wurmonline.shared.util.MaterialUtilities;
  7. import org.gotti.wurmunlimited.modsupport.ModSupportDb;
  8. import org.jubaroo.mods.requiem.RequiemLogging;
  9. import org.jubaroo.mods.requiem.misc.Misc;
  10.  
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.time.Year;
  16. import java.util.Calendar;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. public class PatreonSleepPowderGift {
  21. public static Logger logger = Logger.getLogger(PatreonSleepPowderGift.class.getName());
  22. private static int currentMonth = Calendar.MONTH;
  23. private static int currentYear = Year.now().getValue();
  24.  
  25. public static void onServerStarted() {
  26. try {
  27. Connection con = ModSupportDb.getModSupportDb();
  28. String sql;
  29. String tableName = "RequiemPatreonGift";
  30. if (!ModSupportDb.hasTable(con, tableName)) {
  31. RequiemLogging.debug(tableName + " table not found in ModSupport, creating it now.");
  32. sql = "CREATE TABLE " + tableName + " (NAME VARCHAR(30) NOT NULL DEFAULT 'Unknown', STEAMID LONG NOT NULL DEFAULT 0, GIFTED_MONTH INT NOT NULL DEFAULT 0, GIFTED_YEAR INT NOT NULL DEFAULT 0)";
  33. PreparedStatement ps = con.prepareStatement(sql);
  34. ps.execute();
  35. ps.close();
  36. }
  37. } catch (SQLException e) {
  38. logger.log(Level.SEVERE, "Error in onServerStarted()", e);
  39. throw new RuntimeException(e);
  40. }
  41. }
  42.  
  43. public static void onPlayerLogin(Player player) {
  44. Connection dbcon;
  45. PreparedStatement ps;
  46. boolean steamIdGifted = false;
  47. boolean thisMonth = false;
  48. boolean thisYear = false;
  49. try {
  50. long steamId = Long.parseLong(String.valueOf(player.getSteamId()));
  51. dbcon = ModSupportDb.getModSupportDb();
  52. ps = dbcon.prepareStatement("SELECT * FROM RequiemPatreonGift");
  53. ResultSet rs = ps.executeQuery();
  54. while (rs.next()) {
  55. if (rs.getLong("STEAMID") == steamId & rs.getInt("GIFTED_MONTH") == currentMonth & rs.getInt("GIFTED_YEAR") == currentYear) {
  56. steamIdGifted = true;
  57. thisMonth = true;
  58. thisYear = true;
  59. break;
  60. }
  61. }
  62. rs.close();
  63. ps.close();
  64. if (!steamIdGifted & !thisMonth & !thisYear) {
  65. RequiemLogging.debug("Player " + player.getName() + " has not been gifted this month, giving sleep powder x5 now...");
  66. dbcon = ModSupportDb.getModSupportDb();
  67. ps = dbcon.prepareStatement("INSERT INTO RequiemPatreonGift (NAME,STEAMID,GIFTED_MONTH,GIFTED_YEAR) VALUES(?,?,?,?)");
  68. ps.setString(1, player.getName());
  69. ps.setLong(2, steamId);
  70. ps.setInt(3, currentMonth);
  71. ps.setInt(4, currentYear);
  72. ps.executeUpdate();
  73. ps.close();
  74. // Give items to Patreons
  75. giveItems(player);
  76. }
  77. } catch (SQLException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81.  
  82. private static void giveItems(Player player) {
  83. try {
  84. if (Misc.isRequiemPatreon(player)) {
  85. for (int i = 0; i < 5; i++) {
  86. player.getInventory().insertItem(ItemFactory.createItem(ItemList.sleepPowder, 99f, Materials.MATERIAL_WHEAT, MaterialUtilities.COMMON, null), true);
  87. }
  88. player.getCommunicator().sendServerMessage("A shadowy figure appears behind you and your pocket gets heavier. You quickly turn around to see what is going on only to find that nobody is there. Your monthly sleep powder had been deposited into your inventory!", 255, 192, 203);
  89. }
  90. } catch (FailedException | NoSuchTemplateException e) {
  91. logger.log(Level.SEVERE, "Error in PatreonSleepPowderGift.giveItems()", e);
  92. throw new RuntimeException(e);
  93. }
  94. }
  95.  
  96. public static String monthString(int month){
  97. String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  98. return monthNames[month];
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement