Add entity category loading code to WorldUtil (implemented in BukkitUtil)

This commit is contained in:
Alexander Söderberg 2020-04-11 22:26:31 +02:00
parent 8ef3a90ce0
commit dd9450d36a
4 changed files with 67 additions and 20 deletions

View file

@ -58,13 +58,23 @@ import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Boss;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Golem;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Vehicle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -543,6 +553,48 @@ public class BukkitUtil extends WorldUtil {
Bukkit.getPlayer(player.getUUID()).setFoodLevel(foodLevel);
}
@Override
public Set<com.sk89q.worldedit.world.entity.EntityType> getTypesInCategory(final String category) {
final Collection<Class<?>> allowedInterfaces = new HashSet<>();
switch (category) {
case "animal": {
allowedInterfaces.add(Golem.class);
allowedInterfaces.add(Animals.class);
} break;
case "tameable": {
allowedInterfaces.add(Tameable.class);
} break;
case "vehicle": {
allowedInterfaces.add(Vehicle.class);
} break;
case "hostile": {
allowedInterfaces.add(Monster.class);
allowedInterfaces.add(Boss.class);
allowedInterfaces.add(Slime.class);
} break;
case "hanging": {
allowedInterfaces.add(Hanging.class);
} break;
default: {
PlotSquared.log(Captions.PREFIX + "Unknown entity category requested: " + category);
} break;
}
final Set<com.sk89q.worldedit.world.entity.EntityType> types = new HashSet<>();
outer: for (final EntityType bukkitType : EntityType.values()) {
final Class<? extends Entity> entityClass = bukkitType.getEntityClass();
if (entityClass == null) {
continue;
}
for (final Class<?> allowedInterface : allowedInterfaces) {
if (allowedInterface.isAssignableFrom(entityClass)) {
types.add(BukkitAdapter.adapt(bukkitType));
continue outer;
}
}
}
return types;
}
private static void ensureLoaded(final String world, final int x, final int z, final Consumer<Chunk> chunkConsumer) {
PaperLib.getChunkAtAsync(getWorld(world), x >> 4, z >> 4, true).thenAccept(chunk ->
ensureMainThread(chunkConsumer, chunk));

View file

@ -39,6 +39,7 @@ import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
@ -215,4 +216,7 @@ public abstract class WorldUtil {
public abstract int getFoodLevel(PlotPlayer player);
public abstract void setFoodLevel(PlotPlayer player, int foodLevel);
public abstract Set<EntityType> getTypesInCategory(final String category);
}

View file

@ -25,29 +25,19 @@
*/
package com.github.intellectualsites.plotsquared.plot.util.entity;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.entity.EntityTypes;
import java.util.Arrays;
import java.util.HashSet;
/**
* A collection of {@link EntityCategory entity categories}
*/
public class EntityCategories {
public static final EntityCategory ANIMAL = register("animal");
public static final EntityCategory TAMEABLE = register("tameable",
EntityTypes.HORSE, EntityTypes.OCELOT, EntityTypes.WOLF, EntityTypes.DONKEY,
EntityTypes.MULE, EntityTypes.PARROT, EntityTypes.LLAMA);
public static final EntityCategory VEHICLE = register("vehicle");
public static final EntityCategory HOSTILE = register("hostile",
EntityTypes.ZOMBIE);
public static final EntityCategory HANGING = register("hanging",
EntityTypes.PAINTING, EntityTypes.ITEM_FRAME);
public static final EntityCategory TAMEABLE = register("tameable");
public static final EntityCategory VEHICLE = register("vehicle");
public static final EntityCategory HOSTILE = register("hostile");
public static final EntityCategory HANGING = register("hanging");
public static EntityCategory register(final String id, final EntityType ... types) {
final EntityCategory entityCategory = new EntityCategory(id, new HashSet<>(Arrays.asList(types)));
public static EntityCategory register(final String id) {
final EntityCategory entityCategory = new EntityCategory(id);
EntityCategory.REGISTRY.register(entityCategory.getId(), entityCategory);
return entityCategory;
}

View file

@ -25,6 +25,7 @@
*/
package com.github.intellectualsites.plotsquared.plot.util.entity;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
@ -39,15 +40,15 @@ public class EntityCategory extends Category<EntityType> implements Keyed {
public static final NamespacedRegistry<EntityCategory> REGISTRY = new NamespacedRegistry<>("entity type");
private final Set<EntityType> types;
private final String key;
protected EntityCategory(final String id, final Set<EntityType> types) {
protected EntityCategory(final String id) {
super("plotsquared:" + id);
this.types = types;
this.key = id;
}
@Override protected Set<EntityType> load() {
return types;
return WorldUtil.IMP.getTypesInCategory(this.key);
}
}