Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update for nullable spawner types,
editMeta
util (#2514)
* Add `ItemProperty#editMeta` util * Update & cleanup `ItemTag.spawner_type` * Cleanup `ItemInstrument` and use `editMeta` * Minor cleanup * Support unsetting the instrument * Minor cleanup * `spawnerType` -> `spawnedType` * Update `LocationTag` ones, make it 1.20+ * Correct no mech input check * Use a direct cast * Remove workaround for now fixed Spigot bug
- Loading branch information
Showing
4 changed files
with
79 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 30 additions & 75 deletions
105
plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemSpawnerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,55 @@ | ||
package com.denizenscript.denizen.objects.properties.item; | ||
|
||
import com.denizenscript.denizen.nms.NMSHandler; | ||
import com.denizenscript.denizen.nms.NMSVersion; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.ItemTag; | ||
import com.denizenscript.denizencore.objects.Mechanism; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.objects.properties.Property; | ||
import com.denizenscript.denizencore.tags.Attribute; | ||
import org.bukkit.block.CreatureSpawner; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.inventory.meta.BlockStateMeta; | ||
|
||
public class ItemSpawnerType implements Property { | ||
public class ItemSpawnerType extends ItemProperty<EntityTag> { | ||
|
||
public static boolean describes(ObjectTag item) { | ||
return item instanceof ItemTag | ||
&& ((ItemTag) item).getItemMeta() instanceof BlockStateMeta | ||
&& ((BlockStateMeta) ((ItemTag) item).getItemMeta()).getBlockState() instanceof CreatureSpawner; | ||
} | ||
// <--[property] | ||
// @object ItemTag | ||
// @name spawner_type | ||
// @input EntityTag | ||
// @description | ||
// The entity type a spawner item will spawn, if any. | ||
// For the mechanism: provide no input to unset the type. | ||
// Note that the type can only be unset on 1.20 and above. | ||
// --> | ||
|
||
public static ItemSpawnerType getFrom(ObjectTag _item) { | ||
if (!describes(_item)) { | ||
return null; | ||
} | ||
else { | ||
return new ItemSpawnerType((ItemTag) _item); | ||
} | ||
public static boolean describes(ItemTag item) { | ||
return item.getItemMeta() instanceof BlockStateMeta meta && meta.getBlockState() instanceof CreatureSpawner; | ||
} | ||
|
||
public static final String[] handledTags = new String[] { | ||
"spawner_type" | ||
}; | ||
|
||
public static final String[] handledMechs = new String[] { | ||
"spawner_type" | ||
}; | ||
|
||
public ItemSpawnerType(ItemTag _item) { | ||
item = _item; | ||
} | ||
|
||
ItemTag item; | ||
|
||
@Override | ||
public ObjectTag getObjectAttribute(Attribute attribute) { | ||
|
||
if (attribute == null) { | ||
return null; | ||
} | ||
|
||
// <--[tag] | ||
// @attribute <ItemTag.spawner_type> | ||
// @returns EntityTag | ||
// @mechanism ItemTag.spawner_type | ||
// @group properties | ||
// @description | ||
// Returns the spawn type for a spawner block item. | ||
// --> | ||
if (attribute.startsWith("spawner_type")) { | ||
BlockStateMeta meta = (BlockStateMeta) item.getItemMeta(); | ||
CreatureSpawner state = (CreatureSpawner) meta.getBlockState(); | ||
return new EntityTag(state.getSpawnedType()) | ||
.getObjectAttribute(attribute.fulfill(1)); | ||
} | ||
|
||
return null; | ||
public EntityTag getPropertyValue() { | ||
EntityType spawnedType = ((CreatureSpawner) ((BlockStateMeta) getItemMeta()).getBlockState()).getSpawnedType(); | ||
return spawnedType != null ? new EntityTag(spawnedType) : null; | ||
} | ||
|
||
@Override | ||
public String getPropertyString() { | ||
BlockStateMeta meta = (BlockStateMeta) item.getItemMeta(); | ||
CreatureSpawner state = (CreatureSpawner) meta.getBlockState(); | ||
return state.getSpawnedType().name(); | ||
public void setPropertyValue(EntityTag entity, Mechanism mechanism) { | ||
if (entity == null && NMSHandler.getVersion().isAtMost(NMSVersion.v1_19)) { | ||
mechanism.echoError("must have input of type 'EntityTag', but none was given."); | ||
return; | ||
} | ||
editMeta(BlockStateMeta.class, meta -> { | ||
CreatureSpawner spawner = (CreatureSpawner) meta.getBlockState(); | ||
spawner.setSpawnedType(entity != null ? entity.getBukkitEntityType() : null); | ||
meta.setBlockState(spawner); | ||
}); | ||
} | ||
|
||
@Override | ||
public String getPropertyId() { | ||
return "spawner_type"; | ||
} | ||
|
||
@Override | ||
public void adjust(Mechanism mechanism) { | ||
|
||
// <--[mechanism] | ||
// @object ItemTag | ||
// @name spawner_type | ||
// @input EntityTag | ||
// @description | ||
// Sets the spawn type of a spawner block item. | ||
// @tags | ||
// <ItemTag.spawner_type> | ||
// --> | ||
if (mechanism.matches("spawner_type") && mechanism.requireObject(EntityTag.class)) { | ||
BlockStateMeta meta = (BlockStateMeta) item.getItemMeta(); | ||
CreatureSpawner state = (CreatureSpawner) meta.getBlockState(); | ||
state.setSpawnedType(mechanism.valueAsType(EntityTag.class).getBukkitEntityType()); | ||
meta.setBlockState(state); | ||
item.setItemMeta(meta); | ||
} | ||
public static void register() { | ||
autoRegisterNullable("spawner_type", ItemSpawnerType.class, EntityTag.class, false); | ||
} | ||
} |