Skip to content

Commit

Permalink
add item spawner type property
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 6, 2019
1 parent 8525827 commit 5751a1e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
Expand Up @@ -3,7 +3,6 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
Expand Down Expand Up @@ -88,7 +87,7 @@ else if (name.equals("qualifier")) {
return new MaterialTag(event.getMaterial());
}
else if (statistic.getType() == Statistic.Type.ENTITY) {
return new EntityTag(DenizenEntityType.getByName(event.getEntityType().name()));
return new EntityTag(event.getEntityType());
}
}
return super.getContext(name);
Expand Down
Expand Up @@ -210,7 +210,7 @@ public static EntityTag valueOf(String string, TagContext context) {
randomType = EntityType.values()[CoreUtilities.getRandom().nextInt(EntityType.values().length)];
}

return new EntityTag(DenizenEntityType.getByName(randomType.name()), "RANDOM");
return new EntityTag(randomType, "RANDOM");
}

///////
Expand Down Expand Up @@ -407,7 +407,6 @@ public EntityTag(Entity entity) {
}
}

@Deprecated
public EntityTag(EntityType entityType) {
if (entityType != null) {
this.entity = null;
Expand All @@ -418,13 +417,11 @@ public EntityTag(EntityType entityType) {
}
}

@Deprecated
public EntityTag(EntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}

@Deprecated
public EntityTag(EntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
Expand All @@ -436,7 +433,6 @@ public EntityTag(EntityType entityType, String data1) {
}
}

@Deprecated
public EntityTag(EntityType entityType, String data1, String data2) {
if (entityType != null) {
this.entity = null;
Expand Down
Expand Up @@ -140,6 +140,7 @@ public static void registermainProperties() {
PropertyParser.registerProperty(ItemSpawnerMaxNearbyEntities.class, ItemTag.class);
PropertyParser.registerProperty(ItemSpawnerPlayerRange.class, ItemTag.class);
PropertyParser.registerProperty(ItemSpawnerRange.class, ItemTag.class);
PropertyParser.registerProperty(ItemSpawnerType.class, ItemTag.class);
PropertyParser.registerProperty(ItemUnbreakable.class, ItemTag.class);

// register core MaterialTag properties
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name spawner_count
// @input ListTag
// @input ElementTag(Number)
// @description
// Sets the spawn count of a spawner block item.
// @tags
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name spawner_max_nearby_entities
// @input ListTag
// @input ElementTag(Number)
// @description
// Sets the maximum nearby entities of a spawner block item.
// @tags
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name spawner_player_range
// @input ListTag
// @input ElementTag(Number)
// @description
// Sets the maximum player range of a spawner block item.
// @tags
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name spawner_range
// @input ListTag
// @input ElementTag(Number)
// @description
// Sets the spawn range of a spawner block item.
// @tags
Expand Down
@@ -0,0 +1,109 @@
package com.denizenscript.denizen.objects.properties.item;

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.inventory.meta.BlockStateMeta;

public class ItemSpawnerType implements Property {

public static boolean describes(ObjectTag item) {
return item instanceof ItemTag
&& ((ItemTag) item).getItemStack().getItemMeta() instanceof BlockStateMeta
&& ((BlockStateMeta) ((ItemTag) item).getItemStack().getItemMeta()).hasBlockState()
&& ((BlockStateMeta) ((ItemTag) item).getItemStack().getItemMeta()).getBlockState() instanceof CreatureSpawner;
}

public static ItemSpawnerType getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemSpawnerType((ItemTag) _item);
}
}

public static final String[] handledTags = new String[] {
"spawner_type"
};

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


private 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.getItemStack().getItemMeta();
if (meta.hasBlockState()) {
CreatureSpawner state = (CreatureSpawner) meta.getBlockState();
return new EntityTag(state.getSpawnedType())
.getObjectAttribute(attribute.fulfill(1));
}
}

return null;
}

@Override
public String getPropertyString() {
BlockStateMeta meta = (BlockStateMeta) item.getItemStack().getItemMeta();
if (meta.hasBlockState()) {
CreatureSpawner state = (CreatureSpawner) meta.getBlockState();
return state.getSpawnedType().name();
}
return null;
}

@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.getItemStack().getItemMeta();
if (meta.hasBlockState()) {
CreatureSpawner state = (CreatureSpawner) meta.getBlockState();
state.setSpawnedType(mechanism.valueAsType(EntityTag.class).getBukkitEntityType());
meta.setBlockState(state);
item.getItemStack().setItemMeta(meta);
}
}
}
}

0 comments on commit 5751a1e

Please sign in to comment.