Skip to content

Commit

Permalink
feat: make plugin work in 1.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Pupskuchen committed Apr 5, 2023
1 parent 6b2d76e commit 121a6cd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void onPlayerBedEnter(final PlayerBedEnterEvent event) {
skipper = new NightSkipper(plugin, world);
this.worldSkippers.put(worldName, skipper);
}

skipper.scheduleSkip();
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.pupskuchen.timecontrol.nightskipping;

import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import net.pupskuchen.timecontrol.TimeControl;

class SkipAttempt extends BukkitRunnable {
private final NightSkipper skipper;

public SkipAttempt(final NightSkipper skipper) {
this.skipper = skipper;
}

@Override
public void run() {
this.skipper.skipNight();
}
}


class NightSkipScheduler {
private final TimeControl plugin;
private final NightSkipper skipper;
private final int sleepTicksToWake;

private BukkitTask skipTask;

public NightSkipScheduler(final TimeControl plugin, final NightSkipper skipper,
final int sleepTicksToWake) {
this.plugin = plugin;
this.skipper = skipper;
this.sleepTicksToWake = sleepTicksToWake;
}

public void scheduleSkip() {
this.cancel();
skipTask = new SkipAttempt(skipper).runTaskLater(plugin, sleepTicksToWake);
}

public void cancel() {
if (skipTask == null) {
return;
}

skipTask.cancel();
skipTask = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,48 @@ public class NightSkipper {
private static final int SKIP_PERCENTAGE_FALLBACK = 100;

private final World world;
private final ConfigHandler config;
private final int skipPercentage;
private final TCLogger logger;
private final NightSkipScheduler skipScheduler;

public NightSkipper(final TimeControl plugin, final World world) {
this.world = world;
this.config = plugin.getConfigHandler();
this.skipPercentage = getSkipPercentage(plugin.getConfigHandler());
this.logger = plugin.getTCLogger();
this.skipScheduler = createSkipScheduler(plugin, world);
}

private int getSkipPercentage() {
public void scheduleSkip() {
if (skipScheduler == null || skipThresholdMet(false)) {
return;
}

skipScheduler.scheduleSkip();
logger.debug("Scheduled night skip for world \"%s\".", world.getName());
}

private void cancelScheduledSkip() {
if (skipScheduler != null) {
skipScheduler.cancel();
}
}

private NightSkipScheduler createSkipScheduler(final TimeControl plugin, final World world) {
if (!world.isGameRule("playersSleepingPercentage")) {
// In older versions, players aren't going to leave their beds by themselves,
// so we have to manually schedule the skip.
return new NightSkipScheduler(plugin, this, SKIPPABLE_SLEEP_TICKS + 1);
}

return null;
}

private int getSkipPercentage(final ConfigHandler config) {
if (!config.isPercentageEnabled(world)) {
try {
return world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
} catch (NoSuchFieldError e) {
logger.warn("Could not fetch game-rule value 'playersSleepingPercentage!"
logger.warn("Failed to read game rule 'playersSleepingPercentage!"
+ " Please enable players-sleeping-percentage in the plugin configuration.");
logger.warn("Using fallback percentage of %d %%.", SKIP_PERCENTAGE_FALLBACK);

Expand All @@ -44,33 +71,32 @@ private int getSkipPercentage() {
return config.getConfigPercentage(world);
}

private boolean skipThresholdMet() {
final int skipPercentage = getSkipPercentage();

private boolean skipThresholdMet(final boolean onlyFullyRested) {
if (skipPercentage <= 0) {
return true;
} else if (skipPercentage > 100) {
return false;
}

final int sleepTickThreshold = onlyFullyRested ? SKIPPABLE_SLEEP_TICKS : 1;
final List<Player> players = world.getPlayers();
final int sleeping = (int) players.stream()
.filter((player) -> player.getSleepTicks() >= SKIPPABLE_SLEEP_TICKS).count();
.filter((player) -> player.getSleepTicks() >= sleepTickThreshold).count();
final float sleepingPercentage = ((float) sleeping / players.size()) * 100;

return sleepingPercentage >= skipPercentage;
}

private boolean shouldSkipNight() {
public void skipNight() {
if (!TimeUtil.sleepAllowed(world)) {
return false;
return;
}

final boolean thresholdMet = skipThresholdMet();

return thresholdMet;
}
if (!skipThresholdMet(true)) {
if (!skipThresholdMet(false)) {
cancelScheduledSkip();
}

public void skipNight() {
if (!shouldSkipNight()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: ${project.name}
main: net.pupskuchen.timecontrol.TimeControl
version: ${project.version}
api-version: 1.15
api-version: 1.14
author: pupskuchen
website: https://github.com/Pupskuchen/spigot-TimeControl
load: STARTUP

0 comments on commit 121a6cd

Please sign in to comment.