|
| 1 | +package dev.amble.lib.api.sync.handler; |
| 2 | + |
| 3 | +import java.lang.reflect.Array; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.function.Consumer; |
| 6 | + |
| 7 | +import com.google.gson.*; |
| 8 | +import net.fabricmc.api.EnvType; |
| 9 | +import net.fabricmc.api.Environment; |
| 10 | +import org.jetbrains.annotations.ApiStatus; |
| 11 | + |
| 12 | +import net.minecraft.client.MinecraftClient; |
| 13 | +import net.minecraft.server.MinecraftServer; |
| 14 | + |
| 15 | +import dev.amble.lib.AmbleKit; |
| 16 | +import dev.amble.lib.api.sync.Exclude; |
| 17 | +import dev.amble.lib.api.sync.RootComponent; |
| 18 | +import dev.amble.lib.data.enummap.EnumMap; |
| 19 | + |
| 20 | +public class ComponentManager extends SyncComponent implements TickingComponent { |
| 21 | + @Exclude |
| 22 | + private final EnumMap<SyncComponent.IdLike, SyncComponent> handlers; |
| 23 | + protected final ComponentRegistry registry; |
| 24 | + |
| 25 | + public ComponentManager(SyncComponent.IdLike id, ComponentRegistry registry) { |
| 26 | + super(id); |
| 27 | + |
| 28 | + this.registry = registry; |
| 29 | + handlers = new EnumMap<>(registry::lookup, |
| 30 | + size -> (SyncComponent[]) Array.newInstance(SyncComponent.class, size)); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onCreate() { |
| 35 | + this.registry.fill(this::createHandler); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void onInit(InitContext ctx) { |
| 40 | + this.forEach(component -> SyncComponent.init(component, this.parent, ctx)); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public void postInit(InitContext ctx) { |
| 46 | + this.forEach(component -> component.postInit(ctx)); |
| 47 | + } |
| 48 | + |
| 49 | + private void forEach(Consumer<SyncComponent> consumer) { |
| 50 | + for (SyncComponent component : this.handlers.getValues()) { |
| 51 | + if (component == null) |
| 52 | + continue; |
| 53 | + |
| 54 | + consumer.accept(component); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void createHandler(SyncComponent component) { |
| 59 | + this.handlers.put(component.getId(), component); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Called on the END of a servers tick |
| 64 | + * |
| 65 | + * @param server |
| 66 | + * the current server |
| 67 | + */ |
| 68 | + public void tick(MinecraftServer server) { |
| 69 | + this.forEach(component -> { |
| 70 | + if (!(component instanceof TickingComponent tickable)) |
| 71 | + return; |
| 72 | + |
| 73 | + try { |
| 74 | + tickable.tick(server); |
| 75 | + } catch (Exception e) { |
| 76 | + AmbleKit.LOGGER.error("Ticking failed for {} | {}", component.getId().name(), component.parent().getUuid().toString(), e); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + @Environment(EnvType.CLIENT) |
| 82 | + @Override |
| 83 | + public void tick(MinecraftClient client) { |
| 84 | + this.forEach(component -> { |
| 85 | + if (!(component instanceof TickingComponent tickable)) |
| 86 | + return; |
| 87 | + |
| 88 | + try { |
| 89 | + tickable.tick(client); |
| 90 | + } catch (Exception e) { |
| 91 | + AmbleKit.LOGGER.error("Ticking failed for {} | {}", component.getId().name(), component.parent().getUuid().toString(), e); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @deprecated Use {@link RootComponent#handler(IdLike)} |
| 98 | + */ |
| 99 | + @Deprecated |
| 100 | + @ApiStatus.Internal |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + public <C extends SyncComponent> C get(IdLike id) { |
| 103 | + return (C) this.handlers.get(id); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void dispose() { |
| 108 | + super.dispose(); |
| 109 | + |
| 110 | + this.forEach(SyncComponent::dispose); |
| 111 | + this.handlers.clear(); |
| 112 | + } |
| 113 | + |
| 114 | + @ApiStatus.Internal |
| 115 | + public void set(SyncComponent t) { |
| 116 | + this.handlers.put(t.getId(), t); |
| 117 | + } |
| 118 | + |
| 119 | + public Object serializer() { |
| 120 | + return new Serializer(this.registry, this.getId()); |
| 121 | + } |
| 122 | + public static Object serializer(ComponentRegistry r, IdLike id) { |
| 123 | + return new Serializer(r, id); |
| 124 | + } |
| 125 | + |
| 126 | + static class Serializer implements JsonSerializer<ComponentManager>, JsonDeserializer<ComponentManager> { |
| 127 | + private final IdLike id; |
| 128 | + private final ComponentRegistry registry; |
| 129 | + |
| 130 | + public Serializer(ComponentRegistry registry, IdLike id) { |
| 131 | + this.id = id; |
| 132 | + this.registry = registry; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public ComponentManager deserialize(JsonElement json, java.lang.reflect.Type type, |
| 137 | + JsonDeserializationContext context) throws JsonParseException { |
| 138 | + ComponentManager manager = new ComponentManager(id, registry); |
| 139 | + Map<String, JsonElement> map = json.getAsJsonObject().asMap(); |
| 140 | + |
| 141 | + ComponentRegistry registry = manager.registry; |
| 142 | + |
| 143 | + for (Map.Entry<String, JsonElement> entry : map.entrySet()) { |
| 144 | + String key = entry.getKey(); |
| 145 | + JsonElement element = entry.getValue(); |
| 146 | + |
| 147 | + IdLike id = registry.get(key); |
| 148 | + |
| 149 | + if (id == null) { |
| 150 | + AmbleKit.LOGGER.error("Can't find a component id with name '{}'!", key); |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + manager.set(context.deserialize(element, id.clazz())); |
| 155 | + } |
| 156 | + |
| 157 | + for (int i = 0; i < manager.handlers.size(); i++) { |
| 158 | + if (manager.handlers.get(i) != null) |
| 159 | + continue; |
| 160 | + |
| 161 | + IdLike id = registry.get(i); |
| 162 | + AmbleKit.LOGGER.debug("Appending new component {}", id); |
| 163 | + |
| 164 | + manager.set(id.create()); |
| 165 | + } |
| 166 | + |
| 167 | + return manager; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public JsonElement serialize(ComponentManager manager, java.lang.reflect.Type type, |
| 172 | + JsonSerializationContext context) { |
| 173 | + JsonObject result = new JsonObject(); |
| 174 | + |
| 175 | + manager.forEach(component -> { |
| 176 | + IdLike idLike = component.getId(); |
| 177 | + |
| 178 | + if (idLike == null) { |
| 179 | + AmbleKit.LOGGER.error("Id was null for {}", component.getClass()); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + result.add(idLike.name(), context.serialize(component)); |
| 184 | + }); |
| 185 | + |
| 186 | + return result; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments