Entity#getEntitySpawnReason compatibility for spigot (#4305)

chore: Entity#getEntitySpawnReason compatibility for spigot
This commit is contained in:
Pierre Maurice Schwang 2024-02-02 16:46:39 +01:00 committed by GitHub
parent 966c878a72
commit 448577774a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 5 deletions

View File

@ -779,8 +779,11 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
Iterator<Entity> iterator = entities.iterator(); Iterator<Entity> iterator = entities.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = iterator.next(); Entity entity = iterator.next();
final String spawnReason = entity.getEntitySpawnReason().name(); if (PaperLib.isPaper() && "CUSTOM".equals(entity.getEntitySpawnReason().name())) {
if ("CUSTOM".equals(spawnReason)) { continue;
}
// Fallback for Spigot not having Entity#getEntitySpawnReason
if (entity.getMetadata("ps_custom_spawned").stream().anyMatch(MetadataValue::asBoolean)) {
continue; continue;
} }
switch (entity.getType().toString()) { switch (entity.getType().toString()) {

View File

@ -19,6 +19,7 @@
package com.plotsquared.bukkit.listener; package com.plotsquared.bukkit.listener;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.player.BukkitPlayer;
import com.plotsquared.bukkit.util.BukkitEntityUtil; import com.plotsquared.bukkit.util.BukkitEntityUtil;
import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.bukkit.util.BukkitUtil;
@ -41,6 +42,7 @@ import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.PlotFlagUtil; import com.plotsquared.core.util.PlotFlagUtil;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import io.papermc.lib.PaperLib;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.World; import org.bukkit.World;
@ -78,15 +80,18 @@ import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class EntityEventListener implements Listener { public class EntityEventListener implements Listener {
private final BukkitPlatform platform;
private final PlotAreaManager plotAreaManager; private final PlotAreaManager plotAreaManager;
private final EventDispatcher eventDispatcher; private final EventDispatcher eventDispatcher;
private float lastRadius; private float lastRadius;
@Inject @Inject
public EntityEventListener( public EntityEventListener(
final @NonNull BukkitPlatform platform,
final @NonNull PlotAreaManager plotAreaManager, final @NonNull PlotAreaManager plotAreaManager,
final @NonNull EventDispatcher eventDispatcher final @NonNull EventDispatcher eventDispatcher
) { ) {
this.platform = platform;
this.plotAreaManager = plotAreaManager; this.plotAreaManager = plotAreaManager;
this.eventDispatcher = eventDispatcher; this.eventDispatcher = eventDispatcher;
} }
@ -170,7 +175,18 @@ public class EntityEventListener implements Listener {
return; return;
} }
} }
case "BUILD_IRONGOLEM", "BUILD_SNOWMAN", "BUILD_WITHER", "CUSTOM" -> { case "CUSTOM" -> {
if (!area.isSpawnCustom()) {
event.setCancelled(true);
return;
}
// No need to clutter metadata if running paper
if (!PaperLib.isPaper()) {
entity.setMetadata("ps_custom_spawned", new FixedMetadataValue(this.platform, true));
}
return; // Don't cancel if mob spawning is disabled
}
case "BUILD_IRONGOLEM", "BUILD_SNOWMAN", "BUILD_WITHER" -> {
if (!area.isSpawnCustom()) { if (!area.isSpawnCustom()) {
event.setCancelled(true); event.setCancelled(true);
return; return;

View File

@ -123,9 +123,11 @@ public class EntitySpawnListener implements Listener {
if (!location.isPlotArea() || area == null) { if (!location.isPlotArea() || area == null) {
return; return;
} }
if (PaperLib.isPaper()) {
if (area.isSpawnCustom() && "CUSTOM".equals(entity.getEntitySpawnReason().name())) { if (area.isSpawnCustom() && "CUSTOM".equals(entity.getEntitySpawnReason().name())) {
return; return;
} }
}
Plot plot = location.getOwnedPlotAbs(); Plot plot = location.getOwnedPlotAbs();
EntityType type = entity.getType(); EntityType type = entity.getType();
if (plot == null) { if (plot == null) {