Skip to content

Commit

Permalink
Add an experimental dEllipsoid
Browse files Browse the repository at this point in the history
It's dGreatest! /suicide
  • Loading branch information
mcmonkey4eva committed Nov 18, 2014
1 parent acbf273 commit 3eb7cee
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 5 deletions.
Expand Up @@ -73,6 +73,7 @@ public static void _registerCoreObjects() throws NoSuchMethodException, ClassNot
registerWithObjectFetcher(dChunk.class); // ch@
registerWithObjectFetcher(dPlugin.class); // pl@
registerWithObjectFetcher(ScriptQueue.class);// q@
registerWithObjectFetcher(dEllipsoid.class); // ellipsoid@

_initialize();

Expand Down
135 changes: 135 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dEllipsoid.java
@@ -0,0 +1,135 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizencore.utilities.CoreUtilities;

import java.util.List;


public class dEllipsoid implements dObject {
//////////////////
// OBJECT FETCHER
////////////////

/**
* Gets an Ellipsoid Object from a string form.
*
* @param string the string
*
*/
@Fetchable("ellipsoid")
public static dEllipsoid valueOf(String string) {

string = string.substring("ellipsoid@".length());

This comment has been minimized.

Copy link
@Morphan1

Morphan1 Dec 12, 2014

Contributor

Is this..... is this dangerous?

This comment has been minimized.

Copy link
@mcmonkey4eva

mcmonkey4eva Dec 12, 2014

Author Member

I... suppose this should be checked. You should add such a check while you're making them notable :)


List<String> split = CoreUtilities.Split(string, ',');

if (split.size() != 7)
return null;

dLocation location = new dLocation(dWorld.valueOf(split.get(3)).getWorld(),
aH.getDoubleFrom(split.get(0)), aH.getDoubleFrom(split.get(1)), aH.getDoubleFrom(split.get(2)));
dLocation size = new dLocation(null, aH.getDoubleFrom(split.get(4)),
aH.getDoubleFrom(split.get(5)), aH.getDoubleFrom(split.get(6)));
return new dEllipsoid(location, size);
}

/**
* Determines whether a string is a valid ellipsoid.
*
* @param arg the string
* @return true if matched, otherwise false
*
*/
public static boolean matches(String arg) {

return arg.startsWith("ellipsoid@");
}


///////////////
// Constructors
/////////////

public dEllipsoid(dLocation loc, dLocation size) {
this.loc = loc;
this.size = size;
}

/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////

private dLocation loc;

private dLocation size;

public boolean contains(dLocation test) {
double xbase = test.getX() - loc.getX();
double ybase = test.getY() - loc.getY();
double zbase = test.getZ() - loc.getZ();
return ((xbase * xbase) / (size.getX() * size.getX())
+ (ybase * ybase) / (size.getY() * size.getY())
+ (zbase * zbase) / (size.getZ() * size.getZ()) < 1);
}

String prefix = "ellipsoid";

@Override
public String getPrefix() {
return prefix;
}

@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}

@Override
public boolean isUnique() {
return false;
}

@Override
public String getObjectType() {
return "Ellipsoid";
}

@Override
public String identify() {
return "ellipsoid@" + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getWorld().getName()
+ "," + size.getX() + "," + size.getY() + "," + size.getZ();
}

@Override
public String identifySimple() {
return identify();
}

@Override
public String toString() {
return identify();
}

@Override
public dObject setPrefix(String prefix) {
if (prefix != null)
this.prefix = prefix;
return this;
}

@Override
public String getAttribute(Attribute attribute) {

// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) return returned;
}

return new Element(identify()).getAttribute(attribute);
}

}
18 changes: 13 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -1235,17 +1235,25 @@ else return new Element(this.distance(toLocation))
}

// <--[tag]
// @attribute <l@location.is_within[<cuboid>]>
// @attribute <l@location.is_within[<cuboid>/<ellipsoid>]>
// @returns Element(Boolean)
// @description
// Returns whether the location is within the cuboid.
// -->
if (attribute.startsWith("is_within")
&& attribute.hasContext(1)) {
dCuboid cuboid = dCuboid.valueOf(attribute.getContext(1));
if (cuboid != null)
return new Element(cuboid.isInsideCuboid(this))
.getAttribute(attribute.fulfill(1));
if (dEllipsoid.matches(attribute.getContext(1))) {
dEllipsoid ellipsoid = dEllipsoid.valueOf(attribute.getContext(1));
if (ellipsoid != null)
return new Element(ellipsoid.contains(this))
.getAttribute(attribute.fulfill(1));
}
else {
dCuboid cuboid = dCuboid.valueOf(attribute.getContext(1));
if (cuboid != null)
return new Element(cuboid.isInsideCuboid(this))
.getAttribute(attribute.fulfill(1));
}
}


Expand Down

0 comments on commit 3eb7cee

Please sign in to comment.