Skip to content

Commit

Permalink
Fix + Update EntityMaterial
Browse files Browse the repository at this point in the history
ba0aa4b broke it by making the `instanceof` checks in `getBlockData` use the `EntityTag` object instead of the Bukkit entity object.
This fixes that, and updates it to modern property format.
Tested on `1.20.1` and `1.17.1`.
  • Loading branch information
tal5 committed Jul 2, 2023
1 parent 6372d35 commit a769245
Showing 1 changed file with 41 additions and 98 deletions.
Expand Up @@ -5,127 +5,70 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;

public class EntityMaterial implements Property {

public static boolean describes(ObjectTag object) {
if (!(object instanceof EntityTag entityTag)) {
return false;
}
Entity entity = entityTag.getBukkitEntity();
return entity instanceof Enderman
|| entity instanceof Minecart
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && entity instanceof BlockDisplay);
}

public static EntityMaterial getFrom(ObjectTag _entity) {
if (!describes(_entity)) {
return null;
}
else {
return new EntityMaterial((EntityTag) _entity);
}
}

public static final String[] handledMechs = new String[]{
"material"
};

public EntityMaterial(EntityTag _entity) {
entity = _entity;
}

EntityTag entity;

public static void register() {

// <--[tag]
// @attribute <EntityTag.material>
// @returns MaterialTag
// @mechanism EntityTag.material
// @group properties
// @description
// Returns the block material associated with the entity.
// For endermen, returns the material the enderman is holding.
// For minecarts, returns the material the minecart is carrying.
// For block displays, returns the displayed block.
// -->
PropertyParser.registerTag(EntityMaterial.class, MaterialTag.class, "material", (attribute, object) -> {
return object.getMaterial();
});
public class EntityMaterial extends EntityProperty<MaterialTag> {

// <--[property]
// @object EntityTag
// @name material
// @input MaterialTag
// @description
// An entity's associated block material.
// For endermen, this is the block being held.
// For minecarts, this is the block being carried.
// For block displays, this is the block being displayed.
// -->

public static boolean describes(EntityTag entity) {
return entity.getBukkitEntity() instanceof Enderman
|| entity.getBukkitEntity() instanceof Minecart
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && entity.getBukkitEntity() instanceof BlockDisplay);
}

public BlockData getBlockData() {
if (entity instanceof Enderman enderman) {
return enderman.getCarriedBlock();
@Override
public MaterialTag getPropertyValue() {
BlockData blockData = null;
if (getEntity() instanceof Enderman enderman) {
blockData = enderman.getCarriedBlock();
}
else if (entity instanceof Minecart minecart) {
return minecart.getDisplayBlockData();
else if (getEntity() instanceof Minecart minecart) {
blockData = minecart.getDisplayBlockData();
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && entity instanceof BlockDisplay blockDisplay) {
return blockDisplay.getBlock();
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19)) {
blockData = as(BlockDisplay.class).getBlock();
}
return null;
return blockData != null ? new MaterialTag(blockData) : new MaterialTag(Material.AIR);
}

public MaterialTag getMaterial() {
BlockData data = getBlockData();
if (data == null) {
return new MaterialTag(Material.AIR);
}
return new MaterialTag(data);
@Override
public boolean isDefaultValue(MaterialTag value) {
return value.getMaterial() == Material.AIR;
}

@Override
public String getPropertyString() {
MaterialTag material = getMaterial();
if (material.getMaterial() != Material.AIR) {
return material.identify();
public void setPropertyValue(MaterialTag value, Mechanism mechanism) {
if (getEntity() instanceof Enderman enderman) {
enderman.setCarriedBlock(value.getModernData());
}
else if (getEntity() instanceof Minecart minecart) {
minecart.setDisplayBlockData(value.getModernData());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19)) {
as(BlockDisplay.class).setBlock(value.getModernData());
}
return null;
}

@Override
public String getPropertyId() {
return "material";
}


@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
// @name material
// @input MaterialTag
// @description
// Sets the block material associated with the entity.
// For endermen, sets the material the enderman is holding.
// For minecarts, sets the material the minecart is carrying.
// @tags
// <EntityTag.material>
// -->
if (mechanism.matches("material") && mechanism.requireObject(MaterialTag.class)) {
BlockData data = mechanism.valueAsType(MaterialTag.class).getModernData();
Entity entity = this.entity.getBukkitEntity();
if (entity instanceof Enderman enderman) {
enderman.setCarriedBlock(data);
}
else if (entity instanceof Minecart minecart) {
minecart.setDisplayBlockData(data);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19) && entity instanceof BlockDisplay blockDisplay) {
blockDisplay.setBlock(data);
}
}
public static void register() {
autoRegister("material", EntityMaterial.class, MaterialTag.class, false);
}
}

0 comments on commit a769245

Please sign in to comment.