From dd9450d36a2dfc090136b9b1941dde10df733677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 11 Apr 2020 22:26:31 +0200 Subject: [PATCH] Add entity category loading code to WorldUtil (implemented in BukkitUtil) --- .../plotsquared/bukkit/util/BukkitUtil.java | 52 +++++++++++++++++++ .../plotsquared/plot/util/WorldUtil.java | 4 ++ .../plot/util/entity/EntityCategories.java | 22 +++----- .../plot/util/entity/EntityCategory.java | 9 ++-- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java index 1b9f45de5..52bfb1645 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java @@ -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 getTypesInCategory(final String category) { + final Collection> 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 types = new HashSet<>(); + outer: for (final EntityType bukkitType : EntityType.values()) { + final Class 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 chunkConsumer) { PaperLib.getChunkAtAsync(getWorld(world), x >> 4, z >> 4, true).thenAccept(chunk -> ensureMainThread(chunkConsumer, chunk)); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/WorldUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/WorldUtil.java index 3ec924694..7924b9a1b 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/WorldUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/WorldUtil.java @@ -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 getTypesInCategory(final String category); + } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java index b55ff4296..0f0ae5748 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategories.java @@ -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; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategory.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategory.java index c1edf9217..f6cfa85f0 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategory.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/entity/EntityCategory.java @@ -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 implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("entity type"); - private final Set types; + private final String key; - protected EntityCategory(final String id, final Set types) { + protected EntityCategory(final String id) { super("plotsquared:" + id); - this.types = types; + this.key = id; } @Override protected Set load() { - return types; + return WorldUtil.IMP.getTypesInCategory(this.key); } }