Skip to content

Commit cfb2fc5

Browse files
committed
Improve testing mode.
1 parent 1404301 commit cfb2fc5

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

loader/src/main/java/com/fox2code/foxloader/internal/InternalLoggingHooks.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static void setupLogAgentFoxLoader(LogAgent logAgent) {
4747
Logger logger = logAgent.getLogger();
4848
logger.setUseParentHandlers(false);
4949
logger.addHandler(CONSOLE_HANDLER);
50+
if (FoxLauncher.isTestingMode()) return;
5051
FoxLauncher.installLoggerHelperOn(logger);
5152
if (logAgent.logFile == null || !restoreVanillaLogging) return;
5253
try {

loader/src/main/java/com/fox2code/foxloader/launcher/FoxLauncher.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.fox2code.foxloader.launcher;
2525

2626
import com.fox2code.foxloader.dependencies.DependencyHelper;
27+
import com.fox2code.foxloader.loader.early.EarlyLoader;
2728
import com.fox2code.foxloader.utils.Platform;
2829
import com.fox2code.foxloader.utils.SourceUtil;
2930
import com.fox2code.foxloader.utils.io.CertificateHelper;
@@ -364,7 +365,11 @@ public static void printEarlyStackTrace(Throwable t) {
364365

365366
public static void notifyTestingMode() {
366367
if (foxClassLoader == null && environmentType == null) {
367-
testingMode = true;
368+
// We usually can't call loader classes from launch, but testing mode is an exception.
369+
if (!testingMode) {
370+
testingMode = true;
371+
EarlyLoader.onTestingMode();
372+
}
368373
} else {
369374
throw new IllegalStateException("Cannot notify testing mode outside of unit tests!");
370375
}

loader/src/main/java/com/fox2code/foxloader/loader/early/EarlyLoader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ public static void earlyLoad() {
4242
CoreConstants.CORE = new MinecraftServer();
4343
}
4444
}
45+
46+
public static void onTestingMode() {
47+
if (CoreConstants.CORE != null) return;
48+
CoreConstants.CORE = new Testing();
49+
}
4550
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.loader.early;
25+
26+
import net.minecraft.common.ICoreAccess;
27+
import net.minecraft.common.entity.Entity;
28+
import net.minecraft.common.entity.player.PlayerInteractionHandler;
29+
import net.minecraft.common.util.logging.LogAgent;
30+
31+
import java.io.File;
32+
import java.util.List;
33+
34+
final class Testing implements ICoreAccess {
35+
static final LogAgent TESTING_LOG_AGENT = new LogAgent("TESTING", null);
36+
37+
@Override
38+
public File getMinecraftDir() {
39+
throw new IllegalStateException("getMinecraftDir() is not available in testing mode");
40+
}
41+
42+
@Override
43+
public PlayerInteractionHandler getPlayerInteractionHandler() {
44+
throw new IllegalStateException("getPlayerInteractionHandler() is not available in testing mode");
45+
}
46+
47+
@Override
48+
public void tickSprint(int i) {
49+
throw new IllegalStateException("tickSprint() is not available in testing mode");
50+
}
51+
52+
@Override
53+
public LogAgent getLogger() {
54+
return TESTING_LOG_AGENT;
55+
}
56+
57+
@Override
58+
public void appendAllLoadedEntities(List<Entity> list) {
59+
throw new IllegalStateException("appendAllLoadedEntities() is not available in testing mode");
60+
}
61+
}

0 commit comments

Comments
 (0)