Added time-lock flag.

Valid values are 0 to 24000 for absolute time, or +- any number for relative time.
Closes WORLDGUARD-3379.
This commit is contained in:
wizjany 2015-02-18 01:45:32 -05:00
parent c9d6166fcb
commit 99d042d260
3 changed files with 89 additions and 1 deletions

View File

@ -117,6 +117,7 @@ public final class DefaultFlag {
public static final BooleanFlag NOTIFY_LEAVE = new BooleanFlag("notify-leave");
public static final SetFlag<EntityType> DENY_SPAWN = new SetFlag<EntityType>("deny-spawn", new EntityTypeFlag(null));
public static final EnumFlag<GameMode> GAME_MODE = new EnumFlag<GameMode>("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
};

View File

@ -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);

View File

@ -0,0 +1,86 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
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<String> {
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;
}
}