From 6f271779d9a78af28ff51b746a99382d206ed722 Mon Sep 17 00:00:00 2001 From: VaiTon Date: Fri, 9 Jul 2021 00:48:43 +0200 Subject: [PATCH] feat: fire PlayerPickupExperienceEvent and make it cancellable. Related to https://github.com/GlowstoneMC/Glowstone/issues/922 --- .../entity/objects/GlowExperienceOrb.java | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/glowstone/entity/objects/GlowExperienceOrb.java b/src/main/java/net/glowstone/entity/objects/GlowExperienceOrb.java index 1c5f5aff9..cece606a8 100644 --- a/src/main/java/net/glowstone/entity/objects/GlowExperienceOrb.java +++ b/src/main/java/net/glowstone/entity/objects/GlowExperienceOrb.java @@ -1,13 +1,17 @@ package net.glowstone.entity.objects; +import com.destroystokyo.paper.event.player.PlayerPickupExperienceEvent; import com.flowpowered.network.Message; import com.google.common.base.Preconditions; + import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.UUID; + import lombok.Getter; import lombok.Setter; +import net.glowstone.EventFactory; import net.glowstone.entity.GlowEntity; import net.glowstone.net.message.play.entity.DestroyEntitiesMessage; import net.glowstone.net.message.play.entity.SpawnXpOrbMessage; @@ -61,7 +65,7 @@ public GlowExperienceOrb(Location location, int experience) { @Override public List createSpawnMessage() { return Collections.singletonList( - new SpawnXpOrbMessage(getEntityId(), getLocation(), (short) getExperience())); + new SpawnXpOrbMessage(getEntityId(), getLocation(), (short) getExperience())); } @Override @@ -74,16 +78,21 @@ public void damage(double amount, Entity source, @NotNull EntityDamageEvent.Dama @Override public void pulse() { super.pulse(); + // todo: drag self towards player + if (tickSkipped) { // find player to give experience - // todo: drag self towards player Optional player = getWorld().getPlayers().stream() - .filter(p -> p.getLocation().distanceSquared(location) <= 1) - .findAny(); + .filter(p -> p.getLocation().distanceSquared(location) <= 1) + .findAny(); if (player.isPresent()) { - player.get().giveExp(experience); - world.playSound(location, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F); - remove(); + PlayerPickupExperienceEvent event = new PlayerPickupExperienceEvent(player.get(), this); + event = EventFactory.getInstance().callEvent(event); + if (!event.isCancelled()) { + player.get().giveExp(experience); + world.playSound(location, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F); + remove(); + } return; } } @@ -98,14 +107,14 @@ public void pulse() { private void refresh() { DestroyEntitiesMessage destroyMessage = new DestroyEntitiesMessage( - Collections.singletonList(this.getEntityId())); + Collections.singletonList(this.getEntityId())); List spawnMessages = this.createSpawnMessage(); - Message[] messages = new Message[] {destroyMessage, spawnMessages.get(0)}; + Message[] messages = new Message[]{destroyMessage, spawnMessages.get(0)}; getWorld() - .getRawPlayers() - .stream() - .filter(p -> p.canSeeEntity(this)) - .forEach(p -> p.getSession().sendAll(messages)); + .getRawPlayers() + .stream() + .filter(p -> p.canSeeEntity(this)) + .forEach(p -> p.getSession().sendAll(messages)); } @Override @@ -116,7 +125,7 @@ public void setExperience(int experience) { } @Override - public EntityType getType() { + public @NotNull EntityType getType() { return EntityType.EXPERIENCE_ORB; } }