Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 229
src/client/java/com/kodari/playerstyle/PlayerStyleClient.java
package com.kodari.playerstyle;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.player.PlayerModel;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.entity.state.AvatarRenderState;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class PlayerStyleClient implements ClientModInitializer {
private static final Map<String, Customization> CUSTOMIZATIONS = new HashMap<>();
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> dispatcher.register(
ClientCommands.literal("playerstyle")
.executes(context -> {
context.getSource().sendFeedback(Component.literal(
"Usage: /playerstyle set <player> <chest|hips> <0.5-2.0> or /playerstyle reset <player>"));
return 1;
})
.then(ClientCommands.literal("set")
.then(ClientCommands.argument("player", StringArgumentType.word())
.then(ClientCommands.argument("feature", StringArgumentType.word())
.then(ClientCommands.argument("scale", FloatArgumentType.floatArg(0.5F, 2.0F))
.executes(context -> {
String targetName = StringArgumentType.getString(context, "player");
String feature = StringArgumentType.getString(context, "feature");
float scale = FloatArgumentType.getFloat(context, "scale");
if (!feature.equalsIgnoreCase("chest") && !feature.equalsIgnoreCase("hips")) {
context.getSource().sendFeedback(Component.literal("Feature must be chest or hips."));
return 0;
}
Player target = findPlayer(targetName);
if (target == null) {
context.getSource().sendFeedback(Component.literal("Player is not currently visible to this client: " + targetName));
return 0;
}
Customization customization = CUSTOMIZATIONS.computeIfAbsent(key(target.getPlainTextName()), ignored -> new Customization());
if (feature.equalsIgnoreCase("chest")) {
customization.chest = scale;
} else {
customization.hips = scale;
}
context.getSource().sendFeedback(Component.literal(
"Set " + feature.toLowerCase(Locale.ROOT) + " scale for " + target.getPlainTextName() + " to " + scale + "."));
return 1;
})))))
.then(ClientCommands.literal("reset")
.then(ClientCommands.argument("player", StringArgumentType.word())
.executes(context -> {
String targetName = StringArgumentType.getString(context, "player");
Player target = findPlayer(targetName);
if (target == null) {
context.getSource().sendFeedback(Component.literal("Player is not currently visible to this client: " + targetName));
return 0;
}
CUSTOMIZATIONS.remove(key(target.getPlainTextName()));
context.getSource().sendFeedback(Component.literal("Reset local style for " + target.getPlainTextName() + "."));
return 1;
})))
));
}
public static void apply(PlayerModel model, AvatarRenderState state) {
Minecraft minecraft = Minecraft.getInstance();
Player player = null;
if (minecraft.level != null && minecraft.level.getEntity(state.id) instanceof Player renderedPlayer) {
player = renderedPlayer;
}
Customization customization = player == null ? null : CUSTOMIZATIONS.get(key(player.getPlainTextName()));
float chestScale = customization == null ? 1.0F : customization.chest;
float hipsScale = customization == null ? 1.0F : customization.hips;
model.body.xScale = chestScale;
model.body.zScale = chestScale;
model.jacket.xScale = chestScale;
model.jacket.zScale = chestScale;
model.rightLeg.xScale = hipsScale;
model.rightLeg.zScale = hipsScale;
model.leftLeg.xScale = hipsScale;
model.leftLeg.zScale = hipsScale;
model.rightPants.xScale = hipsScale;
model.rightPants.zScale = hipsScale;
model.leftPants.xScale = hipsScale;
model.leftPants.zScale = hipsScale;
}
private static Player findPlayer(String name) {
ClientLevel level = Minecraft.getInstance().level;
if (level == null) {
return null;
}
for (Player player : level.players()) {
if (player.getPlainTextName().equalsIgnoreCase(name)) {
return player;
}
}
return null;
}
private static String key(String name) {
return name.toLowerCase(Locale.ROOT);
}
private static final class Customization {
private float chest = 1.0F;
private float hips = 1.0F;
}
}