Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the entity remover and increase readability #2571

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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<EntityProperties> 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<EntityProperties> 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();
}
}
}
Loading