This commit is contained in:
TheMode 2019-08-30 01:17:46 +02:00
parent 932fb6ae2b
commit d929730a61
10 changed files with 260 additions and 98 deletions

View File

@ -0,0 +1,66 @@
package fr.themode.minestom.collision;
import fr.themode.minestom.entity.Entity;
/**
* See https://wiki.vg/Entity_metadata#Mobs_2
*/
public class BoundingBox {
private Entity entity;
private float x, y, z;
public BoundingBox(Entity entity, float x, float y, float z) {
this.entity = entity;
this.x = x;
this.y = y;
this.z = z;
}
public boolean intersect(BoundingBox boundingBox) {
return (getMinX() <= boundingBox.getMaxX() && getMaxX() >= boundingBox.getMinX()) &&
(getMinY() <= boundingBox.getMaxY() && getMaxY() >= boundingBox.getMinY()) &&
(getMinZ() <= boundingBox.getMaxZ() && getMaxZ() >= boundingBox.getMinZ());
}
public BoundingBox expand(float x, float y, float z) {
return new BoundingBox(entity, this.x + x, this.y + y, this.z + z);
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
public float getMinX() {
return entity.getPosition().getX() - (x / 2);
}
public float getMaxX() {
return entity.getPosition().getX() + (x / 2);
}
public float getMinY() {
return entity.getPosition().getY() - (y / 2);
}
public float getMaxY() {
return entity.getPosition().getY() + (y / 2);
}
public float getMinZ() {
return entity.getPosition().getZ() - (z / 2);
}
public float getMaxZ() {
return entity.getPosition().getZ() + (z / 2);
}
}

View File

@ -3,6 +3,7 @@ package fr.themode.minestom.entity;
import fr.adamaq01.ozao.net.Buffer; import fr.adamaq01.ozao.net.Buffer;
import fr.themode.minestom.Main; import fr.themode.minestom.Main;
import fr.themode.minestom.Viewable; import fr.themode.minestom.Viewable;
import fr.themode.minestom.collision.BoundingBox;
import fr.themode.minestom.data.Data; import fr.themode.minestom.data.Data;
import fr.themode.minestom.data.DataContainer; import fr.themode.minestom.data.DataContainer;
import fr.themode.minestom.event.Callback; import fr.themode.minestom.event.Callback;
@ -11,6 +12,7 @@ import fr.themode.minestom.event.Event;
import fr.themode.minestom.instance.Chunk; import fr.themode.minestom.instance.Chunk;
import fr.themode.minestom.instance.Instance; import fr.themode.minestom.instance.Instance;
import fr.themode.minestom.net.packet.server.play.*; import fr.themode.minestom.net.packet.server.play.*;
import fr.themode.minestom.net.player.PlayerConnection;
import fr.themode.minestom.utils.Vector; import fr.themode.minestom.utils.Vector;
import fr.themode.minestom.utils.*; import fr.themode.minestom.utils.*;
@ -40,6 +42,8 @@ public abstract class Entity implements Viewable, DataContainer {
protected float lastYaw, lastPitch; protected float lastYaw, lastPitch;
private int id; private int id;
private BoundingBox boundingBox;
protected Entity vehicle; protected Entity vehicle;
// Velocity // Velocity
// TODO gravity implementation for entity other than players // TODO gravity implementation for entity other than players
@ -86,6 +90,8 @@ public abstract class Entity implements Viewable, DataContainer {
this.uuid = UUID.randomUUID(); this.uuid = UUID.randomUUID();
this.position = new Position(); this.position = new Position();
setBoundingBox(0, 0, 0);
synchronized (entityById) { synchronized (entityById) {
entityById.put(id, this); entityById.put(id, this);
} }
@ -103,7 +109,7 @@ public abstract class Entity implements Viewable, DataContainer {
public abstract void update(); public abstract void update();
// Called when entity a new instance is set // Called when a new instance is set
public abstract void spawn(); public abstract void spawn();
public abstract boolean isOnGround(); public abstract boolean isOnGround();
@ -145,7 +151,9 @@ public abstract class Entity implements Viewable, DataContainer {
public void addViewer(Player player) { public void addViewer(Player player) {
this.viewers.add(player); this.viewers.add(player);
player.viewableEntity.add(this); player.viewableEntity.add(this);
player.getPlayerConnection().sendPacket(getVelocityPacket()); PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(getVelocityPacket());
playerConnection.sendPacket(getPassengersPacket()); // TODO fix synchronization
} }
@Override @Override
@ -198,7 +206,7 @@ public abstract class Entity implements Viewable, DataContainer {
// Velocity // Velocity
if (velocityTime != 0) { if (velocityTime != 0) {
if (time >= velocityTime) { if (time >= velocityTime) {
sendPositionSynchronization(); // Send synchronization after velocity ended sendSynchronization(); // Send synchronization after velocity ended
resetVelocity(); resetVelocity();
} else { } else {
if (this instanceof Player) { if (this instanceof Player) {
@ -250,7 +258,7 @@ public abstract class Entity implements Viewable, DataContainer {
// Scheduled synchronization // Scheduled synchronization
if (time - lastSynchronizationTime >= synchronizationDelay) { if (time - lastSynchronizationTime >= synchronizationDelay) {
lastSynchronizationTime = System.currentTimeMillis(); lastSynchronizationTime = System.currentTimeMillis();
sendPositionSynchronization(); sendSynchronization();
} }
} }
@ -296,6 +304,14 @@ public abstract class Entity implements Viewable, DataContainer {
return isActive; return isActive;
} }
public BoundingBox getBoundingBox() {
return boundingBox;
}
public void setBoundingBox(float x, float y, float z) {
this.boundingBox = new BoundingBox(this, x, y, z);
}
public Instance getInstance() { public Instance getInstance() {
return instance; return instance;
} }
@ -350,7 +366,7 @@ public abstract class Entity implements Viewable, DataContainer {
this.passengers.add(entity); this.passengers.add(entity);
entity.vehicle = this; entity.vehicle = this;
sendPassengersPacket(); sendPacketToViewersAndSelf(getPassengersPacket());
} }
public void removePassenger(Entity entity) { public void removePassenger(Entity entity) {
@ -360,7 +376,7 @@ public abstract class Entity implements Viewable, DataContainer {
return; return;
this.passengers.remove(entity); this.passengers.remove(entity);
entity.vehicle = null; entity.vehicle = null;
sendPassengersPacket(); sendPacketToViewersAndSelf(getPassengersPacket());
} }
public boolean hasPassenger() { public boolean hasPassenger() {
@ -371,7 +387,7 @@ public abstract class Entity implements Viewable, DataContainer {
return Collections.unmodifiableSet(passengers); return Collections.unmodifiableSet(passengers);
} }
protected void sendPassengersPacket() { protected SetPassengersPacket getPassengersPacket() {
SetPassengersPacket passengersPacket = new SetPassengersPacket(); SetPassengersPacket passengersPacket = new SetPassengersPacket();
passengersPacket.vehicleEntityId = getEntityId(); passengersPacket.vehicleEntityId = getEntityId();
@ -382,7 +398,7 @@ public abstract class Entity implements Viewable, DataContainer {
} }
passengersPacket.passengersId = passengers; passengersPacket.passengersId = passengers;
sendPacketToViewersAndSelf(passengersPacket); return passengersPacket;
} }
public void triggerStatus(byte status) { public void triggerStatus(byte status) {
@ -427,6 +443,10 @@ public abstract class Entity implements Viewable, DataContainer {
position.setY(y); position.setY(y);
position.setZ(z); position.setZ(z);
for (Entity passenger : getPassengers()) {
passenger.position = getPosition().clone();
}
Instance instance = getInstance(); Instance instance = getInstance();
if (instance != null) { if (instance != null) {
Chunk lastChunk = instance.getChunkAt(lastX, lastZ); Chunk lastChunk = instance.getChunkAt(lastX, lastZ);
@ -435,52 +455,15 @@ public abstract class Entity implements Viewable, DataContainer {
synchronized (instance) { synchronized (instance) {
instance.removeEntityFromChunk(this, lastChunk); instance.removeEntityFromChunk(this, lastChunk);
instance.addEntityToChunk(this, newChunk); instance.addEntityToChunk(this, newChunk);
for (Entity passenger : getPassengers()) {
instance.removeEntityFromChunk(passenger, lastChunk);
instance.addEntityToChunk(passenger, newChunk);
}
} }
if (this instanceof Player) updateView(this, lastChunk, newChunk);
((Player) this).onChunkChange(lastChunk, newChunk); // Refresh loaded chunk for (Entity passenger : getPassengers()) {
updateView(passenger, lastChunk, newChunk);
// Refresh entity viewable list
long[] lastVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), Main.ENTITY_VIEW_DISTANCE);
long[] updatedVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * newChunk.getChunkX(), 0, 16 * newChunk.getChunkZ()), Main.ENTITY_VIEW_DISTANCE);
boolean isPlayer = this instanceof Player;
int[] oldChunksEntity = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunksEntity, updatedVisibleChunksEntity);
for (int index : oldChunksEntity) {
int[] chunkPos = ChunkUtils.getChunkCoord(lastVisibleChunksEntity[index]);
Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]);
if (chunk == null)
continue;
instance.getChunkEntities(chunk).forEach(entity -> {
if (entity instanceof Player) {
Player player = (Player) entity;
removeViewer(player);
if (isPlayer) {
player.removeViewer((Player) this);
}
} else if (isPlayer) {
entity.removeViewer((Player) this);
}
});
}
int[] newChunksEntity = ArrayUtils.getDifferencesBetweenArray(updatedVisibleChunksEntity, lastVisibleChunksEntity);
for (int index : newChunksEntity) {
int[] chunkPos = ChunkUtils.getChunkCoord(updatedVisibleChunksEntity[index]);
Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]);
if (chunk == null)
continue;
instance.getChunkEntities(chunk).forEach(entity -> {
if (entity instanceof Player) {
Player player = (Player) entity;
addViewer(player);
if (this instanceof Player) {
player.addViewer((Player) this);
}
} else if (isPlayer) {
entity.addViewer((Player) this);
}
});
} }
} }
@ -491,6 +474,55 @@ public abstract class Entity implements Viewable, DataContainer {
refreshPosition(position.getX(), position.getY(), position.getZ()); refreshPosition(position.getX(), position.getY(), position.getZ());
} }
private void updateView(Entity entity, Chunk lastChunk, Chunk newChunk) {
if (entity instanceof Player)
((Player) entity).onChunkChange(lastChunk, newChunk); // Refresh loaded chunk
// Refresh entity viewable list
long[] lastVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), Main.ENTITY_VIEW_DISTANCE);
long[] updatedVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * newChunk.getChunkX(), 0, 16 * newChunk.getChunkZ()), Main.ENTITY_VIEW_DISTANCE);
boolean isPlayer = entity instanceof Player;
int[] oldChunksEntity = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunksEntity, updatedVisibleChunksEntity);
for (int index : oldChunksEntity) {
int[] chunkPos = ChunkUtils.getChunkCoord(lastVisibleChunksEntity[index]);
Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]);
if (chunk == null)
continue;
instance.getChunkEntities(chunk).forEach(ent -> {
if (ent instanceof Player) {
Player player = (Player) ent;
removeViewer(player);
if (isPlayer) {
player.removeViewer((Player) entity);
}
} else if (isPlayer) {
ent.removeViewer((Player) entity);
}
});
}
int[] newChunksEntity = ArrayUtils.getDifferencesBetweenArray(updatedVisibleChunksEntity, lastVisibleChunksEntity);
for (int index : newChunksEntity) {
int[] chunkPos = ChunkUtils.getChunkCoord(updatedVisibleChunksEntity[index]);
Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]);
if (chunk == null)
continue;
instance.getChunkEntities(chunk).forEach(ent -> {
if (ent instanceof Player) {
Player player = (Player) ent;
addViewer(player);
if (entity instanceof Player) {
player.addViewer((Player) entity);
}
} else if (isPlayer) {
ent.addViewer((Player) entity);
}
});
}
}
public void refreshView(float yaw, float pitch) { public void refreshView(float yaw, float pitch) {
this.lastYaw = position.getYaw(); this.lastYaw = position.getYaw();
this.lastPitch = position.getPitch(); this.lastPitch = position.getPitch();
@ -529,6 +561,10 @@ public abstract class Entity implements Viewable, DataContainer {
this.scheduledRemoveTime = System.currentTimeMillis() + delay; this.scheduledRemoveTime = System.currentTimeMillis() + delay;
} }
public boolean isRemoveScheduled() {
return scheduledRemoveTime != 0;
}
protected EntityVelocityPacket getVelocityPacket() { protected EntityVelocityPacket getVelocityPacket() {
final float strength = 8000f / Main.TICK_PER_SECOND; final float strength = 8000f / Main.TICK_PER_SECOND;
EntityVelocityPacket velocityPacket = new EntityVelocityPacket(); EntityVelocityPacket velocityPacket = new EntityVelocityPacket();
@ -605,12 +641,13 @@ public abstract class Entity implements Viewable, DataContainer {
buffer.putByte(index0); buffer.putByte(index0);
} }
protected void sendPositionSynchronization() { protected void sendSynchronization() {
EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket(); EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket();
entityTeleportPacket.entityId = getEntityId(); entityTeleportPacket.entityId = getEntityId();
entityTeleportPacket.position = getPosition(); entityTeleportPacket.position = getPosition();
entityTeleportPacket.onGround = isOnGround(); entityTeleportPacket.onGround = isOnGround();
sendPacketToViewers(entityTeleportPacket); sendPacketToViewers(entityTeleportPacket);
sendPacketToViewers(getPassengersPacket());
} }
private void fillAirTickMetaData(Buffer buffer) { private void fillAirTickMetaData(Buffer buffer) {

View File

@ -1,10 +1,7 @@
package fr.themode.minestom.entity; package fr.themode.minestom.entity;
import fr.themode.minestom.event.DeathEvent; import fr.themode.minestom.event.DeathEvent;
import fr.themode.minestom.net.packet.server.play.EntityHeadLookPacket; import fr.themode.minestom.net.packet.server.play.*;
import fr.themode.minestom.net.packet.server.play.EntityLookAndRelativeMovePacket;
import fr.themode.minestom.net.packet.server.play.EntityPacket;
import fr.themode.minestom.net.packet.server.play.SpawnMobPacket;
import fr.themode.minestom.net.player.PlayerConnection; import fr.themode.minestom.net.player.PlayerConnection;
import fr.themode.minestom.utils.ChunkUtils; import fr.themode.minestom.utils.ChunkUtils;
import fr.themode.minestom.utils.EntityUtils; import fr.themode.minestom.utils.EntityUtils;
@ -22,8 +19,7 @@ public abstract class EntityCreature extends LivingEntity {
super.update(); super.update();
} }
public void move(float x, float y, float z) { public void move(float x, float y, float z, boolean updateView) {
// TODO change yaw/pitch based on next position?
Position position = getPosition(); Position position = getPosition();
float newX = position.getX() + x; float newX = position.getX() + x;
float newY = position.getY() + y; float newY = position.getY() + y;
@ -38,33 +34,42 @@ public abstract class EntityCreature extends LivingEntity {
float yaw = (float) (radians * (180.0 / Math.PI)) - 90; float yaw = (float) (radians * (180.0 / Math.PI)) - 90;
float pitch = position.getPitch(); // TODO float pitch = position.getPitch(); // TODO
if (updateView) {
EntityLookAndRelativeMovePacket entityLookAndRelativeMovePacket = new EntityLookAndRelativeMovePacket(); EntityLookAndRelativeMovePacket entityLookAndRelativeMovePacket = new EntityLookAndRelativeMovePacket();
entityLookAndRelativeMovePacket.entityId = getEntityId(); entityLookAndRelativeMovePacket.entityId = getEntityId();
entityLookAndRelativeMovePacket.deltaX = (short) ((newX * 32 - position.getX() * 32) * 128); entityLookAndRelativeMovePacket.deltaX = (short) ((newX * 32 - position.getX() * 32) * 128);
entityLookAndRelativeMovePacket.deltaY = (short) ((newY * 32 - position.getY() * 32) * 128); entityLookAndRelativeMovePacket.deltaY = (short) ((newY * 32 - position.getY() * 32) * 128);
entityLookAndRelativeMovePacket.deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128); entityLookAndRelativeMovePacket.deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128);
entityLookAndRelativeMovePacket.yaw = yaw; entityLookAndRelativeMovePacket.yaw = yaw;
entityLookAndRelativeMovePacket.pitch = pitch; entityLookAndRelativeMovePacket.pitch = pitch;
entityLookAndRelativeMovePacket.onGround = isOnGround(); entityLookAndRelativeMovePacket.onGround = isOnGround();
sendPacketToViewers(entityLookAndRelativeMovePacket); sendPacketToViewers(entityLookAndRelativeMovePacket);
} else {
EntityRelativeMovePacket entityRelativeMovePacket = new EntityRelativeMovePacket();
entityRelativeMovePacket.entityId = getEntityId();
entityRelativeMovePacket.deltaX = (short) ((newX * 32 - position.getX() * 32) * 128);
entityRelativeMovePacket.deltaY = (short) ((newY * 32 - position.getY() * 32) * 128);
entityRelativeMovePacket.deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128);
entityRelativeMovePacket.onGround = isOnGround();
sendPacketToViewers(entityRelativeMovePacket);
}
if (lastYaw != yaw) { if (lastYaw != yaw) {
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket(); EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId(); entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = yaw; entityHeadLookPacket.yaw = yaw;
sendPacketToViewers(entityHeadLookPacket); sendPacketToViewers(entityHeadLookPacket);
refreshView(yaw, pitch);
} }
refreshPosition(newX, newY, newZ); refreshPosition(newX, newY, newZ);
refreshView(yaw, pitch);
} }
public void moveTowards(Position direction, float speed) { public void moveTowards(Position direction, float speed) {
float radians = (float) Math.atan2(direction.getZ() - position.getZ(), direction.getX() - position.getX()); float radians = (float) Math.atan2(direction.getZ() - position.getZ(), direction.getX() - position.getX());
float speedX = (float) (Math.cos(radians) * speed); float speedX = (float) (Math.cos(radians) * speed);
float speedZ = (float) (Math.sin(radians) * speed); float speedZ = (float) (Math.sin(radians) * speed);
move(speedX, 0, speedZ); move(speedX, 0, speedZ, true);
} }
@Override @Override

View File

@ -15,6 +15,7 @@ public class ItemEntity extends ObjectEntity {
public ItemEntity(ItemStack itemStack) { public ItemEntity(ItemStack itemStack) {
super(34); super(34);
this.itemStack = itemStack; this.itemStack = itemStack;
setBoundingBox(0.25f, 0.25f, 0.25f);
setGravity(0.02f); setGravity(0.02f);
} }

View File

@ -1,6 +1,7 @@
package fr.themode.minestom.entity; package fr.themode.minestom.entity;
import fr.adamaq01.ozao.net.Buffer; import fr.adamaq01.ozao.net.Buffer;
import fr.themode.minestom.collision.BoundingBox;
import fr.themode.minestom.entity.property.Attribute; import fr.themode.minestom.entity.property.Attribute;
import fr.themode.minestom.event.PickupItemEvent; import fr.themode.minestom.event.PickupItemEvent;
import fr.themode.minestom.instance.Chunk; import fr.themode.minestom.instance.Chunk;
@ -37,15 +38,16 @@ public abstract class LivingEntity extends Entity {
if (canPickupItem) { if (canPickupItem) {
Chunk chunk = instance.getChunkAt(getPosition()); // TODO check surrounding chunks Chunk chunk = instance.getChunkAt(getPosition()); // TODO check surrounding chunks
Set<Entity> entities = instance.getChunkEntities(chunk); Set<Entity> entities = instance.getChunkEntities(chunk);
BoundingBox livingBoundingBox = getBoundingBox().expand(1, 0.5f, 1);
for (Entity entity : entities) { for (Entity entity : entities) {
if (entity instanceof ItemEntity) { if (entity instanceof ItemEntity) {
ItemEntity itemEntity = (ItemEntity) entity; ItemEntity itemEntity = (ItemEntity) entity;
if (!itemEntity.isPickable()) if (!itemEntity.isPickable())
continue; continue;
float distance = getDistance(entity); BoundingBox itemBoundingBox = itemEntity.getBoundingBox();
if (distance <= 2.04) { if (livingBoundingBox.intersect(itemBoundingBox)) {
synchronized (itemEntity) { synchronized (itemEntity) {
if (itemEntity.shouldRemove()) if (itemEntity.shouldRemove() || itemEntity.isRemoveScheduled())
continue; continue;
ItemStack item = itemEntity.getItemStack(); ItemStack item = itemEntity.getItemStack();
PickupItemEvent pickupItemEvent = new PickupItemEvent(item); PickupItemEvent pickupItemEvent = new PickupItemEvent(item);
@ -55,7 +57,7 @@ public abstract class LivingEntity extends Entity {
collectItemPacket.collectorEntityId = getEntityId(); collectItemPacket.collectorEntityId = getEntityId();
collectItemPacket.pickupItemCount = item.getAmount(); collectItemPacket.pickupItemCount = item.getAmount();
sendPacketToViewersAndSelf(collectItemPacket); sendPacketToViewersAndSelf(collectItemPacket);
entity.remove(); entity.scheduleRemove(500);
}); });
} }
} }

View File

@ -4,6 +4,7 @@ import fr.themode.minestom.Main;
import fr.themode.minestom.bossbar.BossBar; import fr.themode.minestom.bossbar.BossBar;
import fr.themode.minestom.chat.Chat; import fr.themode.minestom.chat.Chat;
import fr.themode.minestom.data.Data; import fr.themode.minestom.data.Data;
import fr.themode.minestom.entity.demo.ChickenCreature;
import fr.themode.minestom.entity.property.Attribute; import fr.themode.minestom.entity.property.Attribute;
import fr.themode.minestom.event.*; import fr.themode.minestom.event.*;
import fr.themode.minestom.instance.Chunk; import fr.themode.minestom.instance.Chunk;
@ -81,6 +82,8 @@ public class Player extends LivingEntity {
// Vehicle // Vehicle
private float sideways; private float sideways;
private float forward; private float forward;
private boolean jump;
private boolean unmount;
protected boolean spawned; protected boolean spawned;
@ -90,6 +93,8 @@ public class Player extends LivingEntity {
this.username = username; this.username = username;
this.playerConnection = playerConnection; this.playerConnection = playerConnection;
setBoundingBox(0.8f, 1.8f, 0.8f);
playerConnection.sendPacket(getPropertiesPacket()); // Send default properties playerConnection.sendPacket(getPropertiesPacket()); // Send default properties
refreshHealth(); refreshHealth();
@ -151,11 +156,12 @@ public class Player extends LivingEntity {
setGameMode(GameMode.SURVIVAL); setGameMode(GameMode.SURVIVAL);
teleport(new Position(0, 66, 0)); teleport(new Position(0, 66, 0));
/*ChickenCreature chickenCreature = new ChickenCreature(); ChickenCreature chickenCreature = new ChickenCreature();
chickenCreature.refreshPosition(2, 65, 2); chickenCreature.refreshPosition(2, 65, 2);
chickenCreature.setInstance(getInstance()); chickenCreature.setInstance(getInstance());
chickenCreature.addPassenger(this);
for (int ix = 0; ix < 4; ix++) /*for (int ix = 0; ix < 4; ix++)
for (int iz = 0; iz < 4; iz++) { for (int iz = 0; iz < 4; iz++) {
ItemEntity itemEntity = new ItemEntity(new ItemStack(1, (byte) 32)); ItemEntity itemEntity = new ItemEntity(new ItemStack(1, (byte) 32));
itemEntity.refreshPosition(ix, 68, iz); itemEntity.refreshPosition(ix, 68, iz);
@ -582,6 +588,14 @@ public class Player extends LivingEntity {
return forward; return forward;
} }
public boolean isVehicleJump() {
return jump;
}
public boolean isVehicleUnmount() {
return unmount;
}
public void openInventory(Inventory inventory) { public void openInventory(Inventory inventory) {
if (inventory == null) if (inventory == null)
throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current"); throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current");
@ -695,9 +709,11 @@ public class Player extends LivingEntity {
this.bossBars.remove(bossBar); this.bossBars.remove(bossBar);
} }
public void refreshVehicleSteer(float sideways, float forward) { public void refreshVehicleSteer(float sideways, float forward, boolean jump, boolean unmount) {
this.sideways = sideways; this.sideways = sideways;
this.forward = forward; this.forward = forward;
this.jump = jump;
this.unmount = unmount;
} }
public long getLastKeepAlive() { public long getLastKeepAlive() {

View File

@ -1,11 +1,15 @@
package fr.themode.minestom.entity.demo; package fr.themode.minestom.entity.demo;
import fr.themode.minestom.Main; import fr.themode.minestom.entity.Entity;
import fr.themode.minestom.entity.EntityCreature; import fr.themode.minestom.entity.EntityCreature;
import fr.themode.minestom.entity.EntityType; import fr.themode.minestom.entity.EntityType;
import fr.themode.minestom.entity.Player; import fr.themode.minestom.entity.Player;
import fr.themode.minestom.utils.Position; import fr.themode.minestom.utils.Position;
import net.tofweb.starlite.*; import fr.themode.minestom.utils.Vector;
import net.tofweb.starlite.CellSpace;
import net.tofweb.starlite.CostBlockManager;
import net.tofweb.starlite.Path;
import net.tofweb.starlite.Pathfinder;
public class ChickenCreature extends EntityCreature { public class ChickenCreature extends EntityCreature {
@ -17,44 +21,69 @@ public class ChickenCreature extends EntityCreature {
public ChickenCreature() { public ChickenCreature() {
super(EntityType.CHICKEN); super(EntityType.CHICKEN);
setBoundingBox(0.4f, 0.7f, 0.4f);
} }
@Override @Override
public void update() { public void update() {
super.update(); super.update();
float speed = 0.125f; float speed = 0.25f;
/*if (hasPassenger()) { if (hasPassenger()) {
Entity passenger = getPassengers().iterator().next(); Entity passenger = getPassengers().iterator().next();
if (passenger instanceof Player) { if (passenger instanceof Player) {
Player player = (Player) passenger; Player player = (Player) passenger;
float sideways = player.getVehicleSideways(); float sideways = player.getVehicleSideways();
float forward = player.getVehicleForward(); float forward = player.getVehicleForward();
boolean jump = player.isVehicleJump();
boolean unmount = player.isVehicleUnmount();
if (jump && isOnGround()) {
setVelocity(new Vector(0, 6, 0), 500);
}
boolean updateView = forward > 0;
if (sideways == 0f && forward == 0f) if (sideways == 0f && forward == 0f)
return; return;
float yaw = player.getYaw(); float yaw = player.getPosition().getYaw();
yaw %= 360; yaw %= 360;
sideways = yaw + (updateView ? -sideways * 90 : sideways * 90);
if (forward > 0) { if (forward > 0) {
forward = yaw * forward; forward = yaw * forward;
} else { } else {
forward = yaw - forward * 180; forward = yaw + forward * 360;
} }
sideways = yaw - sideways * 90;
yaw = (forward + sideways) / 2 % 360; yaw = (forward + sideways) / 2 % 360;
System.out.println("test: " + forward + " : " + sideways);
double radian = Math.toRadians(yaw + 90); double radian = Math.toRadians(yaw + 90);
double cos = Math.cos(radian); double cos = Math.cos(radian);
double sin = Math.sin(radian); double sin = Math.sin(radian);
//System.out.println(sideways + " : " + forward); float x = (float) cos * speed;
//System.out.println(cos + " : " + sin); float z = (float) sin * speed;
double x = cos;
double z = sin;
move(x * speed, 0, z * speed);
}
}*/
if (path == null) { /*BlockPosition blockPosition = getPosition().toBlockPosition();
BlockPosition belowPosition = blockPosition.clone().add(0, -1, 0);
BlockPosition upPosition = blockPosition.clone().add(0, 1, 0);
boolean airCurrent = getInstance().getBlockId(blockPosition) == 0;
boolean airBelow = getInstance().getBlockId(belowPosition) == 0;
boolean airUp = getInstance().getBlockId(upPosition) == 0;
boolean shouldJump = false;
int boundingBoxY = (int) Math.ceil(getBoundingBox().getY());
for (int i = 0; i < boundingBoxY; i++) {
}*/
//System.out.println("test: "+player.isVehicleJump());
//System.out.println(getInstance().getBlockId(getPosition().toBlockPosition()));
move(x, 0, z, updateView);
}
}
/*if (path == null) {
System.out.println("FIND PATH"); System.out.println("FIND PATH");
Player player = Main.getConnectionManager().getPlayer("Raulnil"); Player player = Main.getConnectionManager().getPlayer("Raulnil");
if (player != null) { if (player != null) {
@ -88,7 +117,7 @@ public class ChickenCreature extends EntityCreature {
moveTowards(target, speed); moveTowards(target, speed);
} }
} }
} }*/
} }
@Override @Override

View File

@ -97,7 +97,7 @@ public class PlayerDiggingListener {
player.getInventory().setItemInMainHand(handItem); player.getInventory().setItemInMainHand(handItem);
ItemEntity itemEntity = new ItemEntity(droppedItem); ItemEntity itemEntity = new ItemEntity(droppedItem);
itemEntity.setPickupDelay(1000); itemEntity.setPickupDelay(500);
itemEntity.refreshPosition(player.getPosition().clone().add(0, 1.5f, 0)); itemEntity.refreshPosition(player.getPosition().clone().add(0, 1.5f, 0));
itemEntity.setInstance(player.getInstance()); itemEntity.setInstance(player.getInstance());
Vector velocity = player.getPosition().clone().getDirection().multiply(5); Vector velocity = player.getPosition().clone().getDirection().multiply(5);

View File

@ -6,7 +6,10 @@ import fr.themode.minestom.net.packet.client.play.ClientSteerVehiclePacket;
public class PlayerVehicleListener { public class PlayerVehicleListener {
public static void steerVehicleListener(ClientSteerVehiclePacket packet, Player player) { public static void steerVehicleListener(ClientSteerVehiclePacket packet, Player player) {
player.refreshVehicleSteer(packet.sideways, packet.forward); byte flags = packet.flags;
boolean jump = (flags & 0x1) != 0;
boolean unmount = (flags & 0x2) != 0;
player.refreshVehicleSteer(packet.sideways, packet.forward, jump, unmount);
} }
} }

View File

@ -48,6 +48,9 @@ public class BlockPosition {
this.z = z; this.z = z;
} }
public BlockPosition clone() {
return new BlockPosition(x, y, z);
}
@Override @Override
public String toString() { public String toString() {