mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-30 16:28:00 +01:00
Sync traits from the server
This commit is contained in:
parent
7eeda349d1
commit
f923f7e872
6 changed files with 67 additions and 24 deletions
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<Map.Entry<Trait, Float>> {
|
||||
public static final SpellTraits EMPTY = new SpellTraits(Map.of());
|
||||
|
||||
private static Map<Identifier, SpellTraits> REGISTRY = new HashMap<>();
|
||||
|
||||
public static void load(Map<Identifier, SpellTraits> newRegistry) {
|
||||
REGISTRY = new HashMap<>(newRegistry);
|
||||
}
|
||||
|
||||
public static Map<Identifier, SpellTraits> all() {
|
||||
return new HashMap<>(REGISTRY);
|
||||
}
|
||||
|
||||
private final Map<Trait, Float> traits;
|
||||
|
||||
SpellTraits(Map<Trait, Float> traits) {
|
||||
|
@ -191,13 +202,20 @@ public final class SpellTraits implements Iterable<Map.Entry<Trait, Float>> {
|
|||
}
|
||||
|
||||
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<Item> 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<SpellTraits> 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<Map.Entry<Trait, Float>> {
|
|||
return fromEntries(streamFromJson(traits));
|
||||
}
|
||||
|
||||
public static Optional<SpellTraits> fromPacket(PacketByteBuf buf) {
|
||||
public static Optional<SpellTraits> 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<Trait, Float> 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<Map.Entry<Trait, Float>> {
|
|||
});
|
||||
}
|
||||
if (entries.isEmpty()) {
|
||||
return Optional.empty();
|
||||
return SpellTraits.EMPTY;
|
||||
}
|
||||
return Optional.of(new SpellTraits(entries));
|
||||
return new SpellTraits(entries);
|
||||
}
|
||||
|
||||
public static Optional<SpellTraits> fromString(String traits) {
|
||||
|
|
|
@ -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<Multimap<Identifier, TraitLoader.TraitStream>> implements IdentifiableResourceReloadListener {
|
||||
private static final Identifier ID = Unicopia.id("data/traits");
|
||||
|
||||
public static final TraitLoader INSTANCE = new TraitLoader();
|
||||
|
||||
private Map<Identifier, SpellTraits> 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<Identifier, TraitStream> prepare(ResourceManager manager, Profiler profiler) {
|
||||
profiler.startTick();
|
||||
|
@ -87,9 +77,9 @@ public class TraitLoader extends SinglePreparationResourceReloader<Multimap<Iden
|
|||
@Override
|
||||
protected void apply(Multimap<Identifier, TraitStream> 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -25,14 +25,18 @@ public interface Channel {
|
|||
Identifier SERVER_SELECT_TRIBE_ID = Unicopia.id("select_tribe");
|
||||
S2CPacketType<MsgTribeSelect> SERVER_SELECT_TRIBE = SimpleNetworking.serverToClient(SERVER_SELECT_TRIBE_ID, MsgTribeSelect::new);
|
||||
|
||||
Identifier SERVER_RESOURCES_SEND_ID = Unicopia.id("resources_send");
|
||||
S2CPacketType<MsgServerResources> SERVER_RESOURCES_SEND = SimpleNetworking.serverToClient(SERVER_RESOURCES_SEND_ID, MsgServerResources::new);
|
||||
|
||||
S2CBroadcastPacketType<MsgOtherPlayerCapabilities> SERVER_OTHER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClients(Unicopia.id("other_player_capabilities"), MsgOtherPlayerCapabilities::new);
|
||||
S2CBroadcastPacketType<MsgPlayerAnimationChange> 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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<PlayerEntity> {
|
||||
private final Map<Identifier, SpellTraits> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue