Skip to content

Commit dc09028

Browse files
committed
14759d1a to d4e989e0
skipped 848d9e60
1 parent 56ab3bd commit dc09028

File tree

77 files changed

+1224
-441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1224
-441
lines changed

src/main/java/com/gregtechceu/gtceu/api/mui/base/drawable/IKey.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ static IKey comp(@NotNull IKey... keys) {
160160
* @return dynamic text key
161161
*/
162162
static IKey dynamic(@NotNull Supplier<@NotNull Component> getter) {
163+
Component c = getter.get();
164+
if (c instanceof MutableComponent m) {
165+
return dynamicKey(() -> IKey.lang(m));
166+
} else {
167+
return dynamicKey(() -> IKey.lang(c.copy()));
168+
}
169+
}
170+
171+
/**
172+
* Creates a dynamic text key.
173+
*
174+
* @param getter key supplier
175+
* @return dynamic text key
176+
*/
177+
static IKey dynamicKey(@NotNull Supplier<@NotNull IKey> getter) {
163178
return new DynamicKey(getter);
164179
}
165180

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.gregtechceu.gtceu.api.mui.base.value;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
/**
8+
* An interface that is implemented on {@link IValue} and {@link com.gregtechceu.gtceu.api.mui.value.sync.SyncHandler
9+
* SyncHandler} for easier
10+
* validation and setters.
11+
*/
12+
@ApiStatus.NonExtendable
13+
public interface ISyncOrValue {
14+
15+
/**
16+
* A sync handler or value representing null.
17+
*/
18+
ISyncOrValue EMPTY = new ISyncOrValue() {
19+
20+
@Override
21+
public <T> @Nullable T castNullable(Class<T> type) {
22+
return null;
23+
}
24+
25+
@Override
26+
public boolean isTypeOrEmpty(Class<?> type) {
27+
return true;
28+
}
29+
};
30+
31+
/**
32+
* Returns the given sync handler or value or {@link #EMPTY} if null.
33+
*
34+
* @param syncOrValue sync handler or value
35+
* @return a non-null representation of the given sync handler or value
36+
*/
37+
@NotNull
38+
static ISyncOrValue orEmpty(@Nullable ISyncOrValue syncOrValue) {
39+
return syncOrValue != null ? syncOrValue : EMPTY;
40+
}
41+
42+
/**
43+
* Returns if this sync handler or value is an instance of the given type or if this represents null. This is
44+
* useful, when the value or
45+
* sync handler can be null in the widget.
46+
*
47+
* @param type type to check for
48+
* @return if this sync handler or value is an instance of the type or empty
49+
*/
50+
default boolean isTypeOrEmpty(Class<?> type) {
51+
return type.isAssignableFrom(getClass());
52+
}
53+
54+
/**
55+
* Casts this sync handler or value to the given type or null if this isn't a subtype of the given type.
56+
*
57+
* @param type type to cast this sync handle or value to
58+
* @param <T> type to cast to
59+
* @return this cast sync handler or value
60+
*/
61+
@Nullable
62+
@SuppressWarnings("unchecked")
63+
default <T> T castNullable(Class<T> type) {
64+
return type.isAssignableFrom(getClass()) ? (T) this : null;
65+
}
66+
67+
/**
68+
* Casts this sync handler or value to a {@link IValue IValue&lt;V&gt;} if it is a value handler and the containing
69+
* value is of type
70+
* {@link V} else null.
71+
*
72+
* @param valueType expected type of the containing value
73+
* @param <V> expected type of the containing value
74+
* @return a {@link IValue IValue&lt;V&gt;} if types match or null
75+
*/
76+
@Nullable
77+
default <V> IValue<V> castValueNullable(Class<V> valueType) {
78+
return null;
79+
}
80+
81+
/**
82+
* Casts this sync handler or value to the given type or throws an exception if this isn't a subtype of the given
83+
* type.
84+
*
85+
* @param type type to cast this sync handle or value to
86+
* @param <T> type to cast to
87+
* @return this cast sync handler or value
88+
* @throws IllegalStateException if this is not a subtype of the given type
89+
*/
90+
default <T> T castOrThrow(Class<T> type) {
91+
T t = castNullable(type);
92+
if (t == null) {
93+
if (!isSyncHandler() && !isValueHandler()) {
94+
throw new IllegalStateException("Empty sync handler or value can't be used for anything.");
95+
}
96+
String self = isSyncHandler() ? "sync handler" : "value";
97+
throw new IllegalStateException("Can't cast " + self + " of type '" + getClass().getSimpleName() +
98+
"' to type '" + type.getSimpleName() + "'.");
99+
}
100+
return t;
101+
}
102+
103+
/**
104+
* Returns if the containing value of this is of the given type. If this is not a value it will always return false.
105+
*
106+
* @param type expected value type
107+
* @return if the containing value of this is of the given type
108+
*/
109+
default boolean isValueOfType(Class<?> type) {
110+
return false;
111+
}
112+
113+
/**
114+
* @return if this is a sync handler (false if this represents null)
115+
*/
116+
default boolean isSyncHandler() {
117+
return false;
118+
}
119+
120+
/**
121+
* @return if this is a value handler (false if this represents null)
122+
*/
123+
default boolean isValueHandler() {
124+
return false;
125+
}
126+
}

src/main/java/com/gregtechceu/gtceu/api/mui/base/value/IValue.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @param <T> value type
77
*/
8-
public interface IValue<T> {
8+
public interface IValue<T> extends ISyncOrValue {
99

1010
/**
1111
* Gets the current value.
@@ -20,4 +20,21 @@ public interface IValue<T> {
2020
* @param value new value
2121
*/
2222
void setValue(T value);
23+
24+
Class<T> getValueType();
25+
26+
default boolean isValueOfType(Class<?> type) {
27+
return type.isAssignableFrom(getValueType());
28+
}
29+
30+
@SuppressWarnings("unchecked")
31+
@Override
32+
default <V> IValue<V> castValueNullable(Class<V> valueType) {
33+
return isValueOfType(valueType) ? (IValue<V>) this : null;
34+
}
35+
36+
@Override
37+
default boolean isValueHandler() {
38+
return true;
39+
}
2340
}

src/main/java/com/gregtechceu/gtceu/api/mui/base/widget/ISynced.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.gregtechceu.gtceu.api.mui.base.widget;
22

3+
import com.gregtechceu.gtceu.api.mui.base.value.ISyncOrValue;
34
import com.gregtechceu.gtceu.api.mui.value.sync.GenericSyncValue;
45
import com.gregtechceu.gtceu.api.mui.value.sync.ModularSyncManager;
56
import com.gregtechceu.gtceu.api.mui.value.sync.SyncHandler;
67

8+
import org.jetbrains.annotations.ApiStatus;
79
import org.jetbrains.annotations.NotNull;
810
import org.jetbrains.annotations.Nullable;
911

@@ -28,25 +30,58 @@ default W getThis() {
2830
* Called when this widget gets initialised or when this widget is added to the gui
2931
*
3032
* @param syncManager sync manager
31-
* @param late true if this is called some time after the widget tree of the parent has been initialised
33+
* @param late if this is called at any point after the panel this widget belongs to opened
3234
*/
3335
void initialiseSyncHandler(ModularSyncManager syncManager, boolean late);
3436

3537
/**
36-
* Checks if the received sync handler is valid for this widget.
37-
* <b>Synced widgets must override this!</b>
38-
*
39-
* @param syncHandler received sync handler
40-
* @return true if sync handler is valid
38+
* @deprecated use {@link #isValidSyncOrValue(ISyncOrValue)}
4139
*/
40+
@ApiStatus.ScheduledForRemoval(inVersion = "3.2.0")
41+
@Deprecated
4242
default boolean isValidSyncHandler(SyncHandler syncHandler) {
4343
return false;
4444
}
4545

46+
/**
47+
* Returns if the given value or sync handler is valid for this widget. This is usually a call to
48+
* {@link ISyncOrValue#isTypeOrEmpty(Class)}. If the widget must specify a value (disallow null) instanceof check
49+
* can be used. You can
50+
* check for primitive types which don't have a dedicated {@link com.gregtechceu.gtceu.api.mui.base.value.IValue
51+
* IValue} interface with
52+
* {@link ISyncOrValue#isValueOfType(Class)}.
53+
*
54+
* @param syncOrValue a sync handler or a value, but never null
55+
* @return if the value or sync handler is valid for this class
56+
*/
57+
default boolean isValidSyncOrValue(@NotNull ISyncOrValue syncOrValue) {
58+
return !(syncOrValue instanceof SyncHandler syncHandler) || isValidSyncHandler(syncHandler);
59+
}
60+
61+
/**
62+
* Checks if the given sync handler is valid for this widget and throws an exception if not.
63+
* Override {@link #isValidSyncHandler(SyncHandler)}
64+
*
65+
* @param syncHandler given sync handler
66+
* @throws IllegalStateException if the given sync handler is invalid for this widget.
67+
*/
68+
@ApiStatus.NonExtendable
69+
default void checkValidSyncOrValue(ISyncOrValue syncHandler) {
70+
if (!isValidSyncOrValue(syncHandler)) {
71+
throw new IllegalStateException(
72+
"SyncHandler of type '" + syncHandler.getClass().getSimpleName() + "' is not valid " +
73+
"for widget '" + this + "'.");
74+
}
75+
}
76+
77+
@ApiStatus.ScheduledForRemoval(inVersion = "3.2.0")
78+
@Deprecated
4679
default <T> T castIfTypeElseNull(SyncHandler syncHandler, Class<T> clazz) {
4780
return castIfTypeElseNull(syncHandler, clazz, null);
4881
}
4982

83+
@ApiStatus.ScheduledForRemoval(inVersion = "3.2.0")
84+
@Deprecated
5085
@SuppressWarnings("unchecked")
5186
default <T> T castIfTypeElseNull(SyncHandler syncHandler, Class<T> clazz, @Nullable Consumer<T> setup) {
5287
if (syncHandler != null && clazz.isAssignableFrom(syncHandler.getClass())) {
@@ -57,10 +92,14 @@ default <T> T castIfTypeElseNull(SyncHandler syncHandler, Class<T> clazz, @Nulla
5792
return null;
5893
}
5994

95+
@ApiStatus.ScheduledForRemoval(inVersion = "3.2.0")
96+
@Deprecated
6097
default <T> GenericSyncValue<T> castIfTypeGenericElseNull(SyncHandler syncHandler, Class<T> clazz) {
6198
return castIfTypeGenericElseNull(syncHandler, clazz, null);
6299
}
63100

101+
@ApiStatus.ScheduledForRemoval(inVersion = "3.2.0")
102+
@Deprecated
64103
default <T> GenericSyncValue<T> castIfTypeGenericElseNull(SyncHandler syncHandler, Class<T> clazz,
65104
@Nullable Consumer<GenericSyncValue<T>> setup) {
66105
if (syncHandler instanceof GenericSyncValue<?> genericSyncValue && genericSyncValue.isOfType(clazz)) {

src/main/java/com/gregtechceu/gtceu/api/mui/base/widget/IWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ default void draw(ModularGuiContext context) {
107107
void drawForeground(ModularGuiContext context);
108108

109109
default void transform(IViewportStack stack) {
110-
stack.translate(getArea().rx, getArea().ry, getArea().getPanelLayer() * 20);
110+
stack.translate(getArea().rx, getArea().ry, 0);
111111
}
112112

113113
default Object getAdditionalHoverInfo(IViewportStack viewportStack, int mouseX, int mouseY) {
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
package com.gregtechceu.gtceu.api.mui.drawable.text;
22

3-
import net.minecraft.network.chat.Component;
3+
import com.gregtechceu.gtceu.api.mui.base.drawable.IKey;
4+
45
import net.minecraft.network.chat.MutableComponent;
56

6-
import java.util.Objects;
7+
import org.jetbrains.annotations.Nullable;
8+
79
import java.util.function.Supplier;
810

911
public class DynamicKey extends BaseKey {
1012

11-
private final Supplier<MutableComponent> supplier;
12-
13-
public DynamicKey(Supplier<Component> supplier) {
14-
Objects.requireNonNull(supplier.get(), "IKey returns a null string!");
15-
this.supplier = () -> {
16-
Component c = supplier.get();
17-
if (c instanceof MutableComponent m) {
18-
return m;
19-
} else {
20-
return c.copy();
21-
}
22-
};
13+
private final Supplier<IKey> supplier;
14+
15+
public DynamicKey(Supplier<IKey> supplier) {
16+
// Objects.requireNonNull(supplier.get(), "IKey returns a null string!");
17+
this.supplier = supplier;
2318
}
2419

2520
@Override
2621
public MutableComponent get() {
27-
return this.supplier.get();
22+
return toString(false, null);
23+
}
24+
25+
@Override
26+
public MutableComponent getFormatted(@Nullable FormattingState parentFormatting) {
27+
// formatting is prepended to each key
28+
return toString(true, parentFormatting);
29+
}
30+
31+
private MutableComponent toString(boolean formatted, @Nullable FormattingState parentFormatting) {
32+
IKey key = this.supplier.get();
33+
if (key == null) key = IKey.EMPTY;
34+
if (formatted) {
35+
// merge parent formatting and this formatting to no lose info
36+
return key.getFormatted(FormattingState.merge(parentFormatting, getFormatting()));
37+
} else {
38+
return key.get();
39+
}
2840
}
2941
}

src/main/java/com/gregtechceu/gtceu/api/mui/drawable/text/TextRenderer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ protected int getStartY(float height) {
244244

245245
protected int getStartY(float maxHeight, float height) {
246246
if (this.alignment.y > 0 && maxHeight > 0 && height != maxHeight) {
247-
return (int) (this.y + (maxHeight * this.alignment.y) - height * this.alignment.y);
247+
return this.y + Math.round(maxHeight * this.alignment.y) - Math.round(height * this.alignment.y);
248248
}
249249
return this.y;
250250
}
@@ -255,7 +255,8 @@ protected int getStartX(float lineWidth) {
255255

256256
protected int getStartX(float maxWidth, float lineWidth) {
257257
if (this.alignment.x > 0 && maxWidth > 0) {
258-
return Math.max(this.x, (int) (this.x + (maxWidth * this.alignment.x) - lineWidth * this.alignment.x));
258+
return Math.max(this.x,
259+
this.x + Math.round(maxWidth * this.alignment.x) - Math.round(lineWidth * this.alignment.x));
259260
}
260261
return this.x;
261262
}

src/main/java/com/gregtechceu/gtceu/api/mui/factory/GuiData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.world.level.Level;
88

99
import lombok.Getter;
10+
import org.jetbrains.annotations.NotNull;
1011

1112
import java.util.Objects;
1213

@@ -21,10 +22,11 @@
2122
*/
2223
public class GuiData {
2324

25+
@NotNull
2426
@Getter
2527
private final Player player;
2628

27-
public GuiData(Player player) {
29+
public GuiData(@NotNull Player player) {
2830
this.player = Objects.requireNonNull(player);
2931
}
3032

0 commit comments

Comments
 (0)