1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-20 15:27:35 +01:00

Exclude creative/spectator mode players!

This commit is contained in:
BuildTools 2019-03-05 19:16:39 -08:00
parent 7e0646ea9b
commit cf4e586677
5 changed files with 84 additions and 16 deletions

View File

@ -16,16 +16,18 @@
values: values:
# How often (in seconds) to check if a player is in a bed (increase for slower servers) # How often (in seconds) to check if a player is in a bed (increase for slower servers)
check: 1 check: 1
# Percent of players that need to sleep to skip night (must be 0.0 to 1.0) # Percent of players that need to sleep to skip night (must be between 0 to 100)
percent: 0.5 percent: 50
features: features:
# Toggle night skipping feature. Configure amount of players needed to skip above (percent) # Toggle night skipping feature. Configure amount of players needed to skip above (percent)
skipNight: true skip: true
# Clear weather when skipping night # Clear weather when skipping night
clearWeather: true clearWeather: true
# Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count # Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count
bypass: false bypass: false
# Toggle exclusion of players in creative and spectator mode
ignoreCreative: true
# Prevent all players from entering a bed # Prevent all players from entering a bed
block: false block: false
# Display debug information in console # Display debug information in console

View File

@ -67,4 +67,18 @@ public class Config {
return 0; return 0;
} }
} }
/**
* Fetches a double from the configuration
* @param location Configuration location of the double
*/
public double getDouble(String location) {
try {
return Double.parseDouble(harbor.getConfig().getString(location));
}
catch (Exception e) {
error(e);
return 0.0;
}
}
} }

View File

@ -1,8 +1,12 @@
package mykyta.Harbor.Events; package mykyta.Harbor.Events;
import java.util.ArrayList;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -28,19 +32,46 @@ public class BedEnter implements Listener {
if (event.getBedEnterResult() == BedEnterResult.OK) { if (event.getBedEnterResult() == BedEnterResult.OK) {
World world = event.getPlayer().getWorld(); World world = event.getPlayer().getWorld();
// Increment the sleeping count // Create list of players included in sleep count
if (!config.getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) { ArrayList<Player> players = new ArrayList<Player>();
world.getPlayers().stream().filter(p -> util.isSurvival(event.getPlayer())).forEach(p -> {
if (true) {
players.add(p);
}
});
System.out.println(players);
System.out.println("Included players: " + players.size());
// Increment the sleeping count TODO bypass stuff
if (players.contains(event.getPlayer())) {
util.increment(world); util.increment(world);
// Send a chat message when a player is sleeping
if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0)) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.sleeping")
.replace("[sleeping]", String.valueOf(util.fetch(world))))
.replace("[online]", String.valueOf(world.getPlayers().size()))
.replace("[player]", event.getPlayer().getName())
.replace("[needed]", String.valueOf(Math.max(0, (int) Math.ceil(world.getPlayers().size() * (config.getDouble("values.percent") / 100) - util.fetch(world))))));
}
} }
else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass"))); else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass")));
// Send a chat message // Skip night if threshold is reached
if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0)) { if (config.getBoolean("features.skip") && (util.fetch(world) >= (int) Math.ceil(world.getPlayers().size() * (config.getDouble("values.percent") / 100) - util.fetch(world)))) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.sleeping") Bukkit.getServer().getWorld(event.getPlayer().getWorld().getName()).setTime(1000L);
.replace("[sleeping]", String.valueOf(util.fetch(world))))
.replace("[online]", String.valueOf(world.getPlayers().size())) // Clear weather when it turns day
.replace("[player]", event.getPlayer().getName()) if (config.getBoolean("features.clearWeather")) {
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - util.fetch(world)))))); event.getPlayer().getWorld().setStorm(false);
event.getPlayer().getWorld().setThundering(false);
}
// Send a chat message when night is skipped
if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.skipped").length() != 0)) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.skipped")));
}
} }
} }
} }

View File

@ -1,8 +1,11 @@
package mykyta.Harbor.Events; package mykyta.Harbor.Events;
import java.util.ArrayList;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -18,14 +21,22 @@ public class BedLeave implements Listener {
Config config = new Config(); Config config = new Config();
World world = event.getPlayer().getWorld(); World world = event.getPlayer().getWorld();
// Decrement the sleeping count // Create list of players included in sleep count
if (!config.getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) { ArrayList<Player> players = new ArrayList<Player>();
world.getPlayers().stream().filter(p -> util.isSurvival(event.getPlayer())).forEach(p -> {
if (true) {
players.add(p);
}
});
// Decrement the sleeping count TODO bypass stuff
if (players.contains(event.getPlayer())) {
util.decrement(world); util.decrement(world);
} }
else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass"))); else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass")));
// Send a chat message // Send a chat message when player gets out of bed (if it's not day)
if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0)) { if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0) && Bukkit.getServer().getWorld(world.getName()).getTime() < 1000L) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.left") Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.left")
.replace("[sleeping]", String.valueOf(util.fetch(world)))) .replace("[sleeping]", String.valueOf(util.fetch(world))))
.replace("[online]", String.valueOf(world.getPlayers().size())) .replace("[online]", String.valueOf(world.getPlayers().size()))

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -94,4 +95,13 @@ public class Util {
.replace("[online]", String.valueOf(world.getPlayers().size())) .replace("[online]", String.valueOf(world.getPlayers().size()))
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - (sleeping.get(world)).intValue()))))); .replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - (sleeping.get(world)).intValue())))));
} }
/**
* Returns true if player is in survival mode
* @param player Target player
*/
public boolean isSurvival(Player player) {
if (player.getGameMode() == GameMode.SURVIVAL) return true;
else return false;
}
} }