another cosmetics update
This commit is contained in:
@@ -2,8 +2,8 @@ package dev.tggamesyt.szar;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import dev.tggamesyt.szar.client.ClientCosmetics.NameType;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
@@ -20,6 +20,7 @@ public class ServerCosmetics {
|
||||
|
||||
public static final Identifier SYNC_PACKET =
|
||||
new Identifier(Szar.MOD_ID, "cosmetic_sync");
|
||||
public static final Identifier MOJANG_CAPES_SYNC = new Identifier(Szar.MOD_ID, "mojang_capes_sync");
|
||||
|
||||
private static final String CAPES_URL =
|
||||
"https://raw.githubusercontent.com/tggamesyt/szar/main/capes.json";
|
||||
@@ -51,6 +52,40 @@ public class ServerCosmetics {
|
||||
public static void init() {
|
||||
loadJson();
|
||||
registerCommand();
|
||||
ServerPlayNetworking.registerGlobalReceiver(MOJANG_CAPES_SYNC, (server, player, handler, buf, responseSender) -> {
|
||||
UUID uuid = buf.readUuid();
|
||||
int size = buf.readInt();
|
||||
List<MojangCape> list = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
MojangCape c = new MojangCape();
|
||||
c.id = buf.readString();
|
||||
c.name = buf.readString();
|
||||
c.url = buf.readString();
|
||||
list.add(c);
|
||||
}
|
||||
|
||||
PLAYER_MOJANG_CAPES.put(uuid, list);
|
||||
});
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||
ServerPlayerEntity player = handler.getPlayer();
|
||||
|
||||
// Send this player's own cosmetics to themselves
|
||||
UserCosmetics user = USERS.get(player.getUuid());
|
||||
if (user != null) {
|
||||
sync(player, user);
|
||||
}
|
||||
|
||||
// Optionally: send all other players' cosmetics to this new player
|
||||
for (ServerPlayerEntity other : server.getPlayerManager().getPlayerList()) {
|
||||
if (other.equals(player)) continue;
|
||||
|
||||
UserCosmetics otherUser = USERS.get(other.getUuid());
|
||||
if (otherUser != null) {
|
||||
sync(player, otherUser); // send other players’ cosmetics to the new player
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------- LOAD JSON ---------------- */
|
||||
@@ -117,7 +152,7 @@ public class ServerCosmetics {
|
||||
private static void registerCommand() {
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
|
||||
dispatcher.register(CommandManager.literal("cape")
|
||||
.then(CommandManager.argument("id", StringArgumentType.word())
|
||||
.then(CommandManager.argument("id", StringArgumentType.greedyString()) // <-- change here
|
||||
.suggests((ctx, builder) -> {
|
||||
|
||||
ServerPlayerEntity player = ctx.getSource().getPlayer();
|
||||
@@ -129,16 +164,16 @@ public class ServerCosmetics {
|
||||
// Mojang capes
|
||||
List<MojangCape> mojang = PLAYER_MOJANG_CAPES.get(player.getUuid());
|
||||
if (mojang != null) {
|
||||
for (MojangCape c : mojang) builder.suggest(c.name);
|
||||
for (MojangCape c : mojang)
|
||||
builder.suggest(c.name);
|
||||
}
|
||||
|
||||
builder.suggest("none");
|
||||
return builder.buildFuture();
|
||||
})
|
||||
.executes(ctx -> {
|
||||
|
||||
ServerPlayerEntity player = ctx.getSource().getPlayer();
|
||||
String id = StringArgumentType.getString(ctx, "id");
|
||||
String id = StringArgumentType.getString(ctx, "id"); // this now includes spaces
|
||||
|
||||
UserCosmetics user = USERS.get(player.getUuid());
|
||||
if (user == null) return 0;
|
||||
@@ -156,7 +191,7 @@ public class ServerCosmetics {
|
||||
if (mojang != null) {
|
||||
for (MojangCape c : mojang) {
|
||||
if (c.name.equalsIgnoreCase(id)) {
|
||||
user.selectedCape = null; // vanilla cape
|
||||
user.selectedCape = c.id; // vanilla cape
|
||||
sync(player, user);
|
||||
player.sendMessage(Text.literal("Equipped Mojang cape: " + c.name), false);
|
||||
return 1;
|
||||
@@ -176,30 +211,50 @@ public class ServerCosmetics {
|
||||
return 1;
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
/* ---------------- SYNC ---------------- */
|
||||
|
||||
public static void sync(ServerPlayerEntity player, UserCosmetics user) {
|
||||
PacketByteBuf buf = PacketByteBufs.create();
|
||||
for (ServerPlayerEntity p : player.getServer().getPlayerManager().getPlayerList()) {
|
||||
PacketByteBuf buf = PacketByteBufs.create();
|
||||
|
||||
buf.writeEnumConstant(user.nameType);
|
||||
buf.writeBoolean(user.staticColor != null);
|
||||
if (user.staticColor != null) buf.writeInt(user.staticColor);
|
||||
// Write player UUID first
|
||||
buf.writeUuid(player.getUuid());
|
||||
|
||||
buf.writeBoolean(user.gradientStart != null);
|
||||
if (user.gradientStart != null) {
|
||||
buf.writeInt(user.gradientStart);
|
||||
buf.writeInt(user.gradientEnd);
|
||||
// Cosmetic data
|
||||
buf.writeEnumConstant(user.nameType);
|
||||
buf.writeBoolean(user.staticColor != null);
|
||||
if (user.staticColor != null) buf.writeInt(user.staticColor);
|
||||
|
||||
buf.writeBoolean(user.gradientStart != null);
|
||||
if (user.gradientStart != null) {
|
||||
buf.writeInt(user.gradientStart);
|
||||
buf.writeInt(user.gradientEnd);
|
||||
}
|
||||
|
||||
String textureUrl = null;
|
||||
if (user.selectedCape != null) {
|
||||
textureUrl = CAPES.get(user.selectedCape);
|
||||
if (textureUrl == null) {
|
||||
List<MojangCape> mojang = PLAYER_MOJANG_CAPES.get(player.getUuid());
|
||||
if (mojang != null) {
|
||||
for (MojangCape c : mojang) {
|
||||
if (c.id.equalsIgnoreCase(user.selectedCape)) {
|
||||
textureUrl = c.url;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.writeString(textureUrl == null ? "" : textureUrl);
|
||||
ServerPlayNetworking.send(p, SYNC_PACKET, buf);
|
||||
}
|
||||
|
||||
String textureUrl = user.selectedCape != null
|
||||
? CAPES.get(user.selectedCape)
|
||||
: "";
|
||||
|
||||
buf.writeString(textureUrl == null ? "" : textureUrl);
|
||||
ServerPlayNetworking.send(player, SYNC_PACKET, buf);
|
||||
}
|
||||
public enum NameType {
|
||||
STATIC,
|
||||
GRADIENT
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
}
|
||||
},
|
||||
"result": {
|
||||
"item": "szar:atom",
|
||||
"item": "szar:nuke",
|
||||
"count": 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"item": "minecraft:redstone"
|
||||
},
|
||||
"A": {
|
||||
"item": "szar:atom"
|
||||
"item": "szar:nuke"
|
||||
},
|
||||
"I": {
|
||||
"item": "minecraft:iron_ingot"
|
||||
|
||||
Reference in New Issue
Block a user