From 41fbc8ea903c41bec9ddba5bbe32b377242acb91 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Sat, 13 Jun 2020 22:49:44 +0200 Subject: [PATCH] added a way to scale up the system to more players --- .../louisvallat/sweetdreams/SweetDreams.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/louisvallat/sweetdreams/SweetDreams.java b/src/main/java/xyz/louisvallat/sweetdreams/SweetDreams.java index 0bd1330..4f753c9 100644 --- a/src/main/java/xyz/louisvallat/sweetdreams/SweetDreams.java +++ b/src/main/java/xyz/louisvallat/sweetdreams/SweetDreams.java @@ -1,6 +1,7 @@ package xyz.louisvallat.sweetdreams; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -10,8 +11,13 @@ import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerBedLeaveEvent; import org.bukkit.plugin.java.JavaPlugin; +import java.util.HashMap; +import java.util.Map; + public final class SweetDreams extends JavaPlugin implements Listener { + private final Map sleepingSchedulers = new HashMap<>(); + @Override public void onEnable() { getLogger().info("Enabling plugin..."); @@ -32,19 +38,17 @@ public final class SweetDreams extends JavaPlugin implements Listener { if (world.isThundering() || (world.getTime() >= 12541 && world.getTime() <= 23458)) // https://minecraft.gamepedia.com/Bed#Sleeping { - player.sendMessage("You're sleeping."); - this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> { - world.setTime(1000); - player.sendMessage("You skipped the night for everyone."); - } - , (20)); + sleepingSchedulers.put(player, this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> { + if (world.isThundering()) world.setThundering(false); + else world.setTime(1000); + getServer().broadcastMessage(ChatColor.YELLOW + player.getName() + ChatColor.GOLD + " went to bed."); + }, 100 // 100 ticks = 5 seconds + )); } } @EventHandler(priority = EventPriority.LOWEST) public void onWake(PlayerBedLeaveEvent event) { - final Player player = event.getPlayer(); - - player.sendMessage("Wake up!"); + this.getServer().getScheduler().cancelTask(sleepingSchedulers.get(event.getPlayer())); } }