Skip to content

Commit

Permalink
Fix <player.item_in_hand.lore>, work on OO tag system.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Mar 25, 2013
1 parent ca8fbd1 commit 35ba49e
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 254 deletions.
Expand Up @@ -10,6 +10,7 @@
import net.aufdemrand.denizen.utilities.arguments.Item;
import net.aufdemrand.denizen.utilities.arguments.Location;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.dList;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
Expand All @@ -21,7 +22,6 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.*;

import java.util.Arrays;
Expand Down Expand Up @@ -88,8 +88,7 @@ public void commandEvent(PlayerCommandPreprocessEvent event) {

// Well, this is ugly :(
// Fill tags in any arguments
net.aufdemrand.denizen.utilities.arguments.List args = new net.aufdemrand.denizen.utilities.arguments
.List(Arrays.asList(aH.buildArgs(TagManager.tag(event.getPlayer(), null,
dList args = new dList(Arrays.asList(aH.buildArgs(TagManager.tag(event.getPlayer(), null,
(event.getMessage().split(" ").length > 1 ? event.getMessage().split(" ", 2)[1] : "")))));

String command = event.getMessage().split(" ")[0].replace("/", "").toUpperCase();
Expand Down Expand Up @@ -350,6 +349,7 @@ public void playerEat(EntityRegainHealthEvent event) {
&& !CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
Map<String, String> context = new HashMap<String, String>();
context.put("reason", event.getRegainReason().toString());
context.put("amount", String.valueOf(event.getAmount()));

String determination = doEvent("player regains health", null, (Player) event.getEntity(), context);

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/aufdemrand/denizen/tags/Attribute.java
Expand Up @@ -34,6 +34,12 @@ public boolean startsWith(String string) {
return false;
}

public boolean startsWith(String string, int attribute) {
string = string.toLowerCase();
if (this.attribute.split("\\.")[attribute - 1].startsWith(string)) return true;
return false;
}

public Attribute fulfill(int attributes) {
if (attribute.split("\\.").length >= attributes)
attribute = "";
Expand Down
457 changes: 230 additions & 227 deletions src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java

Large diffs are not rendered by default.

Expand Up @@ -14,7 +14,7 @@ public class Element implements dScriptArgument {
/**
*
* @param string the string or dScript argument String
* @return a dScript List
* @return a dScript dList
*
*/
public static Element valueOf(String string) {
Expand Down Expand Up @@ -119,6 +119,17 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("split") && attribute.startsWith("limit", 2)) {
String split_string = (attribute.hasContext(1) ? attribute.getContext(1) : " ");
Integer limit = (attribute.hasContext(2) ? attribute.getIntContext(2) : 1);
return new dList(Arrays.asList(element.split(split_string, limit))).getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("split")) {
String split_string = (attribute.hasContext(1) ? attribute.getContext(1) : " ");
return new dList(Arrays.asList(element.split(split_string))).getAttribute(attribute.fulfill(1));
}

return element;
}

Expand Down
130 changes: 128 additions & 2 deletions src/main/java/net/aufdemrand/denizen/utilities/arguments/Entity.java
Expand Up @@ -3,6 +3,8 @@
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;
import org.bukkit.entity.LivingEntity;

import java.util.HashMap;
Expand Down Expand Up @@ -74,7 +76,7 @@ public static Entity valueOf(String string) {
private String id = null;
private String prefix = "Entity";

LivingEntity entity;
private LivingEntity entity;

public Entity(LivingEntity entity) {
this.entity = entity;
Expand Down Expand Up @@ -136,9 +138,133 @@ public dScriptArgument setPrefix(String prefix) {
return this;
}

public LivingEntity getEntity(String string) {
return entity;
}

@Override
public String getAttribute(Attribute attribute) {
return null; //To change body of implemented methods use File | Settings | File Templates.

if (attribute == null) return null;

// Desensitize the attribute for comparison
String id = this.id.toLowerCase();

if (attribute.startsWith("name"))
return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1));

if (attribute.startsWith("location.cursor_on")) {
int range = attribute.getIntContext(2);
if (range < 1) range = 50;
return new Location(entity.getTargetBlock(null, range).getLocation())
.getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("location.standing_on"))
return new Location(entity.getLocation().add(0, -1, 0))
.getAttribute(attribute.fulfill(2));

if (attribute.startsWith("location"))
return new Location(entity.getLocation())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("health.formatted")) {
int maxHealth = entity.getMaxHealth();
if (attribute.hasContext(2))
maxHealth = attribute.getIntContext(2);
if ((float) entity.getHealth() / maxHealth < .10)
return new Element("dying").getAttribute(attribute.fulfill(2));
else if ((float) entity.getHealth() / maxHealth < .40)
return new Element("seriously wounded").getAttribute(attribute.fulfill(2));
else if ((float) entity.getHealth() / maxHealth < .75)
return new Element("injured").getAttribute(attribute.fulfill(2));
else if ((float) entity.getHealth() / maxHealth < 1)
return new Element("scraped").getAttribute(attribute.fulfill(2));

else return new Element("healthy").getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("health.percentage")) {
int maxHealth = entity.getMaxHealth();
if (attribute.hasContext(2))
maxHealth = attribute.getIntContext(2);
return new Element(String.valueOf(((float) entity.getHealth() / maxHealth) * 100))
.getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("health"))
return new Element(String.valueOf(entity.getHealth()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("is_inside_vehicle"))
return new Element(String.valueOf(entity.isInsideVehicle()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("killer"))
return new Player(entity.getKiller())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("last_damage_cause"))
return new Element(String.valueOf(entity.getLastDamageCause().getCause().toString()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("last_damage"))
return new Element(String.valueOf(entity.getLastDamage()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("time_lived"))
return new Duration(entity.getTicksLived() / 20)
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("can_pickup_items"))
return new Element(String.valueOf(entity.getCanPickupItems()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("entity_id"))
return new Element(String.valueOf(entity.getEntityId()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("fall_distance"))
return new Element(String.valueOf(entity.getFallDistance()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("uuid"))
return new Element(String.valueOf(entity.getUniqueId().toString()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("has_effect")) {
// Add later
}

if (attribute.startsWith("equipment")) {
// Add later
}

if (attribute.startsWith("world")) {
// Add world dScriptArg
}

if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("debug")) {
return new Element(debug())
.getAttribute(attribute.fulfill(1));
}

return dScriptArgValue();
}

}
Expand Up @@ -300,8 +300,8 @@ public String getAttribute(Attribute attribute) {

if (attribute.startsWith("lore")) {
if (hasItemMeta() && getItemMeta().hasLore())
return new List(getItemMeta().getLore()).getAttribute(attribute.fulfill(1));
else return new List("Empty List", "").getAttribute(attribute.fulfill(1));
return new dList(getItemMeta().getLore()).getAttribute(attribute.fulfill(1));
else return new dList("Empty dList", "").getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("prefix"))
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/net/aufdemrand/denizen/utilities/arguments/Location.java
Expand Up @@ -218,6 +218,130 @@ public dScriptArgument setPrefix(String prefix) {
public String getAttribute(Attribute attribute) {


if (attribute == null) return null;

if (attribute.startsWith("biome.formatted"))
return new Element(getBlock().getBiome().name().toLowerCase().replace('_', ' '))
.getAttribute(attribute.fulfill(2));

if (attribute.startsWith("biome.humidity"))
return new Element(String.valueOf(getBlock().getHumidity()))
.getAttribute(attribute.fulfill(2));

if (attribute.startsWith("biome.temperature"))
return new Element(String.valueOf(getBlock().getTemperature()))
.getAttribute(attribute.fulfill(2));

if (attribute.startsWith("biome"))
return new Element(String.valueOf(getBlock().getBiome().name()))
.getAttribute(attribute.fulfill(1));

// else if (type.equals("BLOCK"))
// {
// if (subType.equals("BELOW"))
// {
// fromLocation = new Location(fromLocation.add(0, -1, 0));
// }
//
// else if (subType.equals("MATERIAL") || specifier.equals("MATERIAL"))
// {
// event.setReplaced(fromLocation.getBlock().getType().toString());
// }
// }
//
// else if (type.equals("DIRECTION"))
// {
// if (fromLocation != null && toLocation != null)
// {
// event.setReplaced(Utilities.getCardinal(Utilities.getYaw
// (toLocation.toVector().subtract
// (fromLocation.toVector()).normalize())));
// }
// }
//
// else if (type.equals("DISTANCE"))
// {
// if (fromLocation != null && toLocation != null)
// {
// if (subType.equals("ASINT"))
// {
// event.setReplaced(String.valueOf((int)fromLocation.distance(toLocation)));
// }
// else if (subType.equals("VERTICAL"))
// {
// if (fromLocation.getWorld().getName() == toLocation.getWorld().getName()
// || specifier.equals("MULTIWORLD"))
// {
// // Only calculate distance between locations on different worlds
// // if the MULTIWORLD specifier is used
// event.setReplaced(String.valueOf(Math.abs(
// fromLocation.getY() - toLocation.getY())));
// }
// }
// else if (subType.equals("HORIZONTAL"))
// {
// if (fromLocation.getWorld().getName() == toLocation.getWorld().getName()
// || specifier.equals("MULTIWORLD"))
// {
// // Only calculate distance between locations on different worlds
// // if the MULTIWORLD specifier is used
// event.setReplaced(String.valueOf(Math.sqrt(
// Math.pow(fromLocation.getX() - toLocation.getX(), 2) +
// Math.pow(fromLocation.getZ() - toLocation.getZ(), 2))));
// }
// }
// else
// event.setReplaced(String.valueOf(fromLocation.distance(toLocation)));
// }
// }
//
// else if (type.equals("FORMATTED"))
// event.setReplaced("X '" + fromLocation.getX()
// + "', Y '" + fromLocation.getY()
// + "', Z '" + fromLocation.getZ()
// + "', in world '" + fromLocation.getWorld().getName() + "'");
//
// else if (type.equals("IS_LIQUID"))
// {
// event.setReplaced(String.valueOf(fromLocation.getBlock().isLiquid()));
// }
//
// else if (type.equals("LIGHT"))
// {
// if (subType.equals("BLOCKS"))
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightFromBlocks()));
// else if (subType.equals("SKY"))
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightFromSky()));
// else
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightLevel()));
// }
//
// else if (type.equals("POWER"))
// {
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getBlockPower()));
// }
//
// else if (type.equals("TIME"))
// {
// if (subType.equals("PERIOD"))
// if (fromLocation.getWorld().getTime() < 13500 ||
// fromLocation.getWorld().getTime() > 23000)
// event.setReplaced("day");
// else if (fromLocation.getWorld().getTime() > 13500)
// event.setReplaced("night");
// }
//
// else if (type.equals("WORLD"))
// event.setReplaced(fromLocation.getWorld().getName());
//
// else if (type.equals("X"))
// event.setReplaced(String.valueOf(fromLocation.getX()));
//
// else if (type.equals("Y"))
// event.setReplaced(String.valueOf(fromLocation.getY()));
//
// else if (type.equals("Z"))
// event.setReplaced(String.valueOf(fromLocation.getZ()));

return dScriptArgValue();
}
Expand Down
Expand Up @@ -10,15 +10,12 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.World;

import java.text.DecimalFormat;
import java.util.*;

public class Player implements dScriptArgument {

/**
*
* @param string the string or dScript argument String
* @return a dScript List
* @return a dScript dList
*
*/
public static Player valueOf(String string) {
Expand Down Expand Up @@ -126,7 +123,7 @@ public String getAttribute(Attribute attribute) {
return new Element(String.valueOf(isOnline())).getAttribute(attribute.fulfill(1));

if (attribute.startsWith("chat_history"))
return new List(PlayerTags.playerChatHistory.get(player))
return new dList(PlayerTags.playerChatHistory.get(player))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("location.bed_spawn"))
Expand Down

0 comments on commit 35ba49e

Please sign in to comment.