added a way to scale up the system to more players

This commit is contained in:
Louis Vallat 2020-06-13 22:49:44 +02:00
parent e25674f4f3
commit 41fbc8ea90

View File

@ -1,6 +1,7 @@
package xyz.louisvallat.sweetdreams; package xyz.louisvallat.sweetdreams;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -10,8 +11,13 @@ import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent; import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public final class SweetDreams extends JavaPlugin implements Listener { public final class SweetDreams extends JavaPlugin implements Listener {
private final Map<Player, Integer> sleepingSchedulers = new HashMap<>();
@Override @Override
public void onEnable() { public void onEnable() {
getLogger().info("Enabling plugin..."); getLogger().info("Enabling plugin...");
@ -32,19 +38,17 @@ public final class SweetDreams extends JavaPlugin implements Listener {
if (world.isThundering() || if (world.isThundering() ||
(world.getTime() >= 12541 && world.getTime() <= 23458)) // https://minecraft.gamepedia.com/Bed#Sleeping (world.getTime() >= 12541 && world.getTime() <= 23458)) // https://minecraft.gamepedia.com/Bed#Sleeping
{ {
player.sendMessage("You're sleeping."); sleepingSchedulers.put(player, this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> { if (world.isThundering()) world.setThundering(false);
world.setTime(1000); else world.setTime(1000);
player.sendMessage("You skipped the night for everyone."); getServer().broadcastMessage(ChatColor.YELLOW + player.getName() + ChatColor.GOLD + " went to bed.");
} }, 100 // 100 ticks = 5 seconds
, (20)); ));
} }
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onWake(PlayerBedLeaveEvent event) { public void onWake(PlayerBedLeaveEvent event) {
final Player player = event.getPlayer(); this.getServer().getScheduler().cancelTask(sleepingSchedulers.get(event.getPlayer()));
player.sendMessage("Wake up!");
} }
} }