diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2024-07-05 02:21:13 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2024-07-05 02:21:13 -0400 |
commit | 59fcef47d9ee1379d0fb0e0228521f269d9d3761 (patch) | |
tree | 87f01e0cc8f22367a1fff141a28a0564cf1f9605 | |
download | PlayersSleepingPercentage-59fcef47d9ee1379d0fb0e0228521f269d9d3761.tar.gz PlayersSleepingPercentage-59fcef47d9ee1379d0fb0e0228521f269d9d3761.tar.bz2 PlayersSleepingPercentage-59fcef47d9ee1379d0fb0e0228521f269d9d3761.zip |
First commit
-rw-r--r-- | .gitignore | 113 | ||||
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | pom.xml | 71 | ||||
-rw-r--r-- | src/main/java/net/sowgro/psp/Command.java | 52 | ||||
-rw-r--r-- | src/main/java/net/sowgro/psp/PlayersSleepingPercentage.java | 18 | ||||
-rw-r--r-- | src/main/java/net/sowgro/psp/SleepListener.java | 64 | ||||
-rw-r--r-- | src/main/resources/config.yml | 2 | ||||
-rw-r--r-- | src/main/resources/plugin.yml | 15 |
8 files changed, 351 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9feda34 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# PlayersSleepingPercentage backport +A spigot plugin that backports the playersSleepingPercentage gamerule added in 1.17 to 1.16 + +This plugin is very simple and could probably be compiled for older versions with minimal modifications. + +### Command / Permission + +| Command | Description | Permission | +|------------------------------------------|-----------------------------------------------------------|-----------------------------| +| `playerssleepingpercentage <percentage>` | Set the percentage of players required to sleep the night | `playersSleepingPercentage` | + +### Config +```yaml +# The percentage of players required to skip the night +PlayersSleepingPercentage: 50 +```
\ No newline at end of file @@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>net.sowgro.PlayersSleepingPercentage</groupId> + <artifactId>PlayersSleepingPercentage</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>PlayersSleepingPercentage</name> + + <properties> + <java.version>1.8</java.version> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <configuration> + <source>${java.version}</source> + <target>${java.version}</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.2.4</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + </build> + + <repositories> + <repository> + <id>spigotmc-repo</id> + <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> + </repository> + <repository> + <id>sonatype</id> + <url>https://oss.sonatype.org/content/groups/public/</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.spigotmc</groupId> + <artifactId>spigot-api</artifactId> + <version>1.16.5-R0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/net/sowgro/psp/Command.java b/src/main/java/net/sowgro/psp/Command.java new file mode 100644 index 0000000..83e0933 --- /dev/null +++ b/src/main/java/net/sowgro/psp/Command.java @@ -0,0 +1,52 @@ +package net.sowgro.psp; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.Collections; +import java.util.List; + +import static net.sowgro.psp.PlayersSleepingPercentage.plugin; + + +public class Command implements CommandExecutor, TabCompleter { + @Override + public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) { + + if (!sender.hasPermission("playersSleepingPercentage")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!"); + return true; + } + + if (args.length < 1 || args[0] == null || args[0].isEmpty()) { + sender.sendMessage(ChatColor.RED + "Invalid argument!"); + return false; + } + + int val; + try { + val = Integer.parseInt(args[0]); + } + catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Input was not a number!"); + return false; + } + + if (val < 0 || val > 100) { + sender.sendMessage(ChatColor.RED + "Input must be between 0 and 100 inclusive!"); + return false; + } + + plugin.getConfig().set("PlayersSleepingPercentage", val); + sender.sendMessage(ChatColor.GREEN + "Set to " + val); + return true; + } + + + @Override + public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) { + return Collections.emptyList(); + } +} diff --git a/src/main/java/net/sowgro/psp/PlayersSleepingPercentage.java b/src/main/java/net/sowgro/psp/PlayersSleepingPercentage.java new file mode 100644 index 0000000..dd29eeb --- /dev/null +++ b/src/main/java/net/sowgro/psp/PlayersSleepingPercentage.java @@ -0,0 +1,18 @@ +package net.sowgro.psp; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Objects; + +public class PlayersSleepingPercentage extends JavaPlugin { + + public static PlayersSleepingPercentage plugin; + + @Override + public void onEnable() { + plugin = this; + saveDefaultConfig(); + getServer().getPluginManager().registerEvents(new SleepListener(), this); + Objects.requireNonNull(plugin.getCommand("playersSleepingPercentage")).setExecutor(new Command()); + } +} diff --git a/src/main/java/net/sowgro/psp/SleepListener.java b/src/main/java/net/sowgro/psp/SleepListener.java new file mode 100644 index 0000000..175f49a --- /dev/null +++ b/src/main/java/net/sowgro/psp/SleepListener.java @@ -0,0 +1,64 @@ +package net.sowgro.psp; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerBedEnterEvent; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerBedLeaveEvent; + +import java.util.HashMap; + +import static net.sowgro.psp.PlayersSleepingPercentage.plugin; + +public class SleepListener implements Listener { + + private final HashMap<World, Integer> eventIDs = new HashMap<>(); + + @EventHandler + public void onPlayerBedEnter(PlayerBedEnterEvent event) { + + // check that the player successfully entered the bed + if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) { + return; + } + + World world = event.getPlayer().getWorld(); + + if (enoughPlayersToSleep(world, 1) && eventIDs.get(world) == null) { + eventIDs.put(world, Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> { + world.setTime(0); + world.setStorm(false); + world.setThundering(false); + eventIDs.remove(world); + }, 100L)); // Delay in server ticks before running the task, to ensure it happens after the player is in bed. + } + } + + @EventHandler + public void onPlayerBedLeave(PlayerBedLeaveEvent event) { + + World world = event.getPlayer().getWorld(); + + if (!enoughPlayersToSleep(world, -1) && eventIDs.get(world) != null) { + Bukkit.getServer().getScheduler().cancelTask(eventIDs.get(world)); + eventIDs.remove(world); + } + } + + /** + * Decides if there are enough players sleeping to satisfy the percentage in the config + * @param world The world to check players in + * @param playersSleeping Used to offset the number of players counted as sleeping. The player who just entered/left a bed is not recognized as sleeping so 1 / -1 is passed in after those events + * @return True if there are enough players sleeping + */ + private boolean enoughPlayersToSleep(World world, int playersSleeping) { + for (Player p : world.getPlayers()) { + if (p.isSleeping()) { playersSleeping++; } + } + double currentPercent = ((double) playersSleeping / world.getPlayers().size()) * 100; + double configPercent = plugin.getConfig().getInt("PlayersSleepingPercentage", 100); + return currentPercent >= configPercent; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..17cdd58 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,2 @@ +# The percentage of players required to skip the night +PlayersSleepingPercentage: 50
\ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..de81710 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,15 @@ +name: PlayersSleepingPercentage +version: '${project.version}' +main: net.sowgro.psp.PlayersSleepingPercentage +api-version: '1.16' + +commands: + playersSleepingPercentage: + description: Set the percentage of players required to sleep + usage: "§cUsage: /playersSleepingPercentage <percentage>" + permission: playersSleepingPercentage + +permissions: + playersSleepingPercentage: + default: op + description: Allows you to set the percentage of players required to sleep |