Skip to content

Commit 8fdaab9

Browse files
committed
Fix some fatal errors when config validation fails in coremods
1 parent ba11e5c commit 8fdaab9

File tree

7 files changed

+73
-44
lines changed

7 files changed

+73
-44
lines changed

src/main/java/com/falsepattern/lib/config/event/AllConfigSyncEvent.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
package com.falsepattern.lib.config.event;
2222

2323
import com.falsepattern.lib.StableAPI;
24+
import com.falsepattern.lib.internal.EventUtil;
2425
import lombok.val;
2526

26-
import cpw.mods.fml.common.FMLCommonHandler;
2727
import cpw.mods.fml.common.eventhandler.Event;
2828
import cpw.mods.fml.relauncher.Side;
2929
import cpw.mods.fml.relauncher.SideOnly;
@@ -44,13 +44,12 @@ public AllConfigSyncEvent() {
4444
@StableAPI.Internal
4545
public static boolean postStart() {
4646
val event = new Start();
47-
FMLCommonHandler.instance().bus().post(event);
48-
return !event.isCanceled();
47+
return EventUtil.postOnCommonBus(event);
4948
}
5049

5150
@StableAPI.Internal
5251
public static void postEnd() {
53-
FMLCommonHandler.instance().bus().post(new End());
52+
EventUtil.postOnCommonBus(new End());
5453
}
5554

5655
@SideOnly(Side.CLIENT)

src/main/java/com/falsepattern/lib/config/event/ConfigSyncEvent.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
package com.falsepattern.lib.config.event;
2222

2323
import com.falsepattern.lib.StableAPI;
24+
import com.falsepattern.lib.internal.EventUtil;
2425
import lombok.val;
2526

26-
import cpw.mods.fml.common.FMLCommonHandler;
2727
import cpw.mods.fml.common.eventhandler.Event;
2828
import cpw.mods.fml.relauncher.Side;
2929
import cpw.mods.fml.relauncher.SideOnly;
@@ -47,18 +47,17 @@ public ConfigSyncEvent(Class<?> configClass) {
4747
@StableAPI.Internal
4848
public static boolean postStart(Class<?> configClass) {
4949
val event = new Start(configClass);
50-
FMLCommonHandler.instance().bus().post(event);
51-
return !event.isCanceled();
50+
return EventUtil.postOnCommonBus(event);
5251
}
5352

5453
@StableAPI.Internal
5554
public static void postEndSuccess(Class<?> configClass) {
56-
FMLCommonHandler.instance().bus().post(new End(configClass, true, null));
55+
EventUtil.postOnCommonBus(new End(configClass, true, null));
5756
}
5857

5958
@StableAPI.Internal
6059
public static void postEndFailure(Class<?> configClass, Throwable error) {
61-
FMLCommonHandler.instance().bus().post(new End(configClass, false, error));
60+
EventUtil.postOnCommonBus(new End(configClass, false, error));
6261
}
6362

6463
@SideOnly(Side.CLIENT)

src/main/java/com/falsepattern/lib/config/event/ConfigSyncRequestEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
package com.falsepattern.lib.config.event;
2222

2323
import com.falsepattern.lib.StableAPI;
24+
import com.falsepattern.lib.internal.EventUtil;
2425

2526
import net.minecraft.entity.player.EntityPlayerMP;
26-
import cpw.mods.fml.common.FMLCommonHandler;
2727
import cpw.mods.fml.common.eventhandler.Event;
2828

2929
import java.util.ArrayList;
@@ -43,12 +43,12 @@ public ConfigSyncRequestEvent() {
4343

4444
@StableAPI.Expose
4545
public static void postClient() {
46-
FMLCommonHandler.instance().bus().post(new Client());
46+
EventUtil.postOnCommonBus(new Client());
4747
}
4848

4949
@StableAPI.Expose
5050
public static void postServer(List<EntityPlayerMP> players) {
51-
FMLCommonHandler.instance().bus().post(new Server(new ArrayList<>(players)));
51+
EventUtil.postOnCommonBus(new Server(new ArrayList<>(players)));
5252
}
5353

5454
@StableAPI(since = "0.10.0")

src/main/java/com/falsepattern/lib/config/event/ConfigValidationFailureEvent.java

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.falsepattern.lib.StableAPI;
2424
import com.falsepattern.lib.config.Config;
25+
import com.falsepattern.lib.internal.EventUtil;
2526
import com.falsepattern.lib.internal.Share;
2627
import com.falsepattern.lib.text.FormattedText;
2728
import com.falsepattern.lib.toasts.GuiToast;
@@ -30,7 +31,6 @@
3031
import lombok.val;
3132

3233
import net.minecraft.util.EnumChatFormatting;
33-
import cpw.mods.fml.common.FMLCommonHandler;
3434
import cpw.mods.fml.common.eventhandler.Event;
3535
import cpw.mods.fml.relauncher.Side;
3636
import cpw.mods.fml.relauncher.SideOnly;
@@ -61,53 +61,45 @@ protected ConfigValidationFailureEvent(String reason, Class<?> configClass, Stri
6161

6262
@StableAPI.Internal
6363
public static void postNumericRangeOutOfBounds(Field field, int listIndex, String value, String min, String max) {
64-
FMLCommonHandler.instance()
65-
.bus()
66-
.post(new NumericRangeOutOfBounds(field.getDeclaringClass(),
67-
field.getName(),
68-
listIndex,
69-
value,
70-
min,
71-
max));
64+
EventUtil.postOnCommonBus(new NumericRangeOutOfBounds(field.getDeclaringClass(),
65+
field.getName(),
66+
listIndex,
67+
value,
68+
min,
69+
max));
7270
}
7371

7472
@StableAPI.Internal
7573
public static void postSize(Field field, int requestedSize, boolean fixedSize, int maxSize, int defaultSize) {
76-
FMLCommonHandler.instance()
77-
.bus()
78-
.post(new ListSizeOutOfBounds(field.getDeclaringClass(),
79-
field.getName(),
80-
requestedSize,
81-
fixedSize,
82-
maxSize,
83-
defaultSize));
74+
EventUtil.postOnCommonBus(new ListSizeOutOfBounds(field.getDeclaringClass(),
75+
field.getName(),
76+
requestedSize,
77+
fixedSize,
78+
maxSize,
79+
defaultSize));
8480
}
8581

8682
@StableAPI.Internal
8783
public static void postStringSizeOutOfBounds(Field field, int listIndex, String text, int maxSize) {
88-
FMLCommonHandler.instance()
89-
.bus()
90-
.post(new StringSizeOutOfBounds(field.getDeclaringClass(),
91-
field.getName(),
92-
listIndex,
93-
text,
94-
maxSize));
84+
EventUtil.postOnCommonBus(new StringSizeOutOfBounds(field.getDeclaringClass(),
85+
field.getName(),
86+
listIndex,
87+
text,
88+
maxSize));
9589
}
9690

9791
@StableAPI.Internal
9892
public static void fieldIsNull(Field field, int listIndex) {
99-
FMLCommonHandler.instance().bus().post(new FieldIsNull(field.getDeclaringClass(), field.getName(), listIndex));
93+
EventUtil.postOnCommonBus(new FieldIsNull(field.getDeclaringClass(), field.getName(), listIndex));
10094
}
10195

10296
@StableAPI.Internal
10397
public static void postStringPatternMismatch(Field field, int listIndex, String text, String pattern) {
104-
FMLCommonHandler.instance()
105-
.bus()
106-
.post(new StringPatternMismatch(field.getDeclaringClass(),
107-
field.getName(),
108-
listIndex,
109-
text,
110-
pattern));
98+
EventUtil.postOnCommonBus(new StringPatternMismatch(field.getDeclaringClass(),
99+
field.getName(),
100+
listIndex,
101+
text,
102+
pattern));
111103
}
112104

113105
@SideOnly(Side.CLIENT)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2022-2023 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice and this permission notice
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal;
23+
24+
import cpw.mods.fml.common.FMLCommonHandler;
25+
import cpw.mods.fml.common.eventhandler.Event;
26+
27+
public class EventUtil {
28+
public static boolean postOnCommonBus(Event event) {
29+
if (!Share.EARLY_INIT_DONE) {
30+
return false;
31+
}
32+
return FMLCommonHandler.instance()
33+
.bus()
34+
.post(event);
35+
}
36+
}

src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public FalsePatternLib() {
5757
@Mod.EventHandler
5858
public void construct(FMLConstructionEvent e) {
5959
proxy.construct(e);
60+
Share.EARLY_INIT_DONE = true;
6061
}
6162

6263
@Mod.EventHandler

src/main/java/com/falsepattern/lib/internal/Share.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public final class Share {
4646
}
4747
}
4848

49+
public static boolean EARLY_INIT_DONE = false;
50+
4951
public static void deprecatedWarning(Throwable stacktrace) {
5052
LOG.warn("DEPRECATED API CALLED!", stacktrace);
5153
}

0 commit comments

Comments
 (0)