Skip to content

Commit

Permalink
Merge branch 'master' into 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Sep 17, 2024
2 parents 087392d + d26fa15 commit 4350928
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@
package net.frozenblock.lib.cape.api;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import net.frozenblock.lib.cape.impl.Cape;
import net.frozenblock.lib.registry.api.FrozenRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

public class CapeRegistry {
public static final Cape DUMMY_CAPE = new Cape(null, null);
private static final Map<ResourceLocation, List<UUID>> CAPE_RESTRICTIONS = new HashMap<>();
private static final ArrayList<Cape> CAPES = new ArrayList<>(Collections.singleton(DUMMY_CAPE));

public class CapeUtil {
public static @NotNull @Unmodifiable List<Cape> getCapes() {
return ImmutableList.copyOf(CAPES);
return ImmutableList.copyOf(FrozenRegistry.CAPE);
}

public static @NotNull @Unmodifiable List<Cape> getUsableCapes(UUID uuid) {
return ImmutableList.copyOf(getCapes().stream().filter(cape -> canPlayerUserCape(uuid, cape)).toList());
}

public static boolean canPlayerUserCape(UUID uuid, ResourceLocation capeID) {
Optional<Cape> optionalCape = FrozenRegistry.CAPE.getOptional(capeID);
return optionalCape.map(cape -> canPlayerUserCape(uuid, cape)).orElse(false);
}

public static boolean canPlayerUserCape(UUID uuid, ResourceLocation texture) {
List<UUID> allowedUUIDs = CAPE_RESTRICTIONS.get(texture);
if (allowedUUIDs == null) return true;
return allowedUUIDs.contains(uuid);
public static boolean canPlayerUserCape(UUID uuid, @NotNull Cape cape) {
return cape.allowedPlayers().map(uuids -> uuids.contains(uuid)).orElse(true);
}

public static void registerCape(ResourceLocation cape) {
CAPES.add(new Cape(cape, buildCapeTextureLocation(cape)));
public static @NotNull Cape registerCape(ResourceLocation id) {
return Registry.register(FrozenRegistry.CAPE, id, new Cape(id, buildCapeTextureLocation(id), Optional.empty()));
}

public static void registerRestrictedCape(ResourceLocation cape, List<UUID> allowedPlayers) {
ResourceLocation textureLocation = buildCapeTextureLocation(cape);
CAPES.add(new Cape(cape, textureLocation));
CAPE_RESTRICTIONS.put(textureLocation, allowedPlayers);
public static @NotNull Cape registerCapeWithWhitelist(ResourceLocation id, List<UUID> allowedPlayers) {
return Registry.register(FrozenRegistry.CAPE, id, new Cape(id, buildCapeTextureLocation(id), Optional.of(allowedPlayers)));
}

private static ResourceLocation buildCapeTextureLocation(@NotNull ResourceLocation cape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,50 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.frozenblock.lib.cape.client.impl.PlayerCapeInterface;
import net.frozenblock.lib.cape.networking.CapeCustomizePacket;
import net.frozenblock.lib.cape.impl.Cape;
import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket;
import net.frozenblock.lib.config.frozenlib_config.FrozenLibConfig;
import net.frozenblock.lib.registry.api.FrozenRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class FrozenClientCapeData {
private static final Map<UUID, ResourceLocation> CAPES_IN_SERVER = new HashMap<>();
private static final Map<UUID, Cape> CAPES_IN_SERVER = new HashMap<>();

public static Optional<ResourceLocation> getCapeTexture(UUID uuid) {
return Optional.ofNullable(CAPES_IN_SERVER.get(uuid));
return Optional.ofNullable(CAPES_IN_SERVER.get(uuid)).map(Cape::texture);
}

public static void setCapeForUUID(UUID uuid, ResourceLocation texture) {
CAPES_IN_SERVER.put(uuid, texture);
setPlayerCapeTexture(uuid, texture);
public static void setCapeForUUID(UUID uuid, ResourceLocation capeId) {
Cape cape = FrozenRegistry.CAPE.getValue(capeId);
if (cape == null) {
removeCapeForUUID(uuid);
} else {
CAPES_IN_SERVER.put(uuid, cape);
setPlayerCapeTexture(uuid, Optional.of(cape));
}
}

public static void removeCapeForUUID(UUID uuid) {
CAPES_IN_SERVER.remove(uuid);
setPlayerCapeTexture(uuid, null);
setPlayerCapeTexture(uuid, Optional.empty());
}

private static void setPlayerCapeTexture(UUID uuid, @Nullable ResourceLocation texture) {
private static void setPlayerCapeTexture(UUID uuid, @NotNull Optional<Cape> cape) {
ClientLevel level = Minecraft.getInstance().level;
if (level != null && level.getPlayerByUUID(uuid) instanceof PlayerCapeInterface capeInterface) {
capeInterface.frozenLib$setCape(texture);
capeInterface.frozenLib$setCape(cape.map(Cape::texture).orElse(null));
}
}

public static void init() {
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> CAPES_IN_SERVER.clear());
ClientPlayConnectionEvents.DISCONNECT.register((clientPacketListener, minecraft) -> CAPES_IN_SERVER.clear());
ClientPlayConnectionEvents.JOIN.register((clientPacketListener, packetSender, minecraft) -> {
ClientPlayNetworking.send(CapeCustomizePacket.createPacket(minecraft.getUser().getProfileId(), FrozenLibConfig.get().cape.texture()));
ClientPlayNetworking.send(CapeCustomizePacket.createPacket(minecraft.getUser().getProfileId(), FrozenLibConfig.get().cape));
});
ClientEntityEvents.ENTITY_LOAD.register((entity, clientLevel) -> {
if (entity instanceof PlayerCapeInterface capeInterface) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/frozenblock/lib/cape/impl/Cape.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package net.frozenblock.lib.cape.impl;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import net.minecraft.resources.ResourceLocation;

public record Cape(ResourceLocation location, ResourceLocation texture) {
public record Cape(ResourceLocation registryId, ResourceLocation texture, Optional<List<UUID>> allowedPlayers) {
}
11 changes: 5 additions & 6 deletions src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.frozenblock.lib.FrozenSharedConstants;
import net.frozenblock.lib.cape.api.CapeRegistry;
import net.frozenblock.lib.cape.networking.CapeCustomizePacket;
import net.minecraft.resources.ResourceLocation;
import net.frozenblock.lib.cape.api.CapeUtil;
import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket;
import net.minecraft.server.level.ServerPlayer;

public class ServerCapeData {
private static final Map<UUID, ResourceLocation> CAPES_IN_SERVER = new HashMap<>();
private static final Map<UUID, Cape> CAPES_IN_SERVER = new HashMap<>();

public static void sendAllCapesToPlayer(ServerPlayer recipent) {
CAPES_IN_SERVER.forEach((uuid, resourceLocation) -> ServerPlayNetworking.send(recipent, CapeCustomizePacket.createPacket(uuid, resourceLocation)));
CAPES_IN_SERVER.forEach((uuid, cape) -> ServerPlayNetworking.send(recipent, CapeCustomizePacket.createPacket(uuid, cape)));
}

public static void init() {
Expand Down Expand Up @@ -77,6 +76,6 @@ public static void init() {
allArray.addAll(builders);
allArray.addAll(composers);

CapeRegistry.registerRestrictedCape(FrozenSharedConstants.id("frozenblock"), allArray);
CapeUtil.registerCapeWithWhitelist(FrozenSharedConstants.id("frozenblock"), allArray);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,83 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.frozenblock.lib.cape.networking;
package net.frozenblock.lib.cape.impl.networking;

import java.util.UUID;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.frozenblock.lib.FrozenSharedConstants;
import net.frozenblock.lib.cape.impl.Cape;
import net.frozenblock.lib.registry.api.FrozenRegistry;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class CapeCustomizePacket implements CustomPacketPayload {
private static final ResourceLocation DUMMY = FrozenSharedConstants.id("dummy");
public static final Type<CapeCustomizePacket> PACKET_TYPE = new Type<>(FrozenSharedConstants.id("cape_packet"));
public static final StreamCodec<FriendlyByteBuf, CapeCustomizePacket> CODEC = StreamCodec.ofMember(CapeCustomizePacket::write, CapeCustomizePacket::new);

private final UUID playerUUID;
private final boolean enabled;
private ResourceLocation capeTexture = null;
private ResourceLocation capeId = null;

private CapeCustomizePacket(UUID uuid, boolean enabled) {
this.playerUUID = uuid;
this.enabled = enabled;
}

private CapeCustomizePacket(UUID uuid, boolean enabled, ResourceLocation capeId) {
this(uuid, enabled && capeId != null);
this.capeTexture = capeId;
this(uuid, enabled);
this.capeId = capeId;
}

public CapeCustomizePacket(@NotNull FriendlyByteBuf buf) {
this(buf.readUUID(), buf.readBoolean());
if (this.enabled) {
this.capeTexture = buf.readResourceLocation();
this.capeId = buf.readResourceLocation();
}
}

public void write(@NotNull FriendlyByteBuf buf) {
buf.writeUUID(this.playerUUID);
buf.writeBoolean(this.enabled);
if (this.enabled) {
buf.writeResourceLocation(this.capeTexture);
buf.writeResourceLocation(this.capeId);
}
}

public static @NotNull CapeCustomizePacket createDisablePacket(UUID uuid) {
return new CapeCustomizePacket(uuid, false);
}

public static @NotNull CapeCustomizePacket createPacket(UUID uuid, ResourceLocation capeId) {
return new CapeCustomizePacket(uuid, true, capeId);
public static @NotNull CapeCustomizePacket createPacket(UUID uuid, @Nullable ResourceLocation capeId) {
return new CapeCustomizePacket(uuid, !shouldDisable(FrozenRegistry.CAPE.getValue(capeId)), capeId);
}

public static void sendCapeToAll(MinecraftServer server, UUID uuid, ResourceLocation capeId) {
CapeCustomizePacket frozenCapePacket = capeId == null ? CapeCustomizePacket.createDisablePacket(uuid) : CapeCustomizePacket.createPacket(uuid, capeId);
@Contract("_, _ -> new")
public static @NotNull CapeCustomizePacket createPacket(UUID uuid, @NotNull Cape cape) {
return new CapeCustomizePacket(uuid, !shouldDisable(cape), cape.registryId());
}

public static void sendCapeToAll(MinecraftServer server, UUID uuid, @Nullable ResourceLocation capeId) {
CapeCustomizePacket frozenCapePacket = CapeCustomizePacket.createPacket(uuid, capeId);
PlayerLookup.all(server).forEach(player -> ServerPlayNetworking.send(player, frozenCapePacket));
}

public static boolean shouldDisable(Cape cape) {
return cape == null || shouldDisable(cape.registryId()) || cape.texture() == null;
}

public static boolean shouldDisable(ResourceLocation capeId) {
return capeId == null || capeId.equals(DUMMY);
}

public UUID getPlayerUUID() {
return this.playerUUID;
}
Expand All @@ -82,8 +100,8 @@ public boolean isEnabled() {
return this.enabled;
}

public ResourceLocation getCapeTexture() {
return this.capeTexture;
public ResourceLocation getCapeId() {
return this.capeId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import net.fabricmc.loader.api.FabricLoader;
import net.frozenblock.lib.FrozenSharedConstants;
import net.frozenblock.lib.cape.api.CapeRegistry;
import net.frozenblock.lib.cape.impl.Cape;
import net.frozenblock.lib.config.api.instance.Config;
import net.frozenblock.lib.config.api.instance.json.JsonConfig;
import net.frozenblock.lib.config.api.instance.json.JsonType;
import net.frozenblock.lib.config.api.registry.ConfigRegistry;
import net.frozenblock.lib.config.api.sync.SyncBehavior;
import net.frozenblock.lib.config.api.sync.annotation.EntrySyncData;
import net.minecraft.resources.ResourceLocation;
import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixerBuilder;
import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixes;

Expand Down Expand Up @@ -80,7 +79,7 @@ public void onSync(FrozenLibConfig syncInstance) {
public boolean wardenSpawnTrackerCommand = false;

@EntrySyncData(value = "cape", behavior = SyncBehavior.UNSYNCABLE)
public Cape cape = CapeRegistry.DUMMY_CAPE;
public ResourceLocation cape = FrozenSharedConstants.id("dummy");

@ConfigEntry.Gui.CollapsibleObject
public final DataFixerConfig dataFixer = new DataFixerConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.frozenblock.lib.FrozenSharedConstants;
import net.frozenblock.lib.cape.api.CapeRegistry;
import net.frozenblock.lib.cape.impl.Cape;
import net.frozenblock.lib.cape.networking.CapeCustomizePacket;
import net.frozenblock.lib.cape.api.CapeUtil;
import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket;
import net.frozenblock.lib.config.api.instance.Config;
import net.frozenblock.lib.config.clothconfig.FrozenClothConfig;
import net.frozenblock.lib.config.frozenlib_config.FrozenLibConfig;
Expand Down Expand Up @@ -137,33 +136,27 @@ private static void setupEntries(@NotNull ConfigCategory category, @NotNull Conf
);

UUID playerUUID = Minecraft.getInstance().getUser().getProfileId();
List<Cape> usableCapes = new ArrayList<>();
CapeRegistry.getCapes().forEach(cape -> {
if (CapeRegistry.canPlayerUserCape(playerUUID, cape.texture())) {
usableCapes.add(cape);
}
List<ResourceLocation> usableCapes = new ArrayList<>();
CapeUtil.getUsableCapes(playerUUID).forEach(cape -> {
usableCapes.add(cape.registryId());
});
if (!usableCapes.isEmpty()) {
var capeEntry = category.addEntry(
FrozenClothConfig.syncedEntry(
entryBuilder.startSelector(text("cape"), usableCapes.toArray(), modifiedConfig.cape)
.setDefaultValue(defaultConfig.cape)
.setNameProvider(o -> {
ResourceLocation capeName = ((Cape) o).location();
Component component;
if (capeName == null) {
component = Component.translatable("cape.frozenlib.none");
} else {
component = Component.translatable("cape." + capeName.getNamespace() + "." + capeName.getPath());
ResourceLocation capeId = (ResourceLocation) o;
if (o != null) {
return Component.translatable("cape." + capeId.getNamespace() + "." + capeId.getPath());
}
return component;
return Component.translatable("cape.frozenlib.none");
})
.setSaveConsumer(newValue -> {
if (newValue instanceof Cape cape) {
config.cape = cape;
if (Minecraft.getInstance().getConnection() != null) {
ClientPlayNetworking.send(CapeCustomizePacket.createPacket(playerUUID, cape.texture()));
}
ResourceLocation capeId = (ResourceLocation) newValue;
config.cape = capeId;
if (Minecraft.getInstance().getConnection() != null) {
ClientPlayNetworking.send(CapeCustomizePacket.createPacket(playerUUID, capeId));
}
})
.setTooltip(tooltip("cape"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.frozenblock.lib.cape.client.FrozenClientCapeData;
import net.frozenblock.lib.cape.networking.CapeCustomizePacket;
import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket;
import net.frozenblock.lib.config.api.instance.Config;
import net.frozenblock.lib.config.api.registry.ConfigRegistry;
import net.frozenblock.lib.config.impl.network.ConfigSyncPacket;
Expand Down Expand Up @@ -331,7 +331,7 @@ private static void receiveCapePacket() {
ClientPlayNetworking.registerGlobalReceiver(CapeCustomizePacket.PACKET_TYPE, (packet, ctx) -> {
UUID uuid = packet.getPlayerUUID();
if (packet.isEnabled()) {
FrozenClientCapeData.setCapeForUUID(uuid, packet.getCapeTexture());
FrozenClientCapeData.setCapeForUUID(uuid, packet.getCapeId());
} else {
FrozenClientCapeData.removeCapeForUUID(uuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.frozenblock.lib.cape.api.CapeRegistry;
import net.frozenblock.lib.cape.api.CapeUtil;
import net.frozenblock.lib.cape.impl.ServerCapeData;
import net.frozenblock.lib.cape.networking.CapeCustomizePacket;
import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket;
import net.frozenblock.lib.config.impl.network.ConfigSyncPacket;
import net.frozenblock.lib.debug.networking.GoalDebugRemovePayload;
import net.frozenblock.lib.debug.networking.ImprovedGameEventDebugPayload;
Expand Down Expand Up @@ -120,8 +120,8 @@ public static void registerNetworking() {
ServerPlayNetworking.registerGlobalReceiver(CapeCustomizePacket.PACKET_TYPE,
(packet, ctx) -> {
UUID uuid = ctx.player().getUUID();
ResourceLocation capeTexture = packet.getCapeTexture();
if (capeTexture == null || CapeRegistry.canPlayerUserCape(uuid, capeTexture)) {
ResourceLocation capeTexture = packet.getCapeId();
if (capeTexture == null || CapeUtil.canPlayerUserCape(uuid, capeTexture)) {
CapeCustomizePacket.sendCapeToAll(ctx.server(), uuid, capeTexture);
}
}
Expand Down
Loading

0 comments on commit 4350928

Please sign in to comment.