mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Fix PlayerCommandEvent being called 2 times
This commit is contained in:
parent
251fbbda32
commit
7007c507c1
@ -1,6 +1,5 @@
|
|||||||
package net.minestom.server.inventory;
|
package net.minestom.server.inventory;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import net.minestom.server.Viewable;
|
import net.minestom.server.Viewable;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.inventory.click.ClickType;
|
import net.minestom.server.inventory.click.ClickType;
|
||||||
@ -17,7 +16,6 @@ import net.minestom.server.network.packet.server.play.WindowPropertyPacket;
|
|||||||
import net.minestom.server.network.player.PlayerConnection;
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
import net.minestom.server.utils.ArrayUtils;
|
import net.minestom.server.utils.ArrayUtils;
|
||||||
import net.minestom.server.utils.MathUtils;
|
import net.minestom.server.utils.MathUtils;
|
||||||
import net.minestom.server.utils.PacketUtils;
|
|
||||||
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;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
@ -50,9 +48,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
private InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
|
private InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
|
||||||
|
|
||||||
// Cached windows packet
|
// Cached windows packet
|
||||||
private ByteBuf windowItemsBuffer;
|
|
||||||
// True if the buffer above is up to date, false otherwise
|
|
||||||
private boolean windowItemsBufferUpdated;
|
|
||||||
|
|
||||||
public Inventory(InventoryType inventoryType, String title) {
|
public Inventory(InventoryType inventoryType, String title) {
|
||||||
this.id = generateId();
|
this.id = generateId();
|
||||||
@ -199,11 +194,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
* Refresh the inventory for all viewers
|
* Refresh the inventory for all viewers
|
||||||
*/
|
*/
|
||||||
public void update() {
|
public void update() {
|
||||||
final ByteBuf packetBuffer = getWindowItemsBuffer();
|
sendPacketToViewers(createNewWindowItemsPacket());
|
||||||
getViewers().forEach(player -> {
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
playerConnection.sendPacket(packetBuffer, true);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,8 +208,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
final PlayerConnection playerConnection = player.getPlayerConnection();
|
||||||
final ByteBuf packetBuffer = getWindowItemsBuffer();
|
playerConnection.sendPacket(createNewWindowItemsPacket());
|
||||||
playerConnection.sendPacket(packetBuffer, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -290,9 +280,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
setSlotPacket.slot = (short) slot;
|
setSlotPacket.slot = (short) slot;
|
||||||
setSlotPacket.itemStack = itemStack;
|
setSlotPacket.itemStack = itemStack;
|
||||||
sendPacketToViewers(setSlotPacket);
|
sendPacketToViewers(setSlotPacket);
|
||||||
|
|
||||||
// The inventory changed, buffer will be updated once requested again
|
|
||||||
this.windowItemsBufferUpdated = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,33 +293,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
*/
|
*/
|
||||||
protected void setItemStackInternal(int slot, ItemStack itemStack) {
|
protected void setItemStackInternal(int slot, ItemStack itemStack) {
|
||||||
itemStacks[slot] = itemStack;
|
itemStacks[slot] = itemStack;
|
||||||
this.windowItemsBufferUpdated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the window items packet as a buffer containing all the
|
|
||||||
* current inventory's items
|
|
||||||
* <p>
|
|
||||||
* The cached window items buffer will be updated if needed
|
|
||||||
*
|
|
||||||
* @return a {@link WindowItemsPacket} buffer
|
|
||||||
*/
|
|
||||||
private ByteBuf getWindowItemsBuffer() {
|
|
||||||
if (!windowItemsBufferUpdated) {
|
|
||||||
refreshWindowItemsBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
return windowItemsBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refresh the inventory {@link WindowItemsPacket} buffer
|
|
||||||
*/
|
|
||||||
private void refreshWindowItemsBuffer() {
|
|
||||||
final WindowItemsPacket windowItemsPacket = createNewWindowItemsPacket();
|
|
||||||
final ByteBuf packetBuffer = PacketUtils.writePacket(windowItemsPacket);
|
|
||||||
this.windowItemsBuffer = packetBuffer;
|
|
||||||
this.windowItemsBufferUpdated = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,6 @@ import net.minestom.server.chat.RichMessage;
|
|||||||
import net.minestom.server.command.CommandManager;
|
import net.minestom.server.command.CommandManager;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.player.PlayerChatEvent;
|
import net.minestom.server.event.player.PlayerChatEvent;
|
||||||
import net.minestom.server.event.player.PlayerCommandEvent;
|
|
||||||
import net.minestom.server.network.ConnectionManager;
|
import net.minestom.server.network.ConnectionManager;
|
||||||
import net.minestom.server.network.PacketWriterUtils;
|
import net.minestom.server.network.PacketWriterUtils;
|
||||||
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
|
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
|
||||||
@ -30,10 +29,7 @@ public class ChatMessageListener {
|
|||||||
// The message is a command
|
// The message is a command
|
||||||
message = message.replaceFirst(cmdPrefix, "");
|
message = message.replaceFirst(cmdPrefix, "");
|
||||||
|
|
||||||
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(player, message);
|
COMMAND_MANAGER.execute(player, message);
|
||||||
player.callCancellableEvent(PlayerCommandEvent.class, playerCommandEvent, () -> {
|
|
||||||
COMMAND_MANAGER.execute(player, playerCommandEvent.getCommand());
|
|
||||||
});
|
|
||||||
|
|
||||||
// Do not call chat event
|
// Do not call chat event
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user