Skip to content

Commit

Permalink
Add EntityTag.puffstate property (#2379)
Browse files Browse the repository at this point in the history
* EntityTag.puffstate property

* merge PuffState Property into EntitySize
  • Loading branch information
Hydroxycobalamin committed Sep 23, 2022
1 parent 9cf2eb3 commit f5f8ae0
Showing 1 changed file with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

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.tags.Attribute;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.Phantom;
import org.bukkit.entity.PufferFish;
import org.bukkit.entity.Slime;

public class EntitySize implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag &&
(((EntityTag) entity).getBukkitEntity() instanceof Slime
|| ((EntityTag) entity).getBukkitEntity() instanceof Phantom);
|| ((EntityTag) entity).getBukkitEntity() instanceof Phantom
|| ((EntityTag) entity).getBukkitEntity() instanceof PufferFish);
}

public static EntitySize getFrom(ObjectTag entity) {
Expand All @@ -26,39 +27,35 @@ public static EntitySize getFrom(ObjectTag entity) {
}
}

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

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

private EntitySize(EntityTag ent) {
entity = ent;
}

EntityTag entity;

public int getSize() {
if (isSlime()) {
return getSlime().getSize();
}
else if (isPhantom()) {
return getPhantom().getSize();
}
else {
return getPufferFish().getPuffState();
}
}

@Override
public String getPropertyString() {
if (entity.getBukkitEntity() instanceof Phantom) {
return String.valueOf(((Phantom) entity.getBukkitEntity()).getSize());
}
return String.valueOf(((Slime) entity.getBukkitEntity()).getSize());
return String.valueOf(getSize());
}

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

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {

if (attribute == null) {
return null;
}
public static void registerTags() {

// <--[tag]
// @attribute <EntityTag.size>
Expand All @@ -67,38 +64,56 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// @group properties
// @description
// Returns the size of a slime-type entity or a Phantom (1-120).
// If the entity is a PufferFish it returns the puff state (0-3).
// -->
if (attribute.startsWith("size")) {
if (entity.getBukkitEntity() instanceof Phantom) {
return new ElementTag(((Phantom) entity.getBukkitEntity()).getSize())
.getObjectAttribute(attribute.fulfill(1));
}
return new ElementTag(((Slime) entity.getBukkitEntity()).getSize())
.getObjectAttribute(attribute.fulfill(1));
}
PropertyParser.registerTag(EntitySize.class, ElementTag.class, "size", (attribute, object) -> {
return new ElementTag(object.getSize());
});

return null;
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
// @name size
// @input ElementTag(Number)
// @description
// Sets the size of a slime-type entity or a Phantom (1-120).
// If the entity is a PufferFish it sets the puff state (0-3).
// @tags
// <EntityTag.size>
// -->
if (mechanism.matches("size") && mechanism.requireInteger()) {
if (entity.getBukkitEntity() instanceof Phantom) {
((Phantom) entity.getBukkitEntity()).setSize(mechanism.getValue().asInt());
return;

PropertyParser.registerMechanism(EntitySize.class, ElementTag.class, "size", (object, mechanism, input) -> {
if (mechanism.requireInteger()) {
if (object.isSlime()) {
object.getSlime().setSize(input.asInt());
}
else if (object.isPhantom()) {
object.getPhantom().setSize(input.asInt());
}
else {
object.getPufferFish().setPuffState(input.asInt());
}
}
((Slime) entity.getBukkitEntity()).setSize(mechanism.getValue().asInt());
}
});
}

public boolean isSlime() {
return entity.getBukkitEntity() instanceof Slime;
}
}

public boolean isPhantom() {
return entity.getBukkitEntity() instanceof Phantom;
}

public Slime getSlime() {
return (Slime) entity.getBukkitEntity();
}

public Phantom getPhantom() {
return (Phantom) entity.getBukkitEntity();
}

public PufferFish getPufferFish() {
return (PufferFish) entity.getBukkitEntity();
}
}

0 comments on commit f5f8ae0

Please sign in to comment.