Skip to content

Commit

Permalink
Implement simple mechanism API.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Oct 19, 2013
1 parent cffbdab commit 96d0a79
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Adjustable.java
@@ -0,0 +1,17 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.scripts.commands.core.AdjustCommand;
import net.aufdemrand.denizen.tags.Attribute;

public interface Adjustable {

/**
* Gets a specific attribute using this object to fetch the necessary data.
*
* @param mechanism the name of the mechanism
* @param value the value of the mechanism
* @return a string result of the fetched attribute
*/
public void adjust(Mechanism mechanism, Element value);

}
27 changes: 27 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Mechanism.java
@@ -0,0 +1,27 @@
package net.aufdemrand.denizen.objects;

public class Mechanism {

boolean fulfilled;
String raw_mechanism;
String outcome = null;

public Mechanism(String string) {
fulfilled = false;
raw_mechanism = string;
}

public void fulfill(String outcome) {
fulfilled = true;
outcome = outcome;
}

public boolean matches(String string) {
return (string.equalsIgnoreCase(raw_mechanism));
}

public boolean fulfilled() {
return fulfilled;
}

}
57 changes: 52 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dChunk.java
Expand Up @@ -19,21 +19,22 @@
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.potion.PotionEffect;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class dChunk extends CraftChunk implements dObject {
public class dChunk extends CraftChunk implements dObject, Adjustable {

//////////////////
// OBJECT FETCHER
////////////////

/**
* Gets a Chunk Object from a string form of x,y,z,world.
* This is not to be confused with the 'x,y,world' of a
* location, which is a finer grain of unit in Worlds..
* Gets a Chunk Object from a string form of x,z,world.
* This is not to be confused with the 'x,y,z,world' of a
* location, which is a finer grain of unit in a dWorlds.
*
* @param string the string or dScript argument String
* @return a dChunk, or null if incorrectly formatted
Expand Down Expand Up @@ -122,7 +123,7 @@ public boolean isUnique() {

@Override
public String identify() {
return "ch@" + ',' + getX() + ',' + getZ() + ',' + getWorld().getName();
return "ch@" + getX() + ',' + getZ() + ',' + getWorld().getName();
}

@Override
Expand Down Expand Up @@ -200,6 +201,21 @@ public String getAttribute(Attribute attribute) {
return entities.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <ch@chunk.players>
// @returns dList(dPlayer)
// @description
// returns a list of players in the chunk.
// -->
if (attribute.startsWith("players")) {
dList entities = new dList();
for (Entity ent : this.getEntities())
if (ent instanceof Player)
entities.add(new dPlayer((Player) ent).identify());

return entities.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <ch@chunk.height_map>
// @returns dList(Element)
Expand Down Expand Up @@ -262,4 +278,35 @@ public String getAttribute(Attribute attribute) {
return new Element(identify()).getAttribute(attribute);
}

@Override
public void adjust(Mechanism mechanism, Element value) {

if (mechanism.matches("unload")) {
unload(true);
return;
}

if (mechanism.matches("unload_safely")) {
unload(true, true);
return;
}

if (mechanism.matches("unload_without_saving")) {
unload(false);
return;
}

if (mechanism.matches("load")) {
load(true);
return;
}

if (mechanism.matches("regenerate")) {
getWorld().regenerateChunk(getX(), getZ());
return;
}


}

}
40 changes: 38 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -4,11 +4,15 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.properties.EntityAge;
import net.aufdemrand.denizen.objects.properties.EntityInfected;
import net.aufdemrand.denizen.objects.properties.EntityProfessional;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
Expand Down Expand Up @@ -45,10 +49,11 @@
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

public class dEntity implements dObject {
public class dEntity implements dObject, Adjustable {

/////////////////////
// PATTERNS
Expand Down Expand Up @@ -854,7 +859,6 @@ public void setEntity(Entity entity) {
this.entity = entity;
}


// Used to store some information about a livingEntity while it's despawned
private class DespawnedEntity {

Expand Down Expand Up @@ -1756,5 +1760,37 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
}


@Override
public void adjust(Mechanism mechanism, Element value) {

if (mechanism.matches("remove_effects")) {
for (PotionEffect potionEffect : this.getLivingEntity().getActivePotionEffects())
getLivingEntity().removePotionEffect(potionEffect.getType());
return;
}

if (mechanism.matches("set_remove_when_far_away"))
getLivingEntity().setRemoveWhenFarAway(value.asBoolean());

if (mechanism.matches("set_custom_name"))
getLivingEntity().setCustomName(value.asString());

if (mechanism.matches("set_custom_name_visibility"))
getLivingEntity().setCustomNameVisible(value.asBoolean());

if (mechanism.matches("set_can_pickup_items"))
getLivingEntity().setCanPickupItems(value.asBoolean());

if (mechanism.matches("set_leash_holder"))
getLivingEntity().setLeashHolder(dEntity.valueOf(value.asString()).getBukkitEntity());

if (mechanism.matches("set_remaining_air"))
getLivingEntity().setRemainingAir(value.asInt());


}




}
Expand Up @@ -85,6 +85,26 @@ public <T extends RegistrationableInstance> T get(Class<T> clazz) {
@Override
public void registerCoreMembers() {

// <--[command]
// @Name Adjust
// @Usage adjust [<dObject>] [<mechanism>](:<value>)
// @Required 2
// @Stable stable
// @Short Adjusts a dObjects mechanism.
// @Author aufdemrand

// @Description
// Many dObjects contains options and properties that need to be adjusted. Denizen employs a mechanism
// interface to deal with those adjustments. To easily accomplish this, use this command with a valid object
// mechanism, and sometimes accompanying value.

// @Usage
// Use to set a custom display name on an entity.
// - adjust e@1000 'set_custom_name:ANGRY!'

// -->
registerCoreMember(AdjustCommand.class,
"ADJUST", "adjust [<dObject>] [<mechanism>](:<value>)", 2);

// <--[command]
// @Name Age
Expand Down
@@ -0,0 +1,79 @@
package net.aufdemrand.denizen.scripts.commands.core;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;

import java.lang.reflect.InvocationTargetException;


public class AdjustCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("object")) {
scriptEntry.addObject("object", arg.asElement());

} else if (!scriptEntry.hasObject("mechanism")) {
if (arg.hasPrefix()) {
scriptEntry.addObject("mechanism", new Element(arg.getPrefix().getValue()));
scriptEntry.addObject("mechanism_value", arg.asElement());
} else
scriptEntry.addObject("mechanism", arg.asElement());

}

}

// Check for valid object

// Check for valid mechanism

}


@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

String object = scriptEntry.getElement("object").asString();

Class object_class = net.aufdemrand.denizen.tags.ObjectFetcher.getObjectClass(object.split("@")[0]);

if (object_class == null)
throw new CommandExecutionException("Unfetchable object found '" + object + '\'');

dObject fetched;

try {
// Check to make sure this is a valid constructor by checking the 'matches' static method
if (!((Boolean) object_class.getMethod("matches", String.class)

This comment has been minimized.

Copy link
@fullwall

fullwall Oct 20, 2013

Contributor

Cache the expensive .getMethod call?

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Oct 20, 2013

Author Contributor

Will do.

.invoke(null, object)))
throw new CommandExecutionException('\'' + object + "' is returning null.");

// Get the object with the 'valueOf' static method
fetched = (dObject) object_class.getMethod("valueOf", String.class)
.invoke(null, object);

// Make sure this object is Adjustable
if (!(fetched instanceof Adjustable))
throw new CommandExecutionException('\'' + object + "' is not adjustable.");

// Do the adjustment!
((Adjustable) fetched).adjust(new Mechanism(scriptEntry.getElement("mechanism").asString()),
scriptEntry.getElement("mechanism_value"));

} catch (NoSuchMethodException e) { } catch (SecurityException e) {
} catch (InvocationTargetException e) { } catch (IllegalAccessException e) { }

// :)

}



}

0 comments on commit 96d0a79

Please sign in to comment.