Skip to content

Commit

Permalink
Switchable Stuff (#2102)
Browse files Browse the repository at this point in the history
* Switchable step 1

* Take 2 - Ready for testing.

* Added the thing to the thing to make it register this thing.

* More testing....

* Tested, and confirmed working.

* Removal of Sneaky Whitespace

* consistency matters!

* - Added `is_switchable` Tag, and Meta documentation.
- Update Meta for `.switched` tag and mechanisms with better verbiage.
  • Loading branch information
Ph4i1ur3 authored and mcmonkey4eva committed Dec 24, 2019
1 parent 11eb589 commit 49e2201
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
Expand Up @@ -749,6 +749,16 @@ public static void registerTags() {
return new ElementTag(object.material.isSolid());
});

// <--[tag]
// @attribute <MaterialTag.is_switchable>
// @returns ElementTag(Boolean)
// @description
// Returns whether the material is Openable, Powerable, or a Dispenser.
// -->
registerTag("is_switchable", (attribute, object) -> {
return new ElementTag(MaterialSwitchable.describes(object));
});

// <--[tag]
// @attribute <MaterialTag.is_transparent>
// @returns ElementTag(Boolean)
Expand Down
Expand Up @@ -159,6 +159,7 @@ public static void registermainProperties() {
PropertyParser.registerProperty(MaterialPickle.class, MaterialTag.class);
PropertyParser.registerProperty(MaterialSlab.class, MaterialTag.class);
PropertyParser.registerProperty(MaterialSnowable.class, MaterialTag.class);
PropertyParser.registerProperty(MaterialSwitchable.class, MaterialTag.class);
PropertyParser.registerProperty(MaterialSwitchFace.class, MaterialTag.class);
PropertyParser.registerProperty(MaterialWaterlogged.class, MaterialTag.class);
}
Expand Down
@@ -0,0 +1,129 @@
package com.denizenscript.denizen.objects.properties.material;

import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.block.data.Powerable;
import org.bukkit.block.data.Openable;
import org.bukkit.block.data.type.Dispenser;

public class MaterialSwitchable implements Property {

public static boolean describes(ObjectTag material) {
return material instanceof MaterialTag
&& ((MaterialTag) material).hasModernData()
&& (((MaterialTag) material).getModernData().data instanceof Powerable
|| ((MaterialTag) material).getModernData().data instanceof Openable
|| ((MaterialTag) material).getModernData().data instanceof Dispenser);
}

public static MaterialSwitchable getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialSwitchable((MaterialTag) _material);
}
}

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

private MaterialSwitchable(MaterialTag _material) {
material = _material;
}

MaterialTag material;

public static void registerTags() {

// <--[tag]
// @attribute <MaterialTag.switched>
// @returns ElementTag(Boolean)
// @group properties
// @description
// Returns whether a Powerable material (like pressure plates) or an Openable material (like doors), or a dispenser is switched.
// -->
PropertyParser.<MaterialSwitchable>registerTag("switched", (attribute, material) -> {
return new ElementTag(material.getState());
});
}

public boolean isPowerable() {
return material.getModernData().data instanceof Powerable;
}

public Powerable getPowerable() {
return (Powerable) material.getModernData().data;
}

public boolean isOpenable() {
return material.getModernData().data instanceof Openable;
}

public Openable getOpenable() {
return (Openable) material.getModernData().data;
}

public Dispenser getDispenser() {
return (Dispenser) material.getModernData().data;
}

public boolean getState() {
if (isOpenable()) {
return getOpenable().isOpen();
}
else if (isPowerable()) {
return getPowerable().isPowered();
}
else {
return getDispenser().isTriggered();
}
}

public void setState(boolean state) {
if (isOpenable()) {
getOpenable().setOpen(state);
return;
}
else if (isPowerable()) {
getPowerable().setPowered(state);
return;
}
else {
getDispenser().setTriggered(state);
return;
}
}

@Override
public String getPropertyString() {
return String.valueOf(getState());
}

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

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object MaterialTag
// @name switched
// @input ElementTag(boolean)
// @description
// Sets whether a Powerable material (like pressure plates) or an Openable material (like doors), or a dispenser is switched.
// @tags
// <MaterialTag.switched>
// -->
if (mechanism.matches("switched") && mechanism.requireBoolean()) {
setState(mechanism.getValue().asBoolean());
}
}
}

0 comments on commit 49e2201

Please sign in to comment.