From 2c60c2b2ba431d7f3e2e99e04eecbd6e55a371a9 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Mon, 26 Aug 2024 10:41:15 +0200 Subject: [PATCH 1/5] Add tag --- .../movecraft/craft/type/CraftType.java | 37 +++++++++++++++++++ .../movecraft/util/EntityUtil.java | 25 +++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index d0f66b287..e111bd929 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -39,14 +39,20 @@ import net.countercraft.movecraft.craft.type.transform.StringTransform; import net.countercraft.movecraft.craft.type.transform.Transform; import net.countercraft.movecraft.processing.MovecraftWorld; +import net.countercraft.movecraft.util.EntityUtil; import net.countercraft.movecraft.util.Pair; import net.countercraft.movecraft.util.Tags; import net.kyori.adventure.key.Key; import net.kyori.adventure.sound.Sound; import org.bukkit.*; +import org.bukkit.entity.Animals; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Monster; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.util.ArrayList; @@ -54,6 +60,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.function.BiFunction; @@ -155,6 +162,7 @@ final public class CraftType { public static final NamespacedKey CAN_HOVER = buildKey("can_hover"); public static final NamespacedKey CAN_HOVER_OVER_WATER = buildKey("can_hover_over_water"); public static final NamespacedKey MOVE_ENTITIES = buildKey("move_entities"); + public static final NamespacedKey MOVE_ENTITIES_LIST = buildKey("move_entities_list"); public static final NamespacedKey ONLY_MOVE_PLAYERS = buildKey("only_move_players"); public static final NamespacedKey USE_GRAVITY = buildKey("use_gravity"); public static final NamespacedKey HOVER_LIMIT = buildKey("hover_limit"); @@ -190,6 +198,7 @@ final public class CraftType { public static final NamespacedKey EXPLOSION_ARMING_TIME = buildKey("explosion_arming_time"); public static final NamespacedKey DIRECTIONAL_DEPENDENT_MATERIALS = buildKey("directional_dependent_materials"); public static final NamespacedKey ALLOW_INTERNAL_COLLISION_EXPLOSION = buildKey("allow_internal_collision_explosion"); + private static final Logger log = LoggerFactory.getLogger(CraftType.class); //endregion @Contract("_ -> new") @@ -495,6 +504,34 @@ public static void registerTypeValidator(Predicate validator, String registerProperty(new BooleanProperty("canHover", CAN_HOVER, type -> false)); registerProperty(new BooleanProperty("canHoverOverWater", CAN_HOVER_OVER_WATER, type -> true)); registerProperty(new BooleanProperty("moveEntities", MOVE_ENTITIES, type -> true)); + registerProperty(new ObjectPropertyImpl("moveEntitiesList", MOVE_ENTITIES_LIST, + (data, type, fileKey, namespacedKey) -> { + var entityStringList = data.getStringList(fileKey); + EnumSet entityList = EnumSet.noneOf(EntityType.class); + for (String entityString : entityStringList) { + String upper = entityString.toUpperCase(Locale.ROOT); + + // User wants a premade list + if(upper.startsWith("@")) { + + switch (upper.substring(1)) { + case "monsters" -> entityList.addAll(EntityUtil.getClassEntities(Monster.class)); + + case "animals" -> entityList.addAll(EntityUtil.getClassEntities(Animals.class)); + + default -> throw new TypeData.InvalidValueException("Value for " + fileKey + " is not a valid EntityType list"); + } + } + + try { + EntityType.valueOf(upper); + } catch (Exception e) { + throw new TypeData.InvalidValueException("Value for " + fileKey + " must be an EntityType"); + } + } + + return entityList; + }, type -> EnumSet.noneOf(EntityType.class))); registerProperty(new BooleanProperty("onlyMovePlayers", ONLY_MOVE_PLAYERS, type -> true)); registerProperty(new BooleanProperty("useGravity", USE_GRAVITY, type -> false)); registerProperty(new IntegerProperty("hoverLimit", HOVER_LIMIT, type -> 0)); diff --git a/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java b/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java new file mode 100644 index 000000000..31edb8a1b --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java @@ -0,0 +1,25 @@ +package net.countercraft.movecraft.util; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import java.util.EnumSet; + +public class EntityUtil { + public static EnumSet getClassEntities(Class clazz) { + var entityList = EnumSet.noneOf(EntityType.class); + + for(EntityType type : EntityType.values()) { + var entityClazz = type.getEntityClass(); + + if (entityClazz == null) + continue; + + if (entityClazz.isAssignableFrom(clazz)) { + entityList.add(type); + } + } + + return entityList; + } +} From d936f58f14015ea1d73ee0b6679280150a96798d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Mon, 26 Aug 2024 10:56:22 +0200 Subject: [PATCH 2/5] Apply --- .../movecraft/async/rotation/RotationTask.java | 8 +++++++- .../movecraft/async/translation/TranslationTask.java | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java index 327e9e86c..9676dcd32 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/rotation/RotationTask.java @@ -51,6 +51,7 @@ import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.InventoryView; +import java.util.EnumSet; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -270,7 +271,8 @@ private void rotateEntitiesOnCraft(Location tOP) { (oldHitBox.getMaxY() + oldHitBox.getMinY())/2.0, (oldHitBox.getMaxZ() + oldHitBox.getMinZ())/2.0); - List entityList = List.of(EntityType.PLAYER, EntityType.PRIMED_TNT); + EnumSet entityList = EnumSet.of(EntityType.PLAYER, EntityType.PRIMED_TNT); + EnumSet shouldBeProcessed = (EnumSet) craft.getType().getObjectProperty(CraftType.MOVE_ENTITIES_LIST); for(Entity entity : craft.getWorld().getNearbyEntities(midpoint, oldHitBox.getXLength() / 2.0 + 1, oldHitBox.getYLength() / 2.0 + 2, @@ -284,6 +286,10 @@ private void rotateEntitiesOnCraft(Location tOP) { continue; }// Player is onboard this craft + if(!shouldBeProcessed.contains(entity)) { + continue; + } + Location adjustedPLoc = entity.getLocation().subtract(tOP); double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation, diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java index d1d23459f..29923830c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/translation/TranslationTask.java @@ -360,6 +360,8 @@ private void preventsTorpedoRocketsPilots() { (oldHitBox.getMaxX() + oldHitBox.getMinX()) / 2.0, (oldHitBox.getMaxY() + oldHitBox.getMinY()) / 2.0, (oldHitBox.getMaxZ() + oldHitBox.getMinZ()) / 2.0); + + EnumSet shouldBeProcessed = (EnumSet) craft.getType().getObjectProperty(CraftType.MOVE_ENTITIES_LIST); for (Entity entity : craft.getWorld().getNearbyEntities(midpoint, oldHitBox.getXLength() / 2.0 + 1, oldHitBox.getYLength() / 2.0 + 2, @@ -385,6 +387,10 @@ private void preventsTorpedoRocketsPilots() { continue; } + if (!shouldBeProcessed.contains(entity)) { + continue; + } + CraftTeleportEntityEvent e = new CraftTeleportEntityEvent(craft, entity); Bukkit.getServer().getPluginManager().callEvent(e); if (e.isCancelled()) From 78b02e3e16740d75a77b0f2e6528c169fbe709d4 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Mon, 26 Aug 2024 13:47:56 +0200 Subject: [PATCH 3/5] Fix --- .../countercraft/movecraft/craft/type/CraftType.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index e111bd929..1710bb245 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -515,9 +515,15 @@ public static void registerTypeValidator(Predicate validator, String if(upper.startsWith("@")) { switch (upper.substring(1)) { - case "monsters" -> entityList.addAll(EntityUtil.getClassEntities(Monster.class)); - - case "animals" -> entityList.addAll(EntityUtil.getClassEntities(Animals.class)); + case "monsters" -> { + entityList.addAll(EntityUtil.getClassEntities(Monster.class)); + continue; + } + + case "animals" -> { + entityList.addAll(EntityUtil.getClassEntities(Animals.class)); + continue; + } default -> throw new TypeData.InvalidValueException("Value for " + fileKey + " is not a valid EntityType list"); } From a21f16fc3c974de35a58365c9ba4dce7b470fe3d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 30 Aug 2024 21:07:04 +0200 Subject: [PATCH 4/5] Create tag + add datapack tags --- .../movecraft/craft/type/CraftType.java | 30 +-------------- .../movecraft/util/EntityUtil.java | 25 ------------- .../net/countercraft/movecraft/util/Tags.java | 37 +++++++++++++++++++ .../minecraft/tags/entity_types/hostiles.json | 3 ++ 4 files changed, 41 insertions(+), 54 deletions(-) delete mode 100644 api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java create mode 100644 datapack/src/main/resources/movecraft-data/data/minecraft/tags/entity_types/hostiles.json diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index 1710bb245..1c8c9ede7 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -39,15 +39,12 @@ import net.countercraft.movecraft.craft.type.transform.StringTransform; import net.countercraft.movecraft.craft.type.transform.Transform; import net.countercraft.movecraft.processing.MovecraftWorld; -import net.countercraft.movecraft.util.EntityUtil; import net.countercraft.movecraft.util.Pair; import net.countercraft.movecraft.util.Tags; import net.kyori.adventure.key.Key; import net.kyori.adventure.sound.Sound; import org.bukkit.*; -import org.bukkit.entity.Animals; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Monster; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -60,7 +57,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.function.BiFunction; @@ -509,31 +505,7 @@ public static void registerTypeValidator(Predicate validator, String var entityStringList = data.getStringList(fileKey); EnumSet entityList = EnumSet.noneOf(EntityType.class); for (String entityString : entityStringList) { - String upper = entityString.toUpperCase(Locale.ROOT); - - // User wants a premade list - if(upper.startsWith("@")) { - - switch (upper.substring(1)) { - case "monsters" -> { - entityList.addAll(EntityUtil.getClassEntities(Monster.class)); - continue; - } - - case "animals" -> { - entityList.addAll(EntityUtil.getClassEntities(Animals.class)); - continue; - } - - default -> throw new TypeData.InvalidValueException("Value for " + fileKey + " is not a valid EntityType list"); - } - } - - try { - EntityType.valueOf(upper); - } catch (Exception e) { - throw new TypeData.InvalidValueException("Value for " + fileKey + " must be an EntityType"); - } + entityList.addAll(Tags.parseEntities(entityString)); } return entityList; diff --git a/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java b/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java deleted file mode 100644 index 31edb8a1b..000000000 --- a/api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.countercraft.movecraft.util; - -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; - -import java.util.EnumSet; - -public class EntityUtil { - public static EnumSet getClassEntities(Class clazz) { - var entityList = EnumSet.noneOf(EntityType.class); - - for(EntityType type : EntityType.values()) { - var entityClazz = type.getEntityClass(); - - if (entityClazz == null) - continue; - - if (entityClazz.isAssignableFrom(clazz)) { - entityList.add(type); - } - } - - return entityList; - } -} diff --git a/api/src/main/java/net/countercraft/movecraft/util/Tags.java b/api/src/main/java/net/countercraft/movecraft/util/Tags.java index 399ebb92a..ad4be51ae 100644 --- a/api/src/main/java/net/countercraft/movecraft/util/Tags.java +++ b/api/src/main/java/net/countercraft/movecraft/util/Tags.java @@ -5,6 +5,7 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Tag; +import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -135,4 +136,40 @@ public static EnumSet parseMaterials(@NotNull String materialName) { } return returnSet; } + + @Nullable + public static EnumSet parseEntityRegistry(@NotNull String string) { + if (!string.startsWith("#")) + return null; + + String nameKey = string.substring(1); + var key = keyFromString(nameKey); + if (key == null) + throw new IllegalArgumentException("Entry " + string + " is not a valid namespace key!"); + + var tag = Bukkit.getTag(Tag.REGISTRY_ENTITY_TYPES, key, EntityType.class); + if (tag == null) + throw new IllegalArgumentException("Entry " + string + " is not a valid tag!"); + + var tags = tag.getValues(); + return tags.isEmpty() ? EnumSet.noneOf(EntityType.class) : EnumSet.copyOf(tags); + } + + /** + * Searches for a tag which matches the provided entityName. Failing that, it attempts to load a matching singular EntityType directly. + * + * @param entityName EntityType name or tag + * @return the set of EntityType the tag/EntityType resolves to + */ + @NotNull + public static EnumSet parseEntities(@NotNull String entityName) { + EnumSet returnSet = EnumSet.noneOf(EntityType.class); + EnumSet tagged = parseEntityRegistry(entityName); + if (tagged != null) { + returnSet.addAll(tagged); + } else { + returnSet.add(EntityType.valueOf(entityName.toUpperCase())); + } + return returnSet; + } } diff --git a/datapack/src/main/resources/movecraft-data/data/minecraft/tags/entity_types/hostiles.json b/datapack/src/main/resources/movecraft-data/data/minecraft/tags/entity_types/hostiles.json new file mode 100644 index 000000000..213b941da --- /dev/null +++ b/datapack/src/main/resources/movecraft-data/data/minecraft/tags/entity_types/hostiles.json @@ -0,0 +1,3 @@ +{ + "values": ["elder_guardian", "wither_skeleton", "stray", "husk", "zombie_villager", "evoker", "vex", "vindicator", "illusioner", "creeper", "skeleton", "spider", "giant", "zombie", "zombified_piglin", "enderman", "cave_spider", "silverfish", "blaze", "wither", "witch", "endermite", "guardian", "drowned", "pillager", "ravager", "piglin", "zoglin", "piglin_brute", "warden", "breeze", "bogged"] +} \ No newline at end of file From d2a7a0a9c60fc24f4729610ddf0fa444f63a0978 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 6 Sep 2024 15:08:42 +0200 Subject: [PATCH 5/5] Remove logging --- .../java/net/countercraft/movecraft/craft/type/CraftType.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index 1c8c9ede7..06b243101 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -48,8 +48,6 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.File; import java.util.ArrayList; @@ -194,7 +192,6 @@ final public class CraftType { public static final NamespacedKey EXPLOSION_ARMING_TIME = buildKey("explosion_arming_time"); public static final NamespacedKey DIRECTIONAL_DEPENDENT_MATERIALS = buildKey("directional_dependent_materials"); public static final NamespacedKey ALLOW_INTERNAL_COLLISION_EXPLOSION = buildKey("allow_internal_collision_explosion"); - private static final Logger log = LoggerFactory.getLogger(CraftType.class); //endregion @Contract("_ -> new")