Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaterialTag.half,faces,distance Property Modernization #2660

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,61 @@

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.BlockData;
import org.bukkit.block.data.type.Leaves;
import org.bukkit.block.data.type.Scaffolding;

public class MaterialDistance implements Property {

public static boolean describes(Object material) {
return material instanceof MaterialTag
&& ((MaterialTag) material).hasModernData()
&& (((MaterialTag) material).getModernData() instanceof Scaffolding
|| ((MaterialTag) material).getModernData() instanceof Leaves);
public class MaterialDistance extends MaterialProperty<ElementTag> {

// <--[property]
// @object MaterialTag
// @name distance
// @input ElementTag(Number)
// @description
// Controls the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold,
// or between a leaves block and the nearest log (a distance of 7 will cause a leaf to decay if 'persistent' is also false, less than 7 will prevent decay).
// -->

public static boolean describes(MaterialTag material) {
BlockData data = material.getModernData();
return data instanceof Scaffolding
|| data instanceof Leaves;
}

public static MaterialDistance getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialDistance((MaterialTag) _material);
}
}
MaterialTag material;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove the old field and use the built-in object field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And with that, should use the MaterialProperty methods (e.g. getBlockData() instead of material.getModernData())


public static final String[] handledMechs = new String[] {
"distance"
};
@Override
public String getPropertyId() {
return "distance";
}

public MaterialDistance(MaterialTag _material) {
material = _material;
@Override
public ElementTag getPropertyValue() {
return new ElementTag(getDistance());
}

MaterialTag material;
@Override
public void setPropertyValue(ElementTag value, Mechanism mechanism) {
if (!mechanism.requireInteger()) {
return;
}
int distance = value.asInt();
if (isScaffolding()) {
if (distance >= 0 && distance <= getScaffolding().getMaximumDistance()) {
getScaffolding().setDistance(distance);
}
else {
mechanism.echoError("Distance must be between 0 and " + getScaffolding().getMaximumDistance());
}
}
else if (isLeaves()) {
getLeaves().setDistance(distance);
}
}

public static void register() {

// <--[tag]
// @attribute <MaterialTag.distance>
// @returns ElementTag(Number)
// @mechanism MaterialTag.distance
// @group properties
// @description
// Returns the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold,
// or between a leaves block and the nearest log (a distance of 7 will cause a leaf to decay if 'persistent' is also false, less than 7 will prevent decay).
// -->
PropertyParser.registerStaticTag(MaterialDistance.class, ElementTag.class, "distance", (attribute, material) -> {
return new ElementTag(material.getDistance());
});
autoRegister("distance", MaterialDistance.class, ElementTag.class, true);
}

public int getDistance() {
Expand Down Expand Up @@ -79,41 +85,16 @@ public boolean isLeaves() {
return material.getModernData() instanceof Leaves;
}

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

@Override
public String getPropertyId() {
return "distance";
public static MaterialDistance getFrom(MaterialTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialDistance(_material);
}
Comment on lines +88 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's shouldn't be any need to keep getFrom - if anything is using this property directly, can use describes & add a constructor that directs to super.
In general should ideally remove the older methods when updating a property, any is/getX methods can just be instanceof pattern matching, any get/setDistance sorta methods can usually just be put directly in get/setPropertyValue, etc..
It's not an absolute requirement, but it'd be nicer to have the properties fully match the modern format.

}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object MaterialTag
// @name distance
// @input ElementTag(Number)
// @description
// Sets the horizontal distance between a scaffolding block and the nearest scaffolding block placed above a 'bottom' scaffold, or between a leaves block and the nearest log.
// @tags
// <MaterialTag.distance>
// -->
if (mechanism.matches("distance") && mechanism.requireInteger()) {
int distance = mechanism.getValue().asInt();
if (isScaffolding()) {
if (distance >= 0 && distance <= getScaffolding().getMaximumDistance()) {
getScaffolding().setDistance(distance);
}
else {
mechanism.echoError("Distance must be between 0 and " + getScaffolding().getMaximumDistance());
}
}
else if (isLeaves()) {
getLeaves().setDistance(distance);
}
}
public MaterialDistance(MaterialTag _material) {
material = _material;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,53 @@

import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.MultipleFacing;

public class MaterialFaces implements Property {
public class MaterialFaces extends MaterialProperty<ListTag> {

public static boolean describes(ObjectTag material) {
return material instanceof MaterialTag
&& ((MaterialTag) material).hasModernData()
&& ((MaterialTag) material).getModernData() instanceof MultipleFacing;
}
// <--[property]
// @object MaterialTag
// @name valid_faces
// @input ListTag
// @description
// Controls a list of the current faces for a material that has multiple faces (like a mushroom block).
// For valid faces, see <@link tag MaterialTag.valid_faces>.
// -->

public static MaterialFaces getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialFaces((MaterialTag) _material);
}
public static boolean describes(MaterialTag material) {
BlockData data = material.getModernData();
return data instanceof MultipleFacing;
}

public static final String[] handledMechs = new String[] {
"faces"
};
MaterialTag material;

public MaterialFaces(MaterialTag _material) {
material = _material;
@Override
public String getPropertyId() {
return "faces";
}

MaterialTag material;
@Override
public ListTag getPropertyValue() {
return getFaceList();
}

@Override
public void setPropertyValue(ListTag list, Mechanism mechanism) {
MultipleFacing facing = getFaces();
for (BlockFace face : facing.getAllowedFaces()) {
facing.setFace(face, false);
}
for (String faceName : list) {
facing.setFace(BlockFace.valueOf(faceName.toUpperCase()), true);
}
}

public static void register() {
autoRegister("faces", MaterialFaces.class, ListTag.class, true);

// <--[tag]
// @attribute <MaterialTag.valid_faces>
Expand All @@ -45,7 +57,7 @@ public static void register() {
// @group properties
// @description
// Returns a list of faces that are valid for a material that has multiple faces.
// See also <@link tag MaterialTag.faces>
// See also <@link property MaterialTag.faces>
// -->
PropertyParser.registerStaticTag(MaterialFaces.class, ListTag.class, "valid_faces", (attribute, material) -> {
ListTag toReturn = new ListTag();
Expand All @@ -54,19 +66,6 @@ public static void register() {
}
return toReturn;
});

// <--[tag]
// @attribute <MaterialTag.faces>
// @returns ListTag
// @mechanism MaterialTag.faces
// @group properties
// @description
// Returns a list of the current faces for a material that has multiple faces (like a mushroom block).
// Output is a direction name like "NORTH".
// -->
PropertyParser.registerStaticTag(MaterialFaces.class, ListTag.class, "faces", (attribute, material) -> {
return material.getFaceList();
});
}

public ListTag getFaceList() {
Expand All @@ -80,38 +79,4 @@ public ListTag getFaceList() {
public MultipleFacing getFaces() {
return (MultipleFacing) material.getModernData();
}

@Override
public String getPropertyString() {
return getFaceList().identify();
}

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

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object MaterialTag
// @name faces
// @input ListTag
// @description
// Sets the current faces for a material that has multiple faces (like a mushroom block).
// @tags
// <MaterialTag.faces>
// <MaterialTag.valid_faces>
// -->
if (mechanism.matches("faces")) {
MultipleFacing facing = getFaces();
for (BlockFace face : facing.getAllowedFaces()) {
facing.setFace(face, false);
}
for (String faceName : mechanism.valueAsType(ListTag.class)) {
facing.setFace(BlockFace.valueOf(faceName.toUpperCase()), true);
}
}
}
}
Loading