Skip to content

Commit

Permalink
Convert "on item despawns" to new ScriptEvent format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jun 23, 2015
1 parent fbff384 commit 7da098c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -621,6 +621,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new FurnaceSmeltsItemScriptEvent());
ScriptEvent.registerScriptEvent(new HangingBreaksScriptEvent());
ScriptEvent.registerScriptEvent(new HorseJumpsScriptEvent());
ScriptEvent.registerScriptEvent(new ItemDespawnsScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMoveScriptEvent());
ScriptEvent.registerScriptEvent(new ItemScrollScriptEvent());
ScriptEvent.registerScriptEvent(new ItemSpawnsScriptEvent());
Expand Down
@@ -0,0 +1,112 @@
package net.aufdemrand.denizen.events.entity;

import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemDespawnEvent;

import java.util.HashMap;

public class ItemDespawnsScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// item despawns (in <notable area>)
// <item> despawns (in <notable area>)
// <material> despawns (in <notable area>)
//
// @Cancellable true
//
// @Triggers when an item entity spawns.
//
// @Context
// <context.item> returns the dItem of the entity.
// <context.entity> returns the dEntity.
// <context.location> returns the location of the entity to be despawned.
//
// -->

public ItemDespawnsScriptEvent() {
instance = this;
}
public static ItemDespawnsScriptEvent instance;
public dItem item;
public dLocation location;
public dEntity entity;
public ItemDespawnEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
String entTest = CoreUtilities.getXthArg(0, lower);
return cmd.equals("despawns")
&& (entTest.equals("item") || dMaterial.matches(entTest) || dItem.matches(entTest));
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String item_test = CoreUtilities.getXthArg(0, lower);

if (!item_test.equals("item")
&& !item_test.equals(item.identifyNoIdentifier()) && !item_test.equals(item.identifySimpleNoIdentifier())) {
return false;
}

if (!runInCheck(scriptContainer, s, lower, location)) {
return false;
}

return true;
}

@Override
public String getName() {
return "ItemDespawns";
}

@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

@Override
public void destroy() {
ItemDespawnEvent.getHandlerList().unregister(this);
}

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}

@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("location", location);
context.put("item", item);
context.put("entity", entity);
return context;
}

@EventHandler
public void onItemDespawns(ItemDespawnEvent event) {
location = new dLocation(event.getLocation());
item = new dItem(event.getEntity().getItemStack());
entity = new dEntity(event.getEntity());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
}
Expand Up @@ -137,41 +137,6 @@ public void timeEvent() {
// Additional EVENTS
/////////////////

// <--[event]
// @Events
// item despawns
// <item> despawns
// <material> despawns
//
// @Triggers when an item entity despawns.
// @Context
// <context.item> returns the dItem of the entity.
// <context.entity> returns the dEntity.
//
// @Determine
// "CANCELLED" to stop the item entity from despawning.
//
// -->
@EventHandler
public void itemDespawn(ItemDespawnEvent event) {

Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getEntity().getItemStack());

context.put("item", item);
context.put("entity", new dEntity(event.getEntity()));

List<String> events = new ArrayList<String>();
events.add("item despawns");
events.add(item.identifySimple() + " despawns");
events.add(item.identifyMaterial() + " despawns");

String determination = doEvents(events, null, null, context, true);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}

// <--[event]
// @Events
// projectile hits block
Expand Down

0 comments on commit 7da098c

Please sign in to comment.