Fixed deny-spawn.

This commit is contained in:
sk89q 2011-06-25 23:27:01 -07:00
parent b7dd63ebd9
commit b5088340fd
5 changed files with 247 additions and 25 deletions

View File

@ -41,6 +41,7 @@
import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -496,13 +497,11 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
} }
WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld()); WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld());
//CreatureType creaType = (CreatureType) CreatureType.valueOf(event.getMobType().toString());
CreatureType creaType = event.getCreatureType(); CreatureType creaType = event.getCreatureType();
Boolean cancelEvent = false;
if (wcfg.blockCreatureSpawn.contains(creaType)) { if (wcfg.blockCreatureSpawn.contains(creaType)) {
cancelEvent = true; event.setCancelled(true);
return;
} }
Location eventLoc = event.getLocation(); Location eventLoc = event.getLocation();
@ -513,29 +512,15 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
ApplicableRegionSet set = mgr.getApplicableRegions(pt); ApplicableRegionSet set = mgr.getApplicableRegions(pt);
if (!set.allows(DefaultFlag.MOB_SPAWNING)) { if (!set.allows(DefaultFlag.MOB_SPAWNING)) {
cancelEvent = true; event.setCancelled(true);
return;
} }
}
// TODO: Monsters and stuff
/*
if (wcfg.useRegions) {
Vector pt = toVector(event.getEntity().getLocation());
RegionManager mgr = plugin.getGlobalRegionManager().get(event.getEntity().getWorld().getName());
Boolean flagValue = mgr.getApplicableRegions(pt).getFlag(DefaultFlag.DENY_SPAWN).getValue("").contains(creaType.getName()); Set<CreatureType> blockTypes = set.getFlag(DefaultFlag.DENY_SPAWN);
if (flagValue != null) { if (blockTypes != null && blockTypes.contains(creaType)) {
if (flagValue) { event.setCancelled(true);
cancelEvent = true; return;
} else {
cancelEvent = false;
}
} }
}*/
if (cancelEvent) {
event.setCancelled(true);
return;
} }
} }

View File

@ -0,0 +1,56 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection.flags;
import org.bukkit.command.CommandSender;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import org.bukkit.entity.CreatureType;
/**
* Represents a creature type.
*
* @author sk89q
*/
public class CreatureTypeFlag extends EnumFlag<CreatureType> {
public CreatureTypeFlag(String name, char legacyCode) {
super(name, legacyCode, CreatureType.class);
}
public CreatureTypeFlag(String name) {
super(name, CreatureType.class);
}
@Override
public CreatureType detectValue(String input) {
CreatureType lowMatch = null;
for (CreatureType type : CreatureType.values()) {
if (type.name().equalsIgnoreCase(input.trim())) {
return type;
}
if (type.name().toLowerCase().startsWith(input.toLowerCase().trim())) {
lowMatch = type;
}
}
return lowMatch;
}
}

View File

@ -19,6 +19,10 @@
package com.sk89q.worldguard.protection.flags; package com.sk89q.worldguard.protection.flags;
import org.bukkit.entity.CreatureType;
import java.util.Set;
/** /**
* *
* @author sk89q * @author sk89q
@ -49,7 +53,7 @@ public final class DefaultFlag {
public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell"); public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell");
public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter"); public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter");
public static final BooleanFlag NOTIFY_LEAVE = new BooleanFlag("notify-leave"); public static final BooleanFlag NOTIFY_LEAVE = new BooleanFlag("notify-leave");
public static final StringFlag DENY_SPAWN = new StringFlag("deny-spawn"); public static final SetFlag<CreatureType> DENY_SPAWN = new SetFlag<CreatureType>("deny-spawn", new CreatureTypeFlag(null));
public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay"); public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay");
public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount"); public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount");
public static final VectorFlag TELE_LOC = new VectorFlag("teleport"); public static final VectorFlag TELE_LOC = new VectorFlag("teleport");

View File

@ -0,0 +1,91 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection.flags;
import org.bukkit.command.CommandSender;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
/**
*
* @author sk89q
*/
public class EnumFlag<T extends Enum<T>> extends Flag<T> {
private Class<T> enumClass;
public EnumFlag(String name, char legacyCode, Class<T> enumClass) {
super(name, legacyCode);
this.enumClass = enumClass;
}
public EnumFlag(String name, Class<T> enumClass) {
super(name);
this.enumClass = enumClass;
}
private T findValue(String input) throws IllegalArgumentException {
try {
return Enum.valueOf(enumClass, input);
} catch (IllegalArgumentException e) {
T val = detectValue(input);
if (val != null) {
return val;
}
throw e;
}
}
/**
* Fuzzy detect the value if the value is not found.
*
* @param input string input
* @return value or null
*/
public T detectValue(String input) {
return null;
}
@Override
public T parseInput(WorldGuardPlugin plugin, CommandSender sender,
String input) throws InvalidFlagFormat {
try {
return findValue(input);
} catch (IllegalArgumentException e) {
throw new InvalidFlagFormat("Unknown value '" + input + "' in "
+ enumClass.getName());
}
}
@Override
public T unmarshal(Object o) {
try {
return Enum.valueOf(enumClass, String.valueOf(o));
} catch (IllegalArgumentException e) {
return null;
}
}
@Override
public Object marshal(T o) {
return o.name();
}
}

View File

@ -0,0 +1,86 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection.flags;
import org.bukkit.command.CommandSender;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import java.util.*;
/**
* Represents a flag that consists of a set.
*
* @author sk89q
*/
public class SetFlag<T> extends Flag<Set<T>> {
private Flag<T> subFlag;
public SetFlag(String name, char legacyCode, Flag<T> subFlag) {
super(name, legacyCode);
this.subFlag = subFlag;
}
public SetFlag(String name, Flag<T> subFlag) {
super(name);
this.subFlag = subFlag;
}
@Override
public Set<T> parseInput(WorldGuardPlugin plugin, CommandSender sender,
String input) throws InvalidFlagFormat {
Set<T> items = new HashSet<T>();
for (String str : input.split(",")) {
items.add(subFlag.parseInput(plugin, sender, str.trim()));
}
return new HashSet<T>(items);
}
@Override
public Set<T> unmarshal(Object o) {
if (o instanceof Collection) {
Collection collection = (Collection) o;
Set<T> items = new HashSet<T>();
for (Object sub : collection) {
T item = subFlag.unmarshal(sub);
if (item != null) {
items.add(item);
}
}
return items;
} else {
return null;
}
}
@Override
public Object marshal(Set<T> o) {
List<Object> list = new ArrayList<Object>();
for (T item : o) {
list.add(subFlag.marshal(item));
}
return list;
}
}