Skip to content

Commit

Permalink
LocationTag.spawner_display_entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 20, 2021
1 parent 30a05ec commit c5cee10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Expand Up @@ -2,12 +2,14 @@

import com.denizenscript.denizen.nms.util.BoundingBox;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.*;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
Expand Down Expand Up @@ -367,4 +369,8 @@ public void setLastHurtBy(LivingEntity mob, LivingEntity damager) {
public void setFallingBlockType(FallingBlock entity, BlockData block) {
throw new UnsupportedOperationException();
}

public EntityTag getMobSpawnerDisplayEntity(CreatureSpawner spawner) {
throw new UnsupportedOperationException();
}
}
Expand Up @@ -1267,12 +1267,24 @@ public static void registerTags() {
// Returns the type of entity spawned by a mob spawner.
// -->
registerTag("spawner_type", (attribute, object) -> {
if (object.getBlockStateForTag(attribute) instanceof CreatureSpawner) {
return new EntityTag(DenizenEntityType.getByName(((CreatureSpawner) object.getBlockStateForTag(attribute)).getSpawnedType().name()));
if (!(object.getBlockStateForTag(attribute) instanceof CreatureSpawner)) {
return null;
}
else {
return new EntityTag(DenizenEntityType.getByName(((CreatureSpawner) object.getBlockStateForTag(attribute)).getSpawnedType().name()));
});

// <--[tag]
// @attribute <LocationTag.spawner_display_entity>
// @mechanism LocationTag.spawner_display_entity
// @returns EntityTag
// @description
// Returns the full "display entity" for the spawner. This can contain more data than just a type.
// -->
registerTag("spawner_display_entity", (attribute, object) -> {
if (!(object.getBlockStateForTag(attribute) instanceof CreatureSpawner)) {
return null;
}
return NMSHandler.getEntityHelper().getMobSpawnerDisplayEntity(((CreatureSpawner) object.getBlockStateForTag(attribute))).describe(attribute.context);
});

// <--[tag]
Expand Down
Expand Up @@ -81,7 +81,7 @@ public List<Location> getBlocksList(PortalCreateEvent event) {
return blocks;
}

public <T extends BlockEntity> T getTE(CraftBlockEntityState<T> cbs) {
public static <T extends BlockEntity> T getTE(CraftBlockEntityState<T> cbs) {
try {
return (T) craftBlockEntityState_tileEntity.get(cbs);
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.v1_17.ReflectionMappingsInfo;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizen.nms.v1_17.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.nms.interfaces.EntityHelper;
Expand Down Expand Up @@ -34,6 +35,7 @@
import net.minecraft.world.entity.monster.EnderMan;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.phys.AABB;
Expand All @@ -46,9 +48,11 @@
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.BlockFace;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftCreatureSpawner;
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_17_R1.entity.*;
import org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory;
Expand Down Expand Up @@ -822,4 +826,11 @@ public void setFallingBlockType(FallingBlock entity, BlockData block) {
Debug.echoError(ex);
}
}

@Override
public EntityTag getMobSpawnerDisplayEntity(CreatureSpawner spawner) {
SpawnerBlockEntity nmsSpawner = BlockHelperImpl.getTE((CraftCreatureSpawner) spawner);
net.minecraft.world.entity.Entity nmsEntity = nmsSpawner.getSpawner().getOrCreateDisplayEntity(((CraftWorld) spawner.getWorld()).getHandle());
return new EntityTag(nmsEntity.getBukkitEntity());
}
}

0 comments on commit c5cee10

Please sign in to comment.