mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Fix ExperienceOrb#setExperienceCount with a proper updating client-side
This commit is contained in:
parent
b193c5f12a
commit
c73ec6f0d6
@ -175,6 +175,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
}
|
||||
|
||||
public void teleport(Position position, Runnable callback) {
|
||||
Check.notNull(position, "Teleport position cannot be null");
|
||||
Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!");
|
||||
|
||||
Runnable runnable = () -> {
|
||||
@ -230,6 +231,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
|
||||
@Override
|
||||
public boolean addViewer(Player player) {
|
||||
Check.notNull(player, "Viewer cannot be null");
|
||||
boolean result = this.viewers.add(player);
|
||||
player.viewableEntities.add(this);
|
||||
return result;
|
||||
@ -237,6 +239,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
|
||||
@Override
|
||||
public boolean removeViewer(Player player) {
|
||||
Check.notNull(player, "Viewer cannot be null");
|
||||
if (!viewers.remove(player))
|
||||
return false;
|
||||
|
||||
@ -404,6 +407,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
|
||||
@Override
|
||||
public <E extends Event> void addEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
|
||||
Check.notNull(eventClass, "Event class cannot be null");
|
||||
Check.notNull(eventCallback, "Event callback cannot be null");
|
||||
List<EventCallback> callbacks = getEventCallbacks(eventClass);
|
||||
callbacks.add(eventCallback);
|
||||
this.eventCallbacks.put(eventClass, callbacks);
|
||||
@ -411,6 +416,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
|
||||
@Override
|
||||
public <E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass) {
|
||||
Check.notNull(eventClass, "Event class cannot be null");
|
||||
return eventCallbacks.getOrDefault(eventClass, new CopyOnWriteArrayList<>());
|
||||
}
|
||||
|
||||
@ -482,6 +488,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
}
|
||||
|
||||
public float getDistance(Entity entity) {
|
||||
Check.notNull(entity, "Entity cannot be null");
|
||||
return getPosition().getDistance(entity.getPosition());
|
||||
}
|
||||
|
||||
@ -738,6 +745,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
* @return true if the entity is in the same chunk as {@code position}
|
||||
*/
|
||||
public boolean sameChunk(Position position) {
|
||||
Check.notNull(position, "Position cannot be null");
|
||||
Position pos = getPosition();
|
||||
int chunkX1 = ChunkUtils.getChunkCoordinate((int) Math.floor(pos.getX()));
|
||||
int chunkZ1 = ChunkUtils.getChunkCoordinate((int) Math.floor(pos.getZ()));
|
||||
|
@ -3,6 +3,9 @@ package net.minestom.server.entity;
|
||||
import net.minestom.server.network.packet.server.play.SpawnExperienceOrbPacket;
|
||||
import net.minestom.server.network.player.PlayerConnection;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ExperienceOrb extends Entity {
|
||||
|
||||
private short experienceCount;
|
||||
@ -32,15 +35,31 @@ public class ExperienceOrb extends Entity {
|
||||
experienceOrbPacket.entityId = getEntityId();
|
||||
experienceOrbPacket.position = getPosition();
|
||||
experienceOrbPacket.expCount = experienceCount;
|
||||
|
||||
playerConnection.sendPacket(experienceOrbPacket);
|
||||
return super.addViewer(player); // Add player to viewers list and send velocity packet
|
||||
playerConnection.sendPacket(getVelocityPacket());
|
||||
|
||||
return super.addViewer(player); // Add player to viewers list
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the experience amount contained in the entity
|
||||
*/
|
||||
public short getExperienceCount() {
|
||||
return experienceCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param experienceCount the new experience amount
|
||||
*/
|
||||
public void setExperienceCount(short experienceCount) {
|
||||
// Remove the entity in order to respawn it with the correct experience count
|
||||
Set<Player> viewerCache = new HashSet<>(getViewers());
|
||||
|
||||
viewerCache.forEach(player -> removeViewer(player));
|
||||
|
||||
this.experienceCount = experienceCount;
|
||||
|
||||
viewerCache.forEach(player -> addViewer(player));
|
||||
}
|
||||
}
|
||||
|
@ -887,6 +887,7 @@ public class Player extends LivingEntity {
|
||||
* @param gameMode the new player gamemode
|
||||
*/
|
||||
public void setGameMode(GameMode gameMode) {
|
||||
Check.notNull(gameMode, "GameMode cannot be null");
|
||||
ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket();
|
||||
changeGameStatePacket.reason = ChangeGameStatePacket.Reason.CHANGE_GAMEMODE;
|
||||
changeGameStatePacket.value = gameMode.getId();
|
||||
|
Loading…
Reference in New Issue
Block a user