Skip to content

Commit

Permalink
Make dEntity.custom_name a property.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fortifier42 committed Dec 24, 2015
1 parent e292cf2 commit 432d546
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -744,6 +744,7 @@ public void onEnable() {
propertyParser.registerProperty(EntityChestCarrier.class, dEntity.class);
propertyParser.registerProperty(EntityColor.class, dEntity.class);
propertyParser.registerProperty(EntityCritical.class, dEntity.class);
propertyParser.registerProperty(EntityCustomName.class, dEntity.class);
propertyParser.registerProperty(EntityElder.class, dEntity.class);
propertyParser.registerProperty(EntityEquipment.class, dEntity.class);
propertyParser.registerProperty(EntityFirework.class, dEntity.class);
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -1521,29 +1521,6 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.custom_name>
// @returns Element
// @group attributes
// @description
// Returns the entity's custom name, if any.
// -->
if (attribute.startsWith("custom_name")) {
return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.custom_name.visible>
// @returns Element(Boolean)
// @group attributes
// @description
// Returns true if the entity's custom name is visible.
// -->
if (attribute.startsWith("custom_name.visible")) {
return new Element(entity.isCustomNameVisible())
.getAttribute(attribute.fulfill(2));
}

// <--[tag]
// @attribute <e@entity.name>
// @returns Element
Expand Down Expand Up @@ -2343,30 +2320,6 @@ public void adjust(Mechanism mechanism) {
if (mechanism.matches("can_pickup_items") && mechanism.requireBoolean())
getLivingEntity().setCanPickupItems(value.asBoolean());

// <--[mechanism]
// @object dEntity
// @name custom_name
// @input Element
// @description
// Sets the custom name of the entity.
// @tags
// <e@entity.custom_name>
// -->
if (mechanism.matches("custom_name"))
entity.setCustomName(value.asString());

// <--[mechanism]
// @object dEntity
// @name custom_name_visibility
// @input Element(Boolean)
// @description
// Sets whether the custom name is visible.
// @tags
// <e@entity.custom_name.visible>
// -->
if (mechanism.matches("custom_name_visibility") && mechanism.requireBoolean())
entity.setCustomNameVisible(value.asBoolean());

// <--[mechanism]
// @object dEntity
// @name fall_distance
Expand Down
@@ -0,0 +1,119 @@
package net.aufdemrand.denizen.objects.properties.entity;

import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;

public class EntityCustomName implements Property {

public static boolean describes(dObject entity) {
return entity instanceof dEntity;
}

public static EntityCustomName getFrom(dObject entity) {
if (!describes(entity)) {
return null;
}
return new EntityCustomName((dEntity) entity);
}

private EntityCustomName(dEntity ent){
entity = ent;
}

dEntity entity;

@Override
public String getPropertyString() {
String name = entity.getBukkitEntity().getCustomName();
if (name == null) {
return null;
}
else {
return name;
}
}

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

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) {
return null;
}

// <--[tag]
// @attribute <e@entity.custom_name_visible>
// @returns Element(Boolean)
// @group attributes
// @description
// Returns true if the entity's custom name is visible.
// -->
if (attribute.startsWith("custom_name_visible") || attribute.startsWith("custom_name.visible")) {
int fulfilled = 1;
if (attribute.startsWith("custom_name.visible")) {
fulfilled = 2;
}
return new Element(entity.getBukkitEntity().isCustomNameVisible()).getAttribute(attribute.fulfill(fulfilled));
}

// <--[tag]
// @attribute <e@entity.custom_name>
// @returns Element
// @group attributes
// @description
// Returns the entity's custom name, if any.
// -->
else if (attribute.startsWith("custom_name")) {
String name = entity.getBukkitEntity().getCustomName();
if (name == null) {
return null;
}
else {
return new Element(name).getAttribute(attribute.fulfill(1));
}
}
else

return null;
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object dEntity
// @name custom_name_visible
// @input Element(Boolean)
// @description
// Sets whether the custom name is visible.
// @tags
// <e@entity.custom_name_visible>
// -->
if ((mechanism.matches("custom_name_visibility") || mechanism.matches("custom_name_visible"))
&& mechanism.requireBoolean()) {
entity.getBukkitEntity().setCustomNameVisible(mechanism.getValue().asBoolean());
}

// <--[mechanism]
// @object dEntity
// @name custom_name
// @input Element
// @description
// Sets the custom name of the entity.
// @tags
// <e@entity.custom_name>
// -->
else if (mechanism.matches("custom_name")) {
entity.getBukkitEntity().setCustomName(mechanism.getValue().asString());
}

}
}

0 comments on commit 432d546

Please sign in to comment.