Skip to content

Commit

Permalink
Add <cu@cuboid.has_town> and <cu@cuboid.list_towns>
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Mar 7, 2015
1 parent 0cee144 commit 8bc5563
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
@@ -0,0 +1,76 @@
package net.gnomeffinway.depenizen.extensions.towny;

import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.tags.Attribute;
import net.gnomeffinway.depenizen.extensions.dObjectExtension;
import net.gnomeffinway.depenizen.objects.dTown;
import org.bukkit.Location;

import java.util.ArrayList;
import java.util.List;

public class TownyCuboidExtension extends dObjectExtension {

public static boolean describes(dObject cuboid) {
return cuboid instanceof dCuboid;
}

public static TownyCuboidExtension getFrom(dObject cuboid) {
if (!describes(cuboid)) return null;
else return new TownyCuboidExtension((dCuboid) cuboid);
}

private TownyCuboidExtension(dCuboid cuboid) {
this.cuboid = cuboid;
}

dCuboid cuboid = null;

@Override
public String getAttribute(Attribute attribute) {

// <--[tag]
// @attribute <cu@cuboid.has_town>
// @returns Element(Boolean)
// @description
// Returns whether the cuboid contains any town at all.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("has_town")) {
for (Location location : cuboid.getBlockLocations()) {
if (TownyUniverse.getTownName(location) != null)
return new Element(true).getAttribute(attribute.fulfill(1));
}
return new Element(false).getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <cu@cuboid.list_towns>
// @returns dList(dTown)
// @description
// Returns all the towns within the cuboid.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("list_towns")) {
dList list = new dList();
List<String> towns = new ArrayList<String>();
try {
for (Location location : cuboid.getBlockLocations()) {
String townName = TownyUniverse.getTownName(location);
if (townName != null && !towns.contains(townName)) {
list.add(new dTown(TownyUniverse.getTownBlock(location).getTown()).identify());
towns.add(townName);
}
}
} catch (NotRegisteredException e) {}
return list.getAttribute(attribute.fulfill(1));
}

return null;
}
}
@@ -1,9 +1,11 @@
package net.gnomeffinway.depenizen.support.plugins;

import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.gnomeffinway.depenizen.extensions.towny.TownyCuboidExtension;
import net.gnomeffinway.depenizen.objects.dNation;
import net.gnomeffinway.depenizen.objects.dTown;
import net.gnomeffinway.depenizen.support.Support;
Expand All @@ -16,6 +18,7 @@ public TownySupport() {
registerObjects(dTown.class, dNation.class);
registerProperty(TownyPlayerExtension.class, dPlayer.class);
registerProperty(TownyLocationExtension.class, dLocation.class);
registerProperty(TownyCuboidExtension.class, dCuboid.class);
registerAdditionalTags("town", "nation");
}

Expand Down

6 comments on commit 8bc5563

@mcmonkey4eva
Copy link
Member

Choose a reason for hiding this comment

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

Surely there's a better way to accomplish this. This'll lag into hell with a large cuboid.

@Morphan1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well... Could loop through each town and then check if the locations are in that town but..
I don't think that'd make a difference

Just try not to use huge cuboids 😛

@mcmonkey4eva
Copy link
Member

Choose a reason for hiding this comment

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

How about loop through each town and do an AABB intersection test?

@Morphan1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there's an easy way to get the max or minimum points of a town

@mcmonkey4eva
Copy link
Member

Choose a reason for hiding this comment

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

Since when does easy limit you?

@Morphan1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like Towny internals just loops through the blocks in every town to check things like this @_@

Please sign in to comment.