This commit is contained in:
Felix Cravic 2020-05-31 15:52:56 +02:00
parent 30ef05183e
commit eed946e948
8 changed files with 72 additions and 20 deletions

View File

@ -1056,7 +1056,7 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(getPropertiesPacket());
syncEquipments();
askSynchronization();
sendSynchronization();
}
protected void refreshHealth() {

View File

@ -1,5 +1,8 @@
package net.minestom.server.entity;
/**
* Contains all the data required to store a skin
*/
public class PlayerSkin {
private String textures;
@ -10,19 +13,21 @@ public class PlayerSkin {
this.signature = signature;
}
/**
* Get the skin textures value
*
* @return the textures value
*/
public String getTextures() {
return textures;
}
public void setTextures(String textures) {
this.textures = textures;
}
/**
* Get the skin signature
*
* @return the skin signature
*/
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
}

View File

@ -12,9 +12,9 @@ public class PlayerMoveEvent extends CancellableEvent {
private Player player;
private Position newPosition;
public PlayerMoveEvent(Player player, float x, float y, float z, float yaw, float pitch) {
public PlayerMoveEvent(Player player, Position newPosition) {
this.player = player;
this.newPosition = new Position(x, y, z, yaw, pitch);
this.newPosition = newPosition;
}
/**

View File

@ -13,6 +13,7 @@ import net.minestom.server.network.PacketWriterUtils;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
import net.minestom.server.network.packet.server.play.WindowPropertyPacket;
import net.minestom.server.utils.ArrayUtils;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.server.utils.item.ItemStackUtils;
@ -56,9 +57,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.itemStacks = new ItemStack[size];
for (int i = 0; i < size; i++) {
itemStacks[i] = ItemStack.getAirItem();
}
ArrayUtils.fill(itemStacks, ItemStack::getAirItem);
}
private static byte generateId() {

View File

@ -13,6 +13,7 @@ import net.minestom.server.network.PacketWriterUtils;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
import net.minestom.server.utils.ArrayUtils;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.item.ItemStackUtils;
@ -36,9 +37,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
public PlayerInventory(Player player) {
this.player = player;
for (int i = 0; i < items.length; i++) {
items[i] = ItemStack.getAirItem();
}
ArrayUtils.fill(items, ItemStack::getAirItem);
}
@Override
@ -201,6 +200,12 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
this.cursorItem = ItemStackUtils.notNull(cursorItem);
}
/**
* Insert an item safely (synchronized) in the appropriate slot
*
* @param slot an internal slot
* @param itemStack the item to insert at the slot
*/
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
itemStack = ItemStackUtils.notNull(itemStack);
@ -245,17 +250,38 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
//refreshSlot(slot); seems to break things concerning +64 stacks
}
/**
* Set an item from a packet slot
*
* @param slot a packet slot
* @param offset offset (generally 9 to ignore armor and craft slots)
* @param itemStack the item stack to set
*/
protected void setItemStack(int slot, int offset, ItemStack itemStack) {
slot = convertSlot(slot, offset);
safeItemInsert(slot, itemStack);
}
/**
* Get the item from a packet slot
*
* @param slot a packet slot
* @param offset offset (generally 9 to ignore armor and craft slots)
* @return the item in the specified slot
*/
protected ItemStack getItemStack(int slot, int offset) {
slot = convertSlot(slot, offset);
return this.items[slot];
}
private void sendSlotRefresh(short slot, ItemStack itemStack) {
/**
* Refresh an inventory slot
*
* @param slot the packet slot
* see {@link net.minestom.server.utils.inventory.PlayerInventoryUtils#convertToPacketSlot(int)}
* @param itemStack the item stack in the slot
*/
protected void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = (byte) (MathUtils.isBetween(slot, 35, INVENTORY_SIZE) ? 0 : -2);
setSlotPacket.slot = slot;
@ -263,6 +289,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
player.getPlayerConnection().sendPacket(setSlotPacket);
}
/**
* Get a {@link WindowItemsPacket} with all the items in the inventory
*
* @return a {@link WindowItemsPacket} with inventory items
*/
private WindowItemsPacket createWindowItemsPacket() {
ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE];

View File

@ -67,7 +67,8 @@ public class PlayerPositionListener {
return;
}
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, x, y, z, yaw, pitch);
Position newPosition = new Position(x, y, z, yaw, pitch);
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition);
player.callEvent(PlayerMoveEvent.class, playerMoveEvent);
if (!playerMoveEvent.isCancelled()) {
consumer.accept(playerMoveEvent.getNewPosition());

View File

@ -1,6 +1,7 @@
package net.minestom.server.utils;
import java.util.ArrayList;
import java.util.function.Supplier;
public class ArrayUtils {
@ -61,4 +62,17 @@ public class ArrayUtils {
return array;
}
/**
* Fill an array by a supplier
*
* @param array the array to fill
* @param supplier the supplier to fill the array
* @param <T> the array type
*/
public static <T> void fill(T[] array, Supplier<T> supplier) {
for (int i = 0; i < array.length; i++) {
array[i] = supplier.get();
}
}
}

View File

@ -36,7 +36,9 @@ public class Position {
}
public float getDistance(Position position) {
return (float) Math.sqrt(MathUtils.square(position.getX() - getX()) + MathUtils.square(position.getY() - getY()) + MathUtils.square(position.getZ() - getZ()));
return (float) Math.sqrt(MathUtils.square(position.getX() - getX()) +
MathUtils.square(position.getY() - getY()) +
MathUtils.square(position.getZ() - getZ()));
}
/**