2021-03-15 23:00:00 +01:00
--- a/net/minecraft/server/network/PlayerConnection.java
+++ b/net/minecraft/server/network/PlayerConnection.java
2023-12-05 17:40:00 +01:00
@@ -183,6 +183,67 @@
2022-12-07 17:00:00 +01:00
import net.minecraft.world.phys.shapes.VoxelShapes;
2022-02-28 16:00:00 +01:00
import org.slf4j.Logger;
2014-11-25 22:32:16 +01:00
+// CraftBukkit start
2022-12-07 17:00:00 +01:00
+import com.mojang.datafixers.util.Pair;
2022-08-14 03:27:22 +02:00
+import java.util.Arrays;
2014-11-25 22:32:16 +01:00
+import java.util.concurrent.ExecutionException;
2021-06-11 07:00:00 +02:00
+import java.util.concurrent.atomic.AtomicInteger;
2023-12-05 17:40:00 +01:00
+import java.util.function.Function;
2022-12-07 17:00:00 +01:00
+import net.minecraft.network.chat.OutgoingChatMessage;
2021-03-15 23:00:00 +01:00
+import net.minecraft.network.protocol.game.PacketPlayOutAttachEntity;
2022-08-14 03:27:22 +02:00
+import net.minecraft.network.protocol.game.PacketPlayOutEntityEquipment;
2022-06-07 18:00:00 +02:00
+import net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity;
2021-03-15 23:00:00 +01:00
+import net.minecraft.world.entity.EntityInsentient;
2021-12-21 22:04:07 +01:00
+import net.minecraft.world.entity.animal.Bucketable;
+import net.minecraft.world.entity.EntityLiving;
2022-08-14 03:27:22 +02:00
+import net.minecraft.world.entity.EnumItemSlot;
+import net.minecraft.world.entity.animal.allay.Allay;
2021-03-15 23:00:00 +01:00
+import net.minecraft.world.inventory.InventoryClickType;
2021-06-11 07:00:00 +02:00
+import net.minecraft.world.inventory.Slot;
2023-09-21 18:40:00 +02:00
+import net.minecraft.world.item.crafting.RecipeHolder;
2021-03-15 23:00:00 +01:00
+import net.minecraft.world.level.RayTrace;
+import net.minecraft.world.phys.MovingObjectPosition;
2016-02-29 22:32:46 +01:00
+import org.bukkit.Location;
2023-07-18 13:23:52 +02:00
+import org.bukkit.craftbukkit.entity.CraftEntity;
2014-11-25 22:32:16 +01:00
+import org.bukkit.craftbukkit.entity.CraftPlayer;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
2023-12-28 22:36:38 +01:00
+import org.bukkit.craftbukkit.inventory.CraftItemType;
2014-11-25 22:32:16 +01:00
+import org.bukkit.craftbukkit.util.CraftChatMessage;
2023-08-01 23:46:19 +02:00
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
2014-11-25 22:32:16 +01:00
+import org.bukkit.craftbukkit.util.LazyPlayerSet;
+import org.bukkit.craftbukkit.util.Waitable;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Event;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.inventory.ClickType;
+import org.bukkit.event.inventory.CraftItemEvent;
+import org.bukkit.event.inventory.InventoryAction;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.event.inventory.InventoryCreativeEvent;
+import org.bukkit.event.inventory.InventoryType.SlotType;
2021-04-18 10:03:23 +02:00
+import org.bukkit.event.inventory.SmithItemEvent;
2014-11-25 22:32:16 +01:00
+import org.bukkit.event.player.AsyncPlayerChatEvent;
+import org.bukkit.event.player.PlayerAnimationEvent;
2022-06-09 13:30:25 +02:00
+import org.bukkit.event.player.PlayerAnimationType;
2014-11-25 22:32:16 +01:00
+import org.bukkit.event.player.PlayerChatEvent;
+import org.bukkit.event.player.PlayerCommandPreprocessEvent;
+import org.bukkit.event.player.PlayerInteractAtEntityEvent;
2016-02-29 22:32:46 +01:00
+import org.bukkit.event.player.PlayerInteractEntityEvent;
2014-11-25 22:32:16 +01:00
+import org.bukkit.event.player.PlayerItemHeldEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
2023-04-10 00:26:45 +02:00
+import org.bukkit.event.player.PlayerRespawnEvent.RespawnReason;
2016-03-14 06:53:46 +01:00
+import org.bukkit.event.player.PlayerSwapHandItemsEvent;
2014-11-25 22:32:16 +01:00
+import org.bukkit.event.player.PlayerTeleportEvent;
+import org.bukkit.event.player.PlayerToggleFlightEvent;
+import org.bukkit.event.player.PlayerToggleSneakEvent;
+import org.bukkit.event.player.PlayerToggleSprintEvent;
+import org.bukkit.inventory.CraftingInventory;
2016-03-04 05:24:51 +01:00
+import org.bukkit.inventory.EquipmentSlot;
2014-11-25 22:32:16 +01:00
+import org.bukkit.inventory.InventoryView;
2021-04-18 10:03:23 +02:00
+import org.bukkit.inventory.SmithingInventory;
2014-11-25 22:32:16 +01:00
+// CraftBukkit end
+
2023-09-21 18:40:00 +02:00
public class PlayerConnection extends ServerCommonPacketListenerImpl implements PacketListenerPlayIn, ServerPlayerConnection, TickablePacketListener {
2014-11-25 22:32:16 +01:00
2022-02-28 16:00:00 +01:00
static final Logger LOGGER = LogUtils.getLogger();
2023-12-05 17:40:00 +01:00
@@ -194,7 +255,9 @@
2023-09-21 18:40:00 +02:00
public final PlayerChunkSender chunkSender;
private int tickCount;
private int ackBlockChangesUpTo = -1;
2021-06-11 07:00:00 +02:00
- private int chatSpamTickCount;
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - multithreaded fields
2021-11-21 23:00:00 +01:00
+ private final AtomicInteger chatSpamTickCount = new AtomicInteger();
2014-11-25 22:32:16 +01:00
+ // CraftBukkit end
2021-06-11 07:00:00 +02:00
private int dropSpamTickCount;
private double firstGoodX;
private double firstGoodY;
2023-12-05 17:40:00 +01:00
@@ -229,7 +292,7 @@
2023-09-21 18:40:00 +02:00
private boolean waitingForSwitchToConfig;
public PlayerConnection(MinecraftServer minecraftserver, NetworkManager networkmanager, EntityPlayer entityplayer, CommonListenerCookie commonlistenercookie) {
- super(minecraftserver, networkmanager, commonlistenercookie);
+ super(minecraftserver, networkmanager, commonlistenercookie, entityplayer); // CraftBukkit
2023-12-05 17:40:00 +01:00
this.chunkSender = new PlayerChunkSender(networkmanager.isMemoryConnection());
networkmanager.setListener(this);
this.player = entityplayer;
@@ -239,9 +302,25 @@
Objects.requireNonNull(minecraftserver);
this.signedMessageDecoder = SignedMessageChain.b.unsigned(uuid, minecraftserver::enforceSecureProfile);
2022-07-27 20:00:00 +02:00
- this.chatMessageChain = new FutureChain(minecraftserver);
+ this.chatMessageChain = new FutureChain(minecraftserver.chatExecutor); // CraftBukkit - async chat
2023-09-21 18:40:00 +02:00
}
+ // CraftBukkit start - add fields
2014-11-25 22:32:16 +01:00
+ private int lastTick = MinecraftServer.currentTick;
2016-02-29 22:32:46 +01:00
+ private int allowedPlayerTicks = 1;
2014-11-25 22:32:16 +01:00
+ private int lastDropTick = MinecraftServer.currentTick;
2017-04-20 14:10:31 +02:00
+ private int lastBookTick = MinecraftServer.currentTick;
2014-11-25 22:32:16 +01:00
+ private int dropCount = 0;
+
2023-12-27 21:56:30 +01:00
+ private boolean hasMoved = false;
2014-11-25 22:32:16 +01:00
+ private double lastPosX = Double.MAX_VALUE;
+ private double lastPosY = Double.MAX_VALUE;
+ private double lastPosZ = Double.MAX_VALUE;
+ private float lastPitch = Float.MAX_VALUE;
+ private float lastYaw = Float.MAX_VALUE;
+ private boolean justTeleported = false;
+ // CraftBukkit end
2023-09-21 18:40:00 +02:00
+
2022-07-27 20:00:00 +02:00
@Override
2018-12-13 01:00:00 +01:00
public void tick() {
2023-09-21 18:40:00 +02:00
if (this.ackBlockChangesUpTo > -1) {
2023-12-05 17:40:00 +01:00
@@ -293,15 +372,21 @@
2014-11-25 22:32:16 +01:00
}
2023-09-21 18:40:00 +02:00
this.keepConnectionAlive();
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
+ for (int spam; (spam = this.chatSpamTickCount.get()) > 0 && !chatSpamTickCount.compareAndSet(spam, spam - 1); ) ;
2014-11-25 22:32:16 +01:00
+ /* Use thread-safe field access instead
2021-06-11 07:00:00 +02:00
if (this.chatSpamTickCount > 0) {
--this.chatSpamTickCount;
2014-11-25 22:32:16 +01:00
}
+ */
+ // CraftBukkit end
2021-06-11 07:00:00 +02:00
if (this.dropSpamTickCount > 0) {
--this.dropSpamTickCount;
2015-05-21 09:15:47 +02:00
}
2023-06-07 17:30:00 +02:00
if (this.player.getLastActionTime() > 0L && this.server.getPlayerIdleTimeout() > 0 && SystemUtils.getMillis() - this.player.getLastActionTime() > (long) this.server.getPlayerIdleTimeout() * 1000L * 60L) {
2021-11-21 23:00:00 +01:00
+ this.player.resetLastActionTime(); // CraftBukkit - SPIGOT-854
2022-06-07 18:00:00 +02:00
this.disconnect(IChatBaseComponent.translatable("multiplayer.disconnect.idling"));
2015-05-21 09:15:47 +02:00
}
2023-12-27 21:56:30 +01:00
@@ -378,6 +463,13 @@
if (entity != this.player && entity.getControllingPassenger() == this.player && entity == this.lastVehicle) {
WorldServer worldserver = this.player.serverLevel();
+ // CraftBukkit - store current player position
+ double prevX = player.getX();
+ double prevY = player.getY();
+ double prevZ = player.getZ();
+ float prevYaw = player.getYRot();
+ float prevPitch = player.getXRot();
+ // CraftBukkit end
double d0 = entity.getX();
double d1 = entity.getY();
double d2 = entity.getZ();
@@ -392,7 +484,33 @@
2021-11-21 23:00:00 +01:00
double d9 = entity.getDeltaMovement().lengthSqr();
2016-03-06 22:33:27 +01:00
double d10 = d6 * d6 + d7 * d7 + d8 * d8;
2021-11-21 23:00:00 +01:00
- if (d10 - d9 > 100.0D && !this.isSingleplayerOwner()) {
2016-03-06 22:33:27 +01:00
+ // CraftBukkit start - handle custom speeds and skipped ticks
+ this.allowedPlayerTicks += (System.currentTimeMillis() / 50) - this.lastTick;
+ this.allowedPlayerTicks = Math.max(this.allowedPlayerTicks, 1);
+ this.lastTick = (int) (System.currentTimeMillis() / 50);
+
2021-06-11 07:00:00 +02:00
+ ++this.receivedMovePacketCount;
+ int i = this.receivedMovePacketCount - this.knownMovePacketCount;
2016-03-06 22:33:27 +01:00
+ if (i > Math.max(this.allowedPlayerTicks, 5)) {
2021-11-21 23:00:00 +01:00
+ PlayerConnection.LOGGER.debug(this.player.getScoreboardName() + " is sending move packets too frequently (" + i + " packets since last tick)");
2016-03-06 22:33:27 +01:00
+ i = 1;
+ }
+
+ if (d10 > 0) {
+ allowedPlayerTicks -= 1;
+ } else {
+ allowedPlayerTicks = 20;
+ }
2018-07-15 02:00:00 +02:00
+ double speed;
2021-06-11 07:00:00 +02:00
+ if (player.getAbilities().flying) {
+ speed = player.getAbilities().flyingSpeed * 20f;
2016-03-06 22:33:27 +01:00
+ } else {
2021-06-11 07:00:00 +02:00
+ speed = player.getAbilities().walkingSpeed * 10f;
2016-03-06 22:33:27 +01:00
+ }
+ speed *= 2f; // TODO: Get the speed of the vehicle instead of the player
+
2021-11-21 23:00:00 +01:00
+ if (d10 - d9 > Math.max(100.0D, Math.pow((double) (10.0F * (float) i * speed), 2)) && !this.isSingleplayerOwner()) {
2016-03-06 22:33:27 +01:00
+ // CraftBukkit end
2022-02-28 16:00:00 +01:00
PlayerConnection.LOGGER.warn("{} (vehicle of {}) moved too quickly! {},{},{}", new Object[]{entity.getName().getString(), this.player.getName().getString(), d6, d7, d8});
2023-09-21 18:40:00 +02:00
this.send(new PacketPlayOutVehicleMove(entity));
2016-03-06 22:33:27 +01:00
return;
2023-12-27 21:56:30 +01:00
@@ -432,14 +550,76 @@
2018-09-26 09:19:16 +02:00
}
2021-11-21 23:00:00 +01:00
entity.absMoveTo(d3, d4, d5, f, f1);
+ player.absMoveTo(d3, d4, d5, this.player.getYRot(), this.player.getXRot()); // CraftBukkit
2022-02-28 16:00:00 +01:00
boolean flag3 = worldserver.noCollision(entity, entity.getBoundingBox().deflate(0.0625D));
2018-09-26 09:19:16 +02:00
2022-02-28 16:00:00 +01:00
if (flag && (flag2 || !flag3)) {
2021-11-21 23:00:00 +01:00
entity.absMoveTo(d0, d1, d2, f, f1);
+ player.absMoveTo(d0, d1, d2, this.player.getYRot(), this.player.getXRot()); // CraftBukkit
2023-09-21 18:40:00 +02:00
this.send(new PacketPlayOutVehicleMove(entity));
2016-02-29 22:32:46 +01:00
return;
2014-11-25 22:32:16 +01:00
}
2016-02-29 22:32:46 +01:00
2015-05-22 15:33:34 +02:00
+ // CraftBukkit start - fire PlayerMoveEvent
2021-11-21 23:00:00 +01:00
+ Player player = this.getCraftPlayer();
2023-12-27 21:56:30 +01:00
+ if (!this.hasMoved) {
+ this.lastPosX = prevX;
+ this.lastPosY = prevY;
+ this.lastPosZ = prevZ;
+ this.lastYaw = prevYaw;
+ this.lastPitch = prevPitch;
+ this.hasMoved = true;
+ }
2015-05-22 15:33:34 +02:00
+ Location from = new Location(player.getWorld(), lastPosX, lastPosY, lastPosZ, lastYaw, lastPitch); // Get the Players previous Event location.
+ Location to = player.getLocation().clone(); // Start off the To location as the Players current location.
+
+ // If the packet contains movement information then we update the To location with the correct XYZ.
2016-02-29 22:32:46 +01:00
+ to.setX(packetplayinvehiclemove.getX());
+ to.setY(packetplayinvehiclemove.getY());
+ to.setZ(packetplayinvehiclemove.getZ());
+
2015-05-22 15:33:34 +02:00
+ // If the packet contains look information then we update the To location with the correct Yaw & Pitch.
2021-11-21 23:00:00 +01:00
+ to.setYaw(packetplayinvehiclemove.getYRot());
+ to.setPitch(packetplayinvehiclemove.getXRot());
2014-11-25 22:32:16 +01:00
+
2015-05-22 15:33:34 +02:00
+ // Prevent 40 event-calls for less than a single pixel of movement >.>
+ double delta = Math.pow(this.lastPosX - to.getX(), 2) + Math.pow(this.lastPosY - to.getY(), 2) + Math.pow(this.lastPosZ - to.getZ(), 2);
+ float deltaAngle = Math.abs(this.lastYaw - to.getYaw()) + Math.abs(this.lastPitch - to.getPitch());
+
2021-11-21 23:00:00 +01:00
+ if ((delta > 1f / 256 || deltaAngle > 10f) && !this.player.isImmobile()) {
2015-05-22 15:33:34 +02:00
+ this.lastPosX = to.getX();
+ this.lastPosY = to.getY();
+ this.lastPosZ = to.getZ();
+ this.lastYaw = to.getYaw();
+ this.lastPitch = to.getPitch();
+
2023-12-27 21:56:30 +01:00
+ Location oldTo = to.clone();
+ PlayerMoveEvent event = new PlayerMoveEvent(player, from, to);
+ this.cserver.getPluginManager().callEvent(event);
2015-05-22 15:33:34 +02:00
+
2023-12-27 21:56:30 +01:00
+ // If the event is cancelled we move the player back to their old location.
+ if (event.isCancelled()) {
+ teleport(from);
+ return;
+ }
2016-02-29 22:32:46 +01:00
+
2023-12-27 21:56:30 +01:00
+ // If a Plugin has changed the To destination then we teleport the Player
+ // there to avoid any 'Moved wrongly' or 'Moved too quickly' errors.
+ // We only do this if the Event was not cancelled.
+ if (!oldTo.equals(event.getTo()) && !event.isCancelled()) {
+ this.player.getBukkitEntity().teleport(event.getTo(), PlayerTeleportEvent.TeleportCause.PLUGIN);
+ return;
+ }
2015-04-15 17:24:53 +02:00
+
2023-12-27 21:56:30 +01:00
+ // Check to see if the Players Location has some how changed during the call of the event.
+ // This can happen due to a plugin teleporting the player instead of using .setTo()
+ if (!from.equals(this.getCraftPlayer().getLocation()) && this.justTeleported) {
+ this.justTeleported = false;
+ return;
2014-11-25 22:32:16 +01:00
+ }
+ }
2016-02-29 22:32:46 +01:00
+ // CraftBukkit end
2015-07-20 23:31:49 +02:00
+
2023-06-07 17:30:00 +02:00
this.player.serverLevel().getChunkSource().move(this.player);
2021-11-21 23:00:00 +01:00
this.player.checkMovementStatistics(this.player.getX() - d0, this.player.getY() - d1, this.player.getZ() - d2);
2022-02-28 16:00:00 +01:00
this.clientVehicleIsFloating = d11 >= -0.03125D && !flag1 && !this.server.isFlightAllowed() && !entity.isNoGravity() && this.noBlocksAround(entity);
2023-12-27 21:56:30 +01:00
@@ -473,6 +653,7 @@
2018-09-03 00:00:50 +02:00
}
2021-06-11 07:00:00 +02:00
this.awaitingPositionFromClient = null;
2023-06-07 17:30:00 +02:00
+ this.player.serverLevel().getChunkSource().move(this.player); // CraftBukkit
2018-09-03 00:00:50 +02:00
}
}
2023-12-27 21:56:30 +01:00
@@ -480,7 +661,7 @@
2020-08-11 23:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleRecipeBookSeenRecipePacket(PacketPlayInRecipeDisplayed packetplayinrecipedisplayed) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinrecipedisplayed, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
- Optional optional = this.server.getRecipeManager().byKey(packetplayinrecipedisplayed.getRecipe());
2023-09-21 18:40:00 +02:00
+ Optional<? extends RecipeHolder<?>> optional = this.server.getRecipeManager().byKey(packetplayinrecipedisplayed.getRecipe()); // CraftBukkit - decompile error
2020-08-11 23:00:00 +02:00
RecipeBookServer recipebookserver = this.player.getRecipeBook();
2019-04-23 04:00:00 +02:00
2021-06-11 07:00:00 +02:00
Objects.requireNonNull(recipebookserver);
2023-12-27 21:56:30 +01:00
@@ -490,6 +671,7 @@
2023-11-08 20:43:34 +01:00
@Override
public void handleRecipeBookChangeSettingsPacket(PacketPlayInRecipeSettings packetplayinrecipesettings) {
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinrecipesettings, this, this.player.serverLevel());
+ CraftEventFactory.callRecipeBookSettingsEvent(this.player, packetplayinrecipesettings.getBookType(), packetplayinrecipesettings.isOpen(), packetplayinrecipesettings.isFiltering()); // CraftBukkit
this.player.getRecipeBook().setBookSetting(packetplayinrecipesettings.getBookType(), packetplayinrecipesettings.isOpen(), packetplayinrecipesettings.isFiltering());
}
2023-12-27 21:56:30 +01:00
@@ -510,6 +692,12 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleCustomCommandSuggestions(PacketPlayInTabComplete packetplayintabcomplete) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayintabcomplete, this, this.player.serverLevel());
2018-07-15 02:00:00 +02:00
+ // CraftBukkit start
2021-11-21 23:00:00 +01:00
+ if (chatSpamTickCount.addAndGet(1) > 500 && !this.server.getPlayerList().isOp(this.player.getGameProfile())) {
2022-06-07 18:00:00 +02:00
+ this.disconnect(IChatBaseComponent.translatable("disconnect.spam"));
2018-07-15 02:00:00 +02:00
+ return;
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
StringReader stringreader = new StringReader(packetplayintabcomplete.getCommand());
2018-08-26 04:00:00 +02:00
2018-12-25 22:00:00 +01:00
if (stringreader.canRead() && stringreader.peek() == '/') {
2023-12-27 21:56:30 +01:00
@@ -519,6 +707,7 @@
2021-11-21 23:00:00 +01:00
ParseResults<CommandListenerWrapper> parseresults = this.server.getCommands().getDispatcher().parse(stringreader, this.player.createCommandSourceStack());
2018-07-15 02:00:00 +02:00
2021-11-21 23:00:00 +01:00
this.server.getCommands().getDispatcher().getCompletionSuggestions(parseresults).thenAccept((suggestions) -> {
2020-06-25 02:00:00 +02:00
+ if (suggestions.isEmpty()) return; // CraftBukkit - don't send through empty suggestions - prevents [<args>] from showing for plugins with nothing more to offer
2023-09-21 18:40:00 +02:00
this.send(new PacketPlayOutTabComplete(packetplayintabcomplete.getId(), suggestions));
2018-07-15 02:00:00 +02:00
});
}
2023-12-27 21:56:30 +01:00
@@ -766,6 +955,13 @@
2019-04-28 04:35:28 +02:00
if (container instanceof ContainerMerchant) {
ContainerMerchant containermerchant = (ContainerMerchant) container;
2022-01-28 23:27:01 +01:00
+ // CraftBukkit start
+ final org.bukkit.event.inventory.TradeSelectEvent tradeSelectEvent = CraftEventFactory.callTradeSelectEvent(this.player, i, containermerchant);
+ if (tradeSelectEvent.isCancelled()) {
+ this.player.getBukkitEntity().updateInventory();
+ return;
+ }
+ // CraftBukkit end
2019-04-28 04:35:28 +02:00
2022-07-27 20:00:00 +02:00
if (!containermerchant.stillValid(this.player)) {
PlayerConnection.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, containermerchant);
2023-12-27 21:56:30 +01:00
@@ -780,6 +976,13 @@
2020-11-02 21:00:00 +01:00
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleEditBook(PacketPlayInBEdit packetplayinbedit) {
2018-07-15 02:00:00 +02:00
+ // CraftBukkit start
+ if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
+ this.disconnect("Book edited too quickly!");
+ return;
+ }
+ this.lastBookTick = MinecraftServer.currentTick;
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
int i = packetplayinbedit.getSlot();
2021-06-11 07:00:00 +02:00
2021-11-21 23:00:00 +01:00
if (PlayerInventory.isHotbarSlot(i) || i == 40) {
2023-12-27 21:56:30 +01:00
@@ -788,7 +991,7 @@
2021-07-06 16:00:00 +02:00
Objects.requireNonNull(list);
optional.ifPresent(list::add);
2021-11-21 23:00:00 +01:00
- Stream stream = packetplayinbedit.getPages().stream().limit(100L);
+ Stream<String> stream = packetplayinbedit.getPages().stream().limit(100L); // CraftBukkit - decompile error
2021-07-06 16:00:00 +02:00
Objects.requireNonNull(list);
stream.forEach(list::add);
2023-12-27 21:56:30 +01:00
@@ -806,7 +1009,7 @@
2021-06-12 08:14:12 +02:00
ItemStack itemstack = this.player.getInventory().getItem(i);
2021-11-21 23:00:00 +01:00
if (itemstack.is(Items.WRITABLE_BOOK)) {
- this.updateBookPages(list, UnaryOperator.identity(), itemstack);
+ this.updateBookPages(list, UnaryOperator.identity(), itemstack.copy(), i, itemstack); // CraftBukkit
2021-06-12 08:14:12 +02:00
}
}
2023-12-27 21:56:30 +01:00
@@ -831,16 +1034,16 @@
2021-06-12 08:14:12 +02:00
2021-11-21 23:00:00 +01:00
this.updateBookPages(list, (s) -> {
2022-06-07 18:00:00 +02:00
return IChatBaseComponent.ChatSerializer.toJson(IChatBaseComponent.literal(s));
2021-06-12 08:14:12 +02:00
- }, itemstack1);
2021-06-14 11:37:21 +02:00
- this.player.getInventory().setItem(i, itemstack1);
2021-06-12 08:14:12 +02:00
+ }, itemstack1, i, itemstack); // CraftBukkit
2021-06-14 11:37:21 +02:00
+ this.player.getInventory().setItem(i, itemstack); // CraftBukkit - event factory updates the hand book
2020-11-04 09:45:54 +01:00
}
}
2022-07-27 20:00:00 +02:00
- private void updateBookPages(List<FilteredText> list, UnaryOperator<String> unaryoperator, ItemStack itemstack) {
+ private void updateBookPages(List<FilteredText> list, UnaryOperator<String> unaryoperator, ItemStack itemstack, int slot, ItemStack handItem) { // CraftBukkit
2021-06-11 07:00:00 +02:00
NBTTagList nbttaglist = new NBTTagList();
2020-11-02 21:00:00 +01:00
2021-11-21 23:00:00 +01:00
if (this.player.isTextFilteringEnabled()) {
2022-06-07 18:00:00 +02:00
- Stream stream = list.stream().map((filteredtext) -> {
+ Stream<NBTTagString> stream = list.stream().map((filteredtext) -> { // CraftBukkit - decompile error
2022-07-27 20:00:00 +02:00
return NBTTagString.valueOf((String) unaryoperator.apply(filteredtext.filteredOrEmpty()));
2021-06-11 07:00:00 +02:00
});
2020-11-02 21:00:00 +01:00
2023-12-27 21:56:30 +01:00
@@ -866,6 +1069,7 @@
2021-06-12 08:14:12 +02:00
}
2021-11-21 23:00:00 +01:00
itemstack.addTagElement("pages", nbttaglist);
2021-06-14 11:37:21 +02:00
+ CraftEventFactory.handleEditBookEvent(player, slot, handItem, itemstack); // CraftBukkit
2021-06-12 08:14:12 +02:00
}
@Override
2023-12-27 21:56:30 +01:00
@@ -922,7 +1126,7 @@
2016-04-24 18:35:59 +02:00
} else {
2023-06-07 17:30:00 +02:00
WorldServer worldserver = this.player.serverLevel();
2016-04-24 18:35:59 +02:00
2021-06-11 07:00:00 +02:00
- if (!this.player.wonGame) {
2021-11-21 23:00:00 +01:00
+ if (!this.player.wonGame && !this.player.isImmobile()) { // CraftBukkit
2021-06-11 07:00:00 +02:00
if (this.tickCount == 0) {
2021-11-21 23:00:00 +01:00
this.resetPosition();
2016-04-24 18:35:59 +02:00
}
2023-12-27 21:56:30 +01:00
@@ -932,6 +1136,7 @@
2021-06-11 07:00:00 +02:00
this.awaitingTeleportTime = this.tickCount;
2021-11-21 23:00:00 +01:00
this.teleport(this.awaitingPositionFromClient.x, this.awaitingPositionFromClient.y, this.awaitingPositionFromClient.z, this.player.getYRot(), this.player.getXRot());
2016-02-29 22:32:46 +01:00
}
+ this.allowedPlayerTicks = 20; // CraftBukkit
2023-12-27 21:56:30 +01:00
2016-02-29 22:32:46 +01:00
} else {
2021-06-11 07:00:00 +02:00
this.awaitingTeleportTime = this.tickCount;
2023-12-27 21:56:30 +01:00
@@ -944,7 +1149,15 @@
2016-02-29 22:32:46 +01:00
if (this.player.isPassenger()) {
2021-11-21 23:00:00 +01:00
this.player.absMoveTo(this.player.getX(), this.player.getY(), this.player.getZ(), f, f1);
2023-06-07 17:30:00 +02:00
this.player.serverLevel().getChunkSource().move(this.player);
2016-02-29 22:32:46 +01:00
+ this.allowedPlayerTicks = 20; // CraftBukkit
} else {
2016-03-03 12:15:04 +01:00
+ // CraftBukkit - Make sure the move is valid but then reset it for plugins to modify
2021-11-21 23:00:00 +01:00
+ double prevX = player.getX();
+ double prevY = player.getY();
+ double prevZ = player.getZ();
2021-06-11 07:00:00 +02:00
+ float prevYaw = player.getYRot();
+ float prevPitch = player.getXRot();
2016-03-03 12:15:04 +01:00
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
double d3 = this.player.getX();
double d4 = this.player.getY();
double d5 = this.player.getZ();
2023-12-27 21:56:30 +01:00
@@ -964,15 +1177,33 @@
2023-12-05 17:40:00 +01:00
++this.receivedMovePacketCount;
int i = this.receivedMovePacketCount - this.knownMovePacketCount;
- if (i > 5) {
+ // CraftBukkit start - handle custom speeds and skipped ticks
+ this.allowedPlayerTicks += (System.currentTimeMillis() / 50) - this.lastTick;
+ this.allowedPlayerTicks = Math.max(this.allowedPlayerTicks, 1);
+ this.lastTick = (int) (System.currentTimeMillis() / 50);
+
+ if (i > Math.max(this.allowedPlayerTicks, 5)) {
PlayerConnection.LOGGER.debug("{} is sending move packets too frequently ({} packets since last tick)", this.player.getName().getString(), i);
i = 1;
}
+ if (packetplayinflying.hasRot || d10 > 0) {
+ allowedPlayerTicks -= 1;
+ } else {
+ allowedPlayerTicks = 20;
+ }
+ double speed;
+ if (player.getAbilities().flying) {
+ speed = player.getAbilities().flyingSpeed * 20f;
+ } else {
+ speed = player.getAbilities().walkingSpeed * 10f;
+ }
2016-02-29 22:32:46 +01:00
+
2023-12-05 17:40:00 +01:00
if (!this.player.isChangingDimension() && (!this.player.level().getGameRules().getBoolean(GameRules.RULE_DISABLE_ELYTRA_MOVEMENT_CHECK) || !this.player.isFallFlying())) {
float f2 = this.player.isFallFlying() ? 300.0F : 100.0F;
2016-02-29 22:32:46 +01:00
2023-12-05 17:40:00 +01:00
- if (d10 - d9 > (double) (f2 * (float) i) && !this.isSingleplayerOwner()) {
+ if (d10 - d9 > Math.max(f2, Math.pow((double) (10.0F * (float) i * speed), 2)) && !this.isSingleplayerOwner()) {
+ // CraftBukkit end
PlayerConnection.LOGGER.warn("{} moved too quickly! {},{},{}", new Object[]{this.player.getName().getString(), d6, d7, d8});
this.teleport(this.player.getX(), this.player.getY(), this.player.getZ(), this.player.getYRot(), this.player.getXRot());
return;
2023-12-27 21:56:30 +01:00
@@ -994,6 +1225,7 @@
2022-02-28 16:00:00 +01:00
boolean flag1 = this.player.verticalCollisionBelow;
2020-06-28 02:15:32 +02:00
2023-06-07 17:30:00 +02:00
this.player.move(EnumMoveType.PLAYER, new Vec3D(d6, d7, d8));
2021-12-02 02:45:58 +01:00
+ this.player.onGround = packetplayinflying.isOnGround(); // CraftBukkit - SPIGOT-5810, SPIGOT-5835, SPIGOT-6828: reset by this.player.move
2023-06-07 17:30:00 +02:00
double d11 = d7;
2020-06-28 02:15:32 +02:00
2023-06-07 17:30:00 +02:00
d6 = d0 - this.player.getX();
2023-12-27 21:56:30 +01:00
@@ -1012,9 +1244,75 @@
2023-06-07 17:30:00 +02:00
}
2022-05-24 12:45:57 +02:00
2023-06-07 17:30:00 +02:00
if (!this.player.noPhysics && !this.player.isSleeping() && (flag2 && worldserver.noCollision(this.player, axisalignedbb) || this.isPlayerCollidingWithAnythingNew(worldserver, axisalignedbb, d0, d1, d2))) {
2022-05-24 12:45:57 +02:00
- this.teleport(d3, d4, d5, f, f1);
2023-03-14 17:30:00 +01:00
+ this.internalTeleport(d3, d4, d5, f, f1, Collections.emptySet()); // CraftBukkit - SPIGOT-1807: Don't call teleport event, when the client thinks the player is falling, because the chunks are not loaded on the client yet.
2023-06-07 17:30:00 +02:00
this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, packetplayinflying.isOnGround());
2020-06-25 02:00:00 +02:00
} else {
+ // CraftBukkit start - fire PlayerMoveEvent
2023-06-11 12:41:52 +02:00
+ // Reset to old location first
+ this.player.absMoveTo(prevX, prevY, prevZ, prevYaw, prevPitch);
+
2021-11-21 23:00:00 +01:00
+ Player player = this.getCraftPlayer();
2023-12-27 21:56:30 +01:00
+ if (!this.hasMoved) {
+ this.lastPosX = prevX;
+ this.lastPosY = prevY;
+ this.lastPosZ = prevZ;
+ this.lastYaw = prevYaw;
+ this.lastPitch = prevPitch;
+ this.hasMoved = true;
+ }
2020-06-25 02:00:00 +02:00
+ Location from = new Location(player.getWorld(), lastPosX, lastPosY, lastPosZ, lastYaw, lastPitch); // Get the Players previous Event location.
+ Location to = player.getLocation().clone(); // Start off the To location as the Players current location.
+
+ // If the packet contains movement information then we update the To location with the correct XYZ.
+ if (packetplayinflying.hasPos) {
+ to.setX(packetplayinflying.x);
+ to.setY(packetplayinflying.y);
+ to.setZ(packetplayinflying.z);
+ }
2016-02-29 22:32:46 +01:00
+
2020-06-25 02:00:00 +02:00
+ // If the packet contains look information then we update the To location with the correct Yaw & Pitch.
2021-06-11 07:00:00 +02:00
+ if (packetplayinflying.hasRot) {
+ to.setYaw(packetplayinflying.yRot);
+ to.setPitch(packetplayinflying.xRot);
2020-06-25 02:00:00 +02:00
+ }
2016-02-29 22:32:46 +01:00
+
2020-06-25 02:00:00 +02:00
+ // Prevent 40 event-calls for less than a single pixel of movement >.>
+ double delta = Math.pow(this.lastPosX - to.getX(), 2) + Math.pow(this.lastPosY - to.getY(), 2) + Math.pow(this.lastPosZ - to.getZ(), 2);
+ float deltaAngle = Math.abs(this.lastYaw - to.getYaw()) + Math.abs(this.lastPitch - to.getPitch());
+
2021-11-21 23:00:00 +01:00
+ if ((delta > 1f / 256 || deltaAngle > 10f) && !this.player.isImmobile()) {
2020-06-25 02:00:00 +02:00
+ this.lastPosX = to.getX();
+ this.lastPosY = to.getY();
+ this.lastPosZ = to.getZ();
+ this.lastYaw = to.getYaw();
+ this.lastPitch = to.getPitch();
+
2023-12-27 21:56:30 +01:00
+ Location oldTo = to.clone();
+ PlayerMoveEvent event = new PlayerMoveEvent(player, from, to);
+ this.cserver.getPluginManager().callEvent(event);
2020-06-25 02:00:00 +02:00
+
2023-12-27 21:56:30 +01:00
+ // If the event is cancelled we move the player back to their old location.
+ if (event.isCancelled()) {
+ teleport(from);
+ return;
+ }
2016-02-29 22:32:46 +01:00
+
2023-12-27 21:56:30 +01:00
+ // If a Plugin has changed the To destination then we teleport the Player
+ // there to avoid any 'Moved wrongly' or 'Moved too quickly' errors.
+ // We only do this if the Event was not cancelled.
+ if (!oldTo.equals(event.getTo()) && !event.isCancelled()) {
+ this.player.getBukkitEntity().teleport(event.getTo(), PlayerTeleportEvent.TeleportCause.PLUGIN);
+ return;
+ }
2016-02-29 22:32:46 +01:00
+
2023-12-27 21:56:30 +01:00
+ // Check to see if the Players Location has some how changed during the call of the event.
+ // This can happen due to a plugin teleporting the player instead of using .setTo()
+ if (!from.equals(this.getCraftPlayer().getLocation()) && this.justTeleported) {
+ this.justTeleported = false;
+ return;
2016-12-20 21:00:00 +01:00
+ }
2016-02-29 22:32:46 +01:00
+ }
2020-06-25 02:00:00 +02:00
+ // CraftBukkit end
2023-06-07 17:30:00 +02:00
this.player.absMoveTo(d0, d1, d2, f, f1);
this.clientIsFloating = d11 >= -0.03125D && !flag1 && this.player.gameMode.getGameModeForPlayer() != EnumGamemode.SPECTATOR && !this.server.isFlightAllowed() && !this.player.getAbilities().mayfly && !this.player.hasEffect(MobEffects.LEVITATION) && !this.player.isFallFlying() && !this.player.isAutoSpinAttack() && this.noBlocksAround(this.player);
this.player.serverLevel().getChunkSource().move(this.player);
2023-12-27 21:56:30 +01:00
@@ -1055,11 +1353,68 @@
2021-11-21 23:00:00 +01:00
return true;
2015-02-26 23:41:06 +01:00
}
2021-06-11 07:00:00 +02:00
+ // CraftBukkit start - Delegate to teleport(Location)
2021-11-21 23:00:00 +01:00
public void teleport(double d0, double d1, double d2, float f, float f1) {
2023-03-14 17:30:00 +01:00
- this.teleport(d0, d1, d2, f, f1, Collections.emptySet());
2021-11-21 23:00:00 +01:00
+ this.teleport(d0, d1, d2, f, f1, PlayerTeleportEvent.TeleportCause.UNKNOWN);
2021-06-11 07:00:00 +02:00
+ }
+
2021-11-21 23:00:00 +01:00
+ public void teleport(double d0, double d1, double d2, float f, float f1, PlayerTeleportEvent.TeleportCause cause) {
2023-03-14 17:30:00 +01:00
+ this.teleport(d0, d1, d2, f, f1, Collections.emptySet(), cause);
2017-03-24 05:23:23 +01:00
}
2023-03-14 17:30:00 +01:00
public void teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set) {
2021-11-21 23:00:00 +01:00
+ this.teleport(d0, d1, d2, f, f1, set, PlayerTeleportEvent.TeleportCause.UNKNOWN);
2022-12-07 17:00:00 +01:00
+ }
+
2023-03-14 17:30:00 +01:00
+ public boolean teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set, PlayerTeleportEvent.TeleportCause cause) { // CraftBukkit - Return event status
2021-11-21 23:00:00 +01:00
+ Player player = this.getCraftPlayer();
2014-11-25 22:32:16 +01:00
+ Location from = player.getLocation();
2015-07-20 23:31:49 +02:00
+
+ double x = d0;
+ double y = d1;
+ double z = d2;
+ float yaw = f;
+ float pitch = f1;
+
2021-11-21 23:00:00 +01:00
+ Location to = new Location(this.getCraftPlayer().getWorld(), x, y, z, yaw, pitch);
2019-07-16 02:15:59 +02:00
+ // SPIGOT-5171: Triggered on join
+ if (from.equals(to)) {
2023-03-14 17:30:00 +01:00
+ this.internalTeleport(d0, d1, d2, f, f1, set);
2021-05-25 00:24:08 +02:00
+ return false; // CraftBukkit - Return event status
2019-07-16 02:15:59 +02:00
+ }
+
2017-03-24 05:08:19 +01:00
+ PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause);
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
2016-05-30 21:29:10 +02:00
+ if (event.isCancelled() || !to.equals(event.getTo())) {
2015-07-20 23:31:49 +02:00
+ set.clear(); // Can't relative teleport
+ to = event.isCancelled() ? event.getFrom() : event.getTo();
+ d0 = to.getX();
+ d1 = to.getY();
+ d2 = to.getZ();
+ f = to.getYaw();
+ f1 = to.getPitch();
+ }
2016-03-01 03:52:34 +01:00
+
2023-03-14 17:30:00 +01:00
+ this.internalTeleport(d0, d1, d2, f, f1, set);
2021-05-25 00:24:08 +02:00
+ return event.isCancelled(); // CraftBukkit - Return event status
2022-09-12 11:27:14 +02:00
+ }
+
2022-02-28 16:00:00 +01:00
+ public void teleport(Location dest) {
2023-03-14 17:30:00 +01:00
+ internalTeleport(dest.getX(), dest.getY(), dest.getZ(), dest.getYaw(), dest.getPitch(), Collections.emptySet());
2023-02-16 09:45:56 +01:00
+ }
+
2023-03-14 17:30:00 +01:00
+ private void internalTeleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set) {
2016-11-17 02:41:03 +01:00
+ // CraftBukkit start
2014-11-25 22:32:16 +01:00
+ if (Float.isNaN(f)) {
+ f = 0;
+ }
+ if (Float.isNaN(f1)) {
+ f1 = 0;
+ }
2016-06-30 06:27:49 +02:00
+
2016-11-17 02:41:03 +01:00
+ this.justTeleported = true;
2014-11-25 22:32:16 +01:00
+ // CraftBukkit end
2023-03-14 17:30:00 +01:00
double d3 = set.contains(RelativeMovement.X) ? this.player.getX() : 0.0D;
double d4 = set.contains(RelativeMovement.Y) ? this.player.getY() : 0.0D;
double d5 = set.contains(RelativeMovement.Z) ? this.player.getZ() : 0.0D;
2023-12-27 21:56:30 +01:00
@@ -1071,6 +1426,14 @@
2021-06-11 07:00:00 +02:00
this.awaitingTeleport = 0;
2015-07-23 21:37:58 +02:00
}
+ // CraftBukkit start - update last location
2021-06-11 07:00:00 +02:00
+ this.lastPosX = this.awaitingPositionFromClient.x;
+ this.lastPosY = this.awaitingPositionFromClient.y;
+ this.lastPosZ = this.awaitingPositionFromClient.z;
2018-09-04 12:57:55 +02:00
+ this.lastYaw = f;
+ this.lastPitch = f1;
2015-07-23 21:37:58 +02:00
+ // CraftBukkit end
+
2021-06-11 07:00:00 +02:00
this.awaitingTeleportTime = this.tickCount;
2021-11-21 23:00:00 +01:00
this.player.absMoveTo(d0, d1, d2, f, f1);
2023-03-14 17:30:00 +01:00
this.player.connection.send(new PacketPlayOutPosition(d0 - d3, d1 - d4, d2 - d5, f - f2, f1 - f3, set, this.awaitingTeleport));
2023-12-27 21:56:30 +01:00
@@ -1079,6 +1442,7 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handlePlayerAction(PacketPlayInBlockDig packetplayinblockdig) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinblockdig, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
BlockPosition blockposition = packetplayinblockdig.getPos();
2014-11-25 22:32:16 +01:00
2021-11-21 23:00:00 +01:00
this.player.resetLastActionTime();
2023-12-27 21:56:30 +01:00
@@ -1089,14 +1453,46 @@
2019-05-27 22:30:00 +02:00
if (!this.player.isSpectator()) {
2021-11-21 23:00:00 +01:00
ItemStack itemstack = this.player.getItemInHand(EnumHand.OFF_HAND);
2019-05-27 22:30:00 +02:00
2021-11-21 23:00:00 +01:00
- this.player.setItemInHand(EnumHand.OFF_HAND, this.player.getItemInHand(EnumHand.MAIN_HAND));
- this.player.setItemInHand(EnumHand.MAIN_HAND, itemstack);
2019-05-27 22:30:00 +02:00
+ // CraftBukkit start - inspiration taken from DispenserRegistry (See SpigotCraft#394)
+ CraftItemStack mainHand = CraftItemStack.asCraftMirror(itemstack);
2021-11-21 23:00:00 +01:00
+ CraftItemStack offHand = CraftItemStack.asCraftMirror(this.player.getItemInHand(EnumHand.MAIN_HAND));
+ PlayerSwapHandItemsEvent swapItemsEvent = new PlayerSwapHandItemsEvent(getCraftPlayer(), mainHand.clone(), offHand.clone());
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(swapItemsEvent);
2019-05-27 22:30:00 +02:00
+ if (swapItemsEvent.isCancelled()) {
2014-11-25 22:32:16 +01:00
+ return;
+ }
2019-05-27 22:30:00 +02:00
+ if (swapItemsEvent.getOffHandItem().equals(offHand)) {
2021-11-21 23:00:00 +01:00
+ this.player.setItemInHand(EnumHand.OFF_HAND, this.player.getItemInHand(EnumHand.MAIN_HAND));
2019-05-27 22:30:00 +02:00
+ } else {
2021-11-21 23:00:00 +01:00
+ this.player.setItemInHand(EnumHand.OFF_HAND, CraftItemStack.asNMSCopy(swapItemsEvent.getOffHandItem()));
2019-05-27 22:30:00 +02:00
+ }
+ if (swapItemsEvent.getMainHandItem().equals(mainHand)) {
2021-11-21 23:00:00 +01:00
+ this.player.setItemInHand(EnumHand.MAIN_HAND, itemstack);
2019-05-27 22:30:00 +02:00
+ } else {
2021-11-21 23:00:00 +01:00
+ this.player.setItemInHand(EnumHand.MAIN_HAND, CraftItemStack.asNMSCopy(swapItemsEvent.getMainHandItem()));
2019-05-27 22:30:00 +02:00
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.player.stopUsingItem();
2019-05-27 22:30:00 +02:00
}
2014-11-25 22:32:16 +01:00
2019-05-27 22:30:00 +02:00
return;
case DROP_ITEM:
if (!this.player.isSpectator()) {
+ // limit how quickly items can be dropped
+ // If the ticks aren't the same then the count starts from 0 and we update the lastDropTick.
+ if (this.lastDropTick != MinecraftServer.currentTick) {
+ this.dropCount = 0;
+ this.lastDropTick = MinecraftServer.currentTick;
+ } else {
+ // Else we increment the drop count and check the amount.
+ this.dropCount++;
+ if (this.dropCount >= 20) {
2021-11-21 23:00:00 +01:00
+ LOGGER.warn(this.player.getScoreboardName() + " dropped their items too quickly!");
2019-05-27 22:30:00 +02:00
+ this.disconnect("You dropped your items too quickly (Hacking?)");
+ return;
2014-11-25 22:32:16 +01:00
+ }
2019-05-27 22:30:00 +02:00
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.player.drop(false);
2019-05-27 22:30:00 +02:00
}
2023-12-27 21:56:30 +01:00
@@ -1134,6 +1530,7 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleUseItemOn(PacketPlayInUseItem packetplayinuseitem) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinuseitem, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
2022-06-07 18:00:00 +02:00
this.player.connection.ackBlockChangesUpTo(packetplayinuseitem.getSequence());
2023-06-07 17:30:00 +02:00
WorldServer worldserver = this.player.serverLevel();
2021-11-21 23:00:00 +01:00
EnumHand enumhand = packetplayinuseitem.getHand();
2023-12-27 21:56:30 +01:00
@@ -1157,6 +1554,7 @@
2021-06-11 07:00:00 +02:00
2022-12-07 17:00:00 +01:00
if (blockposition.getY() < i) {
if (this.awaitingPositionFromClient == null && this.player.distanceToSqr((double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.5D, (double) blockposition.getZ() + 0.5D) < 64.0D && worldserver.mayInteract(this.player, blockposition)) {
+ this.player.stopUsingItem(); // CraftBukkit - SPIGOT-4706
EnumInteractionResult enuminteractionresult = this.player.gameMode.useItemOn(this.player, worldserver, itemstack, enumhand, movingobjectpositionblock);
2016-02-29 22:32:46 +01:00
2022-12-07 17:00:00 +01:00
if (enumdirection == EnumDirection.UP && !enuminteractionresult.consumesAction() && blockposition.getY() >= i - 1 && wasBlockPlacementAttempt(this.player, itemstack)) {
2023-12-27 21:56:30 +01:00
@@ -1185,6 +1583,7 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleUseItem(PacketPlayInBlockPlace packetplayinblockplace) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinblockplace, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
2022-06-07 18:00:00 +02:00
this.ackBlockChangesUpTo(packetplayinblockplace.getSequence());
2023-06-07 17:30:00 +02:00
WorldServer worldserver = this.player.serverLevel();
2021-11-21 23:00:00 +01:00
EnumHand enumhand = packetplayinblockplace.getHand();
2023-12-27 21:56:30 +01:00
@@ -1192,6 +1591,49 @@
2021-11-21 23:00:00 +01:00
this.player.resetLastActionTime();
2022-12-07 17:00:00 +01:00
if (!itemstack.isEmpty() && itemstack.isItemEnabled(worldserver.enabledFeatures())) {
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start
2014-12-07 12:04:22 +01:00
+ // Raytrace to look for 'rogue armswings'
2021-06-11 07:00:00 +02:00
+ float f1 = this.player.getXRot();
+ float f2 = this.player.getYRot();
2021-11-21 23:00:00 +01:00
+ double d0 = this.player.getX();
+ double d1 = this.player.getY() + (double) this.player.getEyeHeight();
+ double d2 = this.player.getZ();
2014-12-07 12:04:22 +01:00
+ Vec3D vec3d = new Vec3D(d0, d1, d2);
+
+ float f3 = MathHelper.cos(-f2 * 0.017453292F - 3.1415927F);
+ float f4 = MathHelper.sin(-f2 * 0.017453292F - 3.1415927F);
+ float f5 = -MathHelper.cos(-f1 * 0.017453292F);
+ float f6 = MathHelper.sin(-f1 * 0.017453292F);
+ float f7 = f4 * f5;
+ float f8 = f3 * f5;
2021-11-21 23:00:00 +01:00
+ double d3 = player.gameMode.getGameModeForPlayer()== EnumGamemode.CREATIVE ? 5.0D : 4.5D;
2014-12-07 12:04:22 +01:00
+ Vec3D vec3d1 = vec3d.add((double) f7 * d3, (double) f6 * d3, (double) f8 * d3);
2023-06-07 17:30:00 +02:00
+ MovingObjectPosition movingobjectposition = this.player.level().clip(new RayTrace(vec3d, vec3d1, RayTrace.BlockCollisionOption.OUTLINE, RayTrace.FluidCollisionOption.NONE, player));
2014-12-07 12:04:22 +01:00
+
2016-11-17 02:41:03 +01:00
+ boolean cancelled;
2019-04-23 04:00:00 +02:00
+ if (movingobjectposition == null || movingobjectposition.getType() != MovingObjectPosition.EnumMovingObjectType.BLOCK) {
2016-03-08 19:00:23 +01:00
+ org.bukkit.event.player.PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(this.player, Action.RIGHT_CLICK_AIR, itemstack, enumhand);
2014-12-09 13:20:44 +01:00
+ cancelled = event.useItemInHand() == Event.Result.DENY;
+ } else {
2020-09-12 00:36:10 +02:00
+ MovingObjectPositionBlock movingobjectpositionblock = (MovingObjectPositionBlock) movingobjectposition;
2023-06-07 17:30:00 +02:00
+ if (player.gameMode.firedInteract && player.gameMode.interactPosition.equals(movingobjectpositionblock.getBlockPos()) && player.gameMode.interactHand == enumhand && ItemStack.isSameItemSameTags(player.gameMode.interactItemStack, itemstack)) {
2021-06-11 07:00:00 +02:00
+ cancelled = player.gameMode.interactResult;
2020-09-03 22:39:01 +02:00
+ } else {
2023-06-17 06:31:40 +02:00
+ org.bukkit.event.player.PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, movingobjectpositionblock.getBlockPos(), movingobjectpositionblock.getDirection(), itemstack, true, enumhand, movingobjectpositionblock.getLocation());
2020-09-03 22:39:01 +02:00
+ cancelled = event.useItemInHand() == Event.Result.DENY;
+ }
2021-06-11 07:00:00 +02:00
+ player.gameMode.firedInteract = false;
2016-11-17 02:41:03 +01:00
+ }
+
2016-12-10 02:36:09 +01:00
+ if (cancelled) {
+ this.player.getBukkitEntity().updateInventory(); // SPIGOT-2524
2020-06-25 02:00:00 +02:00
+ return;
2014-11-25 22:32:16 +01:00
+ }
2022-09-12 11:27:14 +02:00
+ itemstack = this.player.getItemInHand(enumhand); // Update in case it was changed in the event
+ if (itemstack.isEmpty()) {
+ return;
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
EnumInteractionResult enuminteractionresult = this.player.gameMode.useItem(this.player, worldserver, itemstack, enumhand);
2016-02-29 22:32:46 +01:00
2021-11-21 23:00:00 +01:00
if (enuminteractionresult.shouldSwing()) {
2023-12-27 21:56:30 +01:00
@@ -1212,7 +1654,7 @@
2021-11-21 23:00:00 +01:00
Entity entity = packetplayinspectate.getEntity(worldserver);
2018-07-15 02:00:00 +02:00
2019-04-23 04:00:00 +02:00
if (entity != null) {
2021-11-21 23:00:00 +01:00
- this.player.teleportTo(worldserver, entity.getX(), entity.getY(), entity.getZ(), entity.getYRot(), entity.getXRot());
+ this.player.teleportTo(worldserver, entity.getX(), entity.getY(), entity.getZ(), entity.getYRot(), entity.getXRot(), org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.SPECTATE); // CraftBukkit
2019-04-23 04:00:00 +02:00
return;
}
2015-01-06 21:21:14 +01:00
}
2023-12-27 21:56:30 +01:00
@@ -1235,6 +1677,13 @@
2016-02-29 22:32:46 +01:00
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void onDisconnect(IChatBaseComponent ichatbasecomponent) {
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Rarely it would send a disconnect line twice
+ if (this.processedDisconnect) {
+ return;
+ } else {
+ this.processedDisconnect = true;
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
PlayerConnection.LOGGER.info("{} lost connection: {}", this.player.getName().getString(), ichatbasecomponent.getString());
2023-09-21 18:40:00 +02:00
this.removePlayerFromWorld();
super.onDisconnect(ichatbasecomponent);
2023-12-27 21:56:30 +01:00
@@ -1242,10 +1691,18 @@
2023-09-21 18:40:00 +02:00
private void removePlayerFromWorld() {
this.chatMessageChain.close();
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Replace vanilla quit message handling with our own.
+ /*
2021-11-21 23:00:00 +01:00
this.server.invalidateStatus();
2022-07-27 20:00:00 +02:00
this.server.getPlayerList().broadcastSystemMessage(IChatBaseComponent.translatable("multiplayer.player.left", this.player.getDisplayName()).withStyle(EnumChatFormat.YELLOW), false);
2014-11-25 22:32:16 +01:00
+ */
2015-02-26 23:41:06 +01:00
+
2021-11-21 23:00:00 +01:00
this.player.disconnect();
- this.server.getPlayerList().remove(this.player);
+ String quitMessage = this.server.getPlayerList().remove(this.player);
2014-11-25 22:32:16 +01:00
+ if ((quitMessage != null) && (quitMessage.length() > 0)) {
2021-11-21 23:00:00 +01:00
+ this.server.getPlayerList().broadcastMessage(CraftChatMessage.fromString(quitMessage));
2014-11-25 22:32:16 +01:00
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.player.getTextFilter().leave();
2021-06-11 07:00:00 +02:00
}
2015-02-26 23:41:06 +01:00
2023-12-27 21:56:30 +01:00
@@ -1260,7 +1717,16 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleSetCarriedItem(PacketPlayInHeldItemSlot packetplayinhelditemslot) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinhelditemslot, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
if (packetplayinhelditemslot.getSlot() >= 0 && packetplayinhelditemslot.getSlot() < PlayerInventory.getSelectionSize()) {
+ PlayerItemHeldEvent event = new PlayerItemHeldEvent(this.getCraftPlayer(), this.player.getInventory().selected, packetplayinhelditemslot.getSlot());
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+ if (event.isCancelled()) {
2021-11-21 23:00:00 +01:00
+ this.send(new PacketPlayOutHeldItemSlot(this.player.getInventory().selected));
+ this.player.resetLastActionTime();
2014-11-25 22:32:16 +01:00
+ return;
+ }
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
if (this.player.getInventory().selected != packetplayinhelditemslot.getSlot() && this.player.getUsedItemHand() == EnumHand.MAIN_HAND) {
this.player.stopUsingItem();
2020-06-25 02:00:00 +02:00
}
2023-12-27 21:56:30 +01:00
@@ -1269,18 +1735,25 @@
2021-11-21 23:00:00 +01:00
this.player.resetLastActionTime();
2014-11-25 22:32:16 +01:00
} else {
2021-11-21 23:00:00 +01:00
PlayerConnection.LOGGER.warn("{} tried to set an invalid carried item", this.player.getName().getString());
2019-01-05 06:21:07 +01:00
+ this.disconnect("Invalid hotbar selection (Hacking?)"); // CraftBukkit
2014-11-25 22:32:16 +01:00
}
}
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleChat(PacketPlayInChat packetplayinchat) {
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - async chat
2017-11-08 00:36:11 +01:00
+ // SPIGOT-3638
2021-06-11 07:00:00 +02:00
+ if (this.server.isStopped()) {
2017-11-08 00:36:11 +01:00
+ return;
+ }
2014-11-25 22:32:16 +01:00
+ // CraftBukkit end
2022-07-27 20:00:00 +02:00
if (isChatMessageIllegal(packetplayinchat.message())) {
2022-06-07 18:00:00 +02:00
this.disconnect(IChatBaseComponent.translatable("multiplayer.disconnect.illegal_characters"));
2020-11-02 21:00:00 +01:00
} else {
2023-12-05 17:40:00 +01:00
Optional<LastSeenMessages> optional = this.tryHandleChat(packetplayinchat.lastSeenMessages());
2022-12-07 17:00:00 +01:00
if (optional.isPresent()) {
2022-07-27 20:00:00 +02:00
- this.server.submit(() -> {
+ // this.server.submit(() -> { // CraftBukkit - async chat
2022-12-07 17:00:00 +01:00
PlayerChatMessage playerchatmessage;
2022-07-27 20:00:00 +02:00
2022-12-07 17:00:00 +01:00
try {
2023-12-27 21:56:30 +01:00
@@ -1290,7 +1763,7 @@
2023-12-05 17:40:00 +01:00
return;
}
2022-07-27 20:00:00 +02:00
2023-12-05 17:40:00 +01:00
- CompletableFuture<FilteredText> completablefuture = this.filterTextPacket(playerchatmessage.signedContent());
+ CompletableFuture<FilteredText> completablefuture = this.filterTextPacket(playerchatmessage.signedContent()).thenApplyAsync(Function.identity(), this.server.chatExecutor); // CraftBukkit - async chat
IChatBaseComponent ichatbasecomponent = this.server.getChatDecorator().decorate(this.player, playerchatmessage.decoratedContent());
this.chatMessageChain.append(completablefuture, (filteredtext) -> {
2023-12-27 21:56:30 +01:00
@@ -1298,7 +1771,7 @@
2023-12-05 17:40:00 +01:00
this.broadcastChatMessage(playerchatmessage1);
2022-12-07 17:00:00 +01:00
});
2022-06-07 18:00:00 +02:00
- });
2022-07-27 20:00:00 +02:00
+ // }); // CraftBukkit - async chat
2022-06-07 18:00:00 +02:00
}
2020-11-02 21:00:00 +01:00
}
2023-12-27 21:56:30 +01:00
@@ -1313,6 +1786,12 @@
2023-05-10 23:03:53 +02:00
if (optional.isPresent()) {
this.server.submit(() -> {
+ // CraftBukkit start - SPIGOT-7346: Prevent disconnected players from executing commands
+ if (player.hasDisconnected()) {
+ return;
+ }
+ // CraftBukkit end
+
this.performChatCommand(serverboundchatcommandpacket, (LastSeenMessages) optional.get());
this.detectRateSpam();
});
2023-12-27 21:56:30 +01:00
@@ -1322,12 +1801,25 @@
2020-11-02 21:00:00 +01:00
}
2022-12-07 17:00:00 +01:00
private void performChatCommand(ServerboundChatCommandPacket serverboundchatcommandpacket, LastSeenMessages lastseenmessages) {
- ParseResults parseresults = this.parseCommand(serverboundchatcommandpacket.command());
2022-07-27 20:00:00 +02:00
+ // CraftBukkit start
+ String command = "/" + serverboundchatcommandpacket.command();
+ PlayerConnection.LOGGER.info(this.player.getScoreboardName() + " issued server command: " + command);
+
+ PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(getCraftPlayer(), command, new LazyPlayerSet(server));
+ this.cserver.getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ return;
+ }
+ command = event.getMessage().substring(1);
+
2022-12-07 17:00:00 +01:00
+ ParseResults parseresults = this.parseCommand(command);
2022-07-27 20:00:00 +02:00
+ // CraftBukkit end
2022-06-07 18:00:00 +02:00
2022-12-07 17:00:00 +01:00
Map map;
try {
- map = this.collectSignedArguments(serverboundchatcommandpacket, SignableCommand.of(parseresults), lastseenmessages);
+ map = (serverboundchatcommandpacket.command().equals(command)) ? this.collectSignedArguments(serverboundchatcommandpacket, SignableCommand.of(parseresults), lastseenmessages) : Collections.emptyMap(); // CraftBukkit
} catch (SignedMessageChain.a signedmessagechain_a) {
this.handleMessageDecodeFailure(signedmessagechain_a);
return;
2023-12-27 21:56:30 +01:00
@@ -1335,10 +1827,10 @@
2022-12-07 17:00:00 +01:00
CommandSigningContext.a commandsigningcontext_a = new CommandSigningContext.a(map);
- parseresults = CommandDispatcher.mapSource(parseresults, (commandlistenerwrapper) -> {
+ parseresults = CommandDispatcher.<CommandListenerWrapper>mapSource(parseresults, (commandlistenerwrapper) -> { // CraftBukkit - decompile error
2023-09-21 18:40:00 +02:00
return commandlistenerwrapper.withSigningContext(commandsigningcontext_a, this.chatMessageChain);
2022-12-07 17:00:00 +01:00
});
- this.server.getCommands().performCommand(parseresults, serverboundchatcommandpacket.command());
+ this.server.getCommands().performCommand(parseresults, command); // CraftBukkit
}
private void handleMessageDecodeFailure(SignedMessageChain.a signedmessagechain_a) {
2023-12-27 21:56:30 +01:00
@@ -1375,7 +1867,7 @@
2023-12-05 17:40:00 +01:00
private Optional<LastSeenMessages> tryHandleChat(LastSeenMessages.b lastseenmessages_b) {
Optional<LastSeenMessages> optional = this.unpackAndApplyLastSeen(lastseenmessages_b);
- if (this.player.getChatVisibility() == EnumChatVisibility.HIDDEN) {
+ if (this.player.isRemoved() || this.player.getChatVisibility() == EnumChatVisibility.HIDDEN) { // CraftBukkit - dead men tell no tales
this.send(new ClientboundSystemChatPacket(IChatBaseComponent.translatable("chat.disabled.options").withStyle(EnumChatFormat.RED), false));
return Optional.empty();
2022-06-07 18:00:00 +02:00
} else {
2023-12-27 21:56:30 +01:00
@@ -1409,6 +1901,116 @@
2022-07-27 20:00:00 +02:00
return false;
2015-02-26 23:41:06 +01:00
}
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - add method
2022-07-27 20:00:00 +02:00
+ public void chat(String s, PlayerChatMessage original, boolean async) {
2021-11-21 23:00:00 +01:00
+ if (s.isEmpty() || this.player.getChatVisibility() == EnumChatVisibility.HIDDEN) {
2014-11-25 22:32:16 +01:00
+ return;
+ }
2022-12-07 17:00:00 +01:00
+ OutgoingChatMessage outgoing = OutgoingChatMessage.create(original);
2014-11-25 22:32:16 +01:00
+
+ if (!async && s.startsWith("/")) {
+ this.handleCommand(s);
2021-11-21 23:00:00 +01:00
+ } else if (this.player.getChatVisibility() == EnumChatVisibility.SYSTEM) {
2014-11-25 22:32:16 +01:00
+ // Do nothing, this is coming from a plugin
+ } else {
2021-11-21 23:00:00 +01:00
+ Player player = this.getCraftPlayer();
2021-06-11 07:00:00 +02:00
+ AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(async, player, s, new LazyPlayerSet(server));
2022-07-27 20:00:00 +02:00
+ String originalFormat = event.getFormat(), originalMessage = event.getMessage();
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ if (PlayerChatEvent.getHandlerList().getRegisteredListeners().length != 0) {
+ // Evil plugins still listening to deprecated event
+ final PlayerChatEvent queueEvent = new PlayerChatEvent(player, event.getMessage(), event.getFormat(), event.getRecipients());
+ queueEvent.setCancelled(event.isCancelled());
+ Waitable waitable = new Waitable() {
+ @Override
+ protected Object evaluate() {
+ org.bukkit.Bukkit.getPluginManager().callEvent(queueEvent);
2014-12-07 12:04:22 +01:00
+
2014-11-25 22:32:16 +01:00
+ if (queueEvent.isCancelled()) {
+ return null;
+ }
+
+ String message = String.format(queueEvent.getFormat(), queueEvent.getPlayer().getDisplayName(), queueEvent.getMessage());
+ if (((LazyPlayerSet) queueEvent.getRecipients()).isLazy()) {
2022-07-27 20:00:00 +02:00
+ if (originalFormat.equals(queueEvent.getFormat()) && originalMessage.equals(queueEvent.getMessage()) && queueEvent.getPlayer().getName().equalsIgnoreCase(queueEvent.getPlayer().getDisplayName())) {
+ PlayerConnection.this.server.getPlayerList().broadcastChatMessage(original, PlayerConnection.this.player, ChatMessageType.bind(ChatMessageType.CHAT, (Entity) PlayerConnection.this.player));
+ return null;
+ }
+
2021-06-11 07:00:00 +02:00
+ for (EntityPlayer recipient : server.getPlayerList().players) {
2021-11-21 23:00:00 +01:00
+ recipient.getBukkitEntity().sendMessage(PlayerConnection.this.player.getUUID(), message);
2014-11-25 22:32:16 +01:00
+ }
+ } else {
+ for (Player player : queueEvent.getRecipients()) {
2021-11-21 23:00:00 +01:00
+ player.sendMessage(PlayerConnection.this.player.getUUID(), message);
2014-11-25 22:32:16 +01:00
+ }
+ }
2022-07-27 20:00:00 +02:00
+ PlayerConnection.this.server.console.sendMessage(message);
+
2014-11-25 22:32:16 +01:00
+ return null;
+ }};
+ if (async) {
2021-06-11 07:00:00 +02:00
+ server.processQueue.add(waitable);
2014-11-25 22:32:16 +01:00
+ } else {
+ waitable.run();
+ }
+ try {
+ waitable.get();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt(); // This is proper habit for java. If we aren't handling it, pass it on!
+ } catch (ExecutionException e) {
+ throw new RuntimeException("Exception processing chat event", e.getCause());
+ }
+ } else {
+ if (event.isCancelled()) {
+ return;
+ }
2015-02-26 23:41:06 +01:00
+
2014-11-25 22:32:16 +01:00
+ s = String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage());
+ if (((LazyPlayerSet) event.getRecipients()).isLazy()) {
2022-07-27 20:00:00 +02:00
+ if (originalFormat.equals(event.getFormat()) && originalMessage.equals(event.getMessage()) && event.getPlayer().getName().equalsIgnoreCase(event.getPlayer().getDisplayName())) {
+ PlayerConnection.this.server.getPlayerList().broadcastChatMessage(original, PlayerConnection.this.player, ChatMessageType.bind(ChatMessageType.CHAT, (Entity) PlayerConnection.this.player));
+ return;
+ }
+
2021-06-11 07:00:00 +02:00
+ for (EntityPlayer recipient : server.getPlayerList().players) {
2021-11-21 23:00:00 +01:00
+ recipient.getBukkitEntity().sendMessage(PlayerConnection.this.player.getUUID(), s);
2014-11-25 22:32:16 +01:00
+ }
+ } else {
+ for (Player recipient : event.getRecipients()) {
2021-11-21 23:00:00 +01:00
+ recipient.sendMessage(PlayerConnection.this.player.getUUID(), s);
2014-11-25 22:32:16 +01:00
+ }
+ }
2022-07-27 20:00:00 +02:00
+ server.console.sendMessage(s);
2014-11-25 22:32:16 +01:00
+ }
2015-02-26 23:41:06 +01:00
+ }
+ }
+
2022-06-07 18:00:00 +02:00
+ private void handleCommand(String s) {
2021-11-21 23:00:00 +01:00
+ this.LOGGER.info(this.player.getScoreboardName() + " issued server command: " + s);
2014-11-25 22:32:16 +01:00
+
2021-11-21 23:00:00 +01:00
+ CraftPlayer player = this.getCraftPlayer();
2014-11-25 22:32:16 +01:00
+
2021-06-11 07:00:00 +02:00
+ PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet(server));
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ if (event.isCancelled()) {
+ return;
+ }
+
+ try {
2021-06-11 07:00:00 +02:00
+ if (this.cserver.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) {
2014-11-25 22:32:16 +01:00
+ return;
+ }
+ } catch (org.bukkit.command.CommandException ex) {
+ player.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
+ java.util.logging.Logger.getLogger(PlayerConnection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ return;
+ }
2022-06-07 18:00:00 +02:00
+ }
+ // CraftBukkit end
+
2022-12-07 17:00:00 +01:00
private PlayerChatMessage getSignedMessage(PacketPlayInChat packetplayinchat, LastSeenMessages lastseenmessages) throws SignedMessageChain.a {
SignedMessageBody signedmessagebody = new SignedMessageBody(packetplayinchat.message(), packetplayinchat.timeStamp(), packetplayinchat.salt(), lastseenmessages);
2023-12-27 21:56:30 +01:00
@@ -1416,13 +2018,33 @@
2022-07-27 20:00:00 +02:00
}
private void broadcastChatMessage(PlayerChatMessage playerchatmessage) {
- this.server.getPlayerList().broadcastChatMessage(playerchatmessage, this.player, ChatMessageType.bind(ChatMessageType.CHAT, (Entity) this.player));
+ // CraftBukkit start
2022-12-07 17:00:00 +01:00
+ String s = playerchatmessage.signedContent();
2022-07-27 20:00:00 +02:00
+ if (s.isEmpty()) {
+ LOGGER.warn(this.player.getScoreboardName() + " tried to send an empty message");
+ } else if (getCraftPlayer().isConversing()) {
+ final String conversationInput = s;
+ this.server.processQueue.add(new Runnable() {
+ @Override
+ public void run() {
+ getCraftPlayer().acceptConversationInput(conversationInput);
+ }
+ });
+ } else if (this.player.getChatVisibility() == EnumChatVisibility.SYSTEM) { // Re-add "Command Only" flag check
+ this.send(new ClientboundSystemChatPacket(IChatBaseComponent.translatable("chat.cannotSend").withStyle(EnumChatFormat.RED), false));
+ } else {
+ this.chat(s, playerchatmessage, true);
+ }
+ // this.server.getPlayerList().broadcastChatMessage(playerchatmessage, this.player, ChatMessageType.bind(ChatMessageType.CHAT, (Entity) this.player));
+ // CraftBukkit end
this.detectRateSpam();
}
private void detectRateSpam() {
- this.chatSpamTickCount += 20;
- if (this.chatSpamTickCount > 200 && !this.server.getPlayerList().isOp(this.player.getGameProfile())) {
+ // CraftBukkit start - replaced with thread safe throttle
2022-06-07 18:00:00 +02:00
+ // this.chatSpamTickCount += 20;
+ if (this.chatSpamTickCount.addAndGet(20) > 200 && !this.server.getPlayerList().isOp(this.player.getGameProfile())) {
+ // CraftBukkit end
2022-07-27 20:00:00 +02:00
this.disconnect(IChatBaseComponent.translatable("disconnect.spam"));
2022-06-07 18:00:00 +02:00
}
2023-12-27 21:56:30 +01:00
@@ -1444,13 +2066,62 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleAnimate(PacketPlayInArmAnimation packetplayinarmanimation) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinarmanimation, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
this.player.resetLastActionTime();
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Raytrace to look for 'rogue armswings'
2021-06-11 07:00:00 +02:00
+ float f1 = this.player.getXRot();
+ float f2 = this.player.getYRot();
2021-11-21 23:00:00 +01:00
+ double d0 = this.player.getX();
+ double d1 = this.player.getY() + (double) this.player.getEyeHeight();
+ double d2 = this.player.getZ();
2023-06-07 17:30:00 +02:00
+ Location origin = new Location(this.player.level().getWorld(), d0, d1, d2, f2, f1);
2022-08-30 13:50:53 +02:00
+
+ double d3 = player.gameMode.getGameModeForPlayer() == EnumGamemode.CREATIVE ? 5.0D : 4.5D;
+ // SPIGOT-5607: Only call interact event if no block or entity is being clicked. Use bukkit ray trace method, because it handles blocks and entities at the same time
2023-07-18 13:23:52 +02:00
+ // SPIGOT-7429: Make sure to call PlayerInteractEvent for spectators and non-pickable entities
+ org.bukkit.util.RayTraceResult result = this.player.level().getWorld().rayTrace(origin, origin.getDirection(), d3, org.bukkit.FluidCollisionMode.NEVER, false, 0.1, entity -> {
+ Entity handle = ((CraftEntity) entity).getHandle();
+ return entity != this.player.getBukkitEntity() && this.player.getBukkitEntity().canSee(entity) && !handle.isSpectator() && handle.isPickable() && !handle.isPassengerOfSameVehicle(player);
+ });
2022-08-30 13:50:53 +02:00
+ if (result == null) {
2021-11-21 23:00:00 +01:00
+ CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_AIR, this.player.getInventory().getSelected(), EnumHand.MAIN_HAND);
2014-11-25 22:32:16 +01:00
+ }
+
+ // Arm swing animation
2022-06-09 13:30:25 +02:00
+ PlayerAnimationEvent event = new PlayerAnimationEvent(this.getCraftPlayer(), (packetplayinarmanimation.getHand() == EnumHand.MAIN_HAND) ? PlayerAnimationType.ARM_SWING : PlayerAnimationType.OFF_ARM_SWING);
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ if (event.isCancelled()) return;
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.player.swing(packetplayinarmanimation.getHand());
2014-11-25 22:32:16 +01:00
}
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handlePlayerCommand(PacketPlayInEntityAction packetplayinentityaction) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinentityaction, this, this.player.serverLevel());
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
+ if (this.player.isRemoved()) return;
2021-11-21 23:00:00 +01:00
+ switch (packetplayinentityaction.getAction()) {
2019-12-10 23:00:00 +01:00
+ case PRESS_SHIFT_KEY:
+ case RELEASE_SHIFT_KEY:
2021-11-21 23:00:00 +01:00
+ PlayerToggleSneakEvent event = new PlayerToggleSneakEvent(this.getCraftPlayer(), packetplayinentityaction.getAction() == PacketPlayInEntityAction.EnumPlayerAction.PRESS_SHIFT_KEY);
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ if (event.isCancelled()) {
+ return;
+ }
+ break;
+ case START_SPRINTING:
2015-02-26 23:41:06 +01:00
+ case STOP_SPRINTING:
2021-11-21 23:00:00 +01:00
+ PlayerToggleSprintEvent e2 = new PlayerToggleSprintEvent(this.getCraftPlayer(), packetplayinentityaction.getAction() == PacketPlayInEntityAction.EnumPlayerAction.START_SPRINTING);
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(e2);
2014-11-25 22:32:16 +01:00
+
+ if (e2.isCancelled()) {
+ return;
+ }
+ break;
+ }
2015-02-26 23:41:06 +01:00
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.player.resetLastActionTime();
2023-03-14 17:30:00 +01:00
Entity entity;
2016-02-29 22:32:46 +01:00
IJumpable ijumpable;
2023-12-27 21:56:30 +01:00
@@ -1532,6 +2203,12 @@
2023-02-16 09:45:56 +01:00
}
public void sendPlayerChatMessage(PlayerChatMessage playerchatmessage, ChatMessageType.a chatmessagetype_a) {
+ // CraftBukkit start - SPIGOT-7262: if hidden we have to send as disguised message. Query whether we should send at all (but changing this may not be expected).
+ if (!getCraftPlayer().canSee(playerchatmessage.link().sender())) {
+ sendDisguisedChatMessage(playerchatmessage.decoratedContent(), chatmessagetype_a);
+ return;
+ }
+ // CraftBukkit end
2023-06-07 17:30:00 +02:00
this.send(new ClientboundPlayerChatPacket(playerchatmessage.link().sender(), playerchatmessage.link().index(), playerchatmessage.signature(), playerchatmessage.signedBody().pack(this.messageSignatureCache), playerchatmessage.unsignedContent(), playerchatmessage.filterMask(), chatmessagetype_a.toNetwork(this.player.level().registryAccess())));
2023-02-16 09:45:56 +01:00
this.addPendingMessage(playerchatmessage);
}
2023-12-27 21:56:30 +01:00
@@ -1558,6 +2235,7 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleInteract(PacketPlayInUseEntity packetplayinuseentity) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinuseentity, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
2023-06-07 17:30:00 +02:00
final WorldServer worldserver = this.player.serverLevel();
2021-11-21 23:00:00 +01:00
final Entity entity = packetplayinuseentity.getTarget(worldserver);
2023-12-27 21:56:30 +01:00
@@ -1572,13 +2250,51 @@
2021-11-21 23:00:00 +01:00
2023-03-14 17:30:00 +01:00
if (axisalignedbb.distanceToSqr(this.player.getEyePosition()) < PlayerConnection.MAX_INTERACTION_DISTANCE) {
2021-11-21 23:00:00 +01:00
packetplayinuseentity.dispatch(new PacketPlayInUseEntity.c() {
- private void performInteraction(EnumHand enumhand, PlayerConnection.a playerconnection_a) {
+ private void performInteraction(EnumHand enumhand, PlayerConnection.a playerconnection_a, PlayerInteractEntityEvent event) { // CraftBukkit
2022-12-07 17:00:00 +01:00
ItemStack itemstack = PlayerConnection.this.player.getItemInHand(enumhand);
if (itemstack.isItemEnabled(worldserver.enabledFeatures())) {
ItemStack itemstack1 = itemstack.copy();
+ // CraftBukkit start
+ ItemStack itemInHand = PlayerConnection.this.player.getItemInHand(enumhand);
+ boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && entity instanceof EntityInsentient;
+ Item origItem = player.getInventory().getSelected() == null ? null : player.getInventory().getSelected().getItem();
2021-06-11 07:00:00 +02:00
+
2022-12-07 17:00:00 +01:00
+ cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
2022-12-07 17:00:00 +01:00
+ // Entity in bucket - SPIGOT-4048 and SPIGOT-6859a
+ if ((entity instanceof Bucketable && entity instanceof EntityLiving && origItem != null && origItem.asItem() == Items.WATER_BUCKET) && (event.isCancelled() || player.getInventory().getSelected() == null || player.getInventory().getSelected().getItem() != origItem)) {
+ send(new PacketPlayOutSpawnEntity(entity));
2022-08-14 03:27:22 +02:00
+ player.containerMenu.sendAllDataToRemote();
+ }
2014-11-25 22:32:16 +01:00
+
2022-12-07 17:00:00 +01:00
+ if (triggerLeashUpdate && (event.isCancelled() || player.getInventory().getSelected() == null || player.getInventory().getSelected().getItem() != origItem)) {
+ // Refresh the current leash state
+ send(new PacketPlayOutAttachEntity(entity, ((EntityInsentient) entity).getLeashHolder()));
+ }
+
+ if (event.isCancelled() || player.getInventory().getSelected() == null || player.getInventory().getSelected().getItem() != origItem) {
+ // Refresh the current entity metadata
+ entity.getEntityData().refresh(player);
+ // SPIGOT-7136 - Allays
+ if (entity instanceof Allay) {
+ send(new PacketPlayOutEntityEquipment(entity.getId(), Arrays.stream(EnumItemSlot.values()).map((slot) -> Pair.of(slot, ((EntityLiving) entity).getItemBySlot(slot).copy())).collect(Collectors.toList())));
+ player.containerMenu.sendAllDataToRemote();
+ }
+ }
2014-11-25 22:32:16 +01:00
+
2022-12-07 17:00:00 +01:00
+ if (event.isCancelled()) {
+ return;
+ }
+ // CraftBukkit end
EnumInteractionResult enuminteractionresult = playerconnection_a.run(PlayerConnection.this.player, entity, enumhand);
2021-06-11 07:00:00 +02:00
2022-12-07 17:00:00 +01:00
+ // CraftBukkit start
+ if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
+ player.containerMenu.sendAllDataToRemote();
+ }
+ // CraftBukkit end
2016-02-29 22:32:46 +01:00
+
2022-12-07 17:00:00 +01:00
if (enuminteractionresult.consumesAction()) {
CriterionTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(PlayerConnection.this.player, itemstack1, entity);
if (enuminteractionresult.shouldSwing()) {
2023-12-27 21:56:30 +01:00
@@ -1591,23 +2307,29 @@
2021-06-11 07:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void onInteraction(EnumHand enumhand) {
- this.performInteraction(enumhand, EntityHuman::interactOn);
2022-12-07 17:00:00 +01:00
+ this.performInteraction(enumhand, EntityHuman::interactOn, new PlayerInteractEntityEvent(getCraftPlayer(), entity.getBukkitEntity(), (enumhand == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND)); // CraftBukkit
2014-11-25 22:32:16 +01:00
}
2021-06-11 07:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void onInteraction(EnumHand enumhand, Vec3D vec3d) {
this.performInteraction(enumhand, (entityplayer, entity1, enumhand1) -> {
return entity1.interactAt(entityplayer, vec3d, enumhand1);
2021-06-11 07:00:00 +02:00
- });
2021-11-21 23:00:00 +01:00
+ }, new PlayerInteractAtEntityEvent(getCraftPlayer(), entity.getBukkitEntity(), new org.bukkit.util.Vector(vec3d.x, vec3d.y, vec3d.z), (enumhand == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND)); // CraftBukkit
2021-06-11 07:00:00 +02:00
}
2020-06-25 02:00:00 +02:00
2021-06-11 07:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void onAttack() {
2021-06-11 07:00:00 +02:00
- if (!(entity instanceof EntityItem) && !(entity instanceof EntityExperienceOrb) && !(entity instanceof EntityArrow) && entity != PlayerConnection.this.player) {
2022-12-07 17:00:00 +01:00
+ // CraftBukkit
2021-06-11 07:00:00 +02:00
+ if (!(entity instanceof EntityItem) && !(entity instanceof EntityExperienceOrb) && !(entity instanceof EntityArrow) && (entity != PlayerConnection.this.player || player.isSpectator())) {
2022-12-07 17:00:00 +01:00
ItemStack itemstack = PlayerConnection.this.player.getItemInHand(EnumHand.MAIN_HAND);
if (itemstack.isItemEnabled(worldserver.enabledFeatures())) {
PlayerConnection.this.player.attack(entity);
+ // CraftBukkit start
+ if (!itemstack.isEmpty() && itemstack.getCount() <= -1) {
+ player.containerMenu.sendAllDataToRemote();
+ }
+ // CraftBukkit end
}
2021-06-11 07:00:00 +02:00
} else {
2022-06-07 18:00:00 +02:00
PlayerConnection.this.disconnect(IChatBaseComponent.translatable("multiplayer.disconnect.invalid_entity_attacked"));
2024-02-04 00:54:20 +01:00
@@ -1630,17 +2352,17 @@
2023-04-10 00:26:45 +02:00
case PERFORM_RESPAWN:
if (this.player.wonGame) {
this.player.wonGame = false;
- this.player = this.server.getPlayerList().respawn(this.player, true);
+ this.player = this.server.getPlayerList().respawn(this.player, true, RespawnReason.END_PORTAL);
CriterionTriggers.CHANGED_DIMENSION.trigger(this.player, World.END, World.OVERWORLD);
} else {
if (this.player.getHealth() > 0.0F) {
return;
}
- this.player = this.server.getPlayerList().respawn(this.player, false);
+ this.player = this.server.getPlayerList().respawn(this.player, false, RespawnReason.DEATH);
if (this.server.isHardcore()) {
this.player.setGameMode(EnumGamemode.SPECTATOR);
2024-02-04 00:54:20 +01:00
- ((GameRules.GameRuleBoolean) this.player.level().getGameRules().getRule(GameRules.RULE_SPECTATORSGENERATECHUNKS)).set(false, this.server);
+ ((GameRules.GameRuleBoolean) this.player.level().getGameRules().getRule(GameRules.RULE_SPECTATORSGENERATECHUNKS)).set(false, this.player.serverLevel()); // CraftBukkit - per-world
}
}
break;
2023-12-27 21:56:30 +01:00
@@ -1653,15 +2375,21 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleContainerClose(PacketPlayInCloseWindow packetplayinclosewindow) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinclosewindow, this, this.player.serverLevel());
2015-02-26 23:41:06 +01:00
+
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
2014-11-25 22:32:16 +01:00
+ CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
2015-02-26 23:41:06 +01:00
+
2021-11-21 23:00:00 +01:00
this.player.doCloseContainer();
2014-11-25 22:32:16 +01:00
}
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleContainerClick(PacketPlayInWindowClick packetplayinwindowclick) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinwindowclick, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
this.player.resetLastActionTime();
- if (this.player.containerMenu.containerId == packetplayinwindowclick.getContainerId()) {
2015-05-05 22:43:47 +02:00
- if (this.player.isSpectator()) {
2021-11-21 23:00:00 +01:00
+ if (this.player.containerMenu.containerId == packetplayinwindowclick.getContainerId() && this.player.containerMenu.stillValid(this.player)) { // CraftBukkit
2015-05-05 22:43:47 +02:00
+ boolean cancelled = this.player.isSpectator(); // CraftBukkit - see below if
2016-02-29 22:32:46 +01:00
+ if (false/*this.player.isSpectator()*/) { // CraftBukkit
2021-11-21 23:00:00 +01:00
this.player.containerMenu.sendAllDataToRemote();
2022-07-27 20:00:00 +02:00
} else if (!this.player.containerMenu.stillValid(this.player)) {
PlayerConnection.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, this.player.containerMenu);
2023-12-27 21:56:30 +01:00
@@ -1674,7 +2402,284 @@
2022-02-28 16:00:00 +01:00
boolean flag = packetplayinwindowclick.getStateId() != this.player.containerMenu.getStateId();
this.player.containerMenu.suppressRemoteUpdates();
- this.player.containerMenu.clicked(i, packetplayinwindowclick.getButtonNum(), packetplayinwindowclick.getClickType(), this.player);
+ // CraftBukkit start - Call InventoryClickEvent
+ if (packetplayinwindowclick.getSlotNum() < -1 && packetplayinwindowclick.getSlotNum() != -999) {
+ return;
+ }
2019-05-27 22:30:00 +02:00
+
2022-02-28 16:00:00 +01:00
+ InventoryView inventory = this.player.containerMenu.getBukkitView();
+ SlotType type = inventory.getSlotType(packetplayinwindowclick.getSlotNum());
2020-09-03 22:39:01 +02:00
+
2022-02-28 16:00:00 +01:00
+ InventoryClickEvent event;
+ ClickType click = ClickType.UNKNOWN;
+ InventoryAction action = InventoryAction.UNKNOWN;
2014-11-25 22:32:16 +01:00
+
2022-02-28 16:00:00 +01:00
+ ItemStack itemstack = ItemStack.EMPTY;
2014-11-25 22:32:16 +01:00
+
2022-02-28 16:00:00 +01:00
+ switch (packetplayinwindowclick.getClickType()) {
+ case PICKUP:
+ if (packetplayinwindowclick.getButtonNum() == 0) {
+ click = ClickType.LEFT;
+ } else if (packetplayinwindowclick.getButtonNum() == 1) {
+ click = ClickType.RIGHT;
+ }
+ if (packetplayinwindowclick.getButtonNum() == 0 || packetplayinwindowclick.getButtonNum() == 1) {
+ action = InventoryAction.NOTHING; // Don't want to repeat ourselves
+ if (packetplayinwindowclick.getSlotNum() == -999) {
+ if (!player.containerMenu.getCarried().isEmpty()) {
+ action = packetplayinwindowclick.getButtonNum() == 0 ? InventoryAction.DROP_ALL_CURSOR : InventoryAction.DROP_ONE_CURSOR;
+ }
+ } else if (packetplayinwindowclick.getSlotNum() < 0) {
+ action = InventoryAction.NOTHING;
+ } else {
+ Slot slot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (slot != null) {
+ ItemStack clickedItem = slot.getItem();
+ ItemStack cursor = player.containerMenu.getCarried();
+ if (clickedItem.isEmpty()) {
+ if (!cursor.isEmpty()) {
+ action = packetplayinwindowclick.getButtonNum() == 0 ? InventoryAction.PLACE_ALL : InventoryAction.PLACE_ONE;
2016-02-29 22:32:46 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ } else if (slot.mayPickup(player)) {
+ if (cursor.isEmpty()) {
+ action = packetplayinwindowclick.getButtonNum() == 0 ? InventoryAction.PICKUP_ALL : InventoryAction.PICKUP_HALF;
+ } else if (slot.mayPlace(cursor)) {
2023-06-07 17:30:00 +02:00
+ if (ItemStack.isSameItemSameTags(clickedItem, cursor)) {
2022-02-28 16:00:00 +01:00
+ int toPlace = packetplayinwindowclick.getButtonNum() == 0 ? cursor.getCount() : 1;
+ toPlace = Math.min(toPlace, clickedItem.getMaxStackSize() - clickedItem.getCount());
+ toPlace = Math.min(toPlace, slot.container.getMaxStackSize() - clickedItem.getCount());
+ if (toPlace == 1) {
+ action = InventoryAction.PLACE_ONE;
+ } else if (toPlace == cursor.getCount()) {
+ action = InventoryAction.PLACE_ALL;
+ } else if (toPlace < 0) {
+ action = toPlace != -1 ? InventoryAction.PICKUP_SOME : InventoryAction.PICKUP_ONE; // this happens with oversized stacks
+ } else if (toPlace != 0) {
+ action = InventoryAction.PLACE_SOME;
+ }
+ } else if (cursor.getCount() <= slot.getMaxStackSize()) {
+ action = InventoryAction.SWAP_WITH_CURSOR;
+ }
2023-06-07 17:30:00 +02:00
+ } else if (ItemStack.isSameItemSameTags(cursor, clickedItem)) {
2022-02-28 16:00:00 +01:00
+ if (clickedItem.getCount() >= 0) {
+ if (clickedItem.getCount() + cursor.getCount() <= cursor.getMaxStackSize()) {
+ // As of 1.5, this is result slots only
+ action = InventoryAction.PICKUP_ALL;
+ }
2016-02-29 22:32:46 +01:00
+ }
2014-11-25 22:32:16 +01:00
+ }
+ }
+ }
+ }
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ // TODO check on updates
+ case QUICK_MOVE:
+ if (packetplayinwindowclick.getButtonNum() == 0) {
+ click = ClickType.SHIFT_LEFT;
+ } else if (packetplayinwindowclick.getButtonNum() == 1) {
+ click = ClickType.SHIFT_RIGHT;
+ }
+ if (packetplayinwindowclick.getButtonNum() == 0 || packetplayinwindowclick.getButtonNum() == 1) {
+ if (packetplayinwindowclick.getSlotNum() < 0) {
2016-02-29 22:32:46 +01:00
+ action = InventoryAction.NOTHING;
2022-02-28 16:00:00 +01:00
+ } else {
+ Slot slot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (slot != null && slot.mayPickup(this.player) && slot.hasItem()) {
+ action = InventoryAction.MOVE_TO_OTHER_INVENTORY;
+ } else {
+ action = InventoryAction.NOTHING;
+ }
2016-02-29 22:32:46 +01:00
+ }
2014-11-25 22:32:16 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ case SWAP:
+ if ((packetplayinwindowclick.getButtonNum() >= 0 && packetplayinwindowclick.getButtonNum() < 9) || packetplayinwindowclick.getButtonNum() == 40) {
+ click = (packetplayinwindowclick.getButtonNum() == 40) ? ClickType.SWAP_OFFHAND : ClickType.NUMBER_KEY;
+ Slot clickedSlot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (clickedSlot.mayPickup(player)) {
+ ItemStack hotbar = this.player.getInventory().getItem(packetplayinwindowclick.getButtonNum());
+ boolean canCleanSwap = hotbar.isEmpty() || (clickedSlot.container == player.getInventory() && clickedSlot.mayPlace(hotbar)); // the slot will accept the hotbar item
+ if (clickedSlot.hasItem()) {
+ if (canCleanSwap) {
+ action = InventoryAction.HOTBAR_SWAP;
+ } else {
+ action = InventoryAction.HOTBAR_MOVE_AND_READD;
+ }
+ } else if (!clickedSlot.hasItem() && !hotbar.isEmpty() && clickedSlot.mayPlace(hotbar)) {
2016-02-29 22:32:46 +01:00
+ action = InventoryAction.HOTBAR_SWAP;
2014-11-25 22:32:16 +01:00
+ } else {
2022-02-28 16:00:00 +01:00
+ action = InventoryAction.NOTHING;
2014-11-25 22:32:16 +01:00
+ }
2016-02-29 22:32:46 +01:00
+ } else {
+ action = InventoryAction.NOTHING;
2014-11-25 22:32:16 +01:00
+ }
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ case CLONE:
+ if (packetplayinwindowclick.getButtonNum() == 2) {
+ click = ClickType.MIDDLE;
+ if (packetplayinwindowclick.getSlotNum() < 0) {
2016-02-29 22:32:46 +01:00
+ action = InventoryAction.NOTHING;
2022-02-28 16:00:00 +01:00
+ } else {
+ Slot slot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (slot != null && slot.hasItem() && player.getAbilities().instabuild && player.containerMenu.getCarried().isEmpty()) {
+ action = InventoryAction.CLONE_STACK;
+ } else {
+ action = InventoryAction.NOTHING;
+ }
2016-02-29 22:32:46 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ } else {
+ click = ClickType.UNKNOWN;
+ action = InventoryAction.UNKNOWN;
2014-11-25 22:32:16 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ case THROW:
+ if (packetplayinwindowclick.getSlotNum() >= 0) {
+ if (packetplayinwindowclick.getButtonNum() == 0) {
+ click = ClickType.DROP;
+ Slot slot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (slot != null && slot.hasItem() && slot.mayPickup(player) && !slot.getItem().isEmpty() && slot.getItem().getItem() != Item.byBlock(Blocks.AIR)) {
+ action = InventoryAction.DROP_ONE_SLOT;
+ } else {
+ action = InventoryAction.NOTHING;
+ }
+ } else if (packetplayinwindowclick.getButtonNum() == 1) {
+ click = ClickType.CONTROL_DROP;
+ Slot slot = this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum());
+ if (slot != null && slot.hasItem() && slot.mayPickup(player) && !slot.getItem().isEmpty() && slot.getItem().getItem() != Item.byBlock(Blocks.AIR)) {
+ action = InventoryAction.DROP_ALL_SLOT;
+ } else {
+ action = InventoryAction.NOTHING;
+ }
2016-02-29 22:32:46 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ } else {
+ // Sane default (because this happens when they are holding nothing. Don't ask why.)
+ click = ClickType.LEFT;
+ if (packetplayinwindowclick.getButtonNum() == 1) {
+ click = ClickType.RIGHT;
2016-02-29 22:32:46 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ action = InventoryAction.NOTHING;
2014-11-25 22:32:16 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ case QUICK_CRAFT:
+ this.player.containerMenu.clicked(packetplayinwindowclick.getSlotNum(), packetplayinwindowclick.getButtonNum(), packetplayinwindowclick.getClickType(), this.player);
+ break;
+ case PICKUP_ALL:
+ click = ClickType.DOUBLE_CLICK;
2016-02-29 22:32:46 +01:00
+ action = InventoryAction.NOTHING;
2022-02-28 16:00:00 +01:00
+ if (packetplayinwindowclick.getSlotNum() >= 0 && !this.player.containerMenu.getCarried().isEmpty()) {
+ ItemStack cursor = this.player.containerMenu.getCarried();
+ action = InventoryAction.NOTHING;
+ // Quick check for if we have any of the item
2023-12-28 22:36:38 +01:00
+ if (inventory.getTopInventory().contains(CraftItemType.minecraftToBukkit(cursor.getItem())) || inventory.getBottomInventory().contains(CraftItemType.minecraftToBukkit(cursor.getItem()))) {
2022-02-28 16:00:00 +01:00
+ action = InventoryAction.COLLECT_TO_CURSOR;
+ }
2016-02-29 22:32:46 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ break;
+ default:
+ break;
2014-11-25 22:32:16 +01:00
+ }
+
2022-02-28 16:00:00 +01:00
+ if (packetplayinwindowclick.getClickType() != InventoryClickType.QUICK_CRAFT) {
+ if (click == ClickType.NUMBER_KEY) {
+ event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.getSlotNum(), click, action, packetplayinwindowclick.getButtonNum());
+ } else {
+ event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.getSlotNum(), click, action);
+ }
+
+ org.bukkit.inventory.Inventory top = inventory.getTopInventory();
+ if (packetplayinwindowclick.getSlotNum() == 0 && top instanceof CraftingInventory) {
+ org.bukkit.inventory.Recipe recipe = ((CraftingInventory) top).getRecipe();
+ if (recipe != null) {
+ if (click == ClickType.NUMBER_KEY) {
+ event = new CraftItemEvent(recipe, inventory, type, packetplayinwindowclick.getSlotNum(), click, action, packetplayinwindowclick.getButtonNum());
+ } else {
+ event = new CraftItemEvent(recipe, inventory, type, packetplayinwindowclick.getSlotNum(), click, action);
+ }
2014-11-25 22:32:16 +01:00
+ }
+ }
2020-06-25 02:00:00 +02:00
+
2023-07-01 01:25:36 +02:00
+ if (packetplayinwindowclick.getSlotNum() == 3 && top instanceof SmithingInventory) {
2022-02-28 16:00:00 +01:00
+ org.bukkit.inventory.ItemStack result = ((SmithingInventory) top).getResult();
+ if (result != null) {
+ if (click == ClickType.NUMBER_KEY) {
+ event = new SmithItemEvent(inventory, type, packetplayinwindowclick.getSlotNum(), click, action, packetplayinwindowclick.getButtonNum());
+ } else {
+ event = new SmithItemEvent(inventory, type, packetplayinwindowclick.getSlotNum(), click, action);
+ }
2021-04-18 10:03:23 +02:00
+ }
+ }
+
2022-02-28 16:00:00 +01:00
+ event.setCancelled(cancelled);
+ Container oldContainer = this.player.containerMenu; // SPIGOT-1224
+ cserver.getPluginManager().callEvent(event);
+ if (this.player.containerMenu != oldContainer) {
+ return;
+ }
2021-04-18 10:03:23 +02:00
+
2022-02-28 16:00:00 +01:00
+ switch (event.getResult()) {
+ case ALLOW:
+ case DEFAULT:
+ this.player.containerMenu.clicked(i, packetplayinwindowclick.getButtonNum(), packetplayinwindowclick.getClickType(), this.player);
+ break;
+ case DENY:
+ /* Needs enum constructor in InventoryAction
+ if (action.modifiesOtherSlots()) {
2021-06-11 07:00:00 +02:00
+
2022-02-28 16:00:00 +01:00
+ } else {
+ if (action.modifiesCursor()) {
+ this.player.playerConnection.sendPacket(new Packet103SetSlot(-1, -1, this.player.inventory.getCarried()));
+ }
+ if (action.modifiesClicked()) {
+ this.player.playerConnection.sendPacket(new Packet103SetSlot(this.player.activeContainer.windowId, packet102windowclick.slot, this.player.activeContainer.getSlot(packet102windowclick.slot).getItem()));
+ }
+ }*/
+ switch (action) {
+ // Modified other slots
+ case PICKUP_ALL:
+ case MOVE_TO_OTHER_INVENTORY:
+ case HOTBAR_MOVE_AND_READD:
+ case HOTBAR_SWAP:
+ case COLLECT_TO_CURSOR:
+ case UNKNOWN:
+ this.player.containerMenu.sendAllDataToRemote();
+ break;
+ // Modified cursor and clicked
+ case PICKUP_SOME:
+ case PICKUP_HALF:
+ case PICKUP_ONE:
+ case PLACE_ALL:
+ case PLACE_SOME:
+ case PLACE_ONE:
+ case SWAP_WITH_CURSOR:
+ this.player.connection.send(new PacketPlayOutSetSlot(-1, -1, this.player.inventoryMenu.incrementStateId(), this.player.containerMenu.getCarried()));
+ this.player.connection.send(new PacketPlayOutSetSlot(this.player.containerMenu.containerId, this.player.inventoryMenu.incrementStateId(), packetplayinwindowclick.getSlotNum(), this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum()).getItem()));
+ break;
+ // Modified clicked only
+ case DROP_ALL_SLOT:
+ case DROP_ONE_SLOT:
+ this.player.connection.send(new PacketPlayOutSetSlot(this.player.containerMenu.containerId, this.player.inventoryMenu.incrementStateId(), packetplayinwindowclick.getSlotNum(), this.player.containerMenu.getSlot(packetplayinwindowclick.getSlotNum()).getItem()));
+ break;
+ // Modified cursor only
+ case DROP_ALL_CURSOR:
+ case DROP_ONE_CURSOR:
+ case CLONE_STACK:
+ this.player.connection.send(new PacketPlayOutSetSlot(-1, -1, this.player.inventoryMenu.incrementStateId(), this.player.containerMenu.getCarried()));
+ break;
+ // Nothing
+ case NOTHING:
+ break;
2014-11-25 22:32:16 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ }
2017-11-08 00:36:11 +01:00
+
2022-02-28 16:00:00 +01:00
+ if (event instanceof CraftItemEvent || event instanceof SmithItemEvent) {
+ // Need to update the inventory on crafting to
+ // correctly support custom recipes
+ player.containerMenu.sendAllDataToRemote();
+ }
2014-12-05 16:04:01 +01:00
+ }
2022-02-28 16:00:00 +01:00
+ // CraftBukkit end
ObjectIterator objectiterator = Int2ObjectMaps.fastIterable(packetplayinwindowclick.getChangedSlots()).iterator();
2021-06-11 07:00:00 +02:00
2022-02-28 16:00:00 +01:00
while (objectiterator.hasNext()) {
2023-12-27 21:56:30 +01:00
@@ -1704,9 +2709,18 @@
2023-08-01 23:46:19 +02:00
if (!this.player.containerMenu.stillValid(this.player)) {
PlayerConnection.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, this.player.containerMenu);
} else {
2023-09-21 18:40:00 +02:00
- this.server.getRecipeManager().byKey(packetplayinautorecipe.getRecipe()).ifPresent((recipeholder) -> {
- ((ContainerRecipeBook) this.player.containerMenu).handlePlacement(packetplayinautorecipe.isShiftDown(), recipeholder, this.player);
2023-08-01 23:46:19 +02:00
+ // CraftBukkit start - implement PlayerRecipeBookClickEvent
+ org.bukkit.inventory.Recipe recipe = this.cserver.getRecipe(CraftNamespacedKey.fromMinecraft(packetplayinautorecipe.getRecipe()));
+ if (recipe == null) {
+ return;
+ }
+ org.bukkit.event.player.PlayerRecipeBookClickEvent event = CraftEventFactory.callRecipeBookClickEvent(this.player, recipe, packetplayinautorecipe.isShiftDown());
+
+ // Cast to keyed should be safe as the recipe will never be a MerchantRecipe.
2023-09-21 18:40:00 +02:00
+ this.server.getRecipeManager().byKey(CraftNamespacedKey.toMinecraft(((org.bukkit.Keyed) event.getRecipe()).getKey())).ifPresent((recipeholder) -> {
+ ((ContainerRecipeBook) this.player.containerMenu).handlePlacement(event.isShiftClick(), recipeholder, this.player);
2023-08-01 23:46:19 +02:00
});
+ // CraftBukkit end
}
}
}
2023-12-27 21:56:30 +01:00
@@ -1714,6 +2728,7 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handleContainerButtonClick(PacketPlayInEnchantItem packetplayinenchantitem) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinenchantitem, this, this.player.serverLevel());
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
this.player.resetLastActionTime();
if (this.player.containerMenu.containerId == packetplayinenchantitem.getContainerId() && !this.player.isSpectator()) {
2022-07-27 20:00:00 +02:00
if (!this.player.containerMenu.stillValid(this.player)) {
2023-12-27 21:56:30 +01:00
@@ -1756,6 +2771,43 @@
2021-11-21 23:00:00 +01:00
boolean flag1 = packetplayinsetcreativeslot.getSlotNum() >= 1 && packetplayinsetcreativeslot.getSlotNum() <= 45;
boolean flag2 = itemstack.isEmpty() || itemstack.getDamageValue() >= 0 && itemstack.getCount() <= 64 && !itemstack.isEmpty();
+ if (flag || (flag1 && !ItemStack.matches(this.player.inventoryMenu.getSlot(packetplayinsetcreativeslot.getSlotNum()).getItem(), packetplayinsetcreativeslot.getItem()))) { // Insist on valid slot
2016-11-17 02:41:03 +01:00
+ // CraftBukkit start - Call click event
2021-06-11 07:00:00 +02:00
+ InventoryView inventory = this.player.inventoryMenu.getBukkitView();
2021-11-21 23:00:00 +01:00
+ org.bukkit.inventory.ItemStack item = CraftItemStack.asBukkitCopy(packetplayinsetcreativeslot.getItem());
2014-11-25 22:32:16 +01:00
+
+ SlotType type = SlotType.QUICKBAR;
+ if (flag) {
+ type = SlotType.OUTSIDE;
2021-11-21 23:00:00 +01:00
+ } else if (packetplayinsetcreativeslot.getSlotNum() < 36) {
+ if (packetplayinsetcreativeslot.getSlotNum() >= 5 && packetplayinsetcreativeslot.getSlotNum() < 9) {
2014-11-25 22:32:16 +01:00
+ type = SlotType.ARMOR;
+ } else {
+ type = SlotType.CONTAINER;
+ }
+ }
2021-11-21 23:00:00 +01:00
+ InventoryCreativeEvent event = new InventoryCreativeEvent(inventory, type, flag ? -999 : packetplayinsetcreativeslot.getSlotNum(), item);
2021-06-11 07:00:00 +02:00
+ cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ itemstack = CraftItemStack.asNMSCopy(event.getCursor());
+
+ switch (event.getResult()) {
+ case ALLOW:
+ // Plugin cleared the id / stacksize checks
2016-11-17 02:41:03 +01:00
+ flag2 = true;
2014-11-25 22:32:16 +01:00
+ break;
+ case DEFAULT:
+ break;
+ case DENY:
+ // Reset the slot
2021-11-21 23:00:00 +01:00
+ if (packetplayinsetcreativeslot.getSlotNum() >= 0) {
+ this.player.connection.send(new PacketPlayOutSetSlot(this.player.inventoryMenu.containerId, this.player.inventoryMenu.incrementStateId(), packetplayinsetcreativeslot.getSlotNum(), this.player.inventoryMenu.getSlot(packetplayinsetcreativeslot.getSlotNum()).getItem()));
+ this.player.connection.send(new PacketPlayOutSetSlot(-1, this.player.inventoryMenu.incrementStateId(), -1, ItemStack.EMPTY));
2014-11-25 22:32:16 +01:00
+ }
+ return;
+ }
+ }
+ // CraftBukkit end
2016-11-17 02:41:03 +01:00
if (flag1 && flag2) {
2023-03-14 17:30:00 +01:00
this.player.inventoryMenu.getSlot(packetplayinsetcreativeslot.getSlotNum()).setByPlayer(itemstack);
2023-12-27 21:56:30 +01:00
@@ -1778,6 +2830,7 @@
2020-11-02 21:00:00 +01:00
}
2022-07-27 20:00:00 +02:00
private void updateSignText(PacketPlayInUpdateSign packetplayinupdatesign, List<FilteredText> list) {
2021-11-21 23:00:00 +01:00
+ if (this.player.isImmobile()) return; // CraftBukkit
this.player.resetLastActionTime();
2023-06-07 17:30:00 +02:00
WorldServer worldserver = this.player.serverLevel();
2021-11-21 23:00:00 +01:00
BlockPosition blockposition = packetplayinupdatesign.getPos();
2023-12-27 21:56:30 +01:00
@@ -1799,7 +2852,17 @@
2019-04-23 04:00:00 +02:00
@Override
2021-11-21 23:00:00 +01:00
public void handlePlayerAbilities(PacketPlayInAbilities packetplayinabilities) {
2023-06-07 17:30:00 +02:00
PlayerConnectionUtils.ensureRunningOnSameThread(packetplayinabilities, this, this.player.serverLevel());
2021-06-11 07:00:00 +02:00
- this.player.getAbilities().flying = packetplayinabilities.isFlying() && this.player.getAbilities().mayfly;
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
+ if (this.player.getAbilities().mayfly && this.player.getAbilities().flying != packetplayinabilities.isFlying()) {
2021-06-20 01:27:32 +02:00
+ PlayerToggleFlightEvent event = new PlayerToggleFlightEvent(this.player.getBukkitEntity(), packetplayinabilities.isFlying());
2021-06-11 07:00:00 +02:00
+ this.cserver.getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+ if (!event.isCancelled()) {
2021-06-11 07:00:00 +02:00
+ this.player.getAbilities().flying = packetplayinabilities.isFlying(); // Actually set the player's flying status
2014-11-25 22:32:16 +01:00
+ } else {
2021-11-21 23:00:00 +01:00
+ this.player.onUpdateAbilities(); // Tell the player their ability was reverted
2014-11-25 22:32:16 +01:00
+ }
+ }
+ // CraftBukkit end
}
2019-04-23 04:00:00 +02:00
@Override
2023-12-27 21:56:30 +01:00
@@ -1858,7 +2921,7 @@
2023-09-21 18:40:00 +02:00
if (!this.waitingForSwitchToConfig) {
throw new IllegalStateException("Client acknowledged config, but none was requested");
} else {
- this.connection.setListener(new ServerConfigurationPacketListenerImpl(this.server, this.connection, this.createCookie(this.player.clientInformation())));
+ this.connection.setListener(new ServerConfigurationPacketListenerImpl(this.server, this.connection, this.createCookie(this.player.clientInformation()), this.player)); // CraftBukkit
}
2018-07-15 02:00:00 +02:00
}
2017-04-20 14:10:31 +02:00