Skip to content

Commit

Permalink
1.19.3 working
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Dec 8, 2022
1 parent ea2ce1f commit 94bf11c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 72 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ loader_version=0.14.11
fabric_version=0.68.1+1.19.3
quilt_mappings=1
# Mod Properties
mod_version=2.0.1
mod_version=2.0.2
maven_group=org.samo_lego
archives_base_name=fabrictailor
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.minecraft.client.KeyMapping;
import org.lwjgl.glfw.GLFW;
import org.samo_lego.fabrictailor.client.screen.SkinChangeScreen;
import org.samo_lego.fabrictailor.network.NetworkHandler;
import org.samo_lego.fabrictailor.network.SkinPackets;
import org.samo_lego.fabrictailor.util.TextTranslations;

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ public void onInitializeClient() {
forceOpen = false;
});

ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.FT_HELLO, (client, handler, buf, responseSender) -> {
ClientPlayNetworking.registerGlobalReceiver(SkinPackets.FT_HELLO, (client, handler, buf, responseSender) -> {
TAILORED_SERVER = true;
ALLOW_DEFAULT_SKIN = buf.readBoolean();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;

import static org.samo_lego.fabrictailor.FabricTailor.*;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class TailorConfig implements IBrigadierConfigurator {
public String customSkinServer = "";

@SerializedName("allowed_texture_domains")
public Set<String> allowedTextureDomains = Set.of(
public Set<String> allowedTextureDomains = new HashSet<>(Set.of(
"minecraft.net",
"mojang.com",
"crafatar.com",
Expand All @@ -58,7 +59,7 @@ public class TailorConfig implements IBrigadierConfigurator {
"planetminecraft.com",
"googleusercontent.com",
"nocookie.net"
);
));

@Override
public void save() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.minecraft.client.resources.SkinManager;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.*;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.BiomeManager;
import org.samo_lego.fabrictailor.casts.TailoredPlayer;
import org.samo_lego.fabrictailor.mixin.accessors.AChunkMap;
import org.samo_lego.fabrictailor.mixin.accessors.ATrackedEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -32,66 +38,79 @@
import static org.samo_lego.fabrictailor.mixin.accessors.APlayer.getPLAYER_MODEL_PARTS;

@Mixin(ServerPlayer.class)
public class MServerPlayerEntity_TailoredPlayer implements TailoredPlayer {
public abstract class MServerPlayerEntity_TailoredPlayer extends Player implements TailoredPlayer {

@Unique
private static final String STEVE = "MHF_STEVE";
private final ServerPlayer player = (ServerPlayer) (Object) this;
private final GameProfile gameProfile = player.getGameProfile();

@Unique
private final ServerPlayer self = (ServerPlayer) (Object) this;
@Unique
private final GameProfile gameProfile = self.getGameProfile();
@Unique
private final PropertyMap map = this.gameProfile.getProperties();
@Shadow
public ServerGamePacketListenerImpl connection;
@Unique
private String skinValue;
@Unique
private String skinSignature;
private final PropertyMap map = this.gameProfile.getProperties();
@Unique
private long lastSkinChangeTime = 0;

public MServerPlayerEntity_TailoredPlayer(Level level, BlockPos blockPos, float f, GameProfile gameProfile) {
super(level, blockPos, f, gameProfile);
}


/**
* @author Pyrofab
* @see PlayerList#respawn(ServerPlayer, boolean)
* <p>
* This method has been adapted from the Impersonate mod's <a href="https://github.com/Ladysnake/Impersonate/blob/1.16/src/main/java/io/github/ladysnake/impersonate/impl/ServerPlayerSkins.java">source code</a>
* under GNU Lesser General Public License.
* <p>
* Reloads player's skin for all the players (including the one that has changed the skin)
* </p>
*
* @author Pyrofab
*/
@Override
public void reloadSkin() {
// Refreshing tablist for each player
if (player.getServer() == null)
return;

PlayerList playerManager = player.getServer().getPlayerList();
playerManager.broadcastAll(new ClientboundPlayerInfoRemovePacket(new ArrayList<>(Collections.singleton(player.getUUID()))));
playerManager.broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, player));
// Refreshing in tablist for each player
PlayerList playerManager = self.getServer().getPlayerList();
playerManager.broadcastAll(new ClientboundPlayerInfoRemovePacket(new ArrayList<>(Collections.singleton(self.getUUID()))));
playerManager.broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, self));

ServerChunkCache manager = player.getLevel().getChunkSource();
ServerChunkCache manager = self.getLevel().getChunkSource();
ChunkMap storage = manager.chunkMap;
ATrackedEntity trackerEntry = ((AChunkMap) storage).getEntityTrackers().get(player.getId());
ATrackedEntity trackerEntry = ((AChunkMap) storage).getEntityTrackers().get(self.getId());

// Refreshing skin in world for all that see the player
trackerEntry.getSeenBy().forEach(tracking -> trackerEntry.getServerEntity().addPairing(tracking.getPlayer()));

// need to change the player entity on the client
ServerLevel targetWorld = player.getLevel();
player.connection.send(new ClientboundRespawnPacket(targetWorld.dimensionTypeId(),
targetWorld.dimension(),
BiomeManager.obfuscateSeed(targetWorld.getSeed()),
player.gameMode.getGameModeForPlayer(),
player.gameMode.getPreviousGameModeForPlayer(),
targetWorld.isDebug(), targetWorld.isFlat(), (byte) 3,
player.getLastDeathLocation()));

player.connection.teleport(player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot());
player.server.getPlayerList().sendPlayerPermissionLevel(player);
player.connection.send(new ClientboundSetExperiencePacket(player.experienceProgress, player.totalExperience, player.experienceLevel));
player.connection.send(new ClientboundSetHealthPacket(player.getHealth(), player.getFoodData().getFoodLevel(), player.getFoodData().getSaturationLevel()));

for (MobEffectInstance statusEffect : player.getActiveEffects()) {
player.connection.send(new ClientboundUpdateMobEffectPacket(player.getId(), statusEffect));
ServerLevel level = self.getLevel();
this.connection.send(new ClientboundRespawnPacket(level.dimensionTypeId(),
level.dimension(),
BiomeManager.obfuscateSeed(level.getSeed()),
self.gameMode.getGameModeForPlayer(),
self.gameMode.getPreviousGameModeForPlayer(),
level.isDebug(), level.isFlat(), (byte) 3,
self.getLastDeathLocation()));

this.connection.teleport(this.getX(), this.getY(), this.getZ(), this.getYRot(), this.getXRot());

this.connection.send(new ClientboundChangeDifficultyPacket(level.getDifficulty(), level.getLevelData().isDifficultyLocked()));
this.connection.send(new ClientboundSetExperiencePacket(this.experienceProgress, this.totalExperience, this.experienceLevel));
playerManager.sendLevelInfo(self, level);
playerManager.sendPlayerPermissionLevel(self);

this.connection.send(new ClientboundSetHealthPacket(this.getHealth(), this.getFoodData().getFoodLevel(), this.getFoodData().getSaturationLevel()));
for (MobEffectInstance statusEffect : this.getActiveEffects()) {
this.connection.send(new ClientboundUpdateMobEffectPacket(self.getId(), statusEffect));
}

player.onUpdateAbilities();
playerManager.sendLevelInfo(player, targetWorld);
playerManager.sendAllPlayerInfo(player);
this.onUpdateAbilities();
playerManager.sendAllPlayerInfo(self);

}

/**
Expand All @@ -115,8 +134,9 @@ public void setSkin(Property skinData, boolean reload) {
this.skinSignature = skinData.getSignature();

// Reloading skin
if(reload)
if(reload) {
this.reloadSkin();
}

this.lastSkinChangeTime = System.currentTimeMillis();

Expand Down Expand Up @@ -182,7 +202,7 @@ public String getSkinId() {
String skin = this.skinValue;
if (skin == null) {
// Fallback to default skin
var textures = player.getGameProfile().getProperties().get("textures").stream().findAny();
var textures = self.getGameProfile().getProperties().get("textures").stream().findAny();

if (textures.isPresent()) {
skin = textures.get().getValue();
Expand Down Expand Up @@ -218,7 +238,7 @@ private void disableCapeIfNeeded(ServerboundClientInformationPacket packet, Call

// Fake cape rule to be off
playerModel = (byte) (playerModel & ~(1));
this.player.getEntityData().set(getPLAYER_MODEL_PARTS(), playerModel);
this.self.getEntityData().set(getPLAYER_MODEL_PARTS(), playerModel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ private void ft_enableInsecureValue(Property property, CallbackInfoReturnable<St
cir.setReturnValue(property.getValue());
}

@Inject(method = "isAllowedTextureDomain", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
@Inject(method = "isAllowedTextureDomain",
at = @At(value = "INVOKE_ASSIGN",
target = "Ljava/net/URI;getHost()Ljava/lang/String;"),
locals = LocalCapture.CAPTURE_FAILHARD,
cancellable = true)
private static void ft_allowAllTextureDomains(String url, CallbackInfoReturnable<Boolean> cir, URI uri, String domain) {
if (url.startsWith("file://")) {
cir.setReturnValue(true);
cir.setReturnValue(false); // todo, allow files
return;
}

Expand All @@ -36,6 +40,13 @@ private static void ft_allowAllTextureDomains(String url, CallbackInfoReturnable
cir.setReturnValue(config.allowedTextureDomains.contains(topDomain));
}

/**
* Disables "requireSecure" boolean in order to allow skins from all
* domains, specified in config.
*
* @param requireSecure whether to require secure connection, ignored
* @return false
*/
@ModifyVariable(method = "getTextures", at = @At("HEAD"), argsOnly = true)
private boolean ft_disableSecureTextures(boolean requireSecure) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.samo_lego.fabrictailor.casts.TailoredPlayer;
import org.samo_lego.fabrictailor.util.TextTranslations;

import static org.samo_lego.fabrictailor.FabricTailor.*;
import static org.samo_lego.fabrictailor.FabricTailor.THREADPOOL;
import static org.samo_lego.fabrictailor.FabricTailor.config;
import static org.samo_lego.fabrictailor.util.SkinFetcher.fetchSkinByName;

public class NetworkHandler {
public static final ResourceLocation FT_HELLO = new ResourceLocation(MOD_ID, "hello");

public static void onJoin(ServerGamePacketListenerImpl listener, PacketSender _sender, MinecraftServer _server) {
var player = listener.getPlayer();
if (ServerPlayNetworking.canSend(listener, FT_HELLO)) {
ServerPlayNetworking.send(player, FT_HELLO, createHelloPacket(player.hasPermissions(2)));
if (ServerPlayNetworking.canSend(listener, SkinPackets.FT_HELLO)) {
ServerPlayNetworking.send(player, SkinPackets.FT_HELLO, createHelloPacket(player.hasPermissions(2)));
}

THREADPOOL.submit(() -> {
Expand All @@ -51,8 +50,9 @@ public static void onJoin(ServerGamePacketListenerImpl listener, PacketSender _s
skinData = new Property(SkinManager.PROPERTY_TEXTURES, value, signature);
}
// Try to set skin now
if (skinData != null)
if (skinData != null) {
((TailoredPlayer) player).setSkin(skinData, false);
}
((TailoredPlayer) player).resetLastSkinChange();
});
}
Expand All @@ -66,23 +66,8 @@ public static FriendlyByteBuf createHelloPacket(boolean allowDefaultSkinButton)
}

public static void changeVanillaSkinPacket(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl listener, FriendlyByteBuf buf, PacketSender sender) {
long lastChange = ((TailoredPlayer) player).getLastSkinChange();
long now = System.currentTimeMillis();

if (now - lastChange > config.skinChangeTimer * 1000 || lastChange == 0) {
// This is our skin change packet

((TailoredPlayer) player).setSkin(buf.readProperty(), true);
} else {
// Prevent skin change spamming
MutableComponent timeLeft = Component.literal(String.valueOf((config.skinChangeTimer * 1000 - now + lastChange) / 1000))
.withStyle(ChatFormatting.LIGHT_PURPLE);
player.displayClientMessage(
TextTranslations.create("command.fabrictailor.skin.timer.please_wait", timeLeft)
.withStyle(ChatFormatting.RED),
false
);
}
NetworkHandler.onSkinChangePacket(player, buf, () -> {
});
}

public static void defaultSkinPacket(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl listener, FriendlyByteBuf buf, PacketSender sender) {
Expand All @@ -100,15 +85,19 @@ public static void defaultSkinPacket(MinecraftServer server, ServerPlayer player
}

public static void changeHDSkinPacket(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl listener, FriendlyByteBuf buf, PacketSender sender) {
NetworkHandler.onSkinChangePacket(player, buf, () ->
player.displayClientMessage(TextTranslations.create("hint.fabrictailor.client_only")
.withStyle(ChatFormatting.DARK_PURPLE), false));
}


public static void onSkinChangePacket(ServerPlayer player, FriendlyByteBuf buf, Runnable callback) {
long lastChange = ((TailoredPlayer) player).getLastSkinChange();
long now = System.currentTimeMillis();

if (now - lastChange > config.skinChangeTimer * 1000 || lastChange == 0) {

player.displayClientMessage(TextTranslations.create("hint.fabrictailor.client_only")
.withStyle(ChatFormatting.DARK_PURPLE), false);

((TailoredPlayer) player).setSkin(buf.readProperty(), true);
callback.run();
} else {
// Prevent skin change spamming
MutableComponent timeLeft = Component.literal(String.valueOf((config.skinChangeTimer * 1000 - now + lastChange) / 1000))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SkinPackets {
public static final ResourceLocation FABRICTAILOR_VANILLA_CHANGE = new ResourceLocation(MOD_ID, "skin_change_vanilla");
public static final ResourceLocation FABRICTAILOR_HD_CHANGE = new ResourceLocation(MOD_ID, "skin_change_hd");
public static final ResourceLocation FABRICTAILOR_DEFAULT_SKIN = new ResourceLocation(MOD_ID, "default_skin_request");
public static final ResourceLocation FT_HELLO = new ResourceLocation(MOD_ID, "hello");

public static FriendlyByteBuf skin2ByteBuf(@NotNull Property skinData) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
Expand Down

0 comments on commit 94bf11c

Please sign in to comment.