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;
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<Player, Integer> 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()));
}
}