Skip to content

Commit

Permalink
Some updates for 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Oct 8, 2023
1 parent 9c8ade6 commit 2ce633d
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 146 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.samo_lego.taterzens.Taterzens;
import org.samo_lego.taterzens.npc.commands.BungeeCommand;
Expand All @@ -19,7 +23,6 @@

import java.util.Collections;

import static net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket.BRAND;
import static org.samo_lego.taterzens.Taterzens.config;
import static org.samo_lego.taterzens.npc.commands.BungeeCommand.AVAILABLE_SERVERS;
import static org.samo_lego.taterzens.npc.commands.BungeeCommand.BUNGEE_CHANNEL;
Expand All @@ -28,7 +31,7 @@
* Handles bungee packets.
*/
@Mixin(value = ServerGamePacketListenerImpl.class)
public class ServerGamePacketListenerImplMixin_BungeeListener {
public abstract class ServerGamePacketListenerImplMixin_BungeeListener extends ServerCommonPacketListenerImpl {
@Unique
private static final String GET_SERVERS = "GetServers";
@Shadow
Expand All @@ -37,9 +40,13 @@ public class ServerGamePacketListenerImplMixin_BungeeListener {
@Unique
private static final String taterzens$permission = "taterzens.npc.edit.commands.addBungee";

public ServerGamePacketListenerImplMixin_BungeeListener(MinecraftServer minecraftServer, Connection connection, CommonListenerCookie commonListenerCookie) {
super(minecraftServer, connection, commonListenerCookie);
}

@Inject(method = "handleCustomPayload", at = @At("TAIL"))
private void onCustomPayload(ServerboundCustomPayloadPacket packet, CallbackInfo ci) {
ResourceLocation packetId = packet.getIdentifier();
private void taterzens_onCustomPayload(ServerboundCustomPayloadPacket packet, CallbackInfo ci) {
ResourceLocation packetId = packet.payload().id();
CommandSourceStack commandSourceStack = player.createCommandSourceStack();
boolean hasPermission = Taterzens.getInstance().getPlatform().checkPermission(commandSourceStack, taterzens$permission, config.perms.npcCommandPermissionLevel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.samo_lego.taterzens.mixin.network;

import com.mojang.authlib.GameProfile;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.BundlePacket;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.*;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import org.samo_lego.taterzens.interfaces.ITaterzenEditor;
import org.samo_lego.taterzens.mixin.accessors.AClientboundAddPlayerPacket;
import org.samo_lego.taterzens.mixin.accessors.AClientboundPlayerInfoUpdatePacket;
import org.samo_lego.taterzens.mixin.accessors.AClientboundSetEntityDataPacket;
import org.samo_lego.taterzens.npc.TaterzenNPC;
Expand All @@ -34,7 +36,7 @@
* Used to "fake" the TaterzenNPC entity type.
*/
@Mixin(value = ServerGamePacketListenerImpl.class, priority = 900)
public abstract class ServerGamePacketListenerImplMixin_PacketFaker {
public abstract class ServerGamePacketListenerImplMixin_PacketFaker extends ServerCommonPacketListenerImpl {

@Unique
private final Map<UUID, NpcPlayerUpdate> tablistQueue = new LinkedHashMap<>();
Expand All @@ -45,24 +47,26 @@ public abstract class ServerGamePacketListenerImplMixin_PacketFaker {
@Unique
private int queueTick;

@Shadow
public abstract void send(Packet<?> packet, @Nullable PacketSendListener packetSendListener);
@Unique
private final ServerGamePacketListenerImpl self = (ServerGamePacketListenerImpl) (Object) this;

public ServerGamePacketListenerImplMixin_PacketFaker(MinecraftServer minecraftServer, Connection connection, CommonListenerCookie commonListenerCookie) {
super(minecraftServer, connection, commonListenerCookie);
}

@Shadow
public abstract void send(Packet<?> packet);

/**
* Changes entity type if entity is an instance of {@link TaterzenNPC}.
*/
@Inject(method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V",
@Inject(method = "send",
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V"),
cancellable = true)
private void changeEntityType(Packet<?> packet, PacketSendListener listener, CallbackInfo ci) {
private void taterzen_changeEntityType(Packet<?> packet, PacketSendListener listener, CallbackInfo ci) {
Level world = player.level();
if (packet instanceof BundlePacket<?> bPacket && !this.skipCheck) {
for (Packet<?> subPacket : bPacket.subPackets()) {
if (subPacket instanceof ClientboundAddPlayerPacket) {
Entity entity = world.getEntity(((AClientboundAddPlayerPacket) subPacket).getId());
if (subPacket instanceof ClientboundAddEntityPacket) {
Entity entity = world.getEntity(((ClientboundAddEntityPacket) subPacket).getId());

if (!(entity instanceof TaterzenNPC npc)) return;

Expand All @@ -72,13 +76,13 @@ private void changeEntityType(Packet<?> packet, PacketSendListener listener, Cal
//noinspection ConstantConditions
var entry = new ClientboundPlayerInfoUpdatePacket.Entry(profile.getId(), profile, false, 0, GameType.SURVIVAL, npc.getDisplayName(), null);
((AClientboundPlayerInfoUpdatePacket) playerAddPacket).setEntries(Collections.singletonList(entry));
this.send(playerAddPacket, listener);
self.send(playerAddPacket, listener);

// Before we send this packet, we have
// added player to tablist, otherwise client doesn't
// show it ... :mojank:
this.skipCheck = true;
this.send(packet, listener);
self.send(packet, listener);
this.skipCheck = false;

// And now we can remove it from tablist
Expand All @@ -92,7 +96,7 @@ private void changeEntityType(Packet<?> packet, PacketSendListener listener, Cal
tablistQueue.put(uuid, new NpcPlayerUpdate(npc.getGameProfile(), npc.getTabListName(), queueTick + config.taterzenTablistTimeout));
}

this.send(new ClientboundRotateHeadPacket(entity, (byte) ((int) (entity.getYHeadRot() * 256.0F / 360.0F))), listener);
self.send(new ClientboundRotateHeadPacket(entity, (byte) ((int) (entity.getYHeadRot() * 256.0F / 360.0F))), listener);

ci.cancel();
} else if (subPacket instanceof ClientboundSetEntityDataPacket) {
Expand Down Expand Up @@ -137,7 +141,7 @@ private void removeTaterzenFromTablist(CallbackInfo ci) {
ClientboundPlayerInfoRemovePacket taterzensRemovePacket = new ClientboundPlayerInfoRemovePacket(toRemove);

this.skipCheck = true;
this.send(taterzensRemovePacket);
self.send(taterzensRemovePacket);
this.skipCheck = false;
}
}
Loading

0 comments on commit 2ce633d

Please sign in to comment.