Skip to content

Commit

Permalink
add material directional property, for #1940
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 10, 2019
1 parent 7084d9a commit 1061325
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
17 changes: 17 additions & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -505,6 +505,23 @@ public String run(Attribute attribute, dObject object) {
}
});

// <--[tag]
// @attribute <m@material.is_directional>
// @returns Element(Boolean)
// @group properties
// @description
// Returns whether the material is a is_directional material.
// When this returns true, <@link tag m@material.direction>, <@link tag m@material.valid_directions>,
// and <@link mechanism dMaterial.direction> are accessible.
// -->
registerTag("is_bisected", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(MaterialHalf.describes(object))
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <m@material.is_bisected>
// @returns Element(Boolean)
Expand Down
Expand Up @@ -13,9 +13,7 @@
import net.aufdemrand.denizen.objects.properties.inventory.InventorySize;
import net.aufdemrand.denizen.objects.properties.inventory.InventoryTitle;
import net.aufdemrand.denizen.objects.properties.item.*;
import net.aufdemrand.denizen.objects.properties.material.MaterialAge;
import net.aufdemrand.denizen.objects.properties.material.MaterialHalf;
import net.aufdemrand.denizen.objects.properties.material.MaterialLevel;
import net.aufdemrand.denizen.objects.properties.material.*;
import net.aufdemrand.denizen.objects.properties.trade.*;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dList;
Expand Down Expand Up @@ -132,6 +130,7 @@ public static void registermainProperties() {
// register core dMaterial properties
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
PropertyParser.registerProperty(MaterialAge.class, dMaterial.class);
PropertyParser.registerProperty(MaterialDirectional.class, dMaterial.class);
PropertyParser.registerProperty(MaterialHalf.class, dMaterial.class);
PropertyParser.registerProperty(MaterialLevel.class, dMaterial.class);
}
Expand Down
@@ -0,0 +1,120 @@
package net.aufdemrand.denizen.objects.properties.material;

import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Directional;

public class MaterialDirectional implements Property {

public static boolean describes(dObject material) {
return material instanceof dMaterial
&& ((dMaterial) material).hasModernData()
&& ((dMaterial) material).getModernData().data instanceof Directional;
}

public static MaterialDirectional getFrom(dObject _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialDirectional((dMaterial) _material);
}
}

public static final String[] handledTags = new String[] {
"direction", "valid_directions"
};

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


private MaterialDirectional(dMaterial _material) {
material = _material;
}

dMaterial material;

@Override
public String getAttribute(Attribute attribute) {

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

// <--[tag]
// @attribute <m@material.valid_directions>
// @returns Element
// @mechanism dMaterial.direction
// @group properties
// @description
// Returns a list of directions that are valid for a directional material.
// See also <@link tag m@matereial.direction>
// -->
if (attribute.startsWith("valid_directions")) {
dList toReturn = new dList();
for (BlockFace face : getDirectional().getFaces()) {
toReturn.add(face.name());
}
return toReturn.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <m@material.direction>
// @returns Element
// @mechanism dMaterial.direction
// @group properties
// @description
// Returns the current facing direction for a directional material (like a door or a bed).
// Output is a direction name like "NORTH".
// -->
if (attribute.startsWith("direction")) {
return new Element(getDirectional().getFacing().name()).getAttribute(attribute.fulfill(1));
}

return null;
}

public Directional getDirectional() {
return (Directional) material.getModernData().data;
}

public BlockFace getDirection() {
return getDirectional().getFacing();
}

@Override
public String getPropertyString() {
return getDirectional().getFacing().name();
}

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

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object dMaterial
// @name direction
// @input Element
// @description
// Sets the current facing direction for a directional material (like a door or a bed).
// @tags
// <m@material.direction>
// <m@material.valid_directions>
// -->
if (mechanism.matches("direction") && mechanism.requireEnum(false, BlockFace.values())) {
getDirectional().setFacing(BlockFace.valueOf(mechanism.getValue().asString().toUpperCase()));
}
}
}

0 comments on commit 1061325

Please sign in to comment.