Skip to content
Permalink
Browse files

Remove metadata when the entity is done.

Apparently Bukkit doesn't actually do this.
Fixes WORLDGUARD-4169.
  • Loading branch information
wizjany committed Feb 14, 2020
1 parent 0d860bf commit d95c6af1be69e3f393a097eabc322ad8cbcc416d
@@ -236,6 +236,15 @@ public static void trackParentCause(Metadatable target, Object parent) {
WGMetadata.put(target, CAUSE_KEY, parent);
}

/**
* Remove a parent cause from a {@code Metadatable} object.
*
* @param target the target
*/
public static void untrackParentCause(Metadatable target) {
WGMetadata.remove(target, CAUSE_KEY);
}

/**
* Builds causes.
*/
@@ -75,4 +75,13 @@ public static void put(Metadatable target, String key, Object value) {
return null;
}

/**
* Removes metadata from the target.
*
* @param target the target
* @param key the key
*/
public static void remove(Metadatable target, String key) {
target.removeMetadata(key, WorldGuardPlugin.inst());
}
}
@@ -35,6 +35,7 @@
import com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent;
import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent;
import com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent;
import com.sk89q.worldguard.bukkit.internal.WGMetadata;
import com.sk89q.worldguard.bukkit.listener.debounce.BlockPistonExtendKey;
import com.sk89q.worldguard.bukkit.listener.debounce.BlockPistonRetractKey;
import com.sk89q.worldguard.bukkit.listener.debounce.EventDebounce;
@@ -302,16 +303,19 @@ public void onEntityChangeBlock(EntityChangeBlockEvent event) {

Events.fireToCancel(event, new PlaceBlockEvent(event, cause, block.getLocation(), toType));

if (event.isCancelled() && !wasCancelled && entity instanceof FallingBlock) {
FallingBlock fallingBlock = (FallingBlock) entity;
final Material material = fallingBlock.getBlockData().getMaterial();
if (!material.isItem()) return;
ItemStack itemStack = new ItemStack(material, 1);
Item item = block.getWorld().dropItem(fallingBlock.getLocation(), itemStack);
item.setVelocity(new Vector());
if (Events.fireAndTestCancel(new SpawnEntityEvent(event, create(block, entity), item))) {
item.remove();
if (entity instanceof FallingBlock) {
if (event.isCancelled() && !wasCancelled) {
FallingBlock fallingBlock = (FallingBlock) entity;
final Material material = fallingBlock.getBlockData().getMaterial();
if (!material.isItem()) return;
ItemStack itemStack = new ItemStack(material, 1);
Item item = block.getWorld().dropItem(fallingBlock.getLocation(), itemStack);
item.setVelocity(new Vector());
if (Events.fireAndTestCancel(new SpawnEntityEvent(event, create(block, entity), item))) {
item.remove();
}
}
Cause.untrackParentCause(entity);
}
}
}
@@ -321,6 +325,9 @@ public void onEntityChangeBlock(EntityChangeBlockEvent event) {
public void onEntityExplode(EntityExplodeEvent event) {
Entity entity = event.getEntity();
Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, create(entity), event.getLocation().getWorld(), event.blockList(), Material.AIR));
if (entity instanceof Creeper) {
Cause.untrackParentCause(entity);
}
}

@EventHandler(ignoreCancelled = true)

0 comments on commit d95c6af

Please sign in to comment.
You can’t perform that action at this time.