Fixed vehicles + added PlayerStartFlyingEvent & PlayerStopFlyingEvent

This commit is contained in:
Felix Cravic 2020-05-24 22:21:38 +02:00
parent c73ec6f0d6
commit e26ee1380a
11 changed files with 85 additions and 30 deletions

View File

@ -181,11 +181,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
Runnable runnable = () -> { Runnable runnable = () -> {
refreshPosition(position.getX(), position.getY(), position.getZ()); refreshPosition(position.getX(), position.getY(), position.getZ());
refreshView(position.getYaw(), position.getPitch()); refreshView(position.getYaw(), position.getPitch());
EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket(); sendSynchronization();
entityTeleportPacket.entityId = getEntityId();
entityTeleportPacket.position = position;
entityTeleportPacket.onGround = isOnGround();
sendPacketToViewers(entityTeleportPacket);
}; };
if (instance.hasEnabledAutoChunkLoad()) { if (instance.hasEnabledAutoChunkLoad()) {
@ -329,8 +325,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
velocity.setX(velocity.getX() * drag); velocity.setX(velocity.getX() * drag);
velocity.setZ(velocity.getZ() * drag); velocity.setZ(velocity.getZ() * drag);
sendSynchronization();
if (shouldSendVelocityUpdate(time)) { if (shouldSendVelocityUpdate(time)) {
sendPacketToViewers(getVelocityPacket()); sendPacketToViewers(getVelocityPacket());
lastVelocityUpdateTime = time; lastVelocityUpdateTime = time;
@ -706,13 +700,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
position.setPitch(pitch); position.setPitch(pitch);
} }
/**
* Ask for a synchronization (position) to happen during next entity update
*/
public void askSynchronization() {
this.lastSynchronizationTime = 0;
}
public void refreshSneaking(boolean sneaking) { public void refreshSneaking(boolean sneaking) {
this.crouched = sneaking; this.crouched = sneaking;
this.pose = sneaking ? Pose.SNEAKING : Pose.STANDING; this.pose = sneaking ? Pose.SNEAKING : Pose.STANDING;
@ -927,9 +914,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
entityTeleportPacket.position = getPosition(); entityTeleportPacket.position = getPosition();
entityTeleportPacket.onGround = isOnGround(); entityTeleportPacket.onGround = isOnGround();
sendPacketToViewers(entityTeleportPacket); sendPacketToViewers(entityTeleportPacket);
}
if (!passengers.isEmpty()) /**
sendPacketToViewers(getPassengersPacket()); * Ask for a synchronization (position) to happen during next entity update
*/
public void askSynchronization() {
this.lastSynchronizationTime = 0;
} }
private boolean shouldUpdate(long time) { private boolean shouldUpdate(long time) {

View File

@ -736,10 +736,6 @@ public class Player extends LivingEntity {
} }
protected void onChunkChange(Chunk lastChunk, Chunk newChunk) { protected void onChunkChange(Chunk lastChunk, Chunk newChunk) {
float dx = newChunk.getChunkX() - lastChunk.getChunkX();
float dz = newChunk.getChunkZ() - lastChunk.getChunkZ();
double distance = Math.sqrt(dx * dx + dz * dz);
long[] lastVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), MinecraftServer.CHUNK_VIEW_DISTANCE); long[] lastVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), MinecraftServer.CHUNK_VIEW_DISTANCE);
long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * newChunk.getChunkX(), 0, 16 * newChunk.getChunkZ()), MinecraftServer.CHUNK_VIEW_DISTANCE); long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * newChunk.getChunkX(), 0, 16 * newChunk.getChunkZ()), MinecraftServer.CHUNK_VIEW_DISTANCE);
int[] oldChunks = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks); int[] oldChunks = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks);
@ -766,7 +762,8 @@ public class Player extends LivingEntity {
int[] chunkPos = ChunkUtils.getChunkCoord(updatedVisibleChunks[index]); int[] chunkPos = ChunkUtils.getChunkCoord(updatedVisibleChunks[index]);
instance.loadOptionalChunk(chunkPos[0], chunkPos[1], chunk -> { instance.loadOptionalChunk(chunkPos[0], chunkPos[1], chunk -> {
if (chunk == null) { if (chunk == null) {
return; // Cannot load chunk (auto load is not enabled) // Cannot load chunk (auto load is not enabled)
return;
} }
this.viewableChunks.add(chunk); this.viewableChunks.add(chunk);
chunk.addViewer(this); chunk.addViewer(this);

View File

@ -0,0 +1,18 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
public class PlayerStartFlyingEvent extends Event {
private Player player;
public PlayerStartFlyingEvent(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
}

View File

@ -0,0 +1,17 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
public class PlayerStopFlyingEvent extends Event {
private Player player;
public PlayerStopFlyingEvent(Player player) {
this.player = player;
}
public Player getPlayer() {
return player;
}
}

View File

@ -406,11 +406,11 @@ public class Chunk implements Viewable {
this.biomes = null; this.biomes = null;
this.blocksId = null; this.blocksId = null;
this.customBlocksId = null; this.customBlocksId = null;
this.blocksData.clear(); this.blocksData = null;
this.updatableBlocks.clear(); this.updatableBlocks = null;
this.updatableBlocksLastUpdate.clear(); this.updatableBlocksLastUpdate = null;
this.blockEntities.clear(); this.blockEntities = null;
this.viewers.clear(); this.viewers = null;
} }
private int getBlockIndex(int x, int y, int z) { private int getBlockIndex(int x, int y, int z) {

View File

@ -73,6 +73,9 @@ public class BlockBatch implements InstanceBatch {
boolean isLast = counter == data.size(); boolean isLast = counter == data.size();
batchesPool.execute(() -> { batchesPool.execute(() -> {
synchronized (chunk) { synchronized (chunk) {
if (!chunk.isLoaded())
return;
for (BlockData data : dataList) { for (BlockData data : dataList) {
data.apply(chunk); data.apply(chunk);
} }

View File

@ -94,6 +94,9 @@ public class ChunkBatch implements InstanceBatch {
private void singleThreadFlush(Consumer<Chunk> callback) { private void singleThreadFlush(Consumer<Chunk> callback) {
synchronized (chunk) { synchronized (chunk) {
if (!chunk.isLoaded())
return;
for (BlockData data : dataList) { for (BlockData data : dataList) {
data.apply(chunk); data.apply(chunk);
} }

View File

@ -0,0 +1,25 @@
package net.minestom.server.listener;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerStartFlyingEvent;
import net.minestom.server.event.player.PlayerStopFlyingEvent;
import net.minestom.server.network.packet.client.play.ClientPlayerAbilitiesPacket;
public class AbilitiesListener {
public static void listener(ClientPlayerAbilitiesPacket packet, Player player) {
final boolean canFly = player.isAllowFlying() || player.isCreative();
if (canFly) {
boolean isFlying = (packet.flags & 0x2) > 0;
if (isFlying) {
PlayerStartFlyingEvent startFlyingEvent = new PlayerStartFlyingEvent(player);
player.callEvent(PlayerStartFlyingEvent.class, startFlyingEvent);
} else {
PlayerStopFlyingEvent stopFlyingEvent = new PlayerStopFlyingEvent(player);
player.callEvent(PlayerStopFlyingEvent.class, stopFlyingEvent);
}
}
}
}

View File

@ -6,7 +6,6 @@ import net.minestom.server.entity.type.EntityBoat;
import net.minestom.server.network.packet.client.play.ClientSteerBoatPacket; import net.minestom.server.network.packet.client.play.ClientSteerBoatPacket;
import net.minestom.server.network.packet.client.play.ClientSteerVehiclePacket; import net.minestom.server.network.packet.client.play.ClientSteerVehiclePacket;
import net.minestom.server.network.packet.client.play.ClientVehicleMovePacket; import net.minestom.server.network.packet.client.play.ClientVehicleMovePacket;
import net.minestom.server.network.packet.server.play.VehicleMovePacket;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
public class PlayerVehicleListener { public class PlayerVehicleListener {
@ -29,13 +28,14 @@ public class PlayerVehicleListener {
vehicle.refreshView(packet.yaw, packet.pitch); vehicle.refreshView(packet.yaw, packet.pitch);
vehicle.askSynchronization(); vehicle.askSynchronization();
VehicleMovePacket vehicleMovePacket = new VehicleMovePacket(); // This packet causes weird screen distortion
/*VehicleMovePacket vehicleMovePacket = new VehicleMovePacket();
vehicleMovePacket.x = packet.x; vehicleMovePacket.x = packet.x;
vehicleMovePacket.y = packet.y; vehicleMovePacket.y = packet.y;
vehicleMovePacket.z = packet.z; vehicleMovePacket.z = packet.z;
vehicleMovePacket.yaw = packet.yaw; vehicleMovePacket.yaw = packet.yaw;
vehicleMovePacket.pitch = packet.pitch; vehicleMovePacket.pitch = packet.pitch;
player.getPlayerConnection().sendPacket(vehicleMovePacket); player.getPlayerConnection().sendPacket(vehicleMovePacket);*/
} }

View File

@ -41,6 +41,7 @@ public class PacketListenerManager {
addListener(ClientCraftRecipeRequest.class, RecipeListener::listener); addListener(ClientCraftRecipeRequest.class, RecipeListener::listener);
addListener(ClientTabCompletePacket.class, TabCompleteListener::listener); addListener(ClientTabCompletePacket.class, TabCompleteListener::listener);
addListener(ClientPluginMessagePacket.class, PluginMessageListener::listener); addListener(ClientPluginMessagePacket.class, PluginMessageListener::listener);
addListener(ClientPlayerAbilitiesPacket.class, AbilitiesListener::listener);
} }
public <T extends ClientPlayPacket> void process(T packet, Player player) { public <T extends ClientPlayPacket> void process(T packet, Player player) {

View File

@ -24,7 +24,7 @@ public class PacketUtils {
Utils.writeVarIntBuf(buffer, length); Utils.writeVarIntBuf(buffer, length);
buffer.writeBytes(bytes); buffer.writeBytes(bytes);
//System.out.println("WRITE PACKET: " + id + " " + serverPacket.getClass().getSimpleName() + " size: " + length); //System.out.println("WRITE PACKET: " + id + " " + serverPacket.getClass().getSimpleName());
return Unpooled.copiedBuffer(buffer); return Unpooled.copiedBuffer(buffer);
} }