Fix raids and patrols not respecting natual spawning

This commit is contained in:
BuildTools 2019-09-22 20:50:41 -06:00
parent 05466c4f77
commit 75b23c0956

View File

@ -1,5 +1,6 @@
package com.songoda.skyblock.listeners; package com.songoda.skyblock.listeners;
import com.google.common.collect.Sets;
import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager;
import com.songoda.skyblock.config.FileManager.Config; import com.songoda.skyblock.config.FileManager.Config;
@ -689,37 +690,37 @@ public class Entity implements Listener {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@EventHandler @EventHandler
public void onCreatureSpawn(CreatureSpawnEvent event) { public void onCreatureSpawn(CreatureSpawnEvent event) {
if (event.getEntity() instanceof ArmorStand LivingEntity entity = event.getEntity();
|| event.getEntity() instanceof FallingBlock if (entity instanceof ArmorStand)
|| event.getEntity() instanceof org.bukkit.entity.Item) { return;
if (entity.hasMetadata("SkyBlock"))
return;
SpawnReason reason = event.getSpawnReason();
Set<SpawnReason> reasons = Sets.newHashSet(SpawnReason.NATURAL, SpawnReason.JOCKEY, SpawnReason.MOUNT, SpawnReason.valueOf("RAID"), SpawnReason.valueOf("PATROL"));
// Check that the reason of this event is not any of these above.
if (!reasons.stream().filter(r -> r != null).anyMatch(r -> r == reason))
return;
if (!skyblock.getWorldManager().isIslandWorld(entity.getWorld()))
return;
if (skyblock.getIslandManager().hasSetting(entity.getLocation(), IslandRole.Owner, "NaturalMobSpawning"))
return;
if (event.getSpawnReason() != SpawnReason.JOCKEY && event.getSpawnReason() != SpawnReason.MOUNT) {
entity.remove(); // Older versions ignore the event being cancelled, so this fixes that issue.
return; return;
} }
if (!(event.getSpawnReason() == SpawnReason.NATURAL || event.getSpawnReason() == SpawnReason.JOCKEY || event.getSpawnReason() == SpawnReason.MOUNT))
return;
LivingEntity livingEntity = event.getEntity();
if (livingEntity.hasMetadata("SkyBlock"))
return;
if (skyblock.getWorldManager().isIslandWorld(livingEntity.getWorld())) {
if (!skyblock.getIslandManager().hasSetting(livingEntity.getLocation(), IslandRole.Owner, "NaturalMobSpawning")) {
if (event.getSpawnReason() == SpawnReason.JOCKEY || event.getSpawnReason() == SpawnReason.MOUNT) {
Bukkit.getScheduler().scheduleSyncDelayedTask(skyblock, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(skyblock, () -> {
if (NMSUtil.getVersionNumber() > 10) { // getPassengers() was added in 1.11 if (NMSUtil.getVersionNumber() > 10) { // getPassengers() was added in 1.11
for (org.bukkit.entity.Entity passenger : livingEntity.getPassengers()) for (org.bukkit.entity.Entity passenger : entity.getPassengers())
passenger.remove(); passenger.remove();
} else { } else {
if (livingEntity.getPassenger() != null) if (entity.getPassenger() != null)
livingEntity.getPassenger().remove(); entity.getPassenger().remove();
} }
livingEntity.remove(); entity.remove();
}); });
} else { event.setCancelled(true); // For other plugin API reasons.
livingEntity.remove();
}
}
}
} }
} }