Skip to content

Commit

Permalink
Fix Disconnected patch error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Sep 30, 2023
1 parent 1cf1615 commit b58601f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ default boolean checkLimit(long timestamp) {
return false;
}

default CraftServer bridge$craftServer() {
return null;
}

default Logger bridge$logger() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -251,7 +247,6 @@ public CraftPlayer getCraftPlayer() {

@Inject(method = "<init>", 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());
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1730,7 +1718,7 @@ public void teleport(double d0, double d1, double d2, float f, float f1, Set<Rel
}

PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause);
this.cserver.getPluginManager().callEvent(event);
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled() || !to.equals(event.getTo())) {
set.clear(); // Can't relative teleport
Expand Down Expand Up @@ -1792,16 +1780,6 @@ public void internalTeleport(double d0, double d1, double d2, float f, float f1,
this.player.connection.send(new ClientboundPlayerPositionPacket(d0 - d3, d1 - d4, d2 - d5, f - f2, f1 - f3, set, this.awaitingTeleport));
}

@Override
public boolean bridge$processedDisconnect() {
return processedDisconnect;
}

@Override
public CraftServer bridge$craftServer() {
return cserver;
}

@Override
public Logger bridge$logger() {
return LOGGER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Bukkit;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -56,7 +57,7 @@ private void performInteraction(InteractionHand enumhand, ServerGamePacketListen
boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && val$target instanceof Mob;
Item origItem = field_28963.player.getInventory().getSelected() == null ? null : field_28963.player.getInventory().getSelected().getItem();

field_28963.bridge$craftServer().getPluginManager().callEvent(event);
Bukkit.getPluginManager().callEvent(event);

// Entity in bucket - SPIGOT-4048 and SPIGOT-6859a
if ((val$target instanceof Bucketable && val$target instanceof LivingEntity && origItem != null && origItem.asItem() == Items.WATER_BUCKET) && (event.isCancelled() || field_28963.player.getInventory().getSelected() == null || field_28963.player.getInventory().getSelected().getItem() != origItem)) {
Expand Down

0 comments on commit b58601f

Please sign in to comment.