Skip to content

Commit

Permalink
Start updating to 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 20, 2024
1 parent eb8d626 commit d69fcdd
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 50 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
------------------------------------------------------
Version 3.1.0
------------------------------------------------------
- Updated to 1.20.6

------------------------------------------------------
Version 3.0.1
------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ org.gradle.jvmargs=-Xmx2G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.3
fabric_version=0.92.0+1.20.4
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.3
loader_version=0.15.11
fabric_version=0.100.4+1.20.6

# Other Dependencies
jb_annotations_version = 23.0.0
Expand All @@ -24,7 +24,7 @@ license_header = LGPL
gpl_version = 3
modrinth_id = xBZuWXoj
curseforge_id = 360333
curseforge_versions = 1.20.4
curseforge_versions = 1.20.5; 1.20.6
cf_requirements = fabric-api
cf_embeddeds = cardinal-components-api
release_type = release
9 changes: 8 additions & 1 deletion src/main/java/org/ladysnake/impersonate/Impersonate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import dev.onyxstudios.cca.api.v3.entity.RespawnCopyStrategy;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ladysnake.impersonate.impl.ImpersonateCommand;
import org.ladysnake.impersonate.impl.ImpersonateGamerules;
import org.ladysnake.impersonate.impl.PlayerImpersonator;
import org.ladysnake.impersonate.impl.ReloadSkinPacket;

/**
* Main entrypoint for Impersonate
Expand All @@ -39,15 +41,20 @@
public final class Impersonate implements ModInitializer, EntityComponentInitializer {
public static final Logger LOGGER = LogManager.getLogger("Impersonate");
public static final ComponentKey<Impersonator> IMPERSONATION = ComponentRegistryV3.INSTANCE.getOrCreate(
new Identifier("impersonate", "impersonation"),
id("impersonation"),
Impersonator.class
);

public static Identifier id(String path) {
return new Identifier("impersonate", path);
}

@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, acc, dedicated) -> ImpersonateCommand.register(dispatcher));
ImpersonateGamerules.init();
PlayerImpersonator.init();
PayloadTypeRegistry.playS2C().register(ReloadSkinPacket.ID, ReloadSkinPacket.CODEC);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
public final class ImpersonateClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(ServerPlayerSkins.RELOAD_SKIN_PACKET, (client, handler, buf, responseSender) -> client.execute(() -> {
assert client.player != null;
((AbstractClientPlayerEntityAccessor) client.player).setPlayerListEntry(null);
}));
ClientPlayNetworking.registerGlobalReceiver(ReloadSkinPacket.ID, (payload, context) -> {
((AbstractClientPlayerEntityAccessor) context.player()).setPlayerListEntry(null);
});
}
}
23 changes: 14 additions & 9 deletions src/main/java/org/ladysnake/impersonate/impl/PacketMeddling.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
*/
package org.ladysnake.impersonate.impl;

import io.netty.buffer.ByteBuf;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.ChatMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -56,11 +61,11 @@ public static void resolvePlayerListEntries(PlayerListS2CPacket packet, ServerPl
}
}

