From 19fc90e7648690df39a51ebf4f41bafa4dd20e38 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Tue, 23 Jun 2020 18:09:01 +0200 Subject: [PATCH] Fixed Held packet + replaced the short by a byte --- build.gradle | 2 +- src/main/java/fr/themode/demo/PlayerInit.java | 5 ++++- src/main/java/net/minestom/server/entity/Player.java | 10 +++++----- .../server/event/player/PlayerChangeHeldSlotEvent.java | 8 ++++---- .../minestom/server/listener/PlayerHeldListener.java | 7 ++++--- .../packet/server/play/HeldItemChangePacket.java | 4 ++-- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 418048951..6c434da26 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,6 @@ dependencies { api 'net.kyori:text-serializer-plain:3.0.3' // Pathfinding - implementation 'com.github.MadMartian:hydrazine-path-finding:1.02' + implementation 'com.github.MadMartian:hydrazine-path-finding:1.1.0' } diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index c0596a0e5..7e762abd0 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -212,6 +212,7 @@ public class PlayerInit { player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> { player.sendMessage("CLICK PLAYER INVENTORY"); System.out.println("slot player: " + slot); + p.closeInventory(); }); /*Sidebar scoreboard = new Sidebar("Scoreboard Title"); @@ -225,9 +226,11 @@ public class PlayerInit { }); player.addEventCallback(PlayerSpawnEvent.class, event -> { - player.setGameMode(GameMode.CREATIVE); + player.setGameMode(GameMode.SURVIVAL); player.teleport(new Position(0, 41f, 0)); + player.setHeldItemSlot((byte) 5); + player.setGlowing(true); ItemStack item = new ItemStack(Material.STONE_SWORD, (byte) 1); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 34398bff2..96b2216e1 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -84,7 +84,7 @@ public class Player extends LivingEntity implements CommandSender { // Used internally to allow the closing of inventory within the inventory listener private boolean didCloseInventory; - private short heldSlot; + private byte heldSlot; private Position respawnPoint; @@ -1290,7 +1290,7 @@ public class Player extends LivingEntity implements CommandSender { * @param slot the slot that the player has to held * @throws IllegalArgumentException if {@code slot} is not between 0 and 8 */ - public void setHeldItemSlot(short slot) { + public void setHeldItemSlot(byte slot) { Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8"); HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket(); @@ -1304,7 +1304,7 @@ public class Player extends LivingEntity implements CommandSender { * * @return the current held slot for the player */ - public short getHeldSlot() { + public byte getHeldSlot() { return heldSlot; } @@ -1738,11 +1738,11 @@ public class Player extends LivingEntity implements CommandSender { * Also cancel eating if {@link #isEating()} was true *

* Warning: the player will not be noticed by this chance, only his viewers, - * see instead: {@link #setHeldItemSlot(short)} + * see instead: {@link #setHeldItemSlot(byte)} * * @param slot the new held slot */ - public void refreshHeldSlot(short slot) { + public void refreshHeldSlot(byte slot) { this.heldSlot = slot; syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND); diff --git a/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java index 6838f65e2..6a70f608a 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java @@ -11,9 +11,9 @@ import net.minestom.server.utils.validate.Check; public class PlayerChangeHeldSlotEvent extends CancellableEvent { private Player player; - private short slot; + private byte slot; - public PlayerChangeHeldSlotEvent(Player player, short slot) { + public PlayerChangeHeldSlotEvent(Player player, byte slot) { this.player = player; this.slot = slot; } @@ -32,7 +32,7 @@ public class PlayerChangeHeldSlotEvent extends CancellableEvent { * * @return the held slot */ - public short getSlot() { + public byte getSlot() { return slot; } @@ -42,7 +42,7 @@ public class PlayerChangeHeldSlotEvent extends CancellableEvent { * @param slot the new held slot * @throws IllegalArgumentException if {@param slot} is not between 0 and 8 */ - public void setSlot(short slot) { + public void setSlot(byte slot) { Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "The held slot needs to be between 0 and 8"); this.slot = slot; } diff --git a/src/main/java/net/minestom/server/listener/PlayerHeldListener.java b/src/main/java/net/minestom/server/listener/PlayerHeldListener.java index f633d423f..7bd713ac1 100644 --- a/src/main/java/net/minestom/server/listener/PlayerHeldListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerHeldListener.java @@ -8,19 +8,20 @@ import net.minestom.server.utils.MathUtils; public class PlayerHeldListener { public static void heldListener(ClientHeldItemChangePacket packet, Player player) { - final short slot = packet.slot; - if (!MathUtils.isBetween(slot, 0, 8)) { + if (!MathUtils.isBetween(packet.slot, 0, 8)) { // Incorrect packet, ignore return; } + final byte slot = (byte) packet.slot; + PlayerChangeHeldSlotEvent changeHeldSlotEvent = new PlayerChangeHeldSlotEvent(player, slot); player.callEvent(PlayerChangeHeldSlotEvent.class, changeHeldSlotEvent); if (!changeHeldSlotEvent.isCancelled()) { // Event hasn't been canceled, process it - final short resultSlot = changeHeldSlotEvent.getSlot(); + final byte resultSlot = changeHeldSlotEvent.getSlot(); // If the held slot has been changed by the event, send the change to the player if (resultSlot != slot) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java index 44ce887b5..85e8cc204 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java @@ -6,11 +6,11 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier; public class HeldItemChangePacket implements ServerPacket { - public short slot; + public byte slot; @Override public void write(PacketWriter writer) { - writer.writeShort(slot); + writer.writeByte(slot); } @Override