mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Update
This commit is contained in:
parent
8b95e3881d
commit
932fb6ae2b
@ -19,4 +19,5 @@ dependencies {
|
|||||||
// https://mvnrepository.com/artifact/org.lz4/lz4-java
|
// https://mvnrepository.com/artifact/org.lz4/lz4-java
|
||||||
implementation 'com.github.luben:zstd-jni:1.4.3-1'
|
implementation 'com.github.luben:zstd-jni:1.4.3-1'
|
||||||
implementation 'com.esotericsoftware:reflectasm:1.11.9'
|
implementation 'com.esotericsoftware:reflectasm:1.11.9'
|
||||||
|
implementation 'com.github.LynnOwens:starlite:9971b899f7'
|
||||||
}
|
}
|
||||||
|
@ -193,6 +193,7 @@ public abstract class Entity implements Viewable, DataContainer {
|
|||||||
return;
|
return;
|
||||||
} else if (shouldUpdate()) {
|
} else if (shouldUpdate()) {
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
|
this.lastUpdate = time;
|
||||||
|
|
||||||
// Velocity
|
// Velocity
|
||||||
if (velocityTime != 0) {
|
if (velocityTime != 0) {
|
||||||
@ -251,8 +252,6 @@ public abstract class Entity implements Viewable, DataContainer {
|
|||||||
lastSynchronizationTime = System.currentTimeMillis();
|
lastSynchronizationTime = System.currentTimeMillis();
|
||||||
sendPositionSynchronization();
|
sendPositionSynchronization();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lastUpdate = time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldRemove()) {
|
if (shouldRemove()) {
|
||||||
@ -444,6 +443,8 @@ public abstract class Entity implements Viewable, DataContainer {
|
|||||||
long[] lastVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), Main.ENTITY_VIEW_DISTANCE);
|
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);
|
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);
|
int[] oldChunksEntity = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunksEntity, updatedVisibleChunksEntity);
|
||||||
for (int index : oldChunksEntity) {
|
for (int index : oldChunksEntity) {
|
||||||
int[] chunkPos = ChunkUtils.getChunkCoord(lastVisibleChunksEntity[index]);
|
int[] chunkPos = ChunkUtils.getChunkCoord(lastVisibleChunksEntity[index]);
|
||||||
@ -454,9 +455,11 @@ public abstract class Entity implements Viewable, DataContainer {
|
|||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
Player player = (Player) entity;
|
Player player = (Player) entity;
|
||||||
removeViewer(player);
|
removeViewer(player);
|
||||||
if (this instanceof Player) {
|
if (isPlayer) {
|
||||||
player.removeViewer((Player) this);
|
player.removeViewer((Player) this);
|
||||||
}
|
}
|
||||||
|
} else if (isPlayer) {
|
||||||
|
entity.removeViewer((Player) this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -474,6 +477,8 @@ public abstract class Entity implements Viewable, DataContainer {
|
|||||||
if (this instanceof Player) {
|
if (this instanceof Player) {
|
||||||
player.addViewer((Player) this);
|
player.addViewer((Player) this);
|
||||||
}
|
}
|
||||||
|
} else if (isPlayer) {
|
||||||
|
entity.addViewer((Player) this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
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.EntityLookAndRelativeMovePacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.EntityPacket;
|
import fr.themode.minestom.net.packet.server.play.EntityPacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.EntityRelativeMovePacket;
|
|
||||||
import fr.themode.minestom.net.packet.server.play.SpawnMobPacket;
|
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;
|
||||||
import fr.themode.minestom.utils.Position;
|
import fr.themode.minestom.utils.Position;
|
||||||
|
|
||||||
// TODO viewers synchronization each X ticks?
|
// TODO pathfinding
|
||||||
public abstract class EntityCreature extends LivingEntity {
|
public abstract class EntityCreature extends LivingEntity {
|
||||||
|
|
||||||
public EntityCreature(EntityType entityType) {
|
public EntityCreature(EntityType entityType) {
|
||||||
@ -31,15 +32,39 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
if (ChunkUtils.isChunkUnloaded(getInstance(), newX, newZ))
|
if (ChunkUtils.isChunkUnloaded(getInstance(), newX, newZ))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EntityRelativeMovePacket entityRelativeMovePacket = new EntityRelativeMovePacket();
|
float lastYaw = position.getYaw();
|
||||||
entityRelativeMovePacket.entityId = getEntityId();
|
float radians = (float) Math.atan2(newZ - position.getZ(), newX - position.getX());
|
||||||
entityRelativeMovePacket.deltaX = (short) ((newX * 32 - position.getX() * 32) * 128);
|
|
||||||
entityRelativeMovePacket.deltaY = (short) ((newY * 32 - position.getY() * 32) * 128);
|
float yaw = (float) (radians * (180.0 / Math.PI)) - 90;
|
||||||
entityRelativeMovePacket.deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128);
|
float pitch = position.getPitch(); // TODO
|
||||||
entityRelativeMovePacket.onGround = true;
|
|
||||||
sendPacketToViewers(entityRelativeMovePacket);
|
|
||||||
|
EntityLookAndRelativeMovePacket entityLookAndRelativeMovePacket = new EntityLookAndRelativeMovePacket();
|
||||||
|
entityLookAndRelativeMovePacket.entityId = getEntityId();
|
||||||
|
entityLookAndRelativeMovePacket.deltaX = (short) ((newX * 32 - position.getX() * 32) * 128);
|
||||||
|
entityLookAndRelativeMovePacket.deltaY = (short) ((newY * 32 - position.getY() * 32) * 128);
|
||||||
|
entityLookAndRelativeMovePacket.deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128);
|
||||||
|
entityLookAndRelativeMovePacket.yaw = yaw;
|
||||||
|
entityLookAndRelativeMovePacket.pitch = pitch;
|
||||||
|
entityLookAndRelativeMovePacket.onGround = isOnGround();
|
||||||
|
sendPacketToViewers(entityLookAndRelativeMovePacket);
|
||||||
|
|
||||||
|
if (lastYaw != yaw) {
|
||||||
|
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
|
||||||
|
entityHeadLookPacket.entityId = getEntityId();
|
||||||
|
entityHeadLookPacket.yaw = yaw;
|
||||||
|
sendPacketToViewers(entityHeadLookPacket);
|
||||||
|
}
|
||||||
|
|
||||||
refreshPosition(newX, newY, newZ);
|
refreshPosition(newX, newY, newZ);
|
||||||
|
refreshView(yaw, pitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void moveTowards(Position direction, float speed) {
|
||||||
|
float radians = (float) Math.atan2(direction.getZ() - position.getZ(), direction.getX() - position.getX());
|
||||||
|
float speedX = (float) (Math.cos(radians) * speed);
|
||||||
|
float speedZ = (float) (Math.sin(radians) * speed);
|
||||||
|
move(speedX, 0, speedZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -9,6 +9,9 @@ public class ItemEntity extends ObjectEntity {
|
|||||||
private ItemStack itemStack;
|
private ItemStack itemStack;
|
||||||
private boolean pickable = true;
|
private boolean pickable = true;
|
||||||
|
|
||||||
|
private long spawnTime;
|
||||||
|
private long pickupDelay;
|
||||||
|
|
||||||
public ItemEntity(ItemStack itemStack) {
|
public ItemEntity(ItemStack itemStack) {
|
||||||
super(34);
|
super(34);
|
||||||
this.itemStack = itemStack;
|
this.itemStack = itemStack;
|
||||||
@ -22,6 +25,7 @@ public class ItemEntity extends ObjectEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void spawn() {
|
public void spawn() {
|
||||||
|
this.spawnTime = System.currentTimeMillis();
|
||||||
// setVelocity(new Vector(0, 1, 0), 5000);
|
// setVelocity(new Vector(0, 1, 0), 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,10 +58,22 @@ public class ItemEntity extends ObjectEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPickable() {
|
public boolean isPickable() {
|
||||||
return pickable;
|
return pickable && (System.currentTimeMillis() - getSpawnTime() >= pickupDelay);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPickable(boolean pickable) {
|
public void setPickable(boolean pickable) {
|
||||||
this.pickable = pickable;
|
this.pickable = pickable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getPickupDelay() {
|
||||||
|
return pickupDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPickupDelay(long pickupDelay) {
|
||||||
|
this.pickupDelay = pickupDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getSpawnTime() {
|
||||||
|
return spawnTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ 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;
|
||||||
@ -152,7 +151,7 @@ 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());
|
||||||
|
|
||||||
@ -163,7 +162,9 @@ public class Player extends LivingEntity {
|
|||||||
//itemEntity.setNoGravity(true);
|
//itemEntity.setNoGravity(true);
|
||||||
itemEntity.setInstance(getInstance());
|
itemEntity.setInstance(getInstance());
|
||||||
//itemEntity.remove();
|
//itemEntity.remove();
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
getInventory().addItemStack(new ItemStack(1, (byte) 100));
|
||||||
|
|
||||||
TeamsPacket teamsPacket = new TeamsPacket();
|
TeamsPacket teamsPacket = new TeamsPacket();
|
||||||
teamsPacket.teamName = "TEAMNAME" + new Random().nextInt(100);
|
teamsPacket.teamName = "TEAMNAME" + new Random().nextInt(100);
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
package fr.themode.minestom.entity.demo;
|
package fr.themode.minestom.entity.demo;
|
||||||
|
|
||||||
|
import fr.themode.minestom.Main;
|
||||||
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.utils.Position;
|
||||||
|
import net.tofweb.starlite.*;
|
||||||
|
|
||||||
public class ChickenCreature extends EntityCreature {
|
public class ChickenCreature extends EntityCreature {
|
||||||
|
|
||||||
|
private Path path;
|
||||||
|
private int counter;
|
||||||
|
private Position target;
|
||||||
|
private long lastTeleport;
|
||||||
|
private long wait = 500;
|
||||||
|
|
||||||
public ChickenCreature() {
|
public ChickenCreature() {
|
||||||
super(EntityType.CHICKEN);
|
super(EntityType.CHICKEN);
|
||||||
}
|
}
|
||||||
@ -12,7 +22,7 @@ public class ChickenCreature extends EntityCreature {
|
|||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
super.update();
|
super.update();
|
||||||
float speed = 0.05f;
|
float speed = 0.125f;
|
||||||
|
|
||||||
/*if (hasPassenger()) {
|
/*if (hasPassenger()) {
|
||||||
Entity passenger = getPassengers().iterator().next();
|
Entity passenger = getPassengers().iterator().next();
|
||||||
@ -43,11 +53,65 @@ public class ChickenCreature extends EntityCreature {
|
|||||||
move(x * speed, 0, z * speed);
|
move(x * speed, 0, z * speed);
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
// move(0, 0, speed);
|
|
||||||
|
if (path == null) {
|
||||||
|
System.out.println("FIND PATH");
|
||||||
|
Player player = Main.getConnectionManager().getPlayer("Raulnil");
|
||||||
|
if (player != null) {
|
||||||
|
refreshPath(player);
|
||||||
|
this.target = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target == null) {
|
||||||
|
Cell cell = path.pollFirst();
|
||||||
|
if (cell != null) {
|
||||||
|
this.target = new Position(cell.getX(), cell.getY(), cell.getZ());
|
||||||
|
System.out.println("NEW TARGET");
|
||||||
|
} else {
|
||||||
|
path = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path != null && target != null) {
|
||||||
|
if (path.isEmpty()) {
|
||||||
|
System.out.println("FINISHED PATH");
|
||||||
|
path = null;
|
||||||
|
} else {
|
||||||
|
float distance = getPosition().getDistance(target);
|
||||||
|
//System.out.println("DISTANCE: "+distance);
|
||||||
|
if (distance <= 1) {
|
||||||
|
System.out.println("RESET TARGET");
|
||||||
|
target = null;
|
||||||
|
} else {
|
||||||
|
//System.out.println("WALK");
|
||||||
|
moveTowards(target, speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void spawn() {
|
public void spawn() {
|
||||||
// setVelocity(new Vector(0, 1, 0), 3000);
|
// setVelocity(new Vector(0, 1, 0), 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void refreshPath(Player target) {
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
Position position = getPosition();
|
||||||
|
Position targetPosition = target.getPosition();
|
||||||
|
CellSpace space = new CellSpace();
|
||||||
|
space.setGoalCell((int) targetPosition.getX(), (int) targetPosition.getY(), (int) targetPosition.getZ());
|
||||||
|
space.setStartCell((int) position.getX(), (int) position.getY(), (int) position.getZ());
|
||||||
|
|
||||||
|
CostBlockManager blockManager = new CostBlockManager(space);
|
||||||
|
blockManager.blockCell(space.makeNewCell(6, 6, 3));
|
||||||
|
blockManager.blockCell(space.makeNewCell(6, 5, 4));
|
||||||
|
|
||||||
|
Pathfinder pathfinder = new Pathfinder(blockManager);
|
||||||
|
|
||||||
|
this.path = pathfinder.findPath();
|
||||||
|
|
||||||
|
System.out.println("PATH FINDING: " + (System.currentTimeMillis() - time) + " ms");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
16
src/main/java/fr/themode/minestom/event/ItemDropEvent.java
Normal file
16
src/main/java/fr/themode/minestom/event/ItemDropEvent.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package fr.themode.minestom.event;
|
||||||
|
|
||||||
|
import fr.themode.minestom.item.ItemStack;
|
||||||
|
|
||||||
|
public class ItemDropEvent extends CancellableEvent {
|
||||||
|
|
||||||
|
private ItemStack itemStack;
|
||||||
|
|
||||||
|
public ItemDropEvent(ItemStack itemStack) {
|
||||||
|
this.itemStack = itemStack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getItemStack() {
|
||||||
|
return itemStack;
|
||||||
|
}
|
||||||
|
}
|
@ -9,10 +9,13 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
|||||||
private BlockPosition blockPosition;
|
private BlockPosition blockPosition;
|
||||||
private Player.Hand hand;
|
private Player.Hand hand;
|
||||||
|
|
||||||
|
private boolean consumeBlock;
|
||||||
|
|
||||||
public PlayerBlockPlaceEvent(short blockId, BlockPosition blockPosition, Player.Hand hand) {
|
public PlayerBlockPlaceEvent(short blockId, BlockPosition blockPosition, Player.Hand hand) {
|
||||||
this.blockId = blockId;
|
this.blockId = blockId;
|
||||||
this.blockPosition = blockPosition;
|
this.blockPosition = blockPosition;
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
|
this.consumeBlock = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getBlockId() {
|
public short getBlockId() {
|
||||||
@ -26,4 +29,12 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
|||||||
public Player.Hand getHand() {
|
public Player.Hand getHand() {
|
||||||
return hand;
|
return hand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void consumeBlock(boolean consumeBlock) {
|
||||||
|
this.consumeBlock = consumeBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doesConsumeBlock() {
|
||||||
|
return consumeBlock;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,16 @@ import fr.themode.minestom.entity.Player;
|
|||||||
|
|
||||||
public class PlayerInteractEvent extends Event {
|
public class PlayerInteractEvent extends Event {
|
||||||
|
|
||||||
private Entity target;
|
private Entity entityTarget;
|
||||||
private Player.Hand hand;
|
private Player.Hand hand;
|
||||||
|
|
||||||
public PlayerInteractEvent(Entity target, Player.Hand hand) {
|
public PlayerInteractEvent(Entity entityTarget, Player.Hand hand) {
|
||||||
this.target = target;
|
this.entityTarget = entityTarget;
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity getTarget() {
|
public Entity getTarget() {
|
||||||
return target;
|
return entityTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player.Hand getHand() {
|
public Player.Hand getHand() {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package fr.themode.minestom.item;
|
package fr.themode.minestom.item;
|
||||||
|
|
||||||
public class ItemStack {
|
import fr.themode.minestom.data.Data;
|
||||||
|
import fr.themode.minestom.data.DataContainer;
|
||||||
|
|
||||||
|
public class ItemStack implements DataContainer {
|
||||||
|
|
||||||
public static final ItemStack AIR_ITEM = new ItemStack(0, (byte) 1);
|
public static final ItemStack AIR_ITEM = new ItemStack(0, (byte) 1);
|
||||||
|
|
||||||
@ -10,6 +13,8 @@ public class ItemStack {
|
|||||||
private String displayName;
|
private String displayName;
|
||||||
private boolean unbreakable;
|
private boolean unbreakable;
|
||||||
|
|
||||||
|
private Data data;
|
||||||
|
|
||||||
public ItemStack(Material material, byte amount) {
|
public ItemStack(Material material, byte amount) {
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
@ -59,6 +64,17 @@ public class ItemStack {
|
|||||||
ItemStack itemStack = new ItemStack(material, amount);
|
ItemStack itemStack = new ItemStack(material, amount);
|
||||||
itemStack.setDisplayName(displayName);
|
itemStack.setDisplayName(displayName);
|
||||||
itemStack.setUnbreakable(unbreakable);
|
itemStack.setUnbreakable(unbreakable);
|
||||||
|
itemStack.setData(getData());
|
||||||
return itemStack;
|
return itemStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Data getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setData(Data data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,10 @@ import java.util.Map;
|
|||||||
|
|
||||||
public enum Material {
|
public enum Material {
|
||||||
|
|
||||||
AIR(0),
|
AIR(0, 0),
|
||||||
STONE(1),
|
STONE(1, 1),
|
||||||
BOW(525),
|
BOW(525, 0),
|
||||||
ARROW(526);
|
ARROW(526, 0);
|
||||||
|
|
||||||
private static Map<Integer, Material> idToMaterial = new HashMap<>();
|
private static Map<Integer, Material> idToMaterial = new HashMap<>();
|
||||||
|
|
||||||
@ -19,9 +19,11 @@ public enum Material {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
private int blockId;
|
||||||
|
|
||||||
Material(int id) {
|
Material(int id, int blockId) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.blockId = blockId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Material fromId(int id) {
|
public static Material fromId(int id) {
|
||||||
@ -29,7 +31,7 @@ public enum Material {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBlock() {
|
public boolean isBlock() {
|
||||||
return false; // TODO
|
return blockId != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFood() {
|
public boolean isFood() {
|
||||||
|
@ -4,6 +4,8 @@ import fr.themode.minestom.entity.Player;
|
|||||||
import fr.themode.minestom.event.PlayerBlockPlaceEvent;
|
import fr.themode.minestom.event.PlayerBlockPlaceEvent;
|
||||||
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.inventory.PlayerInventory;
|
||||||
|
import fr.themode.minestom.item.ItemStack;
|
||||||
import fr.themode.minestom.net.packet.client.play.ClientPlayerBlockPlacementPacket;
|
import fr.themode.minestom.net.packet.client.play.ClientPlayerBlockPlacementPacket;
|
||||||
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
||||||
import fr.themode.minestom.utils.BlockPosition;
|
import fr.themode.minestom.utils.BlockPosition;
|
||||||
@ -11,6 +13,8 @@ import fr.themode.minestom.utils.BlockPosition;
|
|||||||
public class BlockPlacementListener {
|
public class BlockPlacementListener {
|
||||||
|
|
||||||
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
|
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
|
||||||
|
PlayerInventory playerInventory = player.getInventory();
|
||||||
|
Player.Hand hand = packet.hand;
|
||||||
ClientPlayerDiggingPacket.BlockFace blockFace = packet.blockFace;
|
ClientPlayerDiggingPacket.BlockFace blockFace = packet.blockFace;
|
||||||
BlockPosition blockPosition = packet.blockPosition;
|
BlockPosition blockPosition = packet.blockPosition;
|
||||||
|
|
||||||
@ -18,6 +22,10 @@ public class BlockPlacementListener {
|
|||||||
if (instance == null)
|
if (instance == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
|
||||||
|
if (!usedItem.getMaterial().isBlock())
|
||||||
|
return;
|
||||||
|
|
||||||
int offsetX = blockFace == ClientPlayerDiggingPacket.BlockFace.WEST ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.EAST ? 1 : 0;
|
int offsetX = blockFace == ClientPlayerDiggingPacket.BlockFace.WEST ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.EAST ? 1 : 0;
|
||||||
int offsetY = blockFace == ClientPlayerDiggingPacket.BlockFace.BOTTOM ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.TOP ? 1 : 0;
|
int offsetY = blockFace == ClientPlayerDiggingPacket.BlockFace.BOTTOM ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.TOP ? 1 : 0;
|
||||||
int offsetZ = blockFace == ClientPlayerDiggingPacket.BlockFace.NORTH ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.SOUTH ? 1 : 0;
|
int offsetZ = blockFace == ClientPlayerDiggingPacket.BlockFace.NORTH ? -1 : blockFace == ClientPlayerDiggingPacket.BlockFace.SOUTH ? 1 : 0;
|
||||||
@ -25,10 +33,18 @@ public class BlockPlacementListener {
|
|||||||
blockPosition.add(offsetX, offsetY, offsetZ);
|
blockPosition.add(offsetX, offsetY, offsetZ);
|
||||||
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
|
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent((short) 10, blockPosition, packet.hand);
|
||||||
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
|
player.callEvent(PlayerBlockPlaceEvent.class, playerBlockPlaceEvent);
|
||||||
// TODO if player does not have block in hand, then call InteractEvent with blockPosition
|
|
||||||
if (!playerBlockPlaceEvent.isCancelled()) {
|
if (!playerBlockPlaceEvent.isCancelled()) {
|
||||||
instance.setBlock(blockPosition, "custom_block");
|
instance.setBlock(blockPosition, "custom_block"); // TODO set useItem's block instead
|
||||||
// TODO consume block in hand for survival players
|
if (playerBlockPlaceEvent.doesConsumeBlock()) {
|
||||||
|
usedItem.setAmount((byte) (usedItem.getAmount() - 1));
|
||||||
|
if (usedItem.getAmount() <= 0)
|
||||||
|
usedItem = ItemStack.AIR_ITEM;
|
||||||
|
if (hand == Player.Hand.OFF) {
|
||||||
|
playerInventory.setItemInOffHand(usedItem);
|
||||||
|
} else { // Main
|
||||||
|
playerInventory.setItemInMainHand(usedItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Chunk chunk = instance.getChunkAt(blockPosition);
|
Chunk chunk = instance.getChunkAt(blockPosition);
|
||||||
instance.sendChunkUpdate(player, chunk);
|
instance.sendChunkUpdate(player, chunk);
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
package fr.themode.minestom.listener;
|
package fr.themode.minestom.listener;
|
||||||
|
|
||||||
import fr.themode.minestom.entity.GameMode;
|
import fr.themode.minestom.entity.GameMode;
|
||||||
|
import fr.themode.minestom.entity.ItemEntity;
|
||||||
import fr.themode.minestom.entity.Player;
|
import fr.themode.minestom.entity.Player;
|
||||||
|
import fr.themode.minestom.event.ItemDropEvent;
|
||||||
import fr.themode.minestom.event.PlayerStartDiggingEvent;
|
import fr.themode.minestom.event.PlayerStartDiggingEvent;
|
||||||
import fr.themode.minestom.instance.CustomBlock;
|
import fr.themode.minestom.instance.CustomBlock;
|
||||||
import fr.themode.minestom.instance.Instance;
|
import fr.themode.minestom.instance.Instance;
|
||||||
|
import fr.themode.minestom.inventory.PlayerInventory;
|
||||||
|
import fr.themode.minestom.item.ItemStack;
|
||||||
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
import fr.themode.minestom.net.packet.client.play.ClientPlayerDiggingPacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.EntityEffectPacket;
|
import fr.themode.minestom.net.packet.server.play.EntityEffectPacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.RemoveEntityEffectPacket;
|
import fr.themode.minestom.net.packet.server.play.RemoveEntityEffectPacket;
|
||||||
import fr.themode.minestom.utils.BlockPosition;
|
import fr.themode.minestom.utils.BlockPosition;
|
||||||
|
import fr.themode.minestom.utils.Vector;
|
||||||
|
|
||||||
public class PlayerDiggingListener {
|
public class PlayerDiggingListener {
|
||||||
|
|
||||||
@ -31,11 +36,13 @@ public class PlayerDiggingListener {
|
|||||||
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
|
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
|
||||||
if (!playerStartDiggingEvent.isCancelled()) {
|
if (!playerStartDiggingEvent.isCancelled()) {
|
||||||
player.refreshTargetBlock(customBlock, blockPosition);
|
player.refreshTargetBlock(customBlock, blockPosition);
|
||||||
|
// TODO ACKNOWLEDGE
|
||||||
}
|
}
|
||||||
addEffect(player);
|
addEffect(player);
|
||||||
} else {
|
} else {
|
||||||
player.resetTargetBlock();
|
player.resetTargetBlock();
|
||||||
removeEffect(player);
|
removeEffect(player);
|
||||||
|
// TODO ACKNOWLEDGE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,14 +63,48 @@ public class PlayerDiggingListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case DROP_ITEM_STACK:
|
||||||
|
ItemStack droppedItemStack = player.getInventory().getItemInMainHand().clone();
|
||||||
|
dropItem(player, droppedItemStack, ItemStack.AIR_ITEM);
|
||||||
|
break;
|
||||||
|
case DROP_ITEM:
|
||||||
|
ItemStack droppedItemStack2 = player.getInventory().getItemInMainHand().clone();
|
||||||
|
droppedItemStack2.setAmount((byte) 1);
|
||||||
|
|
||||||
|
ItemStack handItem = player.getInventory().getItemInMainHand();
|
||||||
|
handItem.setAmount((byte) (handItem.getAmount() - 1));
|
||||||
|
handItem = handItem.getAmount() <= 0 ? ItemStack.AIR_ITEM : handItem;
|
||||||
|
dropItem(player, droppedItemStack2, handItem);
|
||||||
|
break;
|
||||||
case UPDATE_ITEM_STATE:
|
case UPDATE_ITEM_STATE:
|
||||||
// TODO check if is updatable item
|
// TODO check if is updatable item
|
||||||
//player.refreshActiveHand(false, false, false);
|
//player.refreshActiveHand(false, false, false);
|
||||||
//player.sendPacketToViewers(player.getMetadataPacket());
|
//player.sendPacketToViewers(player.getMetadataPacket());
|
||||||
break;
|
break;
|
||||||
|
case SWAP_ITEM_HAND:
|
||||||
|
PlayerInventory playerInventory = player.getInventory();
|
||||||
|
ItemStack mainHand = playerInventory.getItemInMainHand().clone();
|
||||||
|
ItemStack offHand = playerInventory.getItemInOffHand().clone();
|
||||||
|
playerInventory.setItemInMainHand(offHand);
|
||||||
|
playerInventory.setItemInOffHand(mainHand);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void dropItem(Player player, ItemStack droppedItem, ItemStack handItem) {
|
||||||
|
ItemDropEvent itemDropEvent = new ItemDropEvent(droppedItem);
|
||||||
|
player.callCancellableEvent(ItemDropEvent.class, itemDropEvent, () -> {
|
||||||
|
player.getInventory().setItemInMainHand(handItem);
|
||||||
|
|
||||||
|
ItemEntity itemEntity = new ItemEntity(droppedItem);
|
||||||
|
itemEntity.setPickupDelay(1000);
|
||||||
|
itemEntity.refreshPosition(player.getPosition().clone().add(0, 1.5f, 0));
|
||||||
|
itemEntity.setInstance(player.getInstance());
|
||||||
|
Vector velocity = player.getPosition().clone().getDirection().multiply(5);
|
||||||
|
itemEntity.setVelocity(velocity, 350);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static void addEffect(Player player) {
|
private static void addEffect(Player player) {
|
||||||
EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
|
EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
|
||||||
entityEffectPacket.entityId = player.getEntityId();
|
entityEffectPacket.entityId = player.getEntityId();
|
||||||
|
@ -10,7 +10,6 @@ import fr.themode.minestom.net.packet.client.ClientPreplayPacket;
|
|||||||
import fr.themode.minestom.net.packet.server.login.JoinGamePacket;
|
import fr.themode.minestom.net.packet.server.login.JoinGamePacket;
|
||||||
import fr.themode.minestom.net.packet.server.login.LoginSuccessPacket;
|
import fr.themode.minestom.net.packet.server.login.LoginSuccessPacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.PlayerInfoPacket;
|
import fr.themode.minestom.net.packet.server.play.PlayerInfoPacket;
|
||||||
import fr.themode.minestom.net.packet.server.play.PlayerPositionAndLookPacket;
|
|
||||||
import fr.themode.minestom.net.packet.server.play.SpawnPositionPacket;
|
import fr.themode.minestom.net.packet.server.play.SpawnPositionPacket;
|
||||||
import fr.themode.minestom.net.player.PlayerConnection;
|
import fr.themode.minestom.net.player.PlayerConnection;
|
||||||
import fr.themode.minestom.utils.Utils;
|
import fr.themode.minestom.utils.Utils;
|
||||||
@ -73,16 +72,10 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
|||||||
|
|
||||||
SpawnPositionPacket spawnPositionPacket = new SpawnPositionPacket();
|
SpawnPositionPacket spawnPositionPacket = new SpawnPositionPacket();
|
||||||
spawnPositionPacket.x = 0;
|
spawnPositionPacket.x = 0;
|
||||||
spawnPositionPacket.y = 18;
|
spawnPositionPacket.y = 0;
|
||||||
spawnPositionPacket.z = 0;
|
spawnPositionPacket.z = 0;
|
||||||
connection.sendPacket(spawnPositionPacket);
|
connection.sendPacket(spawnPositionPacket);
|
||||||
|
|
||||||
PlayerPositionAndLookPacket playerPositionAndLookPacket = new PlayerPositionAndLookPacket();
|
|
||||||
playerPositionAndLookPacket.position = player.getPosition();
|
|
||||||
playerPositionAndLookPacket.flags = 0;
|
|
||||||
playerPositionAndLookPacket.teleportId = 42;
|
|
||||||
connection.sendPacket(playerPositionAndLookPacket);
|
|
||||||
|
|
||||||
PlayerInfoPacket playerInfoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.ADD_PLAYER);
|
PlayerInfoPacket playerInfoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.ADD_PLAYER);
|
||||||
PlayerInfoPacket.AddPlayer addPlayer = new PlayerInfoPacket.AddPlayer(player.getUuid(), username, GameMode.CREATIVE, 10);
|
PlayerInfoPacket.AddPlayer addPlayer = new PlayerInfoPacket.AddPlayer(player.getUuid(), username, GameMode.CREATIVE, 10);
|
||||||
PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property); //new PlayerInfoPacket.AddPlayer.Property("textures", properties.get(username));
|
PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property); //new PlayerInfoPacket.AddPlayer.Property("textures", properties.get(username));
|
||||||
|
@ -0,0 +1,169 @@
|
|||||||
|
package fr.themode.minestom.net.packet.server.play;
|
||||||
|
|
||||||
|
import fr.adamaq01.ozao.net.Buffer;
|
||||||
|
import fr.themode.minestom.chat.Chat;
|
||||||
|
import fr.themode.minestom.item.ItemStack;
|
||||||
|
import fr.themode.minestom.net.packet.server.ServerPacket;
|
||||||
|
import fr.themode.minestom.utils.Utils;
|
||||||
|
|
||||||
|
public class AdvancementsPacket implements ServerPacket {
|
||||||
|
|
||||||
|
public boolean resetAdvancements;
|
||||||
|
public AdvancementMapping[] advancementMappings;
|
||||||
|
public String[] identifiersToRemove;
|
||||||
|
public ProgressMapping[] progressMappings;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(Buffer buffer) {
|
||||||
|
buffer.putBoolean(resetAdvancements);
|
||||||
|
|
||||||
|
Utils.writeVarInt(buffer, advancementMappings.length);
|
||||||
|
for (AdvancementMapping advancementMapping : advancementMappings) {
|
||||||
|
advancementMapping.write(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils.writeVarInt(buffer, identifiersToRemove.length);
|
||||||
|
for (String identifierToRemove : identifiersToRemove) {
|
||||||
|
Utils.writeString(buffer, identifierToRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils.writeVarInt(buffer, progressMappings.length);
|
||||||
|
for (ProgressMapping progressMapping : progressMappings) {
|
||||||
|
progressMapping.write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getId() {
|
||||||
|
return 0x57;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum FrameType {
|
||||||
|
TASK, CHALLENGE, GOAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AdvancementMapping {
|
||||||
|
|
||||||
|
public String key;
|
||||||
|
public Advancement value;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeString(buffer, key);
|
||||||
|
value.write(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Advancement {
|
||||||
|
|
||||||
|
public boolean hasParent;
|
||||||
|
public String identifier;
|
||||||
|
public boolean hasDisplay;
|
||||||
|
public DisplayData displayData;
|
||||||
|
public String[] criterions;
|
||||||
|
public Requirement[] requirements;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
buffer.putBoolean(hasParent);
|
||||||
|
if (identifier != null) {
|
||||||
|
Utils.writeString(buffer, identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.putBoolean(hasDisplay);
|
||||||
|
if (hasDisplay) {
|
||||||
|
displayData.write(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils.writeVarInt(buffer, criterions.length);
|
||||||
|
for (String criterion : criterions) {
|
||||||
|
Utils.writeString(buffer, criterion);
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils.writeVarInt(buffer, requirements.length);
|
||||||
|
for (Requirement requirement : requirements) {
|
||||||
|
requirement.write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DisplayData {
|
||||||
|
public String title;
|
||||||
|
public String description;
|
||||||
|
public ItemStack icon;
|
||||||
|
public FrameType frameType;
|
||||||
|
public int flags;
|
||||||
|
public String backgroundTexture;
|
||||||
|
public float x;
|
||||||
|
public float y;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeString(buffer, Chat.rawText(title));
|
||||||
|
Utils.writeString(buffer, Chat.rawText(description));
|
||||||
|
Utils.writeItemStack(buffer, icon);
|
||||||
|
Utils.writeVarInt(buffer, frameType.ordinal());
|
||||||
|
buffer.putInt(flags);
|
||||||
|
if ((flags & 0x1) != 0) {
|
||||||
|
Utils.writeString(buffer, backgroundTexture);
|
||||||
|
}
|
||||||
|
buffer.putFloat(x);
|
||||||
|
buffer.putFloat(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Requirement {
|
||||||
|
|
||||||
|
public String[] requirements;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeVarInt(buffer, requirements.length);
|
||||||
|
for (String requirement : requirements) {
|
||||||
|
Utils.writeString(buffer, requirement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ProgressMapping {
|
||||||
|
public String key;
|
||||||
|
public AdvancementProgress value;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeString(buffer, key);
|
||||||
|
value.write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AdvancementProgress {
|
||||||
|
public Criteria[] criteria;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeVarInt(buffer, criteria.length);
|
||||||
|
for (Criteria criterion : criteria) {
|
||||||
|
criterion.write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Criteria {
|
||||||
|
public String criterionIdentifier;
|
||||||
|
public CriterionProgress criterionProgress;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
Utils.writeString(buffer, criterionIdentifier);
|
||||||
|
criterionProgress.write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CriterionProgress {
|
||||||
|
public boolean achieved;
|
||||||
|
public long dateOfAchieving;
|
||||||
|
|
||||||
|
private void write(Buffer buffer) {
|
||||||
|
buffer.putBoolean(achieved);
|
||||||
|
if (dateOfAchieving != 0)
|
||||||
|
buffer.putLong(dateOfAchieving);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user