Skip to content

Commit

Permalink
Rewrote dBiome tags in new registerTag format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jul 23, 2015
1 parent 027283a commit f5dac10
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 77 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -717,6 +717,8 @@ public void onEnable() {
ObjectFetcher.registerWithObjectFetcher(dPlugin.class); // pl@
ObjectFetcher.registerWithObjectFetcher(dEllipsoid.class); // ellipsoid@
ObjectFetcher.registerWithObjectFetcher(dBiome.class); // b@
dBiome.registerTags(); // TODO: Automate this once all classes have tag registries


// Register Core dObjects with the ObjectFetcher
ObjectFetcher._registerCoreObjects();
Expand Down
195 changes: 118 additions & 77 deletions src/main/java/net/aufdemrand/denizen/objects/dBiome.java
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.block.Biome;
import org.bukkit.entity.EntityType;

import java.util.HashMap;
import java.util.List;

public class dBiome implements dObject, Adjustable {
Expand Down Expand Up @@ -120,9 +121,8 @@ public dObject setPrefix(String prefix) {
return this;
}

@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;

public static void registerTags() {

// <--[tag]
// @attribute <b@biome.downfall_type>
Expand All @@ -131,90 +131,131 @@ public String getAttribute(Attribute attribute) {
// Returns this biome's downfall type for when a world has weather.
// This can be RAIN, SNOW, or NONE.
// -->
if (attribute.startsWith("downfall_type"))
return new Element(CoreUtilities.toLowerCase(biome.getDownfallType().name()))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <b@biome.humidity>
// @returns Element(Decimal)
// @description
// Returns the humidity of this biome.
// -->
else if (attribute.startsWith("humidity"))
return new Element(biome.getHumidity()).getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <b@biome.temperature>
// @returns Element(Decimal)
// @description
// Returns the temperature of this biome.
// -->
else if (attribute.startsWith("temperature"))
return new Element(biome.getTemperature()).getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <b@biome.spawnable_entities>
// @returns dList(dEntity)
// @description
// Returns all entities that spawn naturally in this biome.
// -->
else if (attribute.startsWith("spawnable_entities")) {
attribute = attribute.fulfill(1);

List<EntityType> entityTypes;
boolean hasAttribute = true;

// <--[tag]
// @attribute <b@biome.spawnable_entities.ambient>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in ambient locations.
// Default examples: BAT
// -->
if (attribute.startsWith("ambient"))
entityTypes = biome.getAmbientEntities();
registerTag("downfall_type", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(CoreUtilities.toLowerCase(((dBiome) object).biome.getDownfallType().name()))
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <b@biome.spawnable_entities.creatures>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in creature locations.
// Default examples: PIG, COW, CHICKEN...
// -->
else if (attribute.startsWith("creatures"))
entityTypes = biome.getCreatureEntities();
// <--[tag]
// @attribute <b@biome.humidity>
// @returns Element(Decimal)
// @description
// Returns the humidity of this biome.
// -->
registerTag("humidity", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dBiome) object).biome.getHumidity())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <b@biome.temperature>
// @returns Element(Decimal)
// @description
// Returns the temperature of this biome.
// -->
registerTag("temperature", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dBiome) object).biome.getTemperature())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <b@biome.spawnable_entities>
// @returns dList(dEntity)
// @description
// Returns all entities that spawn naturally in this biome.
// -->
registerTag("spawnable_entities", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
attribute = attribute.fulfill(1);
BiomeNMS biome = ((dBiome) object).biome;

// <--[tag]
// @attribute <b@biome.spawnable_entities.monsters>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in monster locations.
// Default examples: CREEPER, ZOMBIE, SKELETON...
// -->
else if (attribute.startsWith("monsters"))
entityTypes = biome.getMonsterEntities();
List<EntityType> entityTypes;
boolean hasAttribute = true;

// <--[tag]
// @attribute <b@biome.spawnable_entities.water>
// @attribute <b@biome.spawnable_entities.ambient>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in underwater locations.
// Default examples: SQUID
// Returns the entities that spawn naturally in ambient locations.
// Default examples: BAT
// -->
else if (attribute.startsWith("water"))
entityTypes = biome.getWaterEntities();

else {
entityTypes = biome.getAllEntities();
hasAttribute = false;
if (attribute.startsWith("ambient"))
entityTypes = biome.getAmbientEntities();

// <--[tag]
// @attribute <b@biome.spawnable_entities.creatures>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in creature locations.
// Default examples: PIG, COW, CHICKEN...
// -->
else if (attribute.startsWith("creatures"))
entityTypes = biome.getCreatureEntities();

// <--[tag]
// @attribute <b@biome.spawnable_entities.monsters>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in monster locations.
// Default examples: CREEPER, ZOMBIE, SKELETON...
// -->
else if (attribute.startsWith("monsters"))
entityTypes = biome.getMonsterEntities();

// <--[tag]
// @attribute <b@biome.spawnable_entities.water>
// @returns dList(dEntity)
// @description
// Returns the entities that spawn naturally in underwater locations.
// Default examples: SQUID
// -->
else if (attribute.startsWith("water"))
entityTypes = biome.getWaterEntities();

else {
entityTypes = biome.getAllEntities();
hasAttribute = false;
}

dList list = new dList();
for (EntityType entityType : entityTypes) {
list.add(entityType.name());
}
return list.getAttribute(hasAttribute ? attribute.fulfill(1) : attribute);
}
});
}

public static HashMap<String, TagRunnable> registeredTags = new HashMap<String, TagRunnable>();

public static void registerTag(String name, TagRunnable runnable) {
if (runnable.name == null) {
runnable.name = name;
}
registeredTags.put(name, runnable);
}

@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;

dList list = new dList();
for (EntityType entityType : entityTypes) {
list.add(entityType.name());
// TODO: Scrap getAttribute, make this functionality a core system
String attrLow = CoreUtilities.toLowerCase(attribute.getAttributeWithoutContext(1));
TagRunnable tr = registeredTags.get(attrLow);
if (tr != null) {
if (!tr.name.equals(attrLow)) {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null,
"Using deprecated form of tag '" + tr.name + "': '" + attrLow + "'.");
}
return list.getAttribute(hasAttribute ? attribute.fulfill(1) : attribute);
return tr.run(attribute, this);
}

return new Element(identify()).getAttribute(attribute);
Expand Down

0 comments on commit f5dac10

Please sign in to comment.