diff --git a/src/main/java/com/mohistmc/banner/injection/server/network/InjectionServerGamePacketListenerImpl.java b/src/main/java/com/mohistmc/banner/injection/server/network/InjectionServerGamePacketListenerImpl.java index 488eb3736..b35fa2a80 100644 --- a/src/main/java/com/mohistmc/banner/injection/server/network/InjectionServerGamePacketListenerImpl.java +++ b/src/main/java/com/mohistmc/banner/injection/server/network/InjectionServerGamePacketListenerImpl.java @@ -33,10 +33,6 @@ default boolean checkLimit(long timestamp) { return false; } - default CraftServer bridge$craftServer() { - return null; - } - default Logger bridge$logger() { return null; } diff --git a/src/main/java/com/mohistmc/banner/mixin/server/network/MixinServerGamePacketListenerImpl.java b/src/main/java/com/mohistmc/banner/mixin/server/network/MixinServerGamePacketListenerImpl.java index 66cea26f3..22dc03087 100644 --- a/src/main/java/com/mohistmc/banner/mixin/server/network/MixinServerGamePacketListenerImpl.java +++ b/src/main/java/com/mohistmc/banner/mixin/server/network/MixinServerGamePacketListenerImpl.java @@ -2,6 +2,7 @@ import com.mohistmc.banner.bukkit.BukkitExtraConstants; import com.mohistmc.banner.bukkit.BukkitSnapshotCaptures; +import com.mohistmc.banner.injection.server.network.InjectionServerCommonPacketListenerImpl; import com.mohistmc.banner.injection.server.network.InjectionServerGamePacketListenerImpl; import com.mojang.brigadier.ParseResults; import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; @@ -59,6 +60,7 @@ import net.minecraft.server.network.ServerCommonPacketListenerImpl; import net.minecraft.server.network.ServerConfigurationPacketListenerImpl; import net.minecraft.server.network.ServerGamePacketListenerImpl; +import net.minecraft.server.players.PlayerList; import net.minecraft.util.FutureChain; import net.minecraft.util.Mth; import net.minecraft.world.Container; @@ -152,7 +154,7 @@ import java.util.function.UnaryOperator; @Mixin(ServerGamePacketListenerImpl.class) -public abstract class MixinServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl implements InjectionServerGamePacketListenerImpl { +public abstract class MixinServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl implements InjectionServerGamePacketListenerImpl, InjectionServerCommonPacketListenerImpl { @Shadow public ServerPlayer player; @Mutable @@ -221,16 +223,10 @@ public abstract class MixinServerGamePacketListenerImpl extends ServerCommonPack @Shadow public abstract ServerPlayer getPlayer(); @Shadow private boolean waitingForSwitchToConfig; - - private static final int SURVIVAL_PLACE_DISTANCE_SQUARED = 6 * 6; - private static final int CREATIVE_PLACE_DISTANCE_SQUARED = 7 * 7; - private CraftServer cserver; - public boolean processedDisconnect; private int allowedPlayerTicks; - private int dropCount; + private int dropCount = 0; private int lastTick; private volatile int lastBookTick; - private int lastDropTick; private double lastPosX; private double lastPosY; @@ -251,7 +247,6 @@ public CraftPlayer getCraftPlayer() { @Inject(method = "", at = @At("RETURN")) private void banner$init(MinecraftServer minecraftServer, Connection connection, ServerPlayer serverPlayer, CommonListenerCookie commonListenerCookie, CallbackInfo ci) { - this.cserver = ((CraftServer) Bukkit.getServer()); this.chatMessageChain = new FutureChain(minecraftServer.bridge$chatExecutor()); } @@ -389,7 +384,7 @@ public void handleMoveVehicle(final ServerboundMoveVehiclePacket packetplayinveh if (from.getX() != Double.MAX_VALUE) { Location oldTo = to.clone(); PlayerMoveEvent event = new PlayerMoveEvent(player, from, to); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); // If the event is cancelled we move the player back to their old location. if (event.isCancelled()) { @@ -692,7 +687,7 @@ public void handleMovePlayer(ServerboundMovePlayerPacket packetplayinflying) { if (from.getX() != Double.MAX_VALUE) { Location oldTo = to.clone(); PlayerMoveEvent event = new PlayerMoveEvent(player, from, to); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); // If the event is cancelled we move the player back to their old location. if (event.isCancelled()) { @@ -762,7 +757,7 @@ public void handleMovePlayer(ServerboundMovePlayerPacket packetplayinflying) { CraftItemStack mainHand = CraftItemStack.asCraftMirror(itemStack); CraftItemStack offHand = CraftItemStack.asCraftMirror(this.player.getItemInHand(InteractionHand.MAIN_HAND)); PlayerSwapHandItemsEvent swapItemsEvent = new PlayerSwapHandItemsEvent(getCraftPlayer(), mainHand.clone(), offHand.clone()); - this.cserver.getPluginManager().callEvent(swapItemsEvent); + Bukkit.getPluginManager().callEvent(swapItemsEvent); if (swapItemsEvent.isCancelled()) { return; } @@ -879,35 +874,28 @@ public boolean checkLimit(long timestamp) { @Inject(method = "onDisconnect", cancellable = true, at = @At("HEAD")) private void banner$returnIfProcessed(Component reason, CallbackInfo ci) { - if (processedDisconnect) { + if (this.bridge$processedDisconnect()) { ci.cancel(); } else { - processedDisconnect = true; + this.banner$setProcessedDisconnect(true); } } - @Redirect(method = "onDisconnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerCommonPacketListenerImpl;onDisconnect(Lnet/minecraft/network/chat/Component;)V")) - public void banner$captureQuit(ServerCommonPacketListenerImpl instance, Component reason) { - // do nothing - } - - @Inject(method = "onDisconnect", at = @At("HEAD"), cancellable = true) - private void banner$checkDisconnect(Component reason, CallbackInfo ci) { - // CraftBukkit start - Rarely it would send a disconnect line twice - if (this.processedDisconnect) { - ci.cancel(); - } else { - this.processedDisconnect = true; - } - // CraftBukkit end - } - @Inject(method = "onDisconnect", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;removePlayerFromWorld()V")) - private void banner$setQuitMsg(Component message, CallbackInfo ci) { + /** + * @author Mgazul + * @reason + */ + @Overwrite + private void removePlayerFromWorld() { + this.chatMessageChain.close(); + this.player.disconnect(); String quitMessage = this.server.getPlayerList().bridge$quiltMsg(); - if ((quitMessage != null) && (!quitMessage.isEmpty())) { + if ((quitMessage != null) && (quitMessage.length() > 0)) { this.server.getPlayerList().broadcastMessage(CraftChatMessage.fromString(quitMessage)); } + // CraftBukkit end + this.player.getTextFilter().leave(); } @Inject(method = "handleAnimate", @@ -947,7 +935,7 @@ public boolean checkLimit(long timestamp) { // Arm swing animation PlayerAnimationEvent event = new PlayerAnimationEvent(this.getCraftPlayer(), packet.getHand() == InteractionHand.MAIN_HAND ? PlayerAnimationType.ARM_SWING : PlayerAnimationType.OFF_ARM_SWING); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) ci.cancel(); // CraftBukkit end @@ -965,7 +953,7 @@ public void handleSetCarriedItem(ServerboundSetCarriedItemPacket packet) { } if (packet.getSlot() >= 0 && packet.getSlot() < net.minecraft.world.entity.player.Inventory.getSelectionSize()) { PlayerItemHeldEvent event = new PlayerItemHeldEvent(this.getCraftPlayer(), this.player.getInventory().selected, packet.getSlot()); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { this.send(new ClientboundSetCarriedItemPacket(this.player.getInventory().selected)); this.player.resetLastActionTime(); @@ -1032,7 +1020,7 @@ private void performChatCommand(ServerboundChatCommandPacket packet, LastSeenMes LOGGER.info(this.player.getScoreboardName() + " issued server command: " + command); PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(getCraftPlayer(), command, new LazyPlayerSet(server)); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return; @@ -1078,7 +1066,7 @@ public void chat(String s, PlayerChatMessage original, boolean async) { Player thisPlayer = this.getCraftPlayer(); AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(async, thisPlayer, s, new LazyPlayerSet(this.server)); String originalFormat = event.getFormat(), originalMessage = event.getMessage(); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (PlayerChatEvent.getHandlerList().getRegisteredListeners().length != 0) { PlayerChatEvent queueEvent = new PlayerChatEvent(thisPlayer, event.getMessage(), event.getFormat(), event.getRecipients()); queueEvent.setCancelled(event.isCancelled()); @@ -1155,14 +1143,14 @@ public void handleCommand(String s) { CraftPlayer player = this.getCraftPlayer(); PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet(server)); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return; } try { - if (this.cserver.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) { + if (Bukkit.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) { return; } } catch (org.bukkit.command.CommandException ex) { @@ -1202,13 +1190,13 @@ private void broadcastChatMessage(PlayerChatMessage playerchatmessage) { } if (packetIn.getAction() == ServerboundPlayerCommandPacket.Action.PRESS_SHIFT_KEY || packetIn.getAction() == ServerboundPlayerCommandPacket.Action.RELEASE_SHIFT_KEY) { PlayerToggleSneakEvent event = new PlayerToggleSneakEvent(this.getCraftPlayer(), packetIn.getAction() == ServerboundPlayerCommandPacket.Action.PRESS_SHIFT_KEY); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { ci.cancel(); } } else if (packetIn.getAction() == ServerboundPlayerCommandPacket.Action.START_SPRINTING || packetIn.getAction() == ServerboundPlayerCommandPacket.Action.STOP_SPRINTING) { PlayerToggleSprintEvent e2 = new PlayerToggleSprintEvent(this.getCraftPlayer(), packetIn.getAction() == ServerboundPlayerCommandPacket.Action.START_SPRINTING); - this.cserver.getPluginManager().callEvent(e2); + Bukkit.getPluginManager().callEvent(e2); if (e2.isCancelled()) { ci.cancel(); } @@ -1471,7 +1459,7 @@ public void handleContainerClick(ServerboundContainerClickPacket packet) { event.setCancelled(cancelled); AbstractContainerMenu oldContainer = this.player.containerMenu; // SPIGOT-1224 - cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (this.player.containerMenu != oldContainer) { return; } @@ -1600,7 +1588,7 @@ public void handleSetCreativeModeSlot(final ServerboundSetCreativeModeSlotPacket } } final InventoryCreativeEvent event = new InventoryCreativeEvent(inventory, type, flag ? -999 : packetplayinsetcreativeslot.getSlotNum(), item); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); itemstack = CraftItemStack.asNMSCopy(event.getCursor()); switch (event.getResult()) { case ALLOW: { @@ -1636,7 +1624,7 @@ public void handleSetCreativeModeSlot(final ServerboundSetCreativeModeSlotPacket cancellable = true) private void banner$recipeClickEvent(ServerboundPlaceRecipePacket packet, CallbackInfo ci) { // CraftBukkit start - implement PlayerRecipeBookClickEvent - org.bukkit.inventory.Recipe recipe = this.cserver.getRecipe(CraftNamespacedKey.fromMinecraft(packet.getRecipe())); + org.bukkit.inventory.Recipe recipe = Bukkit.getRecipe(CraftNamespacedKey.fromMinecraft(packet.getRecipe())); if (recipe == null) { ci.cancel(); } @@ -1679,7 +1667,7 @@ public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, (ServerGamePacketListenerImpl) (Object) this, this.player.serverLevel()); if (this.player.getAbilities().mayfly && this.player.getAbilities().flying != packet.isFlying()) { PlayerToggleFlightEvent event = new PlayerToggleFlightEvent(getCraftPlayer(), packet.isFlying()); - this.cserver.getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (!event.isCancelled()) { this.player.getAbilities().flying = packet.isFlying(); } else { @@ -1730,7 +1718,7 @@ public void teleport(double d0, double d1, double d2, float f, float f1, Set