From e1216e89fad70979ead1675eddaf514074adfff7 Mon Sep 17 00:00:00 2001 From: heisluft Date: Sun, 3 Feb 2019 16:37:18 +0100 Subject: [PATCH 1/2] Implemented PigZapEvent --- src/main/java/net/glowstone/GlowWorld.java | 32 +++++++++++++++++++ .../glowstone/entity/GlowLightningStrike.java | 17 ++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/net/glowstone/GlowWorld.java b/src/main/java/net/glowstone/GlowWorld.java index c084396962..6a195c7a23 100644 --- a/src/main/java/net/glowstone/GlowWorld.java +++ b/src/main/java/net/glowstone/GlowWorld.java @@ -1411,6 +1411,38 @@ public T spawn(Location location, Class clazz, return null; // TODO: work on type mismatches } + //Needed for Events which mutate Entities before they are spawned + public T spawn(Location location, T entity, SpawnReason reason) + throws IllegalArgumentException { + checkNotNull(entity).setRawLocation(location, false); + checkNotNull(reason); + checkNotNull(location); + try { + EntitySpawnEvent spawnEvent = null; + if (entity instanceof LivingEntity) { + spawnEvent = EventFactory.getInstance() + .callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason)); + } else if (!(entity instanceof Item)) { // ItemSpawnEvent is called elsewhere + spawnEvent = EventFactory.getInstance().callEvent(new EntitySpawnEvent(entity)); + } + if (spawnEvent != null && spawnEvent.isCancelled()) { + // TODO: separate spawning and construction for better event cancellation + entity.remove(); + } else { + List spawnMessage = entity.createSpawnMessage(); + final GlowEntity finalEntity = entity; + getRawPlayers().stream().filter(player -> player.canSeeEntity(finalEntity)) + .forEach(player -> player.getSession().sendAll(spawnMessage + .toArray(new Message[spawnMessage.size()]))); + } + } catch (NoSuchMethodError | IllegalAccessError e) { + GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e); + } catch (Throwable t) { + GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", t); + } + return entity; + } + /** * Spawns an entity. * diff --git a/src/main/java/net/glowstone/entity/GlowLightningStrike.java b/src/main/java/net/glowstone/entity/GlowLightningStrike.java index f7a3caef2a..ac486dbc67 100644 --- a/src/main/java/net/glowstone/entity/GlowLightningStrike.java +++ b/src/main/java/net/glowstone/entity/GlowLightningStrike.java @@ -8,7 +8,10 @@ import net.glowstone.EventFactory; import net.glowstone.GlowWorld; import net.glowstone.block.GlowBlock; +import net.glowstone.entity.monster.GlowPigZombie; +import net.glowstone.entity.passive.GlowPig; import net.glowstone.entity.physics.BoundingBox; +import net.glowstone.io.entity.EntityStorage; import net.glowstone.net.GlowSession; import net.glowstone.net.message.play.entity.SpawnLightningStrikeMessage; import org.bukkit.Location; @@ -20,9 +23,12 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.LightningStrike; +import org.bukkit.entity.Pig; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; +import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.PigZapEvent; import org.bukkit.util.Vector; /** @@ -113,6 +119,17 @@ public void pulse() { ((Damageable) entity) .damage(5, this, EntityDamageEvent.DamageCause.LIGHTNING); } + if (entity instanceof Pig) { + GlowPig pig = (GlowPig) entity; + GlowPigZombie zombie = EntityStorage.create(GlowPigZombie.class, location); + PigZapEvent event = new PigZapEvent(pig, this, zombie); + if (EventFactory.getInstance().callEvent(event).isCancelled()) { + zombie.remove(); + } + pig.remove(); + world.spawn(location, zombie, CreatureSpawnEvent.SpawnReason.LIGHTNING); + + } entity.setFireTicks(entity.getMaxFireTicks()); } // TODO: Spawn Skeletontrap From 21feffd5c477a8ef8a9d0b287b965050295be3fb Mon Sep 17 00:00:00 2001 From: heisluft Date: Wed, 6 Feb 2019 13:23:29 +0100 Subject: [PATCH 2/2] Fix logic, Remove unnecessary catch clauses and preserve customName of the pig (compliant with vanilla behaviour) --- src/main/java/net/glowstone/GlowWorld.java | 5 +---- .../java/net/glowstone/entity/GlowLightningStrike.java | 8 +++++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/glowstone/GlowWorld.java b/src/main/java/net/glowstone/GlowWorld.java index 6a195c7a23..0fee854168 100644 --- a/src/main/java/net/glowstone/GlowWorld.java +++ b/src/main/java/net/glowstone/GlowWorld.java @@ -1430,13 +1430,10 @@ public T spawn(Location location, T entity, SpawnReason r entity.remove(); } else { List spawnMessage = entity.createSpawnMessage(); - final GlowEntity finalEntity = entity; - getRawPlayers().stream().filter(player -> player.canSeeEntity(finalEntity)) + getRawPlayers().stream().filter(player -> player.canSeeEntity(entity)) .forEach(player -> player.getSession().sendAll(spawnMessage .toArray(new Message[spawnMessage.size()]))); } - } catch (NoSuchMethodError | IllegalAccessError e) { - GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e); } catch (Throwable t) { GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", t); } diff --git a/src/main/java/net/glowstone/entity/GlowLightningStrike.java b/src/main/java/net/glowstone/entity/GlowLightningStrike.java index ac486dbc67..688e99f32e 100644 --- a/src/main/java/net/glowstone/entity/GlowLightningStrike.java +++ b/src/main/java/net/glowstone/entity/GlowLightningStrike.java @@ -125,10 +125,12 @@ public void pulse() { PigZapEvent event = new PigZapEvent(pig, this, zombie); if (EventFactory.getInstance().callEvent(event).isCancelled()) { zombie.remove(); + } else { + // Only the custom name is copied, all other attributes are overwritten + zombie.setCustomName(pig.getCustomName()); + pig.remove(); + world.spawn(location, zombie, CreatureSpawnEvent.SpawnReason.LIGHTNING); } - pig.remove(); - world.spawn(location, zombie, CreatureSpawnEvent.SpawnReason.LIGHTNING); - } entity.setFireTicks(entity.getMaxFireTicks()); }