diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index 98b58ac6..880e02f4 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -60,7 +60,7 @@ public class Unicopia implements ModInitializer { }); ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(TreeTypeLoader.INSTANCE); ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(UEnchantments.POISONED_JOKE); - ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(TraitLoader.INSTANCE); + ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new TraitLoader()); ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(StateMapLoader.INSTANCE); UBlocks.bootstrap(); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java index eaf35933..83359d6b 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java @@ -27,7 +27,7 @@ public record TraitIngredient ( } public static TraitIngredient fromPacket(PacketByteBuf buf) { - return new TraitIngredient(SpellTraits.fromPacket(buf), SpellTraits.fromPacket(buf)); + return new TraitIngredient(SpellTraits.fromPacketOrEmpty(buf), SpellTraits.fromPacketOrEmpty(buf)); } public static TraitIngredient fromJson(JsonObject json) { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java index 28b1ff20..cc1cd2cd 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java @@ -34,10 +34,21 @@ import net.minecraft.network.PacketByteBuf; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.registry.Registry; public final class SpellTraits implements Iterable> { public static final SpellTraits EMPTY = new SpellTraits(Map.of()); + private static Map REGISTRY = new HashMap<>(); + + public static void load(Map newRegistry) { + REGISTRY = new HashMap<>(newRegistry); + } + + public static Map all() { + return new HashMap<>(REGISTRY); + } + private final Map traits; SpellTraits(Map traits) { @@ -191,13 +202,20 @@ public final class SpellTraits implements Iterable> { } public static SpellTraits of(Item item) { - return TraitLoader.INSTANCE.getTraits(item); + return REGISTRY.getOrDefault(Registry.ITEM.getId(item), EMPTY); } public static SpellTraits of(Block block) { return of(block.asItem()); } + public static Stream getItems(Trait trait) { + return REGISTRY.entrySet().stream() + .filter(e -> e.getValue().get(trait) > 0) + .map(Map.Entry::getKey) + .flatMap(id -> Registry.ITEM.getOrEmpty(id).stream()); + } + public static Optional getEmbeddedTraits(ItemStack stack) { if (!stack.hasNbt() || !stack.getNbt().contains("spell_traits", NbtElement.COMPOUND_TYPE)) { return Optional.empty(); @@ -223,17 +241,16 @@ public final class SpellTraits implements Iterable> { return fromEntries(streamFromJson(traits)); } - public static Optional fromPacket(PacketByteBuf buf) { + public static Optional fromPacketOrEmpty(PacketByteBuf buf) { + return buf.readOptional(SpellTraits::fromPacket).filter(SpellTraits::isPresent); + } - boolean present = buf.readBoolean(); - if (!present) { - return Optional.empty(); - } + public static SpellTraits fromPacket(PacketByteBuf buf) { Map entries = new HashMap<>(); int count = buf.readInt(); if (count <= 0) { - return Optional.empty(); + return SpellTraits.EMPTY; } for (int i = 0; i < count; i++) { @@ -248,9 +265,9 @@ public final class SpellTraits implements Iterable> { }); } if (entries.isEmpty()) { - return Optional.empty(); + return SpellTraits.EMPTY; } - return Optional.of(new SpellTraits(entries)); + return new SpellTraits(entries); } public static Optional fromString(String traits) { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitLoader.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitLoader.java index cc25e3b3..7a626fcc 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitLoader.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/TraitLoader.java @@ -3,7 +3,6 @@ package com.minelittlepony.unicopia.ability.magic.spell.trait; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -21,7 +20,6 @@ import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.util.Resources; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; -import net.minecraft.item.Item; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.resource.SinglePreparationResourceReloader; @@ -33,19 +31,11 @@ import net.minecraft.util.registry.Registry; public class TraitLoader extends SinglePreparationResourceReloader> implements IdentifiableResourceReloadListener { private static final Identifier ID = Unicopia.id("data/traits"); - public static final TraitLoader INSTANCE = new TraitLoader(); - - private Map values = new HashMap<>(); - @Override public Identifier getFabricId() { return ID; } - public SpellTraits getTraits(Item item) { - return values.getOrDefault(Registry.ITEM.getId(item), SpellTraits.EMPTY); - } - @Override protected Multimap prepare(ResourceManager manager, Profiler profiler) { profiler.startTick(); @@ -87,9 +77,9 @@ public class TraitLoader extends SinglePreparationResourceReloader prepared, ResourceManager manager, Profiler profiler) { profiler.startTick(); - values = prepared.values().stream() + SpellTraits.load(prepared.values().stream() .flatMap(TraitStream::entries) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, SpellTraits::union)); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, SpellTraits::union))); profiler.endTick(); } diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index b8fcee2f..c225f4a1 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -25,14 +25,18 @@ public interface Channel { Identifier SERVER_SELECT_TRIBE_ID = Unicopia.id("select_tribe"); S2CPacketType SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new); + Identifier SERVER_RESOURCES_SEND_ID = Unicopia.id("resources_send"); + S2CPacketType SERVER_RESOURCES_SEND = SimpleNetworking.serverToClient(SERVER_RESOURCES_SEND_ID, MsgServerResources::new); + S2CBroadcastPacketType SERVER_OTHER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClients(Unicopia.id("other_player_capabilities"), MsgOtherPlayerCapabilities::new); S2CBroadcastPacketType SERVER_PLAYER_ANIMATION_CHANGE = SimpleNetworking.serverToClients(Unicopia.id("other_player_animation_change"), MsgPlayerAnimationChange::new); static void bootstrap() { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { if (!Pony.of(handler.player).isSpeciesPersisted()) { - sender.sendPacket(SERVER_SELECT_TRIBE_ID, new MsgTribeSelect(handler.player).toBuffer()); + sender.sendPacket(SERVER_SELECT_TRIBE.getId(), new MsgTribeSelect(handler.player).toBuffer()); } + sender.sendPacket(SERVER_RESOURCES_SEND.getId(), new MsgServerResources().toBuffer()); }); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java new file mode 100644 index 00000000..d5a6de10 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgServerResources.java @@ -0,0 +1,32 @@ +package com.minelittlepony.unicopia.network; + +import java.util.*; + +import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; +import com.minelittlepony.unicopia.util.network.Packet; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; + +public class MsgServerResources implements Packet { + private final Map entries; + + public MsgServerResources() { + entries = SpellTraits.all(); + } + + public MsgServerResources(PacketByteBuf buffer) { + entries = buffer.readMap(PacketByteBuf::readIdentifier, SpellTraits::fromPacket); + } + + @Override + public void toBuffer(PacketByteBuf buffer) { + buffer.writeMap(entries, PacketByteBuf::writeIdentifier, (r, v) -> v.write(r)); + } + + @Override + public void handle(PlayerEntity sender) { + SpellTraits.load(entries); + } +}