Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandler;

import com.cleanroommc.modularui.widget.Widget;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -33,7 +35,8 @@ default W getThis() {
void initialiseSyncHandler(ModularSyncManager syncManager, boolean late);

/**
* Checks if the received sync handler is valid for this widget.
* Checks if the received sync handler is valid for this widget. <br />
* Called before {@link Widget#setSyncHandler(SyncHandler)} <br />
* <b>Synced widgets must override this!</b>
*
* @param syncHandler received sync handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ public final boolean isMainPanel() {
@ApiStatus.Internal
@Override
public void setSyncHandler(@Nullable SyncHandler syncHandler) {
if (!isValidSyncHandler(syncHandler))
if (syncHandler != null && !isValidSyncHandler(syncHandler))
throw new IllegalStateException("Panel SyncHandler's must implement IPanelHandler!");

super.setSyncHandler(syncHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class PhantomItemSlotSH extends ItemSlotSH {
@ApiStatus.Internal
public PhantomItemSlotSH(ModularSlot slot) {
super(slot);
slot.slotNumber = -1;
}

@Override
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/cleanroommc/modularui/widget/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,21 @@ public void onInit() {}
public void afterInit() {}

/**
* Retrieves, initialises and verifies a linked sync handler.
* Retrieves, verifies, and initialises a linked sync handler.
* Custom logic should be handled in {@link #isValidSyncHandler(SyncHandler)}.
*/
@Override
public void initialiseSyncHandler(ModularSyncManager syncManager, boolean late) {
if (this.syncKey != null) {
this.syncHandler = syncManager.getSyncHandler(getPanel().getName(), this.syncKey);
SyncHandler handler = this.syncHandler;
if (handler == null && this.syncKey != null) {
handler = syncManager.getSyncHandler(getPanel().getName(), this.syncKey);
}
if ((this.syncKey != null || this.syncHandler != null) && !isValidSyncHandler(this.syncHandler)) {
String type = this.syncHandler == null ? null : this.syncHandler.getClass().getName();
this.syncHandler = null;
if (handler != null && !isValidSyncHandler(handler)) {
String type = handler.getClass().getName();
setSyncHandler(null);
throw new IllegalStateException("SyncHandler of type " + type + " is not valid for " + getClass().getName() + ", with key " + this.syncKey);
}
setSyncHandler(handler);
if (this.syncHandler instanceof ValueSyncHandler<?> valueSyncHandler && valueSyncHandler.getChangeListener() == null) {
valueSyncHandler.setChangeListener(this::markTooltipDirty);
}
Expand Down Expand Up @@ -851,12 +853,13 @@ public W syncHandler(String name, int id) {
}

/**
* Used for widgets to set a value handler. Can also be a sync handler
* Used for widgets to set a value handler. <br />
* Will also call {@link #setSyncHandler(SyncHandler)} if it is a SyncHandler
*/
protected void setValue(IValue<?> value) {
this.value = value;
if (value instanceof SyncHandler syncHandler1) {
setSyncHandler(syncHandler1);
if (value instanceof SyncHandler handler && isValidSyncHandler(handler)) {
setSyncHandler(handler);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.intValue = castIfTypeElseNull(syncHandler, IIntValue.class);
return this.intValue != null;
return syncHandler instanceof IIntValue<?>;
}

protected int getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.cleanroommc.modularui.widget.SingleChildWidget;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ButtonWidget<W extends ButtonWidget<W>> extends SingleChildWidget<W> implements Interactable {

Expand Down Expand Up @@ -43,8 +44,7 @@ public static ButtonWidget<?> panelCloseButton() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, InteractionSyncHandler.class);
return this.syncHandler != null;
return syncHandler instanceof InteractionSyncHandler;
}

@Override
Expand Down Expand Up @@ -166,11 +166,16 @@ public W onKeyTapped(IGuiAction.KeyPressed keyTapped) {
}

public W syncHandler(InteractionSyncHandler interactionSyncHandler) {
this.syncHandler = interactionSyncHandler;
setSyncHandler(interactionSyncHandler);
return getThis();
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, InteractionSyncHandler.class);
super.setSyncHandler(syncHandler);
}

public W playClickSound(boolean play) {
this.playClickSound = play;
return getThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cleanroommc.modularui.widget.Widget;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
Expand All @@ -26,8 +27,7 @@ public class DynamicSyncedWidget<W extends DynamicSyncedWidget<W>> extends Widge

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, DynamicSyncHandler.class, t -> t.attachDynamicWidgetListener(this::updateChild));
return this.syncHandler != null;
return syncHandler instanceof DynamicSyncHandler;
}

@Override
Expand All @@ -53,12 +53,16 @@ private void updateChild(IWidget widget) {
}

public W syncHandler(DynamicSyncHandler syncHandler) {
this.syncHandler = syncHandler;
setSyncHandler(syncHandler);
syncHandler.attachDynamicWidgetListener(this::updateChild);
return getThis();
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, DynamicSyncHandler.class, t -> t.attachDynamicWidgetListener(this::updateChild));
super.setSyncHandler(syncHandler);
}

/**
* Sets an initial child. This can only be done before the widget is initialised.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public ItemDisplayWidget() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.value = castIfTypeGenericElseNull(syncHandler, ItemStack.class);
return this.value != null;
return syncHandler instanceof GenericSyncValue<?> gsv && gsv.isOfType(ItemStack.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.doubleValue = castIfTypeElseNull(syncHandler, IDoubleValue.class);
return this.doubleValue != null;
return syncHandler instanceof IDoubleValue<?>;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.doubleValue = castIfTypeElseNull(syncHandler, IDoubleValue.class);
return this.doubleValue != null;
return syncHandler instanceof IDoubleValue<?>;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, FluidSlotSyncHandler.class);
return this.syncHandler != null;
return syncHandler instanceof FluidSlotSyncHandler;
}

@Override
Expand Down Expand Up @@ -285,10 +284,15 @@ public FluidSlot syncHandler(IFluidTank fluidTank) {

public FluidSlot syncHandler(FluidSlotSyncHandler syncHandler) {
setSyncHandler(syncHandler);
this.syncHandler = syncHandler;
return this;
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, FluidSlotSyncHandler.class);
super.setSyncHandler(syncHandler);
}

/* === Recipe viewer ghost slot === */

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, ItemSlotSH.class);
return this.syncHandler != null;
return syncHandler instanceof ItemSlotSH;
}

