Add /votenight support

This commit is contained in:
2026-06-09 20:17:51 -04:00
parent c411e35ea2
commit 9cb21f03cf
8 changed files with 148 additions and 19 deletions
@@ -15,7 +15,8 @@ import java.util.stream.Collectors;
public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
private final Map<String, Set<UUID>> worldVotes = new HashMap<>();
private final Map<String, Set<UUID>> dayVotes = new HashMap<>();
private final Map<String, Set<UUID>> nightVotes = new HashMap<>();
@Override
public void onEnable() {
@@ -24,13 +25,17 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
Objects.requireNonNull(getCommand("voteday")).setExecutor(this);
Objects.requireNonNull(getCommand("voteday")).setTabCompleter(this);
Objects.requireNonNull(getCommand("votenight")).setExecutor(this);
Objects.requireNonNull(getCommand("votenight")).setTabCompleter(this);
Objects.requireNonNull(getCommand("dirtsleep")).setExecutor(this);
Objects.requireNonNull(getCommand("dirtsleep")).setTabCompleter(this);
}
@Override
public void onDisable() {
worldVotes.clear();
dayVotes.clear();
nightVotes.clear();
}
@Override
@@ -45,6 +50,16 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
return true;
}
if (command.getName().equalsIgnoreCase("votenight")) {
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cOnly players can use /votenight."));
return true;
}
handleVoteNight(player);
return true;
}
if (command.getName().equalsIgnoreCase("dirtsleep")) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("dirtsleep.admin")) {
@@ -53,7 +68,8 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
}
reloadConfig();
worldVotes.clear();
dayVotes.clear();
nightVotes.clear();
sender.sendMessage(message("messages.reloaded"));
return true;
}
@@ -78,14 +94,14 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
return;
}
if (!canVoteInWorld(world)) {
if (!canVoteDayInWorld(world)) {
player.sendMessage(message("messages.not-night"));
clearVotes(world);
clearDayVotes(world);
return;
}
String worldKey = world.getName().toLowerCase(Locale.ROOT);
Set<UUID> votes = worldVotes.computeIfAbsent(worldKey, k -> new HashSet<>());
Set<UUID> votes = dayVotes.computeIfAbsent(worldKey, k -> new HashSet<>());
if (votes.contains(player.getUniqueId())) {
player.sendMessage(message("messages.already-voted"));
@@ -107,19 +123,76 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
if (currentVotes >= requiredVotes) {
makeDay(world);
clearVotes(world);
clearDayVotes(world);
clearNightVotes(world);
broadcast(world, message("messages.vote-passed"));
}
}
private void handleVoteNight(Player player) {
if (!getConfig().getBoolean("vote-night.enabled", true)) {
player.sendMessage(message("messages.night-disabled"));
return;
}
World world = player.getWorld();
if (!isWorldEnabled(world)) {
player.sendMessage(message("messages.world-disabled"));
return;
}
if (!canVoteNightInWorld(world)) {
player.sendMessage(message("messages.not-day"));
clearNightVotes(world);
return;
}
String worldKey = world.getName().toLowerCase(Locale.ROOT);
Set<UUID> votes = nightVotes.computeIfAbsent(worldKey, k -> new HashSet<>());
if (votes.contains(player.getUniqueId())) {
player.sendMessage(message("messages.night-already-voted"));
return;
}
votes.add(player.getUniqueId());
int currentVotes = countValidVotes(world, votes);
int requiredVotes = getRequiredVotes(world);
String voteMessage = message("messages.night-vote-added")
.replace("%player%", player.getName())
.replace("%votes%", String.valueOf(currentVotes))
.replace("%required%", String.valueOf(requiredVotes))
.replace("%remaining%", String.valueOf(Math.max(0, requiredVotes - currentVotes)));
broadcast(world, voteMessage);
if (currentVotes >= requiredVotes) {
makeNight(world);
clearNightVotes(world);
clearDayVotes(world);
broadcast(world, message("messages.night-vote-passed"));
}
}
private void makeDay(World world) {
world.setTime(1000);
world.setStorm(false);
world.setThundering(false);
}
private void clearVotes(World world) {
worldVotes.remove(world.getName().toLowerCase(Locale.ROOT));
private void makeNight(World world) {
world.setTime(13000);
}
private void clearDayVotes(World world) {
dayVotes.remove(world.getName().toLowerCase(Locale.ROOT));
}
private void clearNightVotes(World world) {
nightVotes.remove(world.getName().toLowerCase(Locale.ROOT));
}
private int countValidVotes(World world, Set<UUID> votes) {
@@ -135,7 +208,7 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
return worlds.isEmpty() || worlds.contains(world.getName());
}
private boolean canVoteInWorld(World world) {
private boolean canVoteDayInWorld(World world) {
if (world.getEnvironment() != World.Environment.NORMAL) {
return false;
}
@@ -149,6 +222,20 @@ public final class DirtSleepPlugin extends JavaPlugin implements TabExecutor {
return time >= nightStartsAt && time < 24000;
}
private boolean canVoteNightInWorld(World world) {
if (world.getEnvironment() != World.Environment.NORMAL) {
return false;
}
if (!getConfig().getBoolean("vote-night.only-during-day", true)) {
return true;
}
long time = world.getTime();
int dayEndsAt = getConfig().getInt("vote-night.day-ends-at", 12542);
return time >= 0 && time < dayEndsAt;
}
private int getRequiredVotes(World world) {
int eligible = 0;
boolean excludeBypass = getConfig().getBoolean("vote-day.exclude-bypass", true);
+22 -4
View File
@@ -1,8 +1,9 @@
vote-day:
enabled: true
# Empty list = all worlds
worlds: []
# Plugin will only work in these worlds
worlds:
- "world"
# Only allow /voteday during night
only-during-night: true
@@ -18,14 +19,31 @@ vote-day:
# dirtsleep.bypass
exclude-bypass: true
vote-night:
enabled: true
# Only allow /votenight during daytime
only-during-day: true
# Tick time where day effectively ends for vote checks
day-ends-at: 12542
messages:
prefix: "&8[&bDirtSleep&8] &7"
prefix: "&8[&6DirtBagMC&8] &7"
vote-added: "%prefix%&e%player% &7voted for day. &f(%votes%/%required% votes, %remaining% remaining)"
vote-passed: "%prefix%&aVote passed! Skipping to day."
already-voted: "%prefix%&cYou already voted."
not-night: "%prefix%&cYou can only use this at night."
disabled: "%prefix%&cVote day is disabled."
world-disabled: "%prefix%&cVote day is disabled in this world."
night-vote-added: "%prefix%&e%player% &7voted for night. &f(%votes%/%required% votes, %remaining% remaining)"
night-vote-passed: "%prefix%&aVote passed! Skipping to night."
night-already-voted: "%prefix%&cYou already voted for night."
not-day: "%prefix%&cYou can only use this during the day."
night-disabled: "%prefix%&cVote night is disabled."
world-disabled: "%prefix%&cVote commands are disabled in this world."
reloaded: "%prefix%&aConfig reloaded."
no-permission: "%prefix%&cYou do not have permission."
usage: "%prefix%&7Use: &f/dirtsleep reload"
+3
View File
@@ -8,6 +8,9 @@ commands:
voteday:
description: Vote to make it day
usage: /voteday
votenight:
description: Vote to make it night
usage: /votenight
dirtsleep:
description: DirtSleep admin command
usage: /dirtsleep reload
Binary file not shown.
+22 -4
View File
@@ -1,8 +1,9 @@
vote-day:
enabled: true
# Empty list = all worlds
worlds: []
# Plugin will only work in these worlds
worlds:
- "world"
# Only allow /voteday during night
only-during-night: true
@@ -18,14 +19,31 @@ vote-day:
# dirtsleep.bypass
exclude-bypass: true
vote-night:
enabled: true
# Only allow /votenight during daytime
only-during-day: true
# Tick time where day effectively ends for vote checks
day-ends-at: 12542
messages:
prefix: "&8[&bDirtSleep&8] &7"
prefix: "&8[&6DirtBagMC&8] &7"
vote-added: "%prefix%&e%player% &7voted for day. &f(%votes%/%required% votes, %remaining% remaining)"
vote-passed: "%prefix%&aVote passed! Skipping to day."
already-voted: "%prefix%&cYou already voted."
not-night: "%prefix%&cYou can only use this at night."
disabled: "%prefix%&cVote day is disabled."
world-disabled: "%prefix%&cVote day is disabled in this world."
night-vote-added: "%prefix%&e%player% &7voted for night. &f(%votes%/%required% votes, %remaining% remaining)"
night-vote-passed: "%prefix%&aVote passed! Skipping to night."
night-already-voted: "%prefix%&cYou already voted for night."
not-day: "%prefix%&cYou can only use this during the day."
night-disabled: "%prefix%&cVote night is disabled."
world-disabled: "%prefix%&cVote commands are disabled in this world."
reloaded: "%prefix%&aConfig reloaded."
no-permission: "%prefix%&cYou do not have permission."
usage: "%prefix%&7Use: &f/dirtsleep reload"
+3
View File
@@ -8,6 +8,9 @@ commands:
voteday:
description: Vote to make it day
usage: /voteday
votenight:
description: Vote to make it night
usage: /votenight
dirtsleep:
description: DirtSleep admin command
usage: /dirtsleep reload
+1 -1
View File
@@ -1,5 +1,5 @@
#Generated by Maven
#Tue Jun 09 16:14:06 EDT 2026
#Tue Jun 09 20:09:21 EDT 2026
artifactId=DirtSleep
groupId=com.bitnix
version=1.0-SNAPSHOT