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()); playerConnection.sendPacket(getPropertiesPacket());
syncEquipments(); syncEquipments();
askSynchronization(); sendSynchronization();
} }
protected void refreshHealth() { protected void refreshHealth() {

View File

@ -1,5 +1,8 @@
package net.minestom.server.entity; package net.minestom.server.entity;
/**
* Contains all the data required to store a skin
*/
public class PlayerSkin { public class PlayerSkin {
private String textures; private String textures;
@ -10,19 +13,21 @@ public class PlayerSkin {
this.signature = signature; this.signature = signature;
} }
/**
* Get the skin textures value
*
* @return the textures value
*/
public String getTextures() { public String getTextures() {
return textures; return textures;
} }
public void setTextures(String textures) { /**
this.textures = textures; * Get the skin signature
} *
* @return the skin signature
*/
public String getSignature() { public String getSignature() {
return signature; 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 Player player;
private Position newPosition; 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.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.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket; import net.minestom.server.network.packet.server.play.WindowItemsPacket;
import net.minestom.server.network.packet.server.play.WindowPropertyPacket; 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.MathUtils;
import net.minestom.server.utils.inventory.PlayerInventoryUtils; import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.server.utils.item.ItemStackUtils; import net.minestom.server.utils.item.ItemStackUtils;
@ -56,9 +57,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.itemStacks = new ItemStack[size]; this.itemStacks = new ItemStack[size];
for (int i = 0; i < size; i++) { ArrayUtils.fill(itemStacks, ItemStack::getAirItem);
itemStacks[i] = ItemStack.getAirItem();
}
} }
private static byte generateId() { 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.EntityEquipmentPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket; 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.WindowItemsPacket;
import net.minestom.server.utils.ArrayUtils;
import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.item.ItemStackUtils; import net.minestom.server.utils.item.ItemStackUtils;
@ -36,9 +37,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
public PlayerInventory(Player player) { public PlayerInventory(Player player) {
this.player = player; this.player = player;
for (int i = 0; i < items.length; i++) { ArrayUtils.fill(items, ItemStack::getAirItem);
items[i] = ItemStack.getAirItem();
}
} }
@Override @Override
@ -201,6 +200,12 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
this.cursorItem = ItemStackUtils.notNull(cursorItem); 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) { private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
itemStack = ItemStackUtils.notNull(itemStack); itemStack = ItemStackUtils.notNull(itemStack);
@ -245,17 +250,38 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
//refreshSlot(slot); seems to break things concerning +64 stacks //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) { protected void setItemStack(int slot, int offset, ItemStack itemStack) {
slot = convertSlot(slot, offset); slot = convertSlot(slot, offset);
safeItemInsert(slot, itemStack); 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) { protected ItemStack getItemStack(int slot, int offset) {
slot = convertSlot(slot, offset); slot = convertSlot(slot, offset);
return this.items[slot]; 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 setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = (byte) (MathUtils.isBetween(slot, 35, INVENTORY_SIZE) ? 0 : -2); setSlotPacket.windowId = (byte) (MathUtils.isBetween(slot, 35, INVENTORY_SIZE) ? 0 : -2);
setSlotPacket.slot = slot; setSlotPacket.slot = slot;
@ -263,6 +289,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
player.getPlayerConnection().sendPacket(setSlotPacket); 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() { private WindowItemsPacket createWindowItemsPacket() {
ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE]; ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE];

View File

@ -67,7 +67,8 @@ public class PlayerPositionListener {
return; 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); player.callEvent(PlayerMoveEvent.class, playerMoveEvent);
if (!playerMoveEvent.isCancelled()) { if (!playerMoveEvent.isCancelled()) {
consumer.accept(playerMoveEvent.getNewPosition()); consumer.accept(playerMoveEvent.getNewPosition());

View File

@ -1,6 +1,7 @@
package net.minestom.server.utils; package net.minestom.server.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.function.Supplier;
public class ArrayUtils { public class ArrayUtils {
@ -61,4 +62,17 @@ public class ArrayUtils {
return array; 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) { 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()));
} }
/** /**