@Override
Expand Down Expand Up @@ -207,11 +206,16 @@ public ItemSlot slot(IItemHandlerModifiable itemHandler, int index) {
}

public ItemSlot syncHandler(ItemSlotSH syncHandler) {
this.syncHandler = syncHandler;
setSyncHandler(this.syncHandler);
setSyncHandler(syncHandler);
return this;
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, ItemSlotSH.class);
super.setSyncHandler(syncHandler);
}

@SideOnly(Side.CLIENT)
private void drawSlot(ModularSlot slotIn) {
GuiScreen guiScreen = getScreen().getScreenWrapper().getGuiScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
import com.cleanroommc.modularui.integration.recipeviewer.RecipeViewerGhostIngredientSlot;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.ItemSlotSH;
import com.cleanroommc.modularui.value.sync.PhantomItemSlotSH;
import com.cleanroommc.modularui.value.sync.SyncHandler;

Expand All @@ -27,8 +28,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, PhantomItemSlotSH.class);
return this.syncHandler != null && super.isValidSyncHandler(syncHandler);
return syncHandler instanceof PhantomItemSlotSH;
}

@Override
Expand Down Expand Up @@ -89,13 +89,21 @@ public PhantomItemSlotSH getSyncHandler() {

@Override
public PhantomItemSlot slot(ModularSlot slot) {
slot.slotNumber = -1;
this.syncHandler = new PhantomItemSlotSH(slot);
super.isValidSyncHandler(this.syncHandler);
setSyncHandler(this.syncHandler);
return syncHandler(new PhantomItemSlotSH(slot));
}

@Override
public PhantomItemSlot syncHandler(ItemSlotSH syncHandler) {
setSyncHandler(syncHandler);
return this;
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, PhantomItemSlotSH.class);
super.setSyncHandler(syncHandler);
}

@Override
public boolean handleAsVanillaSlot() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.cleanroommc.modularui.value.sync.ValueSyncHandler;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.ParsePosition;
import java.util.function.Consumer;
Expand Down Expand Up @@ -63,15 +64,7 @@ public void onInit() {

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
if (syncHandler instanceof IStringValue<?> iStringValue && syncHandler instanceof ValueSyncHandler<?> valueSyncHandler) {
this.stringValue = iStringValue;
valueSyncHandler.setChangeListener(() -> {
markTooltipDirty();
setText(this.stringValue.getValue().toString());
});
return true;
}
return false;
return syncHandler instanceof IStringValue<?> && syncHandler instanceof ValueSyncHandler<?>;
}

@Override
Expand Down Expand Up @@ -216,6 +209,17 @@ public TextFieldWidget value(IStringValue<?> stringValue) {
return this;
}

@Override
protected void setSyncHandler(@Nullable SyncHandler syncHandler) {
if (syncHandler instanceof ValueSyncHandler<?> valueSyncHandler) {
valueSyncHandler.setChangeListener(() -> {
markTooltipDirty();
setText(this.stringValue.getValue().toString());
});
}
super.setSyncHandler(syncHandler);
}

/**
* Normally, Tooltips on text field widgets are used to display the contents of the widget when the scrollbar is active
* This value is an override, that allows the methods provided by {@link com.cleanroommc.modularui.api.widget.ITooltip} to be used
Expand Down