Skip to content

Commit

Permalink
entity removed from world paper event
Browse files Browse the repository at this point in the history
and replace despawn event trigger with a modern one
  • Loading branch information
mcmonkey4eva committed Jan 24, 2023
1 parent c41d90d commit ec01855
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
Expand Up @@ -36,6 +36,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(EntityKnocksbackEntityScriptEvent.class);
ScriptEvent.registerScriptEvent(EntityLoadCrossbowScriptEvent.class);
ScriptEvent.registerScriptEvent(EntityPathfindScriptEvent.class);
ScriptEvent.registerScriptEvent(EntityRemoveFromWorldScriptEvent.class);
ScriptEvent.registerScriptEvent(ExperienceOrbMergeScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerAbsorbsExperienceScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerBeaconEffectScriptEvent.class);
Expand Down
@@ -0,0 +1,68 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class EntityRemoveFromWorldScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// <entity> removed from world
//
// @Plugin Paper
//
// @Group Paper
//
// @Location true
//
// @Triggers any time an entity is removed from the world for any reason, including chunks unloading.
//
// @Context
// <context.entity> returns the EntityTag that will be removed. Note that this entity will not necessarily be fully spawned at time of firing, so usage will be limited.
//
// -->

public EntityRemoveFromWorldScriptEvent() {
registerCouldMatcher("<entity> removed from world");
}

public EntityTag entity;
public EntityRemoveFromWorldEvent event;

@Override
public boolean matches(ScriptPath path) {
if (!path.tryArgObject(0, entity)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, null);
}

@Override
public ObjectTag getContext(String name) {
return switch (name) {
case "entity" -> entity;
default -> super.getContext(name);
};
}

@EventHandler
public void onPreCreatureSpawn(EntityRemoveFromWorldEvent event) {
this.entity = new EntityTag(event.getEntity());
this.event = event;
fire(event);
}
}
Expand Up @@ -11,12 +11,11 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.EntitiesUnloadEvent;

import java.util.HashMap;

Expand All @@ -40,14 +39,8 @@ public void onEntityDeath(EntityDeathEvent event) {
}

@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
if (event instanceof Cancellable &&((Cancellable) event).isCancelled()) {
return;
}
// TODO: This doesn't work. Awaiting Entity Despawn Event PR's for Bukkit:
// Bukkit: https://github.com/Bukkit/Bukkit/pull/1070
// CraftBukkit: https://github.com/Bukkit/CraftBukkit/pull/1386
for (Entity ent : event.getChunk().getEntities()) {
public void onChunkUnload(EntitiesUnloadEvent event) {
for (Entity ent : event.getEntities()) {
if (!(ent instanceof LivingEntity) || ((LivingEntity) ent).getRemoveWhenFarAway()) {
EntityTag.rememberEntity(ent);
EntityDespawnScriptEvent.instance.entity = new EntityTag(ent);
Expand Down

0 comments on commit ec01855

Please sign in to comment.