Skip to content

Commit

Permalink
update ObjectProperty-using properties to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 3, 2023
1 parent 49590a9 commit 0559148
Show file tree
Hide file tree
Showing 31 changed files with 730 additions and 993 deletions.
@@ -1,10 +1,21 @@
package com.denizenscript.denizen.objects.properties.entity;

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

public class EntityAI extends EntityProperty {
public class EntityAI extends EntityProperty<ElementTag> {

// <--[property]
// @object EntityTag
// @name has_ai
// @input ElementTag(Boolean)
// @description
// Controls whether this entity will use the default Minecraft AI to roam and look around.
// This tends to have implications for other vanilla functionality, including gravity.
// 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.
// -->

public static boolean describes(EntityTag entity) {
return entity.isLivingEntityType();
Expand All @@ -15,42 +26,19 @@ public ElementTag getPropertyValue() {
return new ElementTag(getLivingEntity().hasAI());
}

@Override
public void setPropertyValue(ElementTag param, Mechanism mechanism) {
if (mechanism.requireBoolean()) {
getLivingEntity().setAI(param.asBoolean());
}
}

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

public static void register() {

// <--[tag]
// @attribute <EntityTag.has_ai>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.has_ai
// @group attributes
// @description
// Returns whether the entity uses the default Minecraft AI to roam and look around.
// This tends to have implications for other vanilla functionality, including gravity.
// 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, prop) -> {
return new ElementTag(prop.getLivingEntity().hasAI());
});

// <--[mechanism]
// @object EntityTag
// @name has_ai
// @input ElementTag(Boolean)
// @description
// Sets whether this entity will use the default Minecraft AI to roam and look around.
// This tends to have implications for other vanilla functionality, including gravity.
// @tags
// <EntityTag.has_ai>
// -->
PropertyParser.registerMechanism(EntityAI.class, ElementTag.class, "has_ai", (prop, mechanism, param) -> {
if (mechanism.requireBoolean()) {
prop.getLivingEntity().setAI(param.asBoolean());
}
}, "toggle_ai");
autoRegister("has_ai", EntityAI.class, ElementTag.class, false, "toggle_ai");
}
}
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
Expand All @@ -11,7 +12,19 @@
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Breedable;

public class EntityAge extends EntityProperty {
public class EntityAge extends EntityProperty<ElementTag> {

// <--[property]
// @object EntityTag
// @name age
// @input ElementTag
// @description
// Controls the entity's age.
// Age moves 1 towards zero each tick.
// A newly spawned baby is -24000, a standard adult is 0, an adult that just bred is 6000.
// For the mechanism, inputs can be 'baby', 'adult', or a valid age number.
// Also available: <@link mechanism EntityTag.age_locked> and <@link tag EntityTag.is_baby>
// -->

public static boolean describes(EntityTag entity) {
return entity.getBukkitEntity() instanceof Ageable;
Expand All @@ -26,6 +39,38 @@ public ElementTag getPropertyValue() {
return new ElementTag(as(Ageable.class).getAge());
}

@Override
public void setPropertyValue(ElementTag param, Mechanism mechanism) {
ListTag listHack = param.asType(ListTag.class, mechanism.context);
if (listHack.isEmpty()) {
mechanism.echoError("Missing value for 'age' mechanism!");
return;
}
String input = CoreUtilities.toLowerCase(listHack.get(0));
switch (input) {
case "baby" -> setAge(-24000);
case "adult" -> setAge(0);
default -> {
if (!ArgumentHelper.matchesInteger(input)) {
mechanism.echoError("Invalid age '" + input + "': must be 'baby', 'adult', or a valid age number.");
return;
}
setAge(new ElementTag(input).asInt());
}
}
if (listHack.size() > 1) {
BukkitImplDeprecations.oldAgeLockedControls.warn(mechanism.context);
if (!(getEntity() instanceof Breedable breedable)) {
return;
}
switch (CoreUtilities.toLowerCase(listHack.get(1))) {
case "locked" -> breedable.setAgeLock(true);
case "unlocked" -> breedable.setAgeLock(false);
default -> mechanism.echoError("Invalid lock state '" + listHack.get(1) + "': must be 'locked' or 'unlocked'.");
}
}
}

@Override
public String getPropertyId() {
return "age";
Expand All @@ -41,6 +86,7 @@ public void setAge(int age) {
}

public static void register() {
autoRegister("age", EntityAge.class, ElementTag.class, false);

// <--[tag]
// @attribute <EntityTag.is_baby>
Expand All @@ -53,62 +99,5 @@ public static void register() {
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "is_baby", (attribute, prop) -> {
return new ElementTag(!prop.as(Ageable.class).isAdult());
});

// <--[tag]
// @attribute <EntityTag.age>
// @returns ElementTag(Number)
// @mechanism EntityTag.age
// @group properties
// @description
// If the entity is ageable, returns the entity's age.
// Age moves 1 towards zero each tick.
// A newly spawned baby is -24000, a standard adult is 0, an adult that just bred is 6000.
// -->
PropertyParser.registerTag(EntityAge.class, ElementTag.class, "age", (attribute, prop) -> {
return new ElementTag(prop.as(Ageable.class).getAge());
});

// <--[mechanism]
// @object EntityTag
// @name age
// @input ElementTag
// @description
// Sets the entity's age.
// Inputs can be 'baby', 'adult', or a valid age number.
// A newly spawned baby is -24000, a standard adult is 0, an adult that just bred is 6000.
// Also available: <@link mechanism EntityTag.age_locked>.
// @tags
// <EntityTag.age>
// <EntityTag.is_baby>
// -->
PropertyParser.registerMechanism(EntityAge.class, ListTag.class, "age", (prop, mechanism, param) -> {
if (param.isEmpty()) {
mechanism.echoError("Missing value for 'age' mechanism!");
return;
}
String input = CoreUtilities.toLowerCase(param.get(0));
switch (input) {
case "baby" -> prop.setAge(-24000);
case "adult" -> prop.setAge(0);
default -> {
if (!ArgumentHelper.matchesInteger(input)) {
mechanism.echoError("Invalid age '" + input + "': must be 'baby', 'adult', or a valid age number.");
return;
}
prop.setAge(new ElementTag(input).asInt());
}
}
if (param.size() > 1) {
BukkitImplDeprecations.oldAgeLockedControls.warn(mechanism.context);
if (!(prop.getEntity() instanceof Breedable breedable)) {
return;
}
switch (CoreUtilities.toLowerCase(param.get(1))) {
case "locked" -> breedable.setAgeLock(true);
case "unlocked" -> breedable.setAgeLock(false);
default -> mechanism.echoError("Invalid lock state '" + param.get(1) + "': must be 'locked' or 'unlocked'.");
}
}
});
}
}
@@ -1,20 +1,45 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import net.citizensnpcs.trait.Age;
import org.bukkit.entity.Breedable;

public class EntityAgeLocked extends EntityProperty {
public class EntityAgeLocked extends EntityProperty<ElementTag> {

// <--[property]
// @object EntityTag
// @name age_locked
// @input ElementTag(Boolean)
// @description
// Controls whether the entity is locked into its current age.
// -->

public static boolean describes(EntityTag entity) {
return entity.getBukkitEntity() instanceof Breedable;
}

@Override
public boolean isDefaultValue(ElementTag val) {
return !val.asBoolean();
}

@Override
public ElementTag getPropertyValue() {
return as(Breedable.class).getAgeLock() ? new ElementTag(true) : null;
return new ElementTag(as(Breedable.class).getAgeLock());
}

@Override
public void setPropertyValue(ElementTag param, Mechanism mechanism) {
if (mechanism.requireBoolean()) {
if (object.isCitizensNPC()) {
object.getDenizenNPC().getCitizen().getOrAddTrait(Age.class).setLocked(param.asBoolean());
}
else {
as(Breedable.class).setAgeLock(param.asBoolean());
}
}
}

@Override
Expand All @@ -23,6 +48,7 @@ public String getPropertyId() {
}

public static void register() {
autoRegister("age_locked", EntityAgeLocked.class, ElementTag.class, false, "is_age_locked", "age_lock");

// <--[tag]
// @attribute <EntityTag.is_age_locked>
Expand All @@ -33,18 +59,6 @@ public static void register() {
// Deprecated in favor of <@link tag EntityTag.age_locked>.
// -->

// <--[tag]
// @attribute <EntityTag.age_locked>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.age_locked
// @group properties
// @description
// Returns whether the entity is locked into its current age.
// -->
PropertyParser.registerTag(EntityAgeLocked.class, ElementTag.class, "age_locked", (attribute, prop) -> {
return new ElementTag(prop.as(Breedable.class).getAgeLock());
}, "is_age_locked");

// <--[mechanism]
// @object EntityTag
// @name age_lock
Expand All @@ -53,25 +67,5 @@ public static void register() {
// @description
// Deprecated in favor of <@link mechanism EntityTag.age_locked>.
// -->

// <--[mechanism]
// @object EntityTag
// @name age_locked
// @input ElementTag(Boolean)
// @description
// Sets whether the entity is locked into its current age.
// @tags
// <EntityTag.age_locked>
// -->
PropertyParser.registerMechanism(EntityAgeLocked.class, ElementTag.class, "age_locked", (prop, mechanism, input) -> {
if (mechanism.requireBoolean()) {
if (prop.object.isCitizensNPC()) {
prop.object.getDenizenNPC().getCitizen().getOrAddTrait(Age.class).setLocked(input.asBoolean());
}
else {
prop.as(Breedable.class).setAgeLock(input.asBoolean());
}
}
}, "age_lock");
}
}
Expand Up @@ -2,19 +2,34 @@

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

public class EntityAggressive extends EntityProperty {
public class EntityAggressive extends EntityProperty<ElementTag> {

// <--[property]
// @object EntityTag
// @name aggressive
// @input ElementTag(Boolean)
// @description
// Controls whether the entity is currently aggressive.
// -->

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

@Override
public ElementTag getPropertyValue() {
return new ElementTag(NMSHandler.entityHelper.isAggressive(getMob()));
return new ElementTag(NMSHandler.entityHelper.isAggressive(as(Mob.class)));
}

@Override
public void setPropertyValue(ElementTag param, Mechanism mechanism) {
if (mechanism.requireBoolean()) {
NMSHandler.entityHelper.setAggressive(as(Mob.class), param.asBoolean());
}
}

@Override
Expand All @@ -23,36 +38,6 @@ public String getPropertyId() {
}

public static void register() {

// <--[tag]
// @attribute <EntityTag.aggressive>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.aggressive
// @group properties
// @description
// Returns whether the entity is currently aggressive.
// -->
PropertyParser.registerTag(EntityAggressive.class, ElementTag.class, "aggressive", (attribute, prop) -> {
return new ElementTag(NMSHandler.entityHelper.isAggressive(prop.getMob()));
});

// <--[mechanism]
// @object EntityTag
// @name aggressive
// @input ElementTag(Boolean)
// @description
// Sets whether the entity is currently aggressive.
// @tags
// <EntityTag.aggressive>
// -->
PropertyParser.registerMechanism(EntityAggressive.class, ElementTag.class, "aggressive", (prop, mechanism, input) -> {
if (mechanism.requireBoolean()) {
NMSHandler.entityHelper.setAggressive(prop.getMob(), input.asBoolean());
}
});
}

public Mob getMob() {
return (Mob) getEntity();
autoRegister("aggressive", EntityAggressive.class, ElementTag.class, false);
}
}

0 comments on commit 0559148

Please sign in to comment.