Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert "on item despawns" to new ScriptEvent format.
- Loading branch information
Showing
3 changed files
with
113 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/net/aufdemrand/denizen/events/entity/ItemDespawnsScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters