Cooldown Manager

Discussion in 'Resources' started by LCastr0, Jul 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Smerfa

    hyym, I using code like that in my projects:
    Code:java
    1. import java.util.HashMap;
    2. import java.util.EnumMap;
    3. import java.util.Map;
    4. import java.util.Map.Entry;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    8. import org.bukkit.configuration.serialization.ConfigurationSerialization;
    9.  
    10. public class Cooldown implements ConfigurationSerializable
    11. {
    12. static
    13. {
    14. ConfigurationSerialization.registerClass(Cooldown.class);
    15. ConfigurationSerialization.registerClass(CooldownPlayer.class);
    16. Cooldown.instance = new Cooldown();
    17. }
    18.  
    19. /**
    20.   * returns instance of that class
    21.   * I'm using it as object to use ConfigurationSerializable
    22.   */
    23. public static Cooldown getInstance()
    24. {
    25. if (Cooldown.instance == null) // just for sure...
    26. {
    27. Cooldown.instance = new Cooldown();
    28. }
    29. return Cooldown.instance;
    30. }
    31.  
    32. private static Cooldown instance;
    33.  
    34. /**
    35.   * Enum of possible types of Cooldown, it's much simpler and easier that using strings (for me :P)
    36.   */
    37. public static enum CooldownType
    38. {
    39. FIRST_TYPE(1000 * 30), // 30 sec
    40. COMMAND(1000 * 60), // 1 min
    41. SOMETHING(1000 * 60 * 10), // 10 min
    42. YOU_CAN_ADD_MORE(1000 * 60 * 60), // 1 hour
    43. private long time;
    44.  
    45. CooldownType(final long time)
    46. {
    47. this.time = time;
    48. }
    49.  
    50. /**
    51.   * returns time in milliseconds
    52.   */
    53. public long getTime()
    54. {
    55. return this.time;
    56. }
    57.  
    58. /**
    59.   * Set new time of cooldown -> if you want loading it from confg file
    60.   *
    61.   * @param time
    62.   * new time
    63.   */
    64. public void setTime(final long time)
    65. {
    66. this.time = time;
    67. }
    68.  
    69. /**
    70.   * returns difference between selected time.
    71.   *
    72.   * @param time
    73.   * other time
    74.   */
    75. public long getDelta(final long time)
    76. {
    77. return time - this.getTime();
    78. }
    79. }
    80.  
    81. /**
    82.   * Object for storing data about player
    83.   */
    84. public static final class CooldownPlayer implements ConfigurationSerializable
    85. {
    86. // You can add field with player nickname/UUID here if you need.
    87.  
    88. /**
    89.   * map for storing time of cooldowns
    90.   */
    91. private final EnumMap<CooldownType, Long> cooldown = new EnumMap<>(CooldownType.class);
    92.  
    93. /**
    94.   * returns map
    95.   */
    96. public EnumMap<CooldownType, Long> getCooldownMap()
    97. {
    98. return this.cooldown;
    99. }
    100.  
    101. /**
    102.   * to add other map of cooldowns
    103.   */
    104. public void addCooldowns(final Map<CooldownType, Long> map)
    105. {
    106. this.cooldown.putAll(map);
    107. }
    108.  
    109. /**
    110.   * add new cooldown
    111.   *
    112.   * @param type
    113.   * type of it.
    114.   * @param time
    115.   * time of start
    116.   */
    117. public void addCooldown(final CooldownType type, final Long time)
    118. {
    119. this.cooldown.put(type, time);
    120. }
    121.  
    122. /**
    123.   * add new cooldown with actual time
    124.   *
    125.   * @param type
    126.   * type of it
    127.   */
    128. public void addCooldown(final CooldownType type)
    129. {
    130. this.cooldown.put(type, Cooldown.getATime());
    131. }
    132.  
    133. /**
    134.   * returns time of start for selected type
    135.   */
    136. public long getLastUse(final CooldownType type)
    137. {
    138. final Long lastUse = this.cooldown.get(type);
    139. if (lastUse == null)
    140. return 0;
    141. return lastUse;
    142. }
    143.  
    144. /**
    145.   * returns difference between selected time and time of cooldown start
    146.   */
    147. public long getDelta(final CooldownType type, final long aTime)
    148. {
    149. return aTime - this.getLastUse(type);
    150. }
    151.  
    152. /**
    153.   * returns difference between actual time and time of cooldown start
    154.   */
    155. public long getDelta(final CooldownType type)
    156. {
    157. return Cooldown.getATime() - this.getLastUse(type);
    158. }
    159.  
    160. /**
    161.   * returns result.
    162.   * If cooldown is already ended, it will start next. // you can change that if you need
    163.   */
    164. public CooldownResult getResult(final CooldownType type)
    165. {
    166. final long aTime = Cooldown.getATime();
    167. final long delta = type.getDelta(aTime);
    168. final boolean canUse = delta > type.getTime();
    169. if (canUse)
    170. this.addCooldown(type, aTime);
    171. return new CooldownResult(delta, canUse);
    172. }
    173.  
    174. @Override
    175. public int hashCode()
    176. {
    177. final int prime = 31;
    178. int result = 1;
    179. result = (prime * result) + ((this.cooldown == null) ? 0 : this.cooldown.hashCode());
    180. return result;
    181. }
    182.  
    183. @Override
    184. public boolean equals(final Object obj)
    185. {
    186. if (this == obj)
    187. return true;
    188. if (obj == null)
    189. return false;
    190. if (this.getClass() != obj.getClass())
    191. return false;
    192. final CooldownPlayer other = (CooldownPlayer) obj;
    193. if (this.cooldown == null)
    194. {
    195. if (other.cooldown != null)
    196. return false;
    197. } else if (!this.cooldown.equals(other.cooldown))
    198. return false;
    199. return true;
    200. }
    201.  
    202. @Override
    203. public Map<String, Object> serialize()
    204. {
    205. final Map<String, Object> map = new HashMap<>();
    206. for (final Entry<CooldownType, Long> entry : this.cooldown.entrySet())
    207. {
    208. map.put(entry.getKey().name(), entry.getValue());
    209. }
    210. return map;
    211. }
    212.  
    213. public CooldownPlayer()
    214. {}
    215.  
    216. public CooldownPlayer(final Map<String, Object> map)
    217. {
    218. for (final Entry<String, Object> entry : map.entrySet())
    219. {
    220. this.addCooldown(CooldownType.valueOf(entry.getKey()), ((Number) entry.getValue()).longValue()); //config like get number as int if possible, so I always casting to Number and then using one of methods to get correct type ;)
    221. }
    222. }
    223. }
    224.  
    225. /**
    226.   * Object for storing result
    227.   */
    228. public static final class CooldownResult
    229. {
    230.  
    231. private final long delta;
    232. private final boolean result;
    233. private CooldownResult(final long delta, final boolean result)
    234. {
    235. this.delta = delta;
    236. this.result = result;
    237. }
    238.  
    239. /**
    240.   * Returns time to use - can be negative
    241.   */
    242. public long getDelta()
    243. {
    244. return this.delta;
    245. }
    246.  
    247. /**
    248.   * If that returns true -> player can use what they want, if false -> cooldown is active.
    249.   */
    250. public boolean getResult()
    251. {
    252. return this.result;
    253. }
    254.  
    255. @Override
    256. public int hashCode()
    257. {
    258. final int prime = 31;
    259. int result = 1;
    260. result = (prime * result) + (int) (this.delta ^ (this.delta >>> 32));
    261. result = (prime * result) + (this.result ? 1231 : 1237);
    262. return result;
    263. }
    264.  
    265. @Override
    266. public boolean equals(final Object obj)
    267. {
    268. if (this == obj)
    269. return true;
    270. if (obj == null)
    271. return false;
    272. if (this.getClass() != obj.getClass())
    273. return false;
    274. final CooldownResult other = (CooldownResult) obj;
    275. if (this.delta != other.delta)
    276. return false;
    277. if (this.result != other.result)
    278. return false;
    279. return true;
    280. }
    281. }
    282.  
    283. /**
    284.   * Map for storing all players
    285.   */
    286. private final Map<UUID, CooldownPlayer> cooldowns = new HashMap<>();
    287.  
    288. public Map<UUID, CooldownPlayer> getCooldownsMap()
    289. {
    290. return this.cooldowns;
    291. }
    292.  
    293. /**
    294.   * Resturns result for selected player and type of cooldown, PS: it will start new cooldown if needed.
    295.   * That is the only method that you need -> or static method some lines below.
    296.   */
    297. public CooldownResult getResult(final UUID playerID, final CooldownType type)
    298. {
    299. final CooldownPlayer player = this.getPlayer(playerID);
    300. return player.getResult(type);
    301. }
    302.  
    303. /**
    304.   * Delete player from map -> use when player logout and save cooldowns if you want.
    305.   */
    306. public void removePlayer(final UUID playerID)
    307. {
    308. this.cooldowns.remove(playerID);
    309. }
    310.  
    311. /**
    312.   * add player to map -> when you load it from file if you using saving.
    313.   */
    314. public void addPlayer(final UUID playerID, final CooldownPlayer player)
    315. {
    316. this.cooldowns.put(playerID, player);
    317. }
    318.  
    319. /**
    320.   * returns player from map or create new
    321.   */
    322. public CooldownPlayer getPlayer(final UUID playerID)
    323. {
    324. CooldownPlayer player = this.cooldowns.get(playerID);
    325. if (player == null)
    326. {
    327. player = new CooldownPlayer();
    328. this.cooldowns.put(playerID, player);
    329. }
    330. return player;
    331. }
    332.  
    333. /**
    334.   * Static method to access "getResult" method
    335.   * returns result for selected player and type of cooldown.
    336.   *
    337.   * If you don't need save cooldowns to files (to handle reloads/restarts etc..) then it's the only one method that you need.
    338.   */
    339. public static CooldownResult getResultS(final UUID playerID, final CooldownType type)
    340. {
    341. return Cooldown.getInstance().getResult(playerID, type);
    342. }
    343.  
    344. public static long getATime()
    345. {
    346. return System.currentTimeMillis();
    347. }
    348.  
    349. @SuppressWarnings("unchecked")
    350. public Cooldown(final Map<String, Object> map)
    351. {
    352. for (final Entry<String, Object> entry : map.entrySet())
    353. {
    354. this.addPlayer(UUID.fromString(entry.getKey()), (CooldownPlayer) entry.getValue());
    355. }
    356. }
    357.  
    358. @Override
    359. public Map<String, Object> serialize()
    360. {
    361. final Map<String, Object> map = new HashMap<>();
    362. for (final Entry<UUID, CooldownPlayer> entry : this.cooldowns.entrySet())
    363. {
    364. map.put(entry.getKey().toString(), entry.getValue());
    365. }
    366. return map;
    367. }
    368. }
    369.  

    I must change some methods to remove calls to my plugin, but it still should works fine :p (maybe :<)

    And it's very easy to use!
    eg: command with cooldown
    Code:java
    1. @Override
    2. public boolean onCommand(final CommandSender sender, final Command cmd, final String alias, final String[] args)
    3. {
    4. if (! (sender instanceof Player))
    5. {
    6. return true;
    7. }
    8. Player player = (Player) sender;
    9. UUID id = player.getUniqueId();
    10. CooldownResult result = Cooldown.getResultS(id, CooldownType.HOME_CMD);
    11. if(result.getResult())
    12. {
    13. player.sendMessage("You can use that command!");
    14. }
    15. else
    16. {
    17. player.sendMessage("You must still wait: "+(result.getDelta()/1000)+" seconds to use that command!");
    18. }
    19. }

    And you don't must do anything more -> any registers, any creating players etc.
    Only add own CooldownTypes

    And if you need... then also some system to save it to file/files


    Maybe it's not perfect, but I still love it :p
     
    LCastr0 and MCForger like this.
  2. Offline

    LCastr0

    Smerfa It's a good code too :) I am in my phone but when I get in my computer I read all of your code
     
Thread Status:
Not open for further replies.

Share This Page