public static <P extends Packet<?>> P copyPacket(P packet, Function<PacketByteBuf, P> factory) {
PacketByteBuf buf = PacketByteBufs.create();
public static <P extends Packet<?>> P copyPacket(P packet, PacketCodec<? super RegistryByteBuf, P> codec, DynamicRegistryManager dynamicRegistryManager) {
RegistryByteBuf buf = new RegistryByteBuf(PacketByteBufs.create(), dynamicRegistryManager);
try {
packet.write(buf);
return factory.apply(buf);
codec.encode(buf, packet);
return codec.decode(buf);
} finally {
buf.release();
}
Expand All @@ -69,9 +74,9 @@ public static <P extends Packet<?>> P copyPacket(P packet, Function<PacketByteBu
public static ChatMessageS2CPacket resolveChatMessage(ChatMessageS2CPacket chatPacket, ServerPlayerEntity player) {
@Nullable Text unsignedContent = Optional.ofNullable(chatPacket.unsignedContent()).map(t -> ((RecipientAwareText) t).impersonateResolveAll(player)).orElse(null);
Text name = ((RecipientAwareText) chatPacket.serializedParameters().name()).impersonateResolveAll(player);
@Nullable Text targetName = chatPacket.serializedParameters().targetName() instanceof RecipientAwareText t
Optional<Text> targetName = chatPacket.serializedParameters().targetName().map(text -> text instanceof RecipientAwareText t
? t.impersonateResolveAll(player)
: null;
: null);

// God, I wish we had a Record#copy method in this language
// And yes we need to do a deep copy at the end, to avoid sharing text references
Expand All @@ -82,11 +87,11 @@ public static ChatMessageS2CPacket resolveChatMessage(ChatMessageS2CPacket chatP
chatPacket.body(),
unsignedContent,
chatPacket.filterMask(),
new MessageType.Serialized(
chatPacket.serializedParameters().typeId(),
new MessageType.Parameters(
chatPacket.serializedParameters().type(),
name,
targetName
)
), ChatMessageS2CPacket::new);
), ChatMessageS2CPacket.CODEC, player.getRegistryManager());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import dev.onyxstudios.cca.api.v3.component.CopyableComponent;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
Expand Down Expand Up @@ -198,29 +201,19 @@ public boolean shouldSyncWith(ServerPlayerEntity player) {
@Override
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
GameProfile profile = this.getImpersonatedProfile();
UUID id = profile == null ? null : profile.getId();
String name = profile == null ? null : profile.getName();
buf.writeByte((id != null ? ID_PRESENT : 0) | (name != null ? NAME_PRESENT : 0));
if (id != null) {
buf.writeUuid(id);
}
if (name != null) {
buf.writeString(name);
if (profile == null) {
buf.writeBoolean(false);
} else {
buf.writeBoolean(true);
PacketCodecs.GAME_PROFILE.encode(buf, profile);
}
}

@Override
public void applySyncPacket(PacketByteBuf buf) {
byte flags = buf.readByte();
UUID id = null;
String name = null;
if ((flags & ID_PRESENT) != 0) {
id = buf.readUuid();
}
if ((flags & NAME_PRESENT) != 0) {
name = buf.readString();
}
this.setImpersonatedProfile((id == null && name == null) ? null : new GameProfile(id, name));
boolean present = buf.readBoolean();
GameProfile profile = present ? PacketCodecs.GAME_PROFILE.decode(buf) : null;
this.setImpersonatedProfile(profile);
}

@Override
Expand All @@ -231,9 +224,11 @@ public void readFromNbt(@NotNull NbtCompound tag) {
for (int i = 0; i < impersonations.size(); i++) {
NbtCompound nbtEntry = impersonations.getCompound(i);
Identifier key = Identifier.tryParse(nbtEntry.getString("impersonation_key"));
GameProfile profile = NbtHelper.toGameProfile(nbtEntry);
if (key != null && profile != null) {
this.stackedImpersonations.put(key, profile);
if (key != null) {
ProfileComponent.CODEC
.parse(NbtOps.INSTANCE, nbtEntry)
.resultOrPartial(err -> Impersonate.LOGGER.error("Failed to load impersonated profile: {}", err))
.ifPresent(profile -> this.stackedImpersonations.put(key, profile.gameProfile()));
}
}
this.resetImpersonation();
Expand All @@ -245,9 +240,9 @@ public void writeToNbt(@NotNull NbtCompound tag) {
if (this.isImpersonating()) {
NbtList profiles = new NbtList();
for (var entry : this.stackedImpersonations.entrySet()) {
NbtCompound nbtEntry = new NbtCompound();
NbtCompound nbtEntry = (NbtCompound) ProfileComponent.CODEC.encodeStart(NbtOps.INSTANCE, new ProfileComponent(entry.getValue())).getOrThrow();
nbtEntry.putString("impersonation_key", entry.getKey().toString());
profiles.add(NbtHelper.writeGameProfile(nbtEntry, entry.getValue()));
profiles.add(nbtEntry);
}
tag.put("impersonations", profiles);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/ladysnake/impersonate/impl/ReloadSkinPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.ladysnake.impersonate.impl;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import org.ladysnake.impersonate.Impersonate;

public final class ReloadSkinPacket implements CustomPayload {
public static final CustomPayload.Id<ReloadSkinPacket> ID = new CustomPayload.Id<>(Impersonate.id("impersonation"));
public static final ReloadSkinPacket INSTANCE = new ReloadSkinPacket();
public static final PacketCodec<ByteBuf, ReloadSkinPacket> CODEC = PacketCodec.unit(INSTANCE);

@Override
public Id<? extends CustomPayload> getId() {
return ID;
}

private ReloadSkinPacket() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.play.CommonPlayerSpawnInfo;
import net.minecraft.network.packet.s2c.play.EntityPassengersSetS2CPacket;
import net.minecraft.network.packet.s2c.play.EntityStatusEffectS2CPacket;
Expand Down Expand Up @@ -74,7 +75,6 @@
* @author samo_lego
*/
public final class ServerPlayerSkins {
public static final Identifier RELOAD_SKIN_PACKET = new Identifier("impersonate", "impersonation");
private static final boolean FORCE_VANILLA_RELOADING = Boolean.getBoolean("impersonate.force_vanilla_reloading");
private static final ExecutorService THREADPOOL = Executors.newCachedThreadPool();
private static CompletableFuture<Pair<String, String>> currentSkinTask = CompletableFuture.completedFuture(null);
Expand Down Expand Up @@ -155,10 +155,10 @@ private static void reloadSkin(ServerPlayerEntity player) {
trackerEntry.getEntry().startTracking(tracking);
}

if (FORCE_VANILLA_RELOADING || !ServerPlayNetworking.canSend(player, RELOAD_SKIN_PACKET)) {
if (FORCE_VANILLA_RELOADING || !ServerPlayNetworking.canSend(player, ReloadSkinPacket.ID)) {
reloadSkinVanilla(player);
} else {
ServerPlayNetworking.send(player, RELOAD_SKIN_PACKET, PacketByteBufs.empty());
ServerPlayNetworking.send(player, ReloadSkinPacket.INSTANCE);
}
}

Expand All @@ -167,7 +167,7 @@ private static void reloadSkinVanilla(ServerPlayerEntity player) {
ServerWorld targetWorld = (ServerWorld) player.getWorld();
player.networkHandler.sendPacket(new PlayerRespawnS2CPacket(
new CommonPlayerSpawnInfo(
targetWorld.getDimensionKey(),
targetWorld.getDimensionEntry(),
targetWorld.getRegistryKey(),
BiomeAccess.hashSeed(targetWorld.getSeed()),
player.interactionManager.getGameMode(),
Expand All @@ -184,7 +184,7 @@ private static void reloadSkinVanilla(ServerPlayerEntity player) {
player.networkHandler.sendPacket(new ExperienceBarUpdateS2CPacket(player.experienceProgress, player.totalExperience, player.experienceLevel));
player.networkHandler.sendPacket(new HealthUpdateS2CPacket(player.getHealth(), player.getHungerManager().getFoodLevel(), player.getHungerManager().getSaturationLevel()));
for (StatusEffectInstance statusEffect : player.getStatusEffects()) {
player.networkHandler.sendPacket(new EntityStatusEffectS2CPacket(player.getId(), statusEffect));
player.networkHandler.sendPacket(new EntityStatusEffectS2CPacket(player.getId(), statusEffect, true));
}
player.sendAbilitiesUpdate();
player.server.getPlayerManager().sendWorldInfo(player, targetWorld);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private Text revealImpersonatorsInChatMessageContent(Text messageContent) {

@ModifyVariable(method = "logChatMessage", at = @At("HEAD"), argsOnly = true)
private MessageType.Parameters revealImpersonatorsInChatMessageContent(MessageType.Parameters params) {
return new MessageType.Parameters(params.type(), impersonate$reveal(params.name()), params.targetName() == null ? null : impersonate$reveal(params.targetName()));
return new MessageType.Parameters(params.type(), impersonate$reveal(params.name()), params.targetName().map(this::impersonate$reveal));
}

@Unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private Packet<?> resolveFakeTextsInPackets(Packet<?> packet) {
}
} else if (packet instanceof PlayerListS2CPacket listPacket) {
if (this.existsImpersonator()) {
PlayerListS2CPacket copy = PacketMeddling.copyPacket(listPacket, PlayerListS2CPacket::new);
PlayerListS2CPacket copy = PacketMeddling.copyPacket(listPacket, PlayerListS2CPacket.CODEC, server.getRegistryManager());
PacketMeddling.resolvePlayerListEntries(copy, player);
return copy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void nameInChatGetsRevealed(TestContext ctx) throws NoSuchAlgorithmExcept
ctx.getWorld().getServer(),
new MockClientConnection(NetworkSide.CLIENTBOUND),
player,
ConnectedClientData.createDefault(player.getGameProfile())
ConnectedClientData.createDefault(player.getGameProfile(), false)
);
Impersonator.get(player).impersonate(IMPERSONATION_KEY, new GameProfile(UUID.randomUUID(), "impersonated"));
String text = "Hi";
Expand Down

0 comments on commit d69fcdd

Please sign in to comment.