Skip to content

Commit

Permalink
feat: fire PlayerPickupExperienceEvent and make it cancellable.
Browse files Browse the repository at this point in the history
Related to GlowstoneMC#922
  • Loading branch information
VaiTon committed Jul 8, 2021
1 parent ed097a5 commit 6f27177
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/main/java/net/glowstone/entity/objects/GlowExperienceOrb.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -61,7 +65,7 @@ public GlowExperienceOrb(Location location, int experience) {
@Override
public List<Message> createSpawnMessage() {
return Collections.singletonList(
new SpawnXpOrbMessage(getEntityId(), getLocation(), (short) getExperience()));
new SpawnXpOrbMessage(getEntityId(), getLocation(), (short) getExperience()));
}

@Override
Expand All @@ -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> 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;
}
}
Expand All @@ -98,14 +107,14 @@ public void pulse() {

private void refresh() {
DestroyEntitiesMessage destroyMessage = new DestroyEntitiesMessage(
Collections.singletonList(this.getEntityId()));
Collections.singletonList(this.getEntityId()));
List<Message> 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
Expand All @@ -116,7 +125,7 @@ public void setExperience(int experience) {
}

@Override
public EntityType getType() {
public @NotNull EntityType getType() {
return EntityType.EXPERIENCE_ORB;
}
}

0 comments on commit 6f27177

Please sign in to comment.