|
| 1 | +package com.cleanroommc.modularui.value.sync; |
| 2 | + |
| 3 | +import com.cleanroommc.modularui.api.widget.IWidget; |
| 4 | +import com.cleanroommc.modularui.widget.WidgetTree; |
| 5 | + |
| 6 | +import net.minecraft.network.PacketBuffer; |
| 7 | + |
| 8 | +import org.jetbrains.annotations.ApiStatus; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.function.Consumer; |
| 13 | +import java.util.function.Supplier; |
| 14 | + |
| 15 | +/** |
| 16 | + * This is a variation of {@link DynamicSyncHandler} with the difference that this is linked to a {@link ValueSyncHandler}. |
| 17 | + * This sync handler is automatically notified, when the linked value is updated. The widget provider here has the linked sync handler as an |
| 18 | + * argument instead of a packet. |
| 19 | + * To use it simply pass in a registered value sync handler into the constructor and link it to a |
| 20 | + * {@link com.cleanroommc.modularui.widgets.DynamicSyncedWidget DynamicSyncedWidget}. |
| 21 | + */ |
| 22 | +public class DynamicLinkedSyncHandler<S extends ValueSyncHandler<?>> extends SyncHandler implements IDynamicSyncNotifiable { |
| 23 | + |
| 24 | + private IWidgetProvider<S> widgetProvider; |
| 25 | + private Consumer<IWidget> onWidgetUpdate; |
| 26 | + |
| 27 | + private boolean updateQueued; |
| 28 | + private IWidget lastRejectedWidget; |
| 29 | + |
| 30 | + private final S linkedValue; |
| 31 | + |
| 32 | + public DynamicLinkedSyncHandler(S linkedValue) { |
| 33 | + this.linkedValue = linkedValue; |
| 34 | + linkedValue.setChangeListener(() -> notifyUpdate(false)); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void readOnClient(int id, PacketBuffer buf) throws IOException { |
| 39 | + if (id == 0) { |
| 40 | + updateWidget(parseWidget()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void readOnServer(int id, PacketBuffer buf) throws IOException { |
| 46 | + if (id == 0) { |
| 47 | + // do nothing with the widget on server side |
| 48 | + parseWidget(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void init(String key, PanelSyncManager syncManager) { |
| 54 | + super.init(key, syncManager); |
| 55 | + if (this.updateQueued) { |
| 56 | + notifyUpdate(true); |
| 57 | + this.updateQueued = false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private IWidget parseWidget() { |
| 62 | + getSyncManager().allowTemporarySyncHandlerRegistration(true); |
| 63 | + IWidget widget = this.widgetProvider.createWidget(getSyncManager(), this.linkedValue); |
| 64 | + getSyncManager().allowTemporarySyncHandlerRegistration(false); |
| 65 | + // collects any unregistered sync handlers |
| 66 | + // since the sync manager is currently locked and we no longer allow bypassing the lock it will crash if it finds any |
| 67 | + int unregistered = WidgetTree.countUnregisteredSyncHandlers(widget); |
| 68 | + if (unregistered > 0) { |
| 69 | + throw new IllegalStateException("Widgets created by DynamicSyncHandler can't have implicitly registered sync handlers. All" + |
| 70 | + "sync handlers must be registered with a variant of 'PanelSyncManager#getOrCreateSyncHandler(...)'."); |
| 71 | + } |
| 72 | + return widget; |
| 73 | + } |
| 74 | + |
| 75 | + private void updateWidget(IWidget widget) { |
| 76 | + if (this.onWidgetUpdate == null) { |
| 77 | + // no dynamic widget is yet attached |
| 78 | + // store for later |
| 79 | + // also ignore previous stored widget |
| 80 | + this.lastRejectedWidget = widget; |
| 81 | + } else { |
| 82 | + this.onWidgetUpdate.accept(widget); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Notifies the sync handler to create a new widget. It is allowed to call this method before this sync handler is initialised. |
| 88 | + * The packet will be cached until the sync handler is initialised. Only the last call of this method, while this sync handler is not |
| 89 | + * initialised is effective. |
| 90 | + */ |
| 91 | + private void notifyUpdate(boolean sync) { |
| 92 | + if (!isValid()) { |
| 93 | + // sync handler not yet initialised |
| 94 | + this.updateQueued = true; |
| 95 | + return; |
| 96 | + } |
| 97 | + IWidget widget = parseWidget(); |
| 98 | + if (getSyncManager().isClient()) { |
| 99 | + updateWidget(widget); |
| 100 | + } |
| 101 | + if (sync) sync(0, b -> {}); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets a widget creator which is called on client and server. {@link SyncHandler}s can be created here using |
| 106 | + * {@link PanelSyncManager#getOrCreateSyncHandler(String, int, Class, Supplier)}. Returning null in the function will not update the widget. |
| 107 | + * On client side the result is handed over to a linked {@link com.cleanroommc.modularui.widgets.DynamicSyncedWidget}. |
| 108 | + * |
| 109 | + * @param widgetProvider the widget creator function |
| 110 | + * @return this |
| 111 | + * @see IWidgetProvider |
| 112 | + */ |
| 113 | + public DynamicLinkedSyncHandler<S> widgetProvider(IWidgetProvider<S> widgetProvider) { |
| 114 | + this.widgetProvider = widgetProvider; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * An internal function which is used to link the {@link com.cleanroommc.modularui.widgets.DynamicSyncedWidget}. |
| 120 | + */ |
| 121 | + @ApiStatus.Internal |
| 122 | + @Override |
| 123 | + public void attachDynamicWidgetListener(Consumer<IWidget> onWidgetUpdate) { |
| 124 | + this.onWidgetUpdate = onWidgetUpdate; |
| 125 | + if (this.onWidgetUpdate != null && this.lastRejectedWidget != null) { |
| 126 | + this.onWidgetUpdate.accept(this.lastRejectedWidget); |
| 127 | + this.lastRejectedWidget = null; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public interface IWidgetProvider<S extends ValueSyncHandler<?>> { |
| 132 | + |
| 133 | + /** |
| 134 | + * This is the function which creates a widget on client and server. |
| 135 | + * In this method sync handlers can only be registered with {@link PanelSyncManager#getOrCreateSyncHandler(String, int, Class, Supplier)}. |
| 136 | + * |
| 137 | + * @param syncManager the sync manager of the current panel |
| 138 | + * @param value the linked sync value |
| 139 | + * @return a new widget or null if widget shouldn't be updated |
| 140 | + */ |
| 141 | + @Nullable IWidget createWidget(PanelSyncManager syncManager, S value); |
| 142 | + } |
| 143 | +} |
0 commit comments