Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zorg.esinia.server.events;
- import java.time.Duration;
- import java.time.Instant;
- import java.time.ZoneId;
- import java.time.format.DateTimeFormatter;
- import java.time.format.FormatStyle;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Locale;
- import java.util.UUID;
- import java.util.concurrent.TimeUnit;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.block.BlockSnapshot;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.cause.Cause;
- import org.spongepowered.api.event.game.state.GameStartedServerEvent;
- import org.spongepowered.api.scheduler.Task;
- import org.spongepowered.api.world.Location;
- import org.spongepowered.api.world.World;
- import com.flowpowered.math.vector.Vector3i;
- import zorg.esinia.server.Esinia;
- import zorg.esinia.server.Config;
- import zorg.esinia.server.Config.RespawnBlocksEntry;
- public class EventServerStart {
- private Esinia plugin;
- public Config config;
- public EventServerStart(Esinia plugin) {
- this.plugin = plugin;
- this.config = plugin.config;
- }
- @Listener
- public void onServerStart(GameStartedServerEvent event) {
- DateTimeFormatter formatter =
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
- .withLocale(Locale.US)
- .withZone(ZoneId.systemDefault());
- plugin.getLogger().info("EventServerStart: starting respawn blocks timer.");
- Task.builder()
- .delay(2, TimeUnit.SECONDS)
- .interval(2, TimeUnit.SECONDS)
- .execute(() -> {
- plugin.getLogger().info("EventServerStart: timer: Beep");
- if (config.respawnBlocks != null && !config.respawnBlocks.isEmpty()) {
- plugin.getLogger().info("EventServerStart: timer: respawnBlocks is not null");
- List<RespawnBlocksEntry> blocks = config.respawnBlocks;
- Iterator<RespawnBlocksEntry> iter = blocks.iterator();
- Instant instant = Instant.now();
- for (RespawnBlocksEntry block : (Iterable<RespawnBlocksEntry>) () -> iter) {
- plugin.getLogger().info("EventServerStart: timer: interating through a block.");
- String then_ = block.time;
- Instant then = (Instant) formatter.parse(then_, Instant::from);
- Duration timeElapsed = Duration.between(then, instant);
- plugin.getLogger().info("EventServerStart: timer: duration: " + timeElapsed.getSeconds());
- if(block.timer <= timeElapsed.getSeconds()) {
- plugin.getLogger().info("EventServerStart: timer: block needs a respawn.");
- BlockSnapshot blockShot = block.snapshot;
- Vector3i blockVector3i = blockShot.getPosition();
- UUID blockworldUUID = blockShot.getWorldUniqueId();
- for (World world : Sponge.getGame().getServer().getWorlds()) {
- if (blockworldUUID.equals(world.getUniqueId())) {
- Location<World> blockLocation = new Location<World>(world, blockVector3i);
- blockLocation.setBlock(block.snapshot.getState(), Cause.source(plugin).build());
- iter.remove();
- }
- }
- }
- }
- plugin.saveConfig();
- }
- })
- .submit(plugin);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment