From dd9451c6e5bb88ae11ffea2745a7a778dcc8621d Mon Sep 17 00:00:00 2001 From: crashdemons Date: Mon, 15 Feb 2021 22:15:33 -0600 Subject: [PATCH] attempt support of spawnermobbehavior setting ref crashdemons/PlayerHeads#77 --- .../playerheads/PlayerHeadsListener.java | 30 +++++++++++++++++++ .../src/main/resources/config.yml | 11 +++++++ 2 files changed, 41 insertions(+) diff --git a/PlayerHeads-core/src/main/java/org/shininet/bukkit/playerheads/PlayerHeadsListener.java b/PlayerHeads-core/src/main/java/org/shininet/bukkit/playerheads/PlayerHeadsListener.java index ce90f80c..02a22559 100644 --- a/PlayerHeads-core/src/main/java/org/shininet/bukkit/playerheads/PlayerHeadsListener.java +++ b/PlayerHeads-core/src/main/java/org/shininet/bukkit/playerheads/PlayerHeadsListener.java @@ -33,6 +33,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.entity.EntityDeathEvent; +import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.ItemStack; @@ -218,6 +219,13 @@ private DeathParameters getDeathParameters(EntityDeathEvent event) { } //end vanilla head event + if(plugin.getConfig().getBoolean("trackspawnermobs")){ + if(isSpawnerMob(event.getEntity())){ + VanillDropBehavior spawnerMobBehavior = VanillDropBehavior.fromString( plugin.configFile.getString("spawnermobbehavior") ); + params.vanillaBehavior = params.vanillaBehavior.apply(spawnerMobBehavior); + if(!params.vanillaBehavior.allowsPhBehavior()) return params.cancel(); + } + } VanillDropBehavior chargedcreeperBehavior=VanillDropBehavior.IGNORE; if (params.killer != null) { @@ -320,6 +328,28 @@ public void onEntityDeath(EntityDeathEvent event) { } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled=true) + public void onEntitySpawn(CreatureSpawnEvent event){ + if(!plugin.getConfig().getBoolean("trackspawnermobs")) return; + LivingEntity entity = event.getEntity(); + boolean persistenceSupported = Compatibility.getProvider().supportsEntityTagType(true); + Compatibility.getProvider().setEntityTag(entity, plugin, "phspawnreason", event.getSpawnReason().name().toUpperCase(), persistenceSupported); + } + + private CreatureSpawnEvent.SpawnReason getMobSpawnReason(Entity entity){ + boolean persistenceSupported = Compatibility.getProvider().supportsEntityTagType(true); + String reason = Compatibility.getProvider().getEntityTag(entity, plugin, "phspawnreason", persistenceSupported); + try{ + return CreatureSpawnEvent.SpawnReason.valueOf(reason); + }catch(Exception e){ + return null; + } + } + + private boolean isSpawnerMob(Entity entity){ + return getMobSpawnReason(entity)==CreatureSpawnEvent.SpawnReason.SPAWNER; + } + /** * Event handler for entity deaths. diff --git a/PlayerHeads-core/src/main/resources/config.yml b/PlayerHeads-core/src/main/resources/config.yml index a6cbe4c9..9f00cadf 100644 --- a/PlayerHeads-core/src/main/resources/config.yml +++ b/PlayerHeads-core/src/main/resources/config.yml @@ -125,6 +125,17 @@ requireditems: [wooden_axe,stone_axe,golden_axe,iron_axe,diamond_axe] considermobkillers: false considertameowner: false +#controls whether spawner mobs should be tracked +trackspawnermobs: false + +#spawnermobbehavior can be: +# vanilla - don't allow PlayerHeads to modify deaths caused by spawner mobs at all +# ignore - don't consider spawner mobs at all, add PlayerHeads drop chances anyway if applicable +# block - block all spawner mob caused head drops (including vanilla drops) +# replace - remove vanilla spawner mob caused head drops and add PlayerHeads drop chances instead +# NOTE: requires 'trackspawnermobs' to be enabled. +spawnermobbehavior: ignore + #prevent PlayerHeads from interacting/modifying third-party heads with these names or UUIDs ignoredheadnames: [CSCoreLib,CsCoreLib,CS-CoreLib] ignoredheaduuids: []