diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index 6acf2d9be75..0c3d40eb81d 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1556,6 +1556,7 @@ public String toVariableNameString(EnchantmentOffer eo) { .since("INSERT VERSION")); if (Skript.classExists("org.bukkit.entity.EntitySnapshot")) { + boolean SUPPORTS_GET_AS = Skript.methodExists(EntitySnapshot.class, "getAsString"); Classes.registerClass(new ClassInfo<>(EntitySnapshot.class, "entitysnapshot") .user("entity ?snapshots?") .name("Entity Snapshot") @@ -1565,6 +1566,24 @@ public String toVariableNameString(EnchantmentOffer eo) { "Individual attributes of a snapshot cannot be modified or retrieved.") .requiredPlugins("Minecraft 1.20.2+") .since("INSERT VERSION") + .parser(new Parser() { + @Override + public boolean canParse(ParseContext context) { + return false; + } + + @Override + public String toString(EntitySnapshot o, int flags) { + if (SUPPORTS_GET_AS) + return o.getAsString(); + return o.getEntityType() + " snapshot"; + } + + @Override + public String toVariableNameString(EntitySnapshot o) { + return o.getEntityType() + " snapshot"; + } + }) ); } diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultConverters.java b/src/main/java/ch/njol/skript/classes/data/DefaultConverters.java index 9f0731eaa7c..1b221bdfe2e 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultConverters.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultConverters.java @@ -1,21 +1,14 @@ package ch.njol.skript.classes.data; import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.bukkitutil.EntityUtils; import ch.njol.skript.command.Commands; import ch.njol.skript.entity.EntityData; import ch.njol.skript.entity.EntityType; import ch.njol.skript.entity.XpOrbData; -import ch.njol.skript.util.BlockInventoryHolder; -import ch.njol.skript.util.BlockUtils; -import ch.njol.skript.util.Direction; -import ch.njol.skript.util.EnchantmentType; -import ch.njol.skript.util.Experience; +import ch.njol.skript.util.*; import ch.njol.skript.util.slot.Slot; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.DoubleChest; @@ -24,6 +17,7 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentOffer; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntitySnapshot; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -180,6 +174,8 @@ public DefaultConverters() {} Converters.registerConverter(String.class, World.class, Bukkit::getWorld); + Converters.registerConverter(EntitySnapshot.class, EntityData.class, snapshot -> EntityUtils.toSkriptEntityData(snapshot.getEntityType())); + // // Entity - String (UUID) // Very slow, thus disabled for now // Converters.registerConverter(String.class, Entity.class, new Converter() { // diff --git a/src/test/skript/tests/syntaxes/expressions/ExprEntitySnapshot.sk b/src/test/skript/tests/syntaxes/expressions/ExprEntitySnapshot.sk index ba444ee2ae9..fb37ab36f12 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprEntitySnapshot.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprEntitySnapshot.sk @@ -5,6 +5,8 @@ test "entity snapshots" when running minecraft "1.20.2": set {_snapshot} to entity snapshot of entity clear entity assert {_snapshot} is an entity snapshot with "Get entity snapshot is not an entity snapshot" + broadcast "EntityType: %entity type of {_snapshot}%" + assert type of {_snapshot} is a pig with "Entity type of entity snapshot should be a pig" spawn {_snapshot} at test-location: assert the max health of entity is 20 with "Max health did not stick to the spawned entity" assert the health of entity is 20 with "Health did not stick to the spawned entity"