Skip to content

Commit

Permalink
Added ExprSpawnerEntityType
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGBLP committed Feb 1, 2025
1 parent 63fd758 commit 8a34689
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/main/java/it/jakegblp/lusk/api/BlockWrapper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package it.jakegblp.lusk.api;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.entity.EntityData;
import it.jakegblp.lusk.utils.Constants;
import org.bukkit.Material;
import org.bukkit.block.*;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.meta.BlockDataMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.spawner.Spawner;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.VoxelShape;
import org.jetbrains.annotations.Nullable;
Expand All @@ -20,8 +23,9 @@
import java.util.List;

import static ch.njol.skript.paperlib.PaperLib.isPaper;
import static it.jakegblp.lusk.utils.Constants.MINECRAFT_1_20_1;
import static it.jakegblp.lusk.utils.Constants.PAPER_HAS_1_18_2_EXTENDED_ENTITY_API;
import static it.jakegblp.lusk.utils.Constants.*;
import static it.jakegblp.lusk.utils.EntityUtils.toEntityData;
import static it.jakegblp.lusk.utils.EntityUtils.toEntityType;
import static it.jakegblp.lusk.utils.ItemUtils.getNullableItemStack;
import static it.jakegblp.lusk.utils.ItemUtils.getNullableItemType;
import static it.jakegblp.lusk.utils.LuskUtils.warning;
Expand Down Expand Up @@ -474,4 +478,38 @@ public Float getBlastResistance() {
if (material != null && material.isBlock()) return material.getBlastResistance();
return null;
}

@Nullable
@SuppressWarnings("UnstableApiUsage")
public EntityData<?> getSpawnerEntityType() {
BlockState blockState = getBlockState();
EntityType entityType;
if (blockState instanceof Spawner spawner)
entityType = spawner.getSpawnedType();
else if (MINECRAFT_1_21 && blockState instanceof TrialSpawner trialSpawner) {
if (trialSpawner.isOminous())
entityType = trialSpawner.getOminousConfiguration().getSpawnedType();
else
entityType = trialSpawner.getNormalConfiguration().getSpawnedType();
} else return null;
return toEntityData(entityType);
}

public void setSpawnerEntityType(@Nullable EntityData<?> entityData) {
setSpawnerEntityType(toEntityType(entityData));
}

@SuppressWarnings("UnstableApiUsage")
public void setSpawnerEntityType(@Nullable EntityType entityType) {
BlockState blockState = getBlockState();
if (blockState instanceof Spawner spawner)
spawner.setSpawnedType(entityType);
else if (MINECRAFT_1_21 && blockState instanceof TrialSpawner trialSpawner) {
if (trialSpawner.isOminous())
trialSpawner.getOminousConfiguration().setSpawnedType(entityType);
else
trialSpawner.getNormalConfiguration().setSpawnedType(entityType);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package it.jakegblp.lusk.elements.minecraft.blocks.spawner.expressions;

import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.entity.EntityData;
import it.jakegblp.lusk.api.BlockWrapper;
import it.jakegblp.lusk.api.skript.SimplerPropertyExpression;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.Nullable;

@Name("Spawner/Trial Spawner - Entity Type")
@Description("Gets the spawner entity type of the provided spawners or trial spawners (1.21+).\n**Works with items.**\nCan be set and reset (sets it to pig).")
@Examples("set spawner entity type of {_block} to zombie")
@Since("1.3.4-beta1")
public class ExprSpawnerEntityType extends SimplerPropertyExpression<Object, EntityData> {

static {
register(ExprSpawnerEntityType.class, EntityData.class, "spawner (entity|creature) type", "itemtypes/blocks/blockstates");
}

@Override
public @Nullable EntityData convert(Object from) {
return new BlockWrapper(from).getSpawnerEntityType();
}

@Override
public boolean allowSet() {
return true;
}

@Override
public boolean allowReset() {
return true;
}

@Override
public void set(Object from, EntityData to) {
new BlockWrapper(from).setSpawnerEntityType(to);
}

@Override
public void reset(Object from) {
new BlockWrapper(from).setSpawnerEntityType(EntityType.PIG);
}

@Override
protected String getPropertyName() {
return "spawner entity type";
}

@Override
public Class<? extends EntityData> getReturnType() {
return EntityData.class;
}
}
12 changes: 12 additions & 0 deletions src/main/java/it/jakegblp/lusk/utils/EntityUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.jakegblp.lusk.utils;

import ch.njol.skript.entity.EntityData;
import org.bukkit.entity.*;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
Expand Down Expand Up @@ -259,6 +260,17 @@ public static void setEntityEquipmentSlot(LivingEntity livingEntity, EquipmentSl
}
}
}

@Nullable
public static EntityData<?> toEntityData(EntityType entityType) {
return entityType == null ? null : ch.njol.skript.bukkitutil.EntityUtils.toSkriptEntityData(entityType);
}

@Nullable
public static EntityType toEntityType(EntityData<?> entityType) {
return entityType == null ? null : ch.njol.skript.bukkitutil.EntityUtils.toBukkitEntityType(entityType);
}

//todo: add can pickup items for pre 2.8
//todo: add isImmuneToZombification for hoglins and piglins
}
14 changes: 14 additions & 0 deletions src/test/scripts/expressions/ExprSpawnerEntityType.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test "ExprSpawnerEntityType":
set {_location} to location(10, 11, 10)
set block at {_location} to spawner
set {_spawner} to block at {_location}
assert spawner entity type of {_spawner} is pig with "spawner entity type of spawner at location %{_location}% is not pig (default)"
set spawner entity type of {_spawner} to zombie
assert spawner entity type of {_spawner} is zombie with "spawner entity type of spawner at location %{_location}% is not zombie"

server version >= 1.21.0

set block at {_location} to trial spawner
set {_spawner} to block at {_location}
set spawner entity type of {_spawner} to skeleton
assert spawner entity type of {_spawner} is zombie with "spawner entity type of spawner at location %{_location}% is not skeleton"

0 comments on commit 8a34689

Please sign in to comment.