Small optimizations

This commit is contained in:
Felix Cravic 2020-05-26 20:00:41 +02:00
parent 4ed213249e
commit 3be1173ab5
7 changed files with 36 additions and 18 deletions

View File

@ -28,7 +28,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
private float health;
// Bounding box used for items' pickup (see LivingEntity#setBoundingBox)
private BoundingBox expandedBoundingBox;
protected BoundingBox expandedBoundingBox;
private float[] attributeValues = new float[Attribute.values().length];

View File

@ -189,8 +189,8 @@ public class Player extends LivingEntity {
PlayerInfoPacket playerInfoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.ADD_PLAYER);
PlayerInfoPacket.AddPlayer addPlayer = new PlayerInfoPacket.AddPlayer(getUuid(), getUsername(), getGameMode(), getLatency());
addPlayer.displayName = jsonDisplayName;
//PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property);
//addPlayer.properties.add(prop);
PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property);
addPlayer.properties.add(prop);
playerInfoPacket.playerInfos.add(addPlayer);
playerConnection.sendPacket(playerInfoPacket);
@ -237,8 +237,8 @@ public class Player extends LivingEntity {
// Some client update
playerConnection.sendPacket(getPropertiesPacket()); // Send default properties
refreshHealth();
refreshAbilities();
refreshHealth(); // Heal and send health packet
refreshAbilities(); // Send abilities packet
}
/**
@ -268,11 +268,11 @@ public class Player extends LivingEntity {
packet.process(this);
}
super.update(); // Super update (item pickup)
super.update(); // Super update (item pickup/fire management)
// Target block stage
if (targetCustomBlock != null) {
int animationCount = 10;
final int animationCount = 10;
long since = System.currentTimeMillis() - targetBlockTime;
byte stage = (byte) (since / (blockBreakTime / animationCount) - 1);
if (stage != targetLastStage) {
@ -288,16 +288,15 @@ public class Player extends LivingEntity {
// Experience orb pickup
Chunk chunk = instance.getChunkAt(getPosition()); // TODO check surrounding chunks
Set<Entity> entities = instance.getChunkEntities(chunk);
BoundingBox livingBoundingBox = getBoundingBox().expand(1, 0.5f, 1);
for (Entity entity : entities) {
if (entity instanceof ExperienceOrb) {
ExperienceOrb experienceOrb = (ExperienceOrb) entity;
BoundingBox itemBoundingBox = experienceOrb.getBoundingBox();
if (livingBoundingBox.intersect(itemBoundingBox)) {
if (expandedBoundingBox.intersect(itemBoundingBox)) {
synchronized (experienceOrb) {
if (experienceOrb.shouldRemove() || experienceOrb.isRemoveScheduled())
continue;
PickupExperienceEvent pickupExperienceEvent = new PickupExperienceEvent(experienceOrb.getExperienceCount());
PickupExperienceEvent pickupExperienceEvent = new PickupExperienceEvent(experienceOrb);
callCancellableEvent(PickupExperienceEvent.class, pickupExperienceEvent, () -> {
short experienceCount = pickupExperienceEvent.getExperienceCount(); // TODO give to player
entity.remove();
@ -335,9 +334,8 @@ public class Player extends LivingEntity {
// Multiplayer sync
if (!getViewers().isEmpty()) {
Position position = getPosition();
boolean positionChanged = position.getX() != lastX || position.getZ() != lastZ || position.getY() != lastY;
boolean viewChanged = position.getYaw() != lastYaw || position.getPitch() != lastPitch;
final boolean positionChanged = position.getX() != lastX || position.getZ() != lastZ || position.getY() != lastY;
final boolean viewChanged = position.getYaw() != lastYaw || position.getPitch() != lastPitch;
ServerPacket updatePacket = null;
ServerPacket optionalUpdatePacket = null;
if (positionChanged && viewChanged) {

View File

@ -1,13 +1,20 @@
package net.minestom.server.event.item;
import net.minestom.server.entity.ExperienceOrb;
import net.minestom.server.event.CancellableEvent;
public class PickupExperienceEvent extends CancellableEvent {
private ExperienceOrb experienceOrb;
private short experienceCount;
public PickupExperienceEvent(short experienceCount) {
this.experienceCount = experienceCount;
public PickupExperienceEvent(ExperienceOrb experienceOrb) {
this.experienceOrb = experienceOrb;
this.experienceCount = experienceOrb.getExperienceCount();
}
public ExperienceOrb getExperienceOrb() {
return experienceOrb;
}
public short getExperienceCount() {

View File

@ -363,6 +363,7 @@ public class Chunk implements Viewable {
// Write the packet in the writer thread pools
public void refreshDataPacket(Runnable runnable) {
unloadCheck();
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> {
setFullDataPacket(buf);
runnable.run();

View File

@ -381,6 +381,8 @@ public class InstanceContainer extends Instance {
@Override
public void sendChunk(Player player, Chunk chunk) {
if (!chunk.isLoaded())
return;
if (!PlayerUtils.isNettyClient(player))
return;

View File

@ -9,7 +9,7 @@ public class WorldBorder {
private double centerX, centerZ;
private double currentDiameter;
private volatile double currentDiameter;
private double oldDiameter;
private double newDiameter;
@ -123,6 +123,7 @@ public class WorldBorder {
* @param diameter the new diameter of the world border
*/
public void setDiameter(double diameter) {
this.currentDiameter = diameter;
this.oldDiameter = diameter;
this.newDiameter = diameter;
this.lerpStartTime = 0;
@ -133,6 +134,9 @@ public class WorldBorder {
sendPacket(worldBorderPacket);
}
/**
* Used to update in real-time the current diameter time
*/
protected void update() {
if (lerpStartTime == 0) {
this.currentDiameter = oldDiameter;

View File

@ -101,17 +101,23 @@ public class PlayerInfoPacket implements ServerPacket {
public String name;
public String value;
public boolean signed = false;
public String signature;
public Property(String name, String value) {
public Property(String name, String value, String signature) {
this.name = name;
this.value = value;
this.signature = signature;
}
public Property(String name, String value) {
this(name, value, null);
}
public void write(PacketWriter writer) {
writer.writeSizedString(name);
writer.writeSizedString(value);
final boolean signed = signature != null;
writer.writeBoolean(signed);
if (signed)
writer.writeSizedString(signature);