Ghaz-ranka

Untitled

Jun 20th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package zorg.esinia.server.events;
  2.  
  3. import java.time.Duration;
  4. import java.time.Instant;
  5. import java.time.ZoneId;
  6. import java.time.format.DateTimeFormatter;
  7. import java.time.format.FormatStyle;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.UUID;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. import org.spongepowered.api.Sponge;
  15. import org.spongepowered.api.block.BlockSnapshot;
  16. import org.spongepowered.api.event.Listener;
  17. import org.spongepowered.api.event.cause.Cause;
  18. import org.spongepowered.api.event.game.state.GameStartedServerEvent;
  19. import org.spongepowered.api.scheduler.Task;
  20. import org.spongepowered.api.world.Location;
  21. import org.spongepowered.api.world.World;
  22.  
  23. import com.flowpowered.math.vector.Vector3i;
  24.  
  25. import zorg.esinia.server.Esinia;
  26. import zorg.esinia.server.Config;
  27. import zorg.esinia.server.Config.RespawnBlocksEntry;
  28.  
  29. public class EventServerStart {
  30. private Esinia plugin;
  31. public Config config;
  32. public EventServerStart(Esinia plugin) {
  33. this.plugin = plugin;
  34. this.config = plugin.config;
  35. }
  36.  
  37. @Listener
  38. public void onServerStart(GameStartedServerEvent event) {
  39. DateTimeFormatter formatter =
  40. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
  41. .withLocale(Locale.US)
  42. .withZone(ZoneId.systemDefault());
  43. plugin.getLogger().info("EventServerStart: starting respawn blocks timer.");
  44. Task.builder()
  45. .delay(2, TimeUnit.SECONDS)
  46. .interval(2, TimeUnit.SECONDS)
  47. .execute(() -> {
  48. plugin.getLogger().info("EventServerStart: timer: Beep");
  49. if (config.respawnBlocks != null && !config.respawnBlocks.isEmpty()) {
  50. plugin.getLogger().info("EventServerStart: timer: respawnBlocks is not null");
  51. List<RespawnBlocksEntry> blocks = config.respawnBlocks;
  52. Iterator<RespawnBlocksEntry> iter = blocks.iterator();
  53. Instant instant = Instant.now();
  54. for (RespawnBlocksEntry block : (Iterable<RespawnBlocksEntry>) () -> iter) {
  55. plugin.getLogger().info("EventServerStart: timer: interating through a block.");
  56. String then_ = block.time;
  57. Instant then = (Instant) formatter.parse(then_, Instant::from);
  58. Duration timeElapsed = Duration.between(then, instant);
  59. plugin.getLogger().info("EventServerStart: timer: duration: " + timeElapsed.getSeconds());
  60. if(block.timer <= timeElapsed.getSeconds()) {
  61. plugin.getLogger().info("EventServerStart: timer: block needs a respawn.");
  62. BlockSnapshot blockShot = block.snapshot;
  63. Vector3i blockVector3i = blockShot.getPosition();
  64. UUID blockworldUUID = blockShot.getWorldUniqueId();
  65. for (World world : Sponge.getGame().getServer().getWorlds()) {
  66. if (blockworldUUID.equals(world.getUniqueId())) {
  67. Location<World> blockLocation = new Location<World>(world, blockVector3i);
  68. blockLocation.setBlock(block.snapshot.getState(), Cause.source(plugin).build());
  69. iter.remove();
  70. }
  71. }
  72. }
  73. }
  74. plugin.saveConfig();
  75. }
  76.  
  77. })
  78. .submit(plugin);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment