Skip to content

Commit bd306a0

Browse files
authored
Align snapshot/1.0.11 with version/1.0.11 (#22)
* 🔧 Enhance null safety and add utility methods in settings and utils classes * ✏️ Mark methods as deprecated * 🐞 Add null check to AsyncRequest constructor * Update versioning scheme to include build number for snapshot releases (#13) * 👌 Refactor FileUtils, MathUtils, and RandomUtils for improved functionality and clarity * Align version/1.0.11 with dev (#18) * Update versioning scheme to include build number for snapshot releases * Update issue templates (#16) * Update issue templates (#17) * Align version/1.0.11 with dev (#19) * Update versioning scheme to include build number for snapshot releases * Update issue templates (#16) * Update issue templates (#17) * Align version/1.0.11 with main (#20) * Update issue templates (#16) * Update issue templates (#17)
1 parent 7bef162 commit bd306a0

18 files changed

+242
-61
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ loader_version=0.16.10
77

88
# Mod Properties
99
artifact_id=utilize
10-
mod_version=1.0.10
10+
mod_version=1.0.11
1111
maven_group=nl.devpieter
1212
archives_base_name=Utilize

src/main/java/nl/devpieter/utilize/Utilize.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class Utilize implements ClientModInitializer {
2121

2222
private static final ModContainer MOD_CONTAINER = FabricLoader.getInstance().getModContainer("utilize").orElseThrow();
23+
24+
@Deprecated(since = "1.0.11", forRemoval = true)
2325
public static final Logger LOGGER = LoggerFactory.getLogger("Utilize");
2426

2527
private static boolean BLOCK_SWING_HAND_ONCE = false;
@@ -36,22 +38,27 @@ public void onInitializeClient() {
3638
LOGGER.info("Utilize is running in a development environment.");
3739
}
3840

41+
@Deprecated(since = "1.0.11", forRemoval = true)
3942
public static boolean shouldBlockSwingHandOnce() {
4043
return BLOCK_SWING_HAND_ONCE;
4144
}
4245

46+
@Deprecated(since = "1.0.11", forRemoval = true)
4347
public static void blockSwingHandOnce() {
4448
BLOCK_SWING_HAND_ONCE = true;
4549
}
4650

51+
@Deprecated(since = "1.0.11", forRemoval = true)
4752
public static void blockedSwingHandOnce() {
4853
BLOCK_SWING_HAND_ONCE = false;
4954
}
5055

56+
@Deprecated(since = "1.0.11, refactor", forRemoval = true)
5157
public static boolean shouldBlockScreenId(int screenId) {
5258
return BLOCK_SCREEN_IDS.contains(screenId);
5359
}
5460

61+
@Deprecated(since = "1.0.11, refactor", forRemoval = true)
5562
public static void blockScreenId(int screenId) {
5663
if (BLOCK_SCREEN_IDS.contains(screenId)) return;
5764
BLOCK_SCREEN_IDS.add(screenId);
@@ -68,6 +75,7 @@ public static void blockScreenId(int screenId) {
6875
});
6976
}
7077

78+
@Deprecated(since = "1.0.11, refactor", forRemoval = true)
7179
public static void blockedScreenId(int screenId) {
7280
BLOCK_SCREEN_IDS.removeIf(id -> id == screenId);
7381
}

src/main/java/nl/devpieter/utilize/http/AsyncRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public AsyncRequest() {
2121
}
2222

2323
public AsyncRequest(@Nullable ResultConsumer<T> requestCallback) {
24+
if (requestCallback == null) return;
2425
this.callbacks.add(requestCallback);
2526
}
2627

src/main/java/nl/devpieter/utilize/mixins/ChatHudMixin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public abstract class ChatHudMixin {
3737

3838
@Inject(at = @At("HEAD"), method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V", cancellable = true)
3939
public void onAddMessage(Text message, MessageSignatureData signatureData, MessageIndicator indicator, CallbackInfo ci) {
40-
4140
ReceiveMessageEvent event = new ReceiveMessageEvent(message);
4241
Text result = this.sees.callWithResult(event);
4342

src/main/java/nl/devpieter/utilize/setting/SettingManager.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.reflect.TypeToken;
66
import nl.devpieter.utilize.Utilize;
77
import nl.devpieter.utilize.setting.interfaces.ISetting;
8+
import org.jetbrains.annotations.NotNull;
89
import org.jetbrains.annotations.Nullable;
910

1011
import java.io.*;
@@ -60,7 +61,7 @@ public void tick() {
6061
});
6162
}
6263

63-
public boolean queueSave(File file, ISetting<?> setting) {
64+
public boolean queueSave(@NotNull File file, @NotNull ISetting<?> setting) {
6465
String path = file.getAbsolutePath();
6566
KeyedSetting<?> keyedSetting = setting.asKeyedSetting();
6667

@@ -72,11 +73,11 @@ public boolean queueSave(File file, ISetting<?> setting) {
7273
return true;
7374
}
7475

75-
public boolean queueSave(File file, List<ISetting<?>> settings) {
76+
public boolean queueSave(@NotNull File file, @NotNull List<ISetting<?>> settings) {
7677
boolean success = true;
7778

7879
for (ISetting<?> setting : settings) {
79-
if (!queueSave(file, setting)) success = false;
80+
if (!this.queueSave(file, setting)) success = false;
8081
}
8182

8283
return success;
@@ -99,19 +100,18 @@ public void forceSaveQueue() {
99100
this.saveQueue.clear();
100101
}
101102

102-
public <T> boolean loadSetting(File file, ISetting<T> setting) {
103+
public <T> boolean loadSetting(@NotNull File file, @NotNull ISetting<T> setting) {
103104
List<KeyedSetting<?>> batch = this.readBatchFromFile(file);
104-
return loadSettingFromBatch(setting, batch);
105+
return this.loadSettingFromBatch(setting, batch);
105106
}
106107

107-
public boolean loadSettings(File file, List<ISetting<?>> settings) {
108+
public boolean loadSettings(@NotNull File file, @NotNull List<ISetting<?>> settings) {
108109
List<KeyedSetting<?>> batch = this.readBatchFromFile(file);
109-
for (ISetting<?> setting : settings) loadSettingFromBatch(setting, batch);
110-
110+
for (ISetting<?> setting : settings) this.loadSettingFromBatch(setting, batch);
111111
return true;
112112
}
113113

114-
private <T> boolean loadSettingFromBatch(ISetting<T> setting, List<KeyedSetting<?>> batch) {
114+
private <T> boolean loadSettingFromBatch(@NotNull ISetting<T> setting, @Nullable List<KeyedSetting<?>> batch) {
115115
if (batch == null || batch.isEmpty()) {
116116
setting.setValue(setting.getDefault());
117117
return true;
@@ -120,7 +120,7 @@ private <T> boolean loadSettingFromBatch(ISetting<T> setting, List<KeyedSetting<
120120
for (KeyedSetting<?> keyedSetting : batch) {
121121
if (!keyedSetting.key().equals(setting.getIdentifier())) continue;
122122

123-
JsonElement jsonElement = gson.toJsonTree(keyedSetting.value());
123+
JsonElement jsonElement = this.gson.toJsonTree(keyedSetting.value());
124124
T value = this.gson.fromJson(jsonElement, setting.getType());
125125

126126
setting.setValue(setting.shouldAllowNull() ? value : value != null ? value : setting.getDefault());
@@ -131,7 +131,7 @@ private <T> boolean loadSettingFromBatch(ISetting<T> setting, List<KeyedSetting<
131131
return false;
132132
}
133133

134-
private boolean saveBatchToFile(File file, List<KeyedSetting<?>> settings) {
134+
private boolean saveBatchToFile(@NotNull File file, @NotNull List<KeyedSetting<?>> settings) {
135135
List<KeyedSetting<?>> currentSettings = this.readBatchFromFile(file);
136136
if (currentSettings == null) currentSettings = new ArrayList<>();
137137

@@ -155,7 +155,7 @@ private boolean saveBatchToFile(File file, List<KeyedSetting<?>> settings) {
155155
}
156156
}
157157

158-
private @Nullable List<KeyedSetting<?>> readBatchFromFile(File file) {
158+
private @Nullable List<KeyedSetting<?>> readBatchFromFile(@NotNull File file) {
159159
try (Reader reader = new FileReader(file)) {
160160
return this.gson.fromJson(reader, new TypeToken<List<KeyedSetting<?>>>() {
161161
}.getType());

src/main/java/nl/devpieter/utilize/setting/base/SettingBase.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nl.devpieter.utilize.setting.base;
22

33
import nl.devpieter.utilize.setting.interfaces.ISetting;
4+
import org.jetbrains.annotations.NotNull;
45

56
public abstract class SettingBase<T> implements ISetting<T> {
67

@@ -11,11 +12,11 @@ public abstract class SettingBase<T> implements ISetting<T> {
1112

1213
private T value;
1314

14-
public SettingBase(String identifier, T defaultValue) {
15+
public SettingBase(@NotNull String identifier, T defaultValue) {
1516
this(identifier, defaultValue, false);
1617
}
1718

18-
public SettingBase(String identifier, T defaultValue, boolean allowNull) {
19+
public SettingBase(@NotNull String identifier, T defaultValue, boolean allowNull) {
1920
this.identifier = identifier;
2021
this.defaultValue = defaultValue;
2122
this.value = defaultValue;
@@ -24,22 +25,22 @@ public SettingBase(String identifier, T defaultValue, boolean allowNull) {
2425

2526
@Override
2627
public String getIdentifier() {
27-
return identifier;
28+
return this.identifier;
2829
}
2930

3031
@Override
3132
public boolean shouldAllowNull() {
32-
return allowNull;
33+
return this.allowNull;
3334
}
3435

3536
@Override
3637
public T getValue() {
37-
return value;
38+
return this.value;
3839
}
3940

4041
@Override
4142
public T getDefault() {
42-
return defaultValue;
43+
return this.defaultValue;
4344
}
4445

4546
@Override

src/main/java/nl/devpieter/utilize/setting/settings/BooleanSetting.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
import nl.devpieter.utilize.setting.base.SettingBase;
44
import nl.devpieter.utilize.setting.interfaces.IBooleanSetting;
5+
import org.jetbrains.annotations.NotNull;
56

67
public class BooleanSetting extends SettingBase<Boolean> implements IBooleanSetting {
78

8-
public BooleanSetting(String identifier, Boolean defaultValue) {
9+
public BooleanSetting(@NotNull String identifier, Boolean defaultValue) {
910
super(identifier, defaultValue);
1011
}
1112

12-
public BooleanSetting(String identifier, Boolean defaultValue, boolean allowNull) {
13+
public BooleanSetting(@NotNull String identifier, Boolean defaultValue, boolean allowNull) {
1314
super(identifier, defaultValue, allowNull);
1415
}
1516

1617
@Override
1718
public void toggle() {
19+
if (this.getValue() == null) throw new IllegalStateException("Cannot toggle a null value. Use setValue() instead.");
1820
this.setValue(!this.getValue());
1921
}
2022

src/main/java/nl/devpieter/utilize/setting/settings/FloatSetting.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@
22

33
import nl.devpieter.utilize.setting.base.SettingBase;
44
import nl.devpieter.utilize.setting.interfaces.INumberSetting;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
57

68
import java.lang.reflect.Type;
79

810
public class FloatSetting extends SettingBase<Float> implements INumberSetting<Float> {
911

10-
public FloatSetting(String identifier, Float defaultValue) {
12+
public FloatSetting(@NotNull String identifier, Float defaultValue) {
1113
super(identifier, defaultValue);
1214
}
1315

14-
public FloatSetting(String identifier, Float defaultValue, boolean allowNull) {
16+
public FloatSetting(@NotNull String identifier, Float defaultValue, boolean allowNull) {
1517
super(identifier, defaultValue, allowNull);
1618
}
1719

1820
@Override
1921
public void increment() {
22+
if (this.getValue() == null) throw new IllegalStateException("Cannot increment a null value. Use setValue() instead.");
2023
this.setValue(this.getValue() + 1.0F);
2124
}
2225

2326
@Override
24-
public void increment(Float amount) {
25-
this.setValue(this.getValue() + amount);
27+
public void increment(@Nullable Float amount) {
28+
if (amount == null && !this.shouldAllowNull()) throw new IllegalArgumentException("Amount cannot be null");
29+
30+
if (amount == null) this.setValue(0.0F);
31+
else if (this.getValue() == null) this.setValue(amount);
32+
else this.setValue(this.getValue() + amount);
2633
}
2734

2835
@Override
2936
public void decrement() {
37+
if (this.getValue() == null) throw new IllegalStateException("Cannot decrement a null value. Use setValue() instead.");
3038
this.setValue(this.getValue() - 1.0F);
3139
}
3240

3341
@Override
34-
public void decrement(Float amount) {
35-
this.setValue(this.getValue() - amount);
42+
public void decrement(@Nullable Float amount) {
43+
if (amount == null && !this.shouldAllowNull()) throw new IllegalArgumentException("Amount cannot be null");
44+
45+
if (amount == null) this.setValue(0.0F);
46+
else if (this.getValue() == null) this.setValue(-amount);
47+
else this.setValue(this.getValue() - amount);
3648
}
3749

3850
@Override

src/main/java/nl/devpieter/utilize/setting/settings/IntSetting.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@
22

33
import nl.devpieter.utilize.setting.base.SettingBase;
44
import nl.devpieter.utilize.setting.interfaces.INumberSetting;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
57

68
import java.lang.reflect.Type;
79

810
public class IntSetting extends SettingBase<Integer> implements INumberSetting<Integer> {
911

10-
public IntSetting(String identifier, Integer defaultValue) {
12+
public IntSetting(@NotNull String identifier, Integer defaultValue) {
1113
super(identifier, defaultValue);
1214
}
1315

14-
public IntSetting(String identifier, Integer defaultValue, boolean allowNull) {
16+
public IntSetting(@NotNull String identifier, Integer defaultValue, boolean allowNull) {
1517
super(identifier, defaultValue, allowNull);
1618
}
1719

1820
@Override
1921
public void increment() {
22+
if (this.getValue() == null) throw new IllegalStateException("Cannot increment a null value. Use setValue() instead.");
2023
this.setValue(this.getValue() + 1);
2124
}
2225

2326
@Override
24-
public void increment(Integer amount) {
25-
this.setValue(this.getValue() + amount);
27+
public void increment(@Nullable Integer amount) {
28+
if (amount == null && !this.shouldAllowNull()) throw new IllegalArgumentException("Amount cannot be null");
29+
30+
if (amount == null) this.setValue(0);
31+
else if (this.getValue() == null) this.setValue(amount);
32+
else this.setValue(this.getValue() + amount);
2633
}
2734

2835
@Override
2936
public void decrement() {
37+
if (this.getValue() == null) throw new IllegalStateException("Cannot decrement a null value. Use setValue() instead.");
3038
this.setValue(this.getValue() - 1);
3139
}
3240

3341
@Override
34-
public void decrement(Integer amount) {
35-
this.setValue(this.getValue() - amount);
42+
public void decrement(@Nullable Integer amount) {
43+
if (amount == null && !this.shouldAllowNull()) throw new IllegalArgumentException("Amount cannot be null");
44+
45+
if (amount == null) this.setValue(0);
46+
else if (this.getValue() == null) this.setValue(-amount);
47+
else this.setValue(this.getValue() - amount);
3648
}
3749

3850
@Override

src/main/java/nl/devpieter/utilize/setting/settings/ListSetting.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,49 @@ public ListSetting(String identifier, List<T> defaultValue, boolean allowNull) {
1919

2020
@Override
2121
public boolean contains(T value) {
22+
if (this.getValue() == null) return false;
2223
return this.getValue().contains(value);
2324
}
2425

2526
@Override
2627
public void add(T value) {
28+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot add element.");
2729
this.getValue().add(value);
2830
}
2931

3032
@Override
3133
public void remove(T value) {
34+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot remove element.");
3235
this.getValue().remove(value);
3336
}
3437

3538
@Override
3639
public void removeAt(int index) {
40+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot remove element at index.");
41+
if (index < 0 || index >= this.getValue().size()) throw new IndexOutOfBoundsException("Index out of bounds for list.");
42+
3743
this.getValue().remove(index);
3844
}
3945

4046
@Override
4147
public void removeFirst() {
48+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot remove first element.");
49+
if (this.getValue().isEmpty()) throw new IllegalStateException("List is empty, cannot remove first element.");
50+
4251
this.getValue().removeFirst();
4352
}
4453

4554
@Override
4655
public void removeLast() {
56+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot remove last element.");
57+
if (this.getValue().isEmpty()) throw new IllegalStateException("List is empty, cannot remove last element.");
58+
4759
this.getValue().removeLast();
4860
}
4961

5062
@Override
5163
public void clear() {
64+
if (this.getValue() == null) throw new IllegalStateException("List value is null, cannot clear.");
5265
this.getValue().clear();
5366
}
5467

0 commit comments

Comments
 (0)