Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add material directional property, for #1940
- Loading branch information
1 parent
7084d9a
commit 1061325
Showing
3 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...src/main/java/net/aufdemrand/denizen/objects/properties/material/MaterialDirectional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} | ||
} |