Skip to content

Commit

Permalink
Move some dScript code to a property
Browse files Browse the repository at this point in the history
So the file can be moved to the core
  • Loading branch information
mcmonkey4eva committed Nov 5, 2014
1 parent e949577 commit e501a53
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 78 deletions.
81 changes: 7 additions & 74 deletions src/main/java/net/aufdemrand/denizen/objects/dScript.java
Expand Up @@ -5,10 +5,7 @@
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.core.CooldownCommand;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptHelper;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
Expand Down Expand Up @@ -234,54 +231,6 @@ public String getAttribute(Attribute attribute) {
return new Element(container.getContainerType())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <s@script.cooled_down[<player>]>
// @returns Element(Boolean)
// @description
// Returns whether the script is currently cooled down for the player. Any global
// cooldown present on the script will also be taken into account. Not specifying a player will result in
// using the attached player available in the script entry. Not having a valid player will result in 'null'.
// -->
if (attribute.startsWith("cooled_down")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
if (player != null && player.isValid())
return new Element(CooldownCommand.checkCooldown(player, container.getName()))
.getAttribute(attribute.fulfill(1));
else return "null";
}

// <--[tag]
// @attribute <s@script.requirements[<player>].check[<path>]>
// @returns Element
// @description
// Returns whether the player specified (defaults to current) has the requirement.
// Must be an INTERACT script.
// -->
if (attribute.startsWith("requirements.check")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
if (attribute.hasContext(2))
return new Element(((InteractScriptContainer)container).checkRequirements(player,
attribute.getScriptEntry().getNPC(),
attribute.getContext(2)))
.getAttribute(attribute.fulfill(2));
}

// <--[tag]
// @attribute <s@script.cooldown[<player>]>
// @returns Duration
// @description
// Returns the time left for the player to cooldown for the script.
// -->
if (attribute.startsWith("cooldown")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
return CooldownCommand.getCooldownDuration(player, name)
.getAttribute(attribute.fulfill(1));

}

// <--[tag]
// @attribute <s@script.name>
// @returns Element
Expand Down Expand Up @@ -399,21 +348,18 @@ else return new Element(TagManager.tag(attribute.getScriptEntry() == null ? null
}

// <--[tag]
// @attribute <s@script.step[<player>]>
// @attribute <s@script.to_json>
// @returns Element
// @description
// Returns the name of a script step that the player is currently on.
// Converts the YAML Script Container to a JSON array.
// Best used with 'yaml data' type scripts.
// -->
if (attribute.startsWith("step")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());

if (player != null && player.isValid())
return new Element(InteractScriptHelper.getCurrentStep(player, container.getName()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("to_json")) {
JSONObject jsobj = new JSONObject(container.getConfigurationSection("").getMap());
jsobj.remove("TYPE");
return new Element(jsobj.toString()).getAttribute(attribute.fulfill(1));
}


/////////////////
// dObject attributes
///////////////
Expand Down Expand Up @@ -453,19 +399,6 @@ else return new Element(TagManager.tag(attribute.getScriptEntry() == null ? null
return new Element(getObjectType()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <s@script.to_json>
// @returns Element
// @description
// Converts the YAML Script Container to a JSON array.
// Best used with 'yaml data' type scripts.
// -->
if (attribute.startsWith("to_json")) {
JSONObject jsobj = new JSONObject(container.getConfigurationSection("").getMap());
jsobj.remove("TYPE");
return new Element(jsobj.toString()).getAttribute(attribute.fulfill(1));
}

// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
Expand Down
@@ -1,9 +1,7 @@
package net.aufdemrand.denizen.objects.properties;

import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.properties.bukkit.BukkitScriptProperties;
import net.aufdemrand.denizen.objects.properties.entity.*;
import net.aufdemrand.denizen.objects.properties.inventory.*;
import net.aufdemrand.denizen.objects.properties.item.*;
Expand All @@ -29,6 +27,9 @@ public PropertyParser() {
describes.clear();
getFrom.clear();

// register properties that add Bukkit code to core objects
registerProperty(BukkitScriptProperties.class, dScript.class);

// register core dEntity properties
registerProperty(EntityAge.class, dEntity.class);
registerProperty(EntityAngry.class, dEntity.class);
Expand Down
@@ -0,0 +1,121 @@
package net.aufdemrand.denizen.objects.properties.bukkit;

import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.scripts.commands.core.CooldownCommand;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptHelper;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.core.EscapeTags;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;

import java.util.ArrayList;

public class BukkitScriptProperties implements Property {

public static boolean describes(dObject script) {
return script instanceof dScript;
}

public static BukkitScriptProperties getFrom(dObject script) {
if (!describes(script)) return null;
else return new BukkitScriptProperties((dScript)script);
}


private BukkitScriptProperties(dScript script) {
this.script = script;
}

dScript script;

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) return "null";

// <--[tag]
// @attribute <s@script.cooled_down[<player>]>
// @returns Element(Boolean)
// @description
// Returns whether the script is currently cooled down for the player. Any global
// cooldown present on the script will also be taken into account. Not specifying a player will result in
// using the attached player available in the script entry. Not having a valid player will result in 'null'.
// -->
if (attribute.startsWith("cooled_down")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
if (player != null && player.isValid())
return new Element(CooldownCommand.checkCooldown(player, script.getContainer().getName()))
.getAttribute(attribute.fulfill(1));
else return "null";
}

// <--[tag]
// @attribute <s@script.requirements[<player>].check[<path>]>
// @returns Element
// @description
// Returns whether the player specified (defaults to current) has the requirement.
// Must be an INTERACT script.
// -->
if (attribute.startsWith("requirements.check")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
if (attribute.hasContext(2))
return new Element(((InteractScriptContainer)script.getContainer()).checkRequirements(player,
attribute.getScriptEntry().getNPC(),
attribute.getContext(2)))
.getAttribute(attribute.fulfill(2));
}

// <--[tag]
// @attribute <s@script.cooldown[<player>]>
// @returns Duration
// @description
// Returns the time left for the player to cooldown for the script.
// -->
if (attribute.startsWith("cooldown")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
return CooldownCommand.getCooldownDuration(player, script.getName())
.getAttribute(attribute.fulfill(1));

}


// <--[tag]
// @attribute <s@script.step[<player>]>
// @returns Element
// @description
// Returns the name of a script step that the player is currently on.
// Must be an INTERACT script.
// -->
if (attribute.startsWith("step")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());

if (player != null && player.isValid())
return new Element(InteractScriptHelper.getCurrentStep(player, script.getContainer().getName()))
.getAttribute(attribute.fulfill(1));
}
return null;
}


@Override
public String getPropertyString() {
return null;
}

@Override
public String getPropertyId() {
return "BukkitScriptProperties";
}

@Override
public void adjust(Mechanism mechanism) {
// None
}
}

0 comments on commit e501a53

Please sign in to comment.