Skip to content

Commit

Permalink
Add some more item world events. Make "on entity targets entity" use …
Browse files Browse the repository at this point in the history
…delayed targeting task instead of event's target method.
  • Loading branch information
davidcernat committed Jul 29, 2013
1 parent eb0439a commit 708cd0d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/aH.java
Expand Up @@ -158,8 +158,9 @@ public <T extends dObject> T asType(Class<? extends dObject> clazz) {
.invoke(null, value);

// dB.log("Created: " + clazz.cast(arg).debug());

return (T) clazz.cast(arg).setPrefix(prefix);
if (arg != null) {
return (T) clazz.cast(arg).setPrefix(prefix);
}

} catch (IllegalAccessException e) {
e.printStackTrace();
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -5,7 +5,6 @@
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -389,6 +388,25 @@ public String getLore(String prefix) {
return "";
}

/**
* Check whether this item contains the lore specific
* to item scripts.
*
* @return True if it does, otherwise false
*
*/
public boolean isItemscript() {

if (containsLore("§0id:")) {
return true;
}
return false;
}

public String getMaterial() {
return getItemStack().getType().name().toLowerCase();
}

public void setAmount(int value) {
if (item != null)
item.setAmount(value);
Expand Down Expand Up @@ -479,7 +497,7 @@ public String identify() {
return "i@" + getSaved(this);

// If not a saved item, but is a custom item, return the script id
else if (containsLore("§0id:")) {
else if (isItemscript()) {
return "i@" + getLore("§0id:");
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -270,6 +270,10 @@ public String getAttribute(Attribute attribute) {
else
return new Element(item).getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("last")) {
return new Element(get(size() - 1)).getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("prefix"))
return new Element(prefix)
Expand Down
Expand Up @@ -109,9 +109,9 @@ public static String doEvents(List<String> eventNames, dNPC npc, Player player,
if (script == null) continue;

for (String eventName : eventNames) {

if (!script.contains("EVENTS.ON " + eventName.toUpperCase())) continue;

// Fetch script from Event
//
// Note: a "new dPlayer(null)" will not be null itself,
Expand Down Expand Up @@ -175,12 +175,17 @@ public void blockBreak(BlockBreakEvent event) {
events.add("player breaks " + blockType + " with " + item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add("player breaks block with " +
item.identify().split(":")[0]);
events.add("player breaks " + blockType + " with " +
item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add("player breaks block with itemscript "
+ item.getMaterial());
events.add("player breaks " + blockType + " with itemscript "
+ item.getMaterial());
}

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

Expand Down Expand Up @@ -781,11 +786,15 @@ public void entityTarget(EntityTargetEvent event) {
// and endermen for instance

else if (dEntity.matches(determination)) {
dEntity newTarget = dEntity.valueOf(determination);
final dEntity attacker = new dEntity(entity);
final dEntity newTarget = dEntity.valueOf(determination);

if (newTarget.isSpawned()) {
event.setTarget(newTarget.getBukkitEntity());
}
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {

attacker.target(newTarget.getLivingEntity());
}
}, 1);
}
}

Expand Down Expand Up @@ -883,12 +892,17 @@ public void inventoryClickEvent(InventoryClickEvent event) {
item.identify() + " in " + type + " inventory");

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add(interaction + " on " +
item.identify().split(":")[0] + " in inventory");
events.add(interaction + " on " +
item.identify().split(":")[0] + " in " + type + " inventory");
}
if (item.isItemscript()) {
events.add(interaction + " on " +
item.getMaterial() + " in inventory");
events.add(interaction + " on " +
item.getMaterial() + " in " + type + " inventory");
}
}

String determination = doEvents(events, null, player, context);
Expand Down Expand Up @@ -1139,9 +1153,11 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
events.add(interaction + " with " + item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add(interaction + " with " + item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add(interaction + " with itemscript " + item.getMaterial());
}
}

if (event.hasBlock()) {
Expand All @@ -1156,9 +1172,11 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
events.add(interaction + " with " + item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add(interaction + " with " + item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add(interaction + " with itemscript " + item.getMaterial());
}
}
}

Expand Down Expand Up @@ -1202,12 +1220,17 @@ else if (entity instanceof Player) {
item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add("player right clicks entity with " +
item.identify().split(":")[0]);
events.add("player right clicks " + entityType + " with " +
item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add("player right clicks entity with itemscript " +
item.getMaterial());
events.add("player right clicks " + entityType + " with itemscript " +
item.getMaterial());
}

if (entity instanceof ItemFrame) {
dItem itemFrame = new dItem(((ItemFrame) entity).getItem());
Expand All @@ -1221,6 +1244,10 @@ else if (entity instanceof Player) {
events.add("player right clicks " + entityType + " " +
itemFrame.identify().split(":")[0]);
}
if (itemFrame.isItemscript()) {
events.add("player right clicks " + entityType +
" itemscript " + item.getMaterial());
}
}

determination = doEvents(events, npc, event.getPlayer(), context);
Expand All @@ -1241,9 +1268,11 @@ public void playerItemConsume(PlayerItemConsumeEvent event) {
events.add("player consumes " + item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add("player consumes " + item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add("player consumes itemscript " + item.getMaterial());
}

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

Expand Down

2 comments on commit 708cd0d

@Morphan1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this broke some major stuff (somehow o~O)
http://hastebin.com/yuliyovoja.avrasm

@davidcernat
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the fix for that is, like, the easiest fix ever. :P

Please sign in to comment.