diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java index 75cd42cb..bf7f3c15 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java @@ -117,6 +117,7 @@ public final class DefaultFlag { public static final BooleanFlag NOTIFY_LEAVE = new BooleanFlag("notify-leave"); public static final SetFlag DENY_SPAWN = new SetFlag("deny-spawn", new EntityTypeFlag(null)); public static final EnumFlag GAME_MODE = new EnumFlag("game-mode", GameMode.class); + public static final StringFlag TIME_LOCK = new StringFlag("time-lock"); public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay"); public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount"); public static final DoubleFlag MIN_HEAL = new DoubleFlag("heal-min-health"); @@ -149,7 +150,7 @@ public final class DefaultFlag { SNOW_FALL, SNOW_MELT, ICE_FORM, ICE_MELT, SOIL_DRY, GAME_MODE, MUSHROOMS, LEAF_DECAY, GRASS_SPREAD, MYCELIUM_SPREAD, VINE_GROWTH, SEND_CHAT, RECEIVE_CHAT, FIRE_SPREAD, LAVA_FIRE, LAVA_FLOW, WATER_FLOW, - TELE_LOC, SPAWN_LOC, POTION_SPLASH, + TELE_LOC, SPAWN_LOC, POTION_SPLASH, TIME_LOCK, BLOCKED_CMDS, ALLOWED_CMDS, PRICE, BUYABLE, ENABLE_SHOP }; diff --git a/src/main/java/com/sk89q/worldguard/session/SessionManager.java b/src/main/java/com/sk89q/worldguard/session/SessionManager.java index 5a204b27..29ac56a1 100644 --- a/src/main/java/com/sk89q/worldguard/session/SessionManager.java +++ b/src/main/java/com/sk89q/worldguard/session/SessionManager.java @@ -147,6 +147,7 @@ private Session createSession(Player player) { session.register(new FarewellFlag(session)); session.register(new GameModeFlag(session)); session.register(new InvincibilityFlag(session)); + session.register(new TimeLockFlag(session)); session.register(new GodMode(session)); session.register(new WaterBreathing(session)); session.initialize(player); diff --git a/src/main/java/com/sk89q/worldguard/session/handler/TimeLockFlag.java b/src/main/java/com/sk89q/worldguard/session/handler/TimeLockFlag.java new file mode 100644 index 00000000..90801b0f --- /dev/null +++ b/src/main/java/com/sk89q/worldguard/session/handler/TimeLockFlag.java @@ -0,0 +1,86 @@ +/* + * WorldGuard, a suite of tools for Minecraft + * Copyright (C) sk89q + * Copyright (C) WorldGuard team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldguard.session.handler; + +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.DefaultFlag; +import com.sk89q.worldguard.session.MoveType; +import com.sk89q.worldguard.session.Session; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import javax.annotation.Nullable; +import java.util.regex.Pattern; + +public class TimeLockFlag extends FlagValueChangeHandler { + + private Long time; + private boolean relative; + private Long initialTime; + private boolean initialRelative; + + Pattern timePattern = Pattern.compile("(\\+|-)?\\d+"); + + public TimeLockFlag(Session session) { + super(session, DefaultFlag.TIME_LOCK); + } + + private void updatePlayerTime(Player player, @Nullable String value) { + // store settings, regardless of if we change anything + initialRelative = player.isPlayerTimeRelative(); + initialTime = player.getPlayerTime(); + if (value == null || !timePattern.matcher(value).matches()) { + // invalid input + return; + } + if (value.startsWith("+") || value.startsWith("-")) { + relative = true; + } else { + relative = false; + } + time = Long.valueOf(value); + if (!relative && (time < 0L || time > 24000L)) { // invalid time, reset to 0 + time = 0L; + } + player.setPlayerTime(time, relative); + } + + @Override + protected void onInitialValue(Player player, ApplicableRegionSet set, String value) { + initialTime = player.getPlayerTime(); + initialRelative = player.isPlayerTimeRelative(); + updatePlayerTime(player, value); + } + + @Override + protected boolean onSetValue(Player player, Location from, Location to, ApplicableRegionSet toSet, String currentValue, String lastValue, MoveType moveType) { + updatePlayerTime(player, currentValue); + return true; + } + + @Override + protected boolean onAbsentValue(Player player, Location from, Location to, ApplicableRegionSet toSet, String lastValue, MoveType moveType) { + player.setPlayerTime(initialTime, initialRelative); + initialRelative = true; + initialTime = 0L; + return true; + } + +}