diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/EntityRemover.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/EntityRemover.java index f2310a211f..bd8d4bc7cb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/EntityRemover.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/EntityRemover.java @@ -24,6 +24,7 @@ import com.sk89q.worldedit.function.EntityFunction; import javax.annotation.Nullable; +import java.util.function.Predicate; import java.util.regex.Pattern; import static com.google.common.base.Preconditions.checkNotNull; @@ -33,95 +34,28 @@ */ public class EntityRemover { - public enum Type { - ALL("all") { - @Override - boolean matches(EntityProperties type) { - for (Type value : values()) { - if (value != this && value.matches(type)) { - return true; - } - } - return false; - } - }, - PROJECTILES("projectiles?|arrows?") { - @Override - boolean matches(EntityProperties type) { - return type.isProjectile(); - } - }, - ITEMS("items?|drops?") { - @Override - boolean matches(EntityProperties type) { - return type.isItem(); - } - }, - FALLING_BLOCKS("falling(blocks?|sand|gravel)") { - @Override - boolean matches(EntityProperties type) { - return type.isFallingBlock(); - } - }, - PAINTINGS("paintings?|art") { - @Override - boolean matches(EntityProperties type) { - return type.isPainting(); - } - }, - ITEM_FRAMES("(item)frames?") { - @Override - boolean matches(EntityProperties type) { - return type.isItemFrame(); - } - }, - BOATS("boats?") { - @Override - boolean matches(EntityProperties type) { - return type.isBoat(); - } - }, - MINECARTS("(mine)?carts?") { - @Override - boolean matches(EntityProperties type) { - return type.isMinecart(); - } - }, - TNT("tnt") { - @Override - boolean matches(EntityProperties type) { - return type.isTNT(); - } - }, - XP_ORBS("xp") { - @Override - boolean matches(EntityProperties type) { - return type.isExperienceOrb(); - } - }; - - private final Pattern pattern; - - Type(String pattern) { - this.pattern = Pattern.compile(pattern); - } - - public boolean matches(String str) { - return pattern.matcher(str).matches(); - } + private final Type type; - abstract boolean matches(EntityProperties type); + private EntityRemover(Type type) { + this.type = type; + } - @Nullable - public static Type findByPattern(String str) { - for (Type type : values()) { - if (type.matches(str)) { - return type; + public EntityFunction createFunction() { + final Type type = this.type; + checkNotNull(type, "type can't be null"); + return entity -> { + EntityProperties registryType = entity.getFacet(EntityProperties.class); + if (registryType != null) { + if (type.matcher.test(registryType)) { + //FAWE start - Calling this async violates thread safety + TaskManager.taskManager().sync(entity::remove); + //FAWE end + return true; } } - return null; - } + return false; + }; } public static EntityRemover fromString(String str) { @@ -134,28 +68,49 @@ public static EntityRemover fromString(String str) { } } - private final Type type; + public enum Type { + ALL("all", Type::isAll), + PROJECTILES("projectiles?|arrows?", EntityProperties::isProjectile), + ITEMS("items?|drops?", EntityProperties::isItem), + FALLING_BLOCKS("falling(blocks?|sand|gravel)", EntityProperties::isFallingBlock), + PAINTINGS("paintings?|art", EntityProperties::isPainting), + ITEM_FRAMES("(item)frames?", EntityProperties::isItemFrame), + BOATS("boats?", EntityProperties::isBoat), + MINECARTS("(mine)?carts?", EntityProperties::isMinecart), + TNT("tnt", EntityProperties::isTNT), + XP_ORBS("xp", EntityProperties::isExperienceOrb); - private EntityRemover(Type type) { - this.type = type; - } + private final Pattern pattern; + private final Predicate matcher; - public EntityFunction createFunction() { - final Type type = this.type; - checkNotNull(type, "type can't be null"); - return entity -> { - EntityProperties registryType = entity.getFacet(EntityProperties.class); - if (registryType != null) { - if (type.matches(registryType)) { - //FAWE start - Calling this async violates thread safety - TaskManager.taskManager().sync(entity::remove); - //FAWE end - return true; + private static final Type[] VALUES; + + Type(String pattern, final Predicate matcher) { + this.pattern = Pattern.compile(pattern); + this.matcher = matcher; + } + @Nullable + public static Type findByPattern(String str) { + for (Type type : Type.VALUES) { + if (type.pattern.matcher(str).matches()) { + return type; } } + return null; + } + + private static boolean isAll(EntityProperties type) { + for (Type value : Type.VALUES) { + if (value.matcher.test(type)) { + return true; + } + } return false; - }; - } + } + static { + VALUES = values(); + } + } }