Skip to content

Commit

Permalink
update a few EntityTag properties to new core property format
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 22, 2023
1 parent c8556e5 commit 319ee0d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 136 deletions.
Expand Up @@ -2,40 +2,17 @@

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;

public class EntityAI implements Property {
public class EntityAI extends EntityProperty {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag
&& ((EntityTag) entity).isLivingEntity();
public static boolean describes(EntityTag entity) {
return entity.isLivingEntityType();
}

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

public static final String[] handledMechs = new String[] {
"has_ai", "toggle_ai"
};

public EntityAI(EntityTag ent) {
entity = ent;
}

EntityTag entity;

@Override
public String getPropertyString() {
return String.valueOf(entity.getLivingEntity().hasAI());
public ElementTag getPropertyValue() {
return new ElementTag(getLivingEntity().hasAI());
}

@Override
Expand All @@ -56,13 +33,9 @@ public static void register() {
// This generally shouldn't be used with NPCs. NPCs do not have vanilla AI, regardless of what this tag returns.
// Other programmatic methods of blocking AI might also not be accounted for by this tag.
// -->
PropertyParser.registerTag(EntityAI.class, ElementTag.class, "has_ai", (attribute, object) -> {
return new ElementTag(object.entity.getLivingEntity().hasAI());
PropertyParser.registerTag(EntityAI.class, ElementTag.class, "has_ai", (attribute, prop) -> {
return new ElementTag(prop.getLivingEntity().hasAI());
});
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
Expand All @@ -74,8 +47,10 @@ public void adjust(Mechanism mechanism) {
// @tags
// <EntityTag.has_ai>
// -->
if ((mechanism.matches("has_ai") || mechanism.matches("toggle_ai")) && mechanism.requireBoolean()) {
entity.getLivingEntity().setAI(mechanism.getValue().asBoolean());
}
PropertyParser.registerMechanism(EntityAI.class, ElementTag.class, "has_ai", (prop, mechanism, param) -> {
if (mechanism.requireBoolean()) {
prop.getLivingEntity().setAI(param.asBoolean());
}
}, "toggle_ai");
}
}
Expand Up @@ -3,80 +3,53 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import net.citizensnpcs.trait.Age;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Breedable;

public class EntityAge implements Property {
public class EntityAge extends EntityProperty {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag
&& ((EntityTag) entity).getBukkitEntity() instanceof Ageable;
public static boolean describes(EntityTag entity) {
return entity.getBukkitEntity() instanceof Ageable;
}

public static EntityAge getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityAge((EntityTag) entity);
}
@Override
public ElementTag getPropertyValue() {
return new ElementTag(getAgeable().getAge() + (getLock() ? "|locked" : ""));
}

public static final String[] handledMechs = new String[] {
"age_lock", "age"
};
@Override
public String getPropertyId() {
return "age";
}

public EntityAge(EntityTag entity) {
ageable = entity;
super(entity);
}

EntityTag ageable;

public void setAge(int val) {
if (ageable.isCitizensNPC()) {
ageable.getDenizenNPC().getCitizen().getOrAddTrait(Age.class).setAge(val);
if (object.isCitizensNPC()) {
object.getDenizenNPC().getCitizen().getOrAddTrait(Age.class).setAge(val);
}
else {
getAgeable().setAge(val);
}
}

public void setLock(boolean bool) {
if (isBreedable()) {
getBreedable().setAgeLock(bool);
if (getEntity() instanceof Breedable breedable) {
breedable.setAgeLock(bool);
}
}

public boolean getLock() {
return !isBreedable() || getBreedable().getAgeLock();
}

public boolean isBreedable() {
return ageable.getBukkitEntity() instanceof Breedable;
return !(getEntity() instanceof Breedable breedable) || breedable.getAgeLock();
}

public Ageable getAgeable() {
return (Ageable) ageable.getBukkitEntity();
}

public Breedable getBreedable() {
return (Breedable) ageable.getBukkitEntity();
}

@Override
public String getPropertyString() {
return getAgeable().getAge() + (getLock() ? "|locked" : "");
}

@Override
public String getPropertyId() {
return "age";
return (Ageable) getEntity();
}

public static void register() {
Expand All @@ -93,8 +66,8 @@ public static void register() {
// A standard adult is 0.
// An adult that just bred is 6000.
// -->
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "age", (attribute, object) -> {
return new ElementTag(object.getAgeable().getAge());
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "age", (attribute, prop) -> {
return new ElementTag(prop.getAgeable().getAge());
});

// <--[tag]
Expand All @@ -105,8 +78,8 @@ public static void register() {
// @description
// If the entity is ageable, returns whether the entity is age locked.
// -->
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "is_age_locked", (attribute, object) -> {
return new ElementTag(object.getLock());
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "is_age_locked", (attribute, prop) -> {
return new ElementTag(prop.getLock());
});

// <--[tag]
Expand All @@ -117,13 +90,9 @@ public static void register() {
// @description
// If the entity is ageable, returns whether the entity is a baby.
// -->
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "is_baby", (attribute, object) -> {
return new ElementTag(!object.getAgeable().isAdult());
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "is_baby", (attribute, prop) -> {
return new ElementTag(!prop.getAgeable().isAdult());
});
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
Expand All @@ -138,9 +107,11 @@ public void adjust(Mechanism mechanism) {
// <EntityTag.is_age_locked>
// <EntityTag.ageable>
// -->
if (mechanism.matches("age_lock") && mechanism.requireBoolean()) {
setLock(mechanism.getValue().asBoolean());
}
PropertyParser.registerMechanism(EntityAge.class, ElementTag.class, "age_lock", (prop, mechanism, param) -> {
if (mechanism.requireBoolean()) {
prop.setLock(param.asBoolean());
}
});

// <--[mechanism]
// @object EntityTag
Expand All @@ -158,38 +129,36 @@ public void adjust(Mechanism mechanism) {
// <EntityTag.is_age_locked>
// <EntityTag.ageable>
// -->
if (mechanism.matches("age") && mechanism.requireObject(ListTag.class)) {
ListTag list = mechanism.valueAsType(ListTag.class);
if (list.isEmpty()) {
PropertyParser.registerMechanism(EntityAge.class, ListTag.class, "age", (prop, mechanism, param) -> {
if (param.isEmpty()) {
mechanism.echoError("Missing value for 'age' mechanism!");
return;
}
String input = list.get(0);
String input = param.get(0);
if (input.equalsIgnoreCase("baby")) {
setAge(-24000);
prop.setAge(-24000);
}
else if (input.equalsIgnoreCase("adult")) {
setAge(0);
prop.setAge(0);
}
else if (ArgumentHelper.matchesInteger(input)) {
setAge(new ElementTag(input).asInt());
prop.setAge(new ElementTag(input).asInt());
}
else {
mechanism.echoError("Invalid age '" + input + "': must be 'baby', 'adult', or a valid age number.");
}
if (list.size() > 1) {
input = list.get(1);
if (param.size() > 1) {
input = param.get(1);
if (input.equalsIgnoreCase("locked")) {
setLock(true);
prop.setLock(true);
}
else if (input.equalsIgnoreCase("unlocked")) {
setLock(false);
prop.setLock(false);
}
else {
mechanism.echoError("Invalid lock state '" + input + "': must be 'locked' or 'unlocked'.");
}
}
}

});
}
}
Expand Up @@ -2,44 +2,26 @@

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.Mob;

public class EntityAggressive implements Property {
public class EntityAggressive extends EntityProperty {

public static boolean describes(ObjectTag object) {
return object instanceof EntityTag
&& ((EntityTag) object).getBukkitEntity() instanceof Mob;
public static boolean describes(EntityTag entity) {
return entity.getBukkitEntity() instanceof Mob;
}

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

EntityTag entity;

@Override
public String getPropertyString() {
return String.valueOf(NMSHandler.entityHelper.isAggressive(getMob()));
public ElementTag getPropertyValue() {
return new ElementTag(NMSHandler.entityHelper.isAggressive(getMob()));
}

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

public EntityAggressive(EntityTag entity) {
this.entity = entity;
}

public static void register() {

// <--[tag]
Expand All @@ -50,8 +32,8 @@ public static void register() {
// @description
// Returns whether the entity is currently aggressive.
// -->
PropertyParser.registerTag(EntityAggressive.class, ElementTag.class, "aggressive", (attribute, object) -> {
return new ElementTag(NMSHandler.entityHelper.isAggressive(object.getMob()));
PropertyParser.registerTag(EntityAggressive.class, ElementTag.class, "aggressive", (attribute, prop) -> {
return new ElementTag(NMSHandler.entityHelper.isAggressive(prop.getMob()));
});

// <--[mechanism]
Expand All @@ -63,14 +45,14 @@ public static void register() {
// @tags
// <EntityTag.aggressive>
// -->
PropertyParser.registerMechanism(EntityAggressive.class, ElementTag.class, "aggressive", (object, mechanism, input) -> {
PropertyParser.registerMechanism(EntityAggressive.class, ElementTag.class, "aggressive", (prop, mechanism, input) -> {
if (mechanism.requireBoolean()) {
NMSHandler.entityHelper.setAggressive(object.getMob(), input.asBoolean());
NMSHandler.entityHelper.setAggressive(prop.getMob(), input.asBoolean());
}
});
}

public Mob getMob() {
return (Mob) entity.getBukkitEntity();
return (Mob) getEntity();
}
}
@@ -0,0 +1,29 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.properties.ObjectProperty;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;

public abstract class EntityProperty extends ObjectProperty<EntityTag> {

public EntityProperty() {
}

public EntityProperty(EntityTag entity) {
object = entity;
}

public Entity getEntity() {
return object.getBukkitEntity();
}

public LivingEntity getLivingEntity() {
return object.getLivingEntity();
}

public EntityType getType() {
return object.getBukkitEntityType();
}
}
Expand Up @@ -100,7 +100,7 @@ public void execute(final ScriptEntry scriptEntry) {
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
if (EntityAge.describes(entity)) {
EntityAge property = EntityAge.getFrom(entity);
EntityAge property = new EntityAge(entity);
if (ageType != null) {
if (ageType.equals(AgeType.BABY)) {
property.setAge(-24000);
Expand Down

0 comments on commit 319ee0d

Please sign in to comment.