Skip to content

Commit

Permalink
Fix dItem identification issue. Add Animate command. Example usage: -…
Browse files Browse the repository at this point in the history
… animate entity:n@<npc.id> animation:arm_swing
  • Loading branch information
davidcernat committed Jul 4, 2013
1 parent 7bb0b40 commit 618176c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 30 deletions.
28 changes: 15 additions & 13 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public dItem(ItemStack item) {

// Bukkit itemstack associated

private ItemStack item;
private ItemStack item = null;

public ItemStack getItemStack() {
return item;
Expand Down Expand Up @@ -359,19 +359,21 @@ public String debug() {
@Override
public String identify() {
// If saved item, return that
if (isSaved(this))
return "i@" + getSaved(this);

// If not a saved item, but is a custom item, return the script id
else if (CustomNBT.hasCustomNBT(getItemStack(), "denizen-script-id"))
return CustomNBT.getCustomNBT(getItemStack(), "denizen-script-id");

// Else, return the material name and data (if not 0)
else if (getItemStack() != null)
return getItemStack().getType().name().toLowerCase()
if (getItemStack() == null) return null;

if (getItemStack().getTypeId() != 0) {

if (isSaved(this))
return "i@" + getSaved(this);

// If not a saved item, but is a custom item, return the script id
else if (CustomNBT.hasCustomNBT(getItemStack(), "denizen-script-id"))
return CustomNBT.getCustomNBT(getItemStack(), "denizen-script-id");
}

// Else, return the material name and data
return getItemStack().getType().name().toLowerCase()
+ (getItemStack().getData().getData() != 0 ? ":" + getItemStack().getData().getData() : "");

return "null";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ public <T extends RegistrationableInstance> T get(Class<T> clazz) {

@Override
public void registerCoreMembers() {
registerCoreMember(AnnounceCommand.class,
"ANNOUNCE", "announce [\"announcement text\"] (to_ops)", 1);

registerCoreMember(AttackCommand.class,
"ATTACK", "attack (stop)", 0);

registerCoreMember(AnimateChestCommand.class,
"ANIMATECHEST", "animatechest [location:x,y,z,world] ({open}|close) (sound:{true}|false)", 1);

registerCoreMember(AnchorCommand.class,
"ANCHOR", "anchor [id:name] [assume|add|remove|walkto|walknear] (range:#)", 2);

registerCoreMember(AnimateCommand.class,
"ANIMATE", "animate [entities:<entity>|...] [animation:<name>]", 1);

registerCoreMember(AnimateChestCommand.class,
"ANIMATECHEST", "animatechest [location:x,y,z,world] ({open}|close) (sound:{true}|false)", 1);

registerCoreMember(AnnounceCommand.class,
"ANNOUNCE", "announce [\"announcement text\"] (to_ops)", 1);

registerCoreMember(AssignmentCommand.class,
"ASSIGNMENT", "assignment [{set}|remove] (script:assignment_script)", 1);

registerCoreMember(AttackCommand.class,
"ATTACK", "attack (stop)", 0);

registerCoreMember(BurnCommand.class,
"BURN", "burn [entities:<entity>|...] (duration:<value>)", 1);
Expand All @@ -87,11 +91,11 @@ public void registerCoreMembers() {
registerCoreMember(CopyBlockCommand.class,
"COPYBLOCK", "copyblock [location:x,y,z,world] [to:x,y,z,world]", 1);

registerCoreMember(DetermineCommand.class,
"DETERMINE", "determine [\"value\"]", 1);

registerCoreMember(DefineCommand.class,
"DEFINE", "define [id] [\"value\"]", 2);

registerCoreMember(DetermineCommand.class,
"DETERMINE", "determine [\"value\"]", 1);

registerCoreMember(DisengageCommand.class,
"DISENGAGE", "disengage (npcid:#)", 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import java.util.List;

import org.bukkit.EntityEffect;
import org.bukkit.entity.Player;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.citizensnpcs.util.PlayerAnimation;

/**
* Animate a list of entities using an EntityEffect.
*
* @author David Cernat
*/

public class AnimateCommand extends AbstractCommand {

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

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

if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentType(dList.class)
&& arg.matchesPrefix("entity, entities, e")) {
// Entity arg
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

if (!scriptEntry.hasObject("animation")
&& arg.matchesEnum(PlayerAnimation.values()))
// add type
scriptEntry.addObject("animation", PlayerAnimation.valueOf(arg.getValue().toUpperCase()));

if (!scriptEntry.hasObject("animation")
&& arg.matchesEnum(EntityEffect.values()))
// add type
scriptEntry.addObject("effect", EntityEffect.valueOf(arg.getValue().toUpperCase()));
}

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITY");

if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("animation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ANIMATION");
}

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
PlayerAnimation animation = scriptEntry.hasObject("animation") ?
(PlayerAnimation) scriptEntry.getObject("animation") : null;
EntityEffect effect = scriptEntry.hasObject("effect") ?
(EntityEffect) scriptEntry.getObject("effect") : null;

// Report to dB
dB.report(getName(), animation != null ? animation.name() : effect.name());

// Go through all the entities and animate them
for (dEntity entity : entities) {
if (entity.isSpawned() == true) {
if (animation != null && entity.getBukkitEntity() instanceof Player) {

Player player = (Player) entity.getBukkitEntity();

PlayerAnimation[] animationArray = PlayerAnimation.class.getEnumConstants();

for (PlayerAnimation current : animationArray) {

if (current.equals(animation)) {
current.play(player);
break;
}
}
}
else entity.getBukkitEntity().playEffect(effect);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
public void playerInteractEntity(PlayerInteractEntityEvent event) {

Entity entity = event.getRightClicked();
dItem item = new dItem(event.getPlayer().getItemInHand());

String determination;
Map<String, Object> context = new HashMap<String, Object>();
Expand All @@ -935,13 +936,22 @@ public void playerInteractEntity(PlayerInteractEntityEvent event) {
List<String> events = new ArrayList<String>();
events.add("player right clicks entity");
events.add("player right clicks " + entity.getType().name());

events.add("player right clicks entity with " +
item.identify().split(":")[0]);
events.add("player right clicks entity with " +
item.identify());
events.add("player right clicks " + entity.getType().name() + " with " +
item.identify().split(":")[0]);
events.add("player right clicks " + entity.getType().name() + " with " +
item.identify());

if (entity instanceof ItemFrame) {
dItem item = new dItem(((ItemFrame) entity).getItem());
context.put("item", item);
dItem itemFrame = new dItem(((ItemFrame) entity).getItem());
context.put("itemframe", itemFrame);

events.add("player right clicks " + entity.getType().name() + " " +
item.identify().split(":")[0]);
itemFrame.identify().split(":")[0]);
}

determination = doEvents(events, null, event.getPlayer(), context);
Expand Down

0 comments on commit 618176c

Please sign in to comment.