Skip to content

Commit

Permalink
add mech and tag 'cuboid.add_member'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 2, 2019
1 parent 6c1d925 commit 76a0abd
Showing 1 changed file with 83 additions and 49 deletions.
132 changes: 83 additions & 49 deletions plugin/src/main/java/com/denizenscript/denizen/objects/CuboidTag.java
Expand Up @@ -1169,6 +1169,60 @@ public String run(Attribute attribute, ObjectTag object) {
}
});

// <--[tag]
// @attribute <CuboidTag.add_member[<cuboid>]>
// @returns CuboidTag
// @description
// Returns a modified copy of this cuboid, with the input cuboid added at the end.
// -->
registerTag("add", new TagRunnable() {
@Override
public String run(Attribute attribute, ObjectTag object) {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag CuboidTag.add[...] must have a value.");
return null;
}
else {
CuboidTag cuboid;

try {
cuboid = ((CuboidTag) object).clone();
}
catch (CloneNotSupportedException ex) {
Debug.echoError(ex); // This should never happen
return null;
}
CuboidTag subCuboid = CuboidTag.valueOf(attribute.getContext(1));
attribute = attribute.fulfill(1);
int member = cuboid.pairs.size() + 1;
// <--[tag]
// @attribute <CuboidTag.add_member[<cuboid>].at[<index>]>
// @returns CuboidTag
// @description
// Returns a modified copy of this cuboid, with the input cuboid added at the specified index.
// -->
if (attribute.matches("at")) {
attribute.fulfill(1);
if (!attribute.hasContext(1)) {
attribute.fulfill(1);
Debug.echoError("The tag CuboidTag.add[...].at[...] must have an 'at' value.");
return null;
}
member = attribute.getIntContext(1);
}
if (member < 1) {
member = 1;
}
if (member > cuboid.pairs.size() + 1) {
member = cuboid.pairs.size() + 1;
}
LocationPair pair = subCuboid.pairs.get(0);
cuboid.pairs.add(member - 1, new LocationPair(pair.point_1, pair.point_2));
return cuboid.getAttribute(attribute.fulfill(1));
}
}
});

// <--[tag]
// @attribute <CuboidTag.center>
// @returns LocationTag
Expand Down Expand Up @@ -1725,10 +1779,11 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object CuboidTag
// @name set_member
// @input (#,)dCuboid
// @input (#,)CuboidTag
// @description
// Sets a given sub-cuboid of the cuboid.
// Input is of the form like "2,cu@..." where 2 is the sub-cuboid index or just a direct CuboidTag input.
// Input is of the form like "2,cu@..." where 2 is the sub-cuboid index, or just a direct CuboidTag input.
// The default index, if unspecified, is 1 (ie the first member).
// @tags
// <CuboidTag.get>
// <CuboidTag.set[<cuboid>].at[<#>]>
Expand All @@ -1751,56 +1806,35 @@ public void adjust(Mechanism mechanism) {
pairs.set(member - 1, new LocationPair(pair.point_1, pair.point_2));
}

// TODO: Better mechanisms!

if (mechanism.matches("outset")) {
int mod = 1;
if (mechanism.hasValue() && mechanism.requireInteger("Invalid integer specified. Assuming '1'.")) {
mod = mechanism.getValue().asInt();
}
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
pair.high.add(mod, mod, mod);
// Modify the locations, need to readjust the distances generated
pair.generateDistances();
}

// TODO: Make sure negative numbers don't collapse (and invert) the Cuboid
return;
}

if (mechanism.matches("expand")) {
int mod = 1;
if (mechanism.hasValue() && mechanism.requireInteger("Invalid integer specified. Assuming '1'.")) {
mod = mechanism.getValue().asInt();
}
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
pair.high.add(mod, mod, mod);
// Modify the locations, need to readjust the distances generated
pair.generateDistances();
// <--[mechanism]
// @object CuboidTag
// @name add_member
// @input (#,)CuboidTag
// @description
// Adds a sub-member to the cuboid (optionally at a specified index - otherwise, at the end).
// Input is of the form like "2,cu@..." where 2 is the sub-cuboid index, or just a direct CuboidTag input.
// Note that the index is where the member will end up. So, index 1 will add the cuboid as the very first member (moving the rest up +1 index value).
// @tags
// <CuboidTag.get>
// <CuboidTag.add_member[<cuboid>]>
// <CuboidTag.add_member[<cuboid>].at[<#>]>
// -->
if (mechanism.matches("add_member")) {
String value = mechanism.getValue().asString();
int comma = value.indexOf(',');
int member = pairs.size() + 1;
if (comma > 0) {
member = new ElementTag(value.substring(0, comma)).asInt();
}

// TODO: Make sure negative numbers don't collapse (and invert)
// the Cuboid
return;
}

if (mechanism.matches("set_location")) {
int mod = 1;
if (mechanism.hasValue() && mechanism.requireInteger("Invalid integer specified. Assuming '1'.")) {
mod = mechanism.getValue().asInt();
CuboidTag subCuboid = CuboidTag.valueOf(comma == -1 ? value : value.substring(comma + 1));
if (member < 1) {
member = 1;
}
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
pair.high.add(mod, mod, mod);
// Modify the locations, need to readjust the distances generated
pair.generateDistances();
if (member > pairs.size()) {
member = pairs.size();
}

// TODO: Make sure negative numbers don't collapse (and invert)
// the Cuboid
return;
LocationPair pair = subCuboid.pairs.get(0);
pairs.add(member - 1, new LocationPair(pair.point_1, pair.point_2));
}

CoreUtilities.autoPropertyMechanism(this, mechanism);
Expand Down

0 comments on commit 76a0abd

Please sign in to comment.