Minestom/src/main/java/fr/themode/minestom/entity/Player.java

283 lines
9.6 KiB
Java
Raw Normal View History

2019-08-03 15:25:24 +02:00
package fr.themode.minestom.entity;
import fr.themode.minestom.Main;
2019-08-19 17:04:19 +02:00
import fr.themode.minestom.bossbar.BossBar;
import fr.themode.minestom.chat.Chat;
2019-08-18 23:52:11 +02:00
import fr.themode.minestom.instance.CustomBlock;
2019-08-13 17:52:09 +02:00
import fr.themode.minestom.inventory.Inventory;
2019-08-12 08:30:59 +02:00
import fr.themode.minestom.inventory.PlayerInventory;
import fr.themode.minestom.item.ItemStack;
import fr.themode.minestom.net.packet.server.play.*;
2019-08-03 15:25:24 +02:00
import fr.themode.minestom.net.player.PlayerConnection;
2019-08-19 17:04:19 +02:00
import fr.themode.minestom.utils.GroupedCollections;
2019-08-18 20:38:09 +02:00
import fr.themode.minestom.utils.Position;
2019-08-03 15:25:24 +02:00
2019-08-19 17:04:19 +02:00
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
2019-08-19 17:04:19 +02:00
import java.util.concurrent.CopyOnWriteArraySet;
2019-08-10 08:44:35 +02:00
public class Player extends LivingEntity {
private long lastKeepAlive;
2019-08-10 04:16:01 +02:00
private String username;
2019-08-03 15:25:24 +02:00
private PlayerConnection playerConnection;
2019-08-12 08:30:59 +02:00
private GameMode gameMode;
private PlayerInventory inventory;
private short heldSlot;
2019-08-13 17:52:09 +02:00
private Inventory openInventory;
2019-08-12 08:30:59 +02:00
2019-08-18 23:52:11 +02:00
private CustomBlock targetCustomBlock;
2019-08-18 20:38:09 +02:00
private Position targetBlockPosition;
private long targetBlockTime;
2019-08-19 17:04:19 +02:00
private Set<BossBar> bossBars = new CopyOnWriteArraySet<>();
2019-08-10 08:44:35 +02:00
// TODO set proper UUID
public Player(UUID uuid, String username, PlayerConnection playerConnection) {
2019-08-19 17:04:19 +02:00
super(93); // TODO correct ?
this.uuid = uuid;
this.username = username;
this.playerConnection = playerConnection;
2019-08-12 08:30:59 +02:00
this.inventory = new PlayerInventory(this);
}
2019-08-10 08:44:35 +02:00
@Override
public void update() {
2019-08-18 20:38:09 +02:00
// Target block stage
2019-08-18 23:52:11 +02:00
if (instance != null && targetCustomBlock != null) {
int timeBreak = targetCustomBlock.getBreakDelay(this);
2019-08-18 20:38:09 +02:00
int animationCount = 10;
long since = System.currentTimeMillis() - targetBlockTime;
byte stage = (byte) (since / (timeBreak / animationCount));
2019-08-18 23:52:11 +02:00
sendBlockBreakAnimation(targetBlockPosition, stage);// TODO send to all near players
2019-08-18 20:38:09 +02:00
if (stage > 9) {
instance.setBlock(targetBlockPosition.getX(), targetBlockPosition.getY(), targetBlockPosition.getZ(), (short) 0);
2019-08-19 17:04:19 +02:00
testParticle(targetBlockPosition.getX() + 0.5f, targetBlockPosition.getY(), targetBlockPosition.getZ() + 0.5f, targetCustomBlock.getType());
2019-08-18 23:52:11 +02:00
resetTargetBlock();
2019-08-18 20:38:09 +02:00
}
}
2019-08-19 17:04:19 +02:00
// Item pickup
if (instance != null) {
GroupedCollections<ObjectEntity> objectEntities = instance.getObjectEntities();
for (ObjectEntity objectEntity : objectEntities) {
if (objectEntity instanceof ItemEntity) {
float distance = getDistance(objectEntity);
if (distance <= 1) { // FIXME set correct value
getInventory().addItemStack(((ItemEntity) objectEntity).getItemStack());
objectEntity.remove();
}
}
}
}
2019-08-18 20:38:09 +02:00
// Multiplayer sync
EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket();
entityTeleportPacket.entityId = getEntityId();
entityTeleportPacket.x = x;
entityTeleportPacket.y = y;
entityTeleportPacket.z = z;
entityTeleportPacket.yaw = yaw;
entityTeleportPacket.pitch = pitch;
entityTeleportPacket.onGround = true;
for (Player onlinePlayer : Main.getConnectionManager().getOnlinePlayers()) {
if (!onlinePlayer.equals(this))
onlinePlayer.getPlayerConnection().sendPacket(entityTeleportPacket);
}
2019-08-11 08:56:30 +02:00
playerConnection.sendPacket(new UpdateViewPositionPacket(Math.floorDiv((int) x, 16), Math.floorDiv((int) z, 16)));
}
2019-08-18 23:52:11 +02:00
public void sendBlockBreakAnimation(Position blockPosition, byte destroyStage) {
BlockBreakAnimationPacket breakAnimationPacket = new BlockBreakAnimationPacket();
breakAnimationPacket.entityId = getEntityId() + 1;
breakAnimationPacket.blockPosition = blockPosition;
breakAnimationPacket.destroyStage = destroyStage;
playerConnection.sendPacket(breakAnimationPacket);
}
2019-08-19 17:04:19 +02:00
private void testParticle(float x, float y, float z, int blockId) {
2019-08-18 23:52:11 +02:00
ParticlePacket particlePacket = new ParticlePacket();
particlePacket.particleId = 3; // Block particle
particlePacket.longDistance = false;
particlePacket.x = x;
particlePacket.y = y;
particlePacket.z = z;
particlePacket.offsetX = 0.55f;
particlePacket.offsetY = 0.75f;
particlePacket.offsetZ = 0.55f;
particlePacket.particleData = 0.25f;
particlePacket.particleCount = 100;
2019-08-19 17:04:19 +02:00
particlePacket.blockId = blockId;
2019-08-18 23:52:11 +02:00
playerConnection.sendPacket(particlePacket);
}
2019-08-18 20:38:09 +02:00
public void sendMessage(String message) {
2019-08-19 17:04:19 +02:00
ChatMessagePacket chatMessagePacket = new ChatMessagePacket(Chat.rawText(message), ChatMessagePacket.Position.CHAT);
2019-08-18 20:38:09 +02:00
playerConnection.sendPacket(chatMessagePacket);
}
2019-08-11 13:57:23 +02:00
public void teleport(double x, double y, double z) {
PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.x = x;
positionAndLookPacket.y = y;
positionAndLookPacket.z = z;
positionAndLookPacket.yaw = getYaw();
positionAndLookPacket.pitch = getPitch();
positionAndLookPacket.flags = 0x00;
positionAndLookPacket.teleportId = 67;
getPlayerConnection().sendPacket(positionAndLookPacket);
2019-08-11 09:33:27 +02:00
}
public String getUsername() {
return username;
2019-08-10 08:44:35 +02:00
}
2019-08-03 15:25:24 +02:00
public PlayerConnection getPlayerConnection() {
return playerConnection;
}
2019-08-10 04:16:01 +02:00
2019-08-12 08:30:59 +02:00
public PlayerInventory getInventory() {
return inventory;
}
public GameMode getGameMode() {
return gameMode;
}
public void setGameMode(GameMode gameMode) {
ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket();
changeGameStatePacket.reason = ChangeGameStatePacket.Reason.CHANGE_GAMEMODE;
changeGameStatePacket.value = gameMode.getId();
playerConnection.sendPacket(changeGameStatePacket);
refreshGameMode(gameMode);
}
public void setHeldItemSlot(short slot) {
if (slot < 0 || slot > 8)
throw new IllegalArgumentException("Slot has to be between 0 and 8");
HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket();
heldItemChangePacket.slot = slot;
playerConnection.sendPacket(heldItemChangePacket);
refreshHeldSlot(slot);
}
public short getHeldSlot() {
return heldSlot;
}
public ItemStack getHeldItemStack() {
return inventory.getItemStack(heldSlot);
}
2019-08-13 17:52:09 +02:00
public Inventory getOpenInventory() {
return openInventory;
}
2019-08-18 23:52:11 +02:00
public CustomBlock getCustomBlockTarget() {
return targetCustomBlock;
}
2019-08-19 17:04:19 +02:00
public Set<BossBar> getBossBars() {
return Collections.unmodifiableSet(bossBars);
}
2019-08-13 17:52:09 +02:00
public void openInventory(Inventory inventory) {
if (inventory == null)
throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current");
if (getOpenInventory() != null) {
getOpenInventory().removeViewer(this);
}
OpenWindowPacket openWindowPacket = new OpenWindowPacket();
openWindowPacket.windowId = inventory.getUniqueId();
openWindowPacket.windowType = inventory.getInventoryType().getWindowType();
openWindowPacket.title = inventory.getTitle();
playerConnection.sendPacket(openWindowPacket);
inventory.addViewer(this);
refreshOpenInventory(inventory);
}
public void closeInventory() {
Inventory openInventory = getOpenInventory();
CloseWindowPacket closeWindowPacket = new CloseWindowPacket();
if (openInventory == null) {
closeWindowPacket.windowId = 0;
} else {
closeWindowPacket.windowId = openInventory.getUniqueId();
openInventory.removeViewer(this);
refreshOpenInventory(null);
}
playerConnection.sendPacket(closeWindowPacket);
2019-08-14 06:50:03 +02:00
inventory.update();
2019-08-13 17:52:09 +02:00
}
2019-08-12 08:30:59 +02:00
public void refreshGameMode(GameMode gameMode) {
this.gameMode = gameMode;
}
2019-08-10 04:16:01 +02:00
public void refreshView(float yaw, float pitch) {
this.yaw = yaw;
this.pitch = pitch;
}
public void refreshOnGround(boolean onGround) {
this.onGround = onGround;
}
2019-08-10 08:44:35 +02:00
public void refreshSneaking(boolean sneaking) {
2019-08-19 17:04:19 +02:00
sneaking = sneaking;
2019-08-10 08:44:35 +02:00
}
public void refreshSprinting(boolean sprinting) {
2019-08-19 17:04:19 +02:00
sprinting = sprinting;
2019-08-10 08:44:35 +02:00
}
public void refreshKeepAlive(long lastKeepAlive) {
this.lastKeepAlive = lastKeepAlive;
}
2019-08-12 08:30:59 +02:00
public void refreshHeldSlot(short slot) {
this.heldSlot = slot;
}
2019-08-13 17:52:09 +02:00
public void refreshOpenInventory(Inventory openInventory) {
this.openInventory = openInventory;
}
2019-08-18 23:52:11 +02:00
public void refreshTargetBlock(CustomBlock targetCustomBlock, Position targetBlockPosition) {
this.targetCustomBlock = targetCustomBlock;
2019-08-18 20:38:09 +02:00
this.targetBlockPosition = targetBlockPosition;
this.targetBlockTime = targetBlockPosition == null ? 0 : System.currentTimeMillis();
}
2019-08-18 23:52:11 +02:00
public void resetTargetBlock() {
this.targetCustomBlock = null;
this.targetBlockPosition = null;
this.targetBlockTime = 0;
}
2019-08-19 17:04:19 +02:00
public void refreshAddBossbar(BossBar bossBar) {
this.bossBars.add(bossBar);
}
public void refreshRemoveBossbar(BossBar bossBar) {
this.bossBars.remove(bossBar);
}
public long getLastKeepAlive() {
return lastKeepAlive;
}
public enum Hand {
MAIN,
OFF
}
2019-08-03 15:25:24 +02:00
}