Fixed Held packet + replaced the short by a byte

This commit is contained in:
Felix Cravic 2020-06-23 18:09:01 +02:00
parent b47efa35ca
commit 19fc90e764
6 changed files with 20 additions and 16 deletions

View File

@ -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'
}

View File

@ -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);

View File

@ -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
* <p>
* 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);

View File

@ -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;
}

View File

@ -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) {

View File

@ -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