Skip to content

Commit 63f3147

Browse files
committed
begin work on fabric implementation
1 parent 60af3ff commit 63f3147

19 files changed

+1022
-3
lines changed

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.diffplug.gradle.spotless.SpotlessExtension
2727
plugins {
2828
alias(libs.plugins.spotless)
2929
alias(libs.plugins.nexusPublish)
30+
alias(libs.plugins.fabricLoom) apply false
3031
}
3132

3233
defaultTasks("clean", "build")

fabric/build.gradle.kts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
plugins {
26+
alias(libs.plugins.fabricLoom)
27+
}
28+
29+
dependencies {
30+
minecraft(libs.minecraft)
31+
modImplementation(libs.fabricLoader)
32+
mappings(loom.officialMojangMappings())
33+
34+
modImplementation(platform(libs.fabricApiBom))
35+
modImplementation(libs.fabricApiNetworkingV1)
36+
37+
api(projects.npcLibApi)
38+
api(projects.npcLibCommon)
39+
40+
implementation(libs.geantyref)
41+
}
42+
43+
tasks.withType<JavaCompile> {
44+
options.release.set(21)
45+
}
46+
47+
tasks.withType<ProcessResources> {
48+
val props = mapOf("version" to project.version)
49+
inputs.properties(props)
50+
filesMatching("fabric.mod.json") {
51+
expand(props)
52+
}
53+
}
54+
55+
loom {
56+
accessWidenerPath.set(project.file("src/main/resources/npc_lib.accesswidener"))
57+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.juliarn.npclib.fabric;
26+
27+
import net.fabricmc.api.ModInitializer;
28+
import net.minecraft.server.MinecraftServer;
29+
30+
public final class FabricModInitializer implements ModInitializer {
31+
32+
public static MinecraftServer theServer;
33+
34+
@Override
35+
public void onInitialize() {
36+
37+
}
38+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.juliarn.npclib.fabric;
26+
27+
import com.github.juliarn.npclib.api.Platform;
28+
import com.github.juliarn.npclib.common.platform.CommonPlatformBuilder;
29+
import com.github.juliarn.npclib.fabric.protocol.FabricProtocolAdapter;
30+
import net.minecraft.server.level.ServerLevel;
31+
import net.minecraft.server.level.ServerPlayer;
32+
import net.minecraft.world.item.ItemStack;
33+
import org.jetbrains.annotations.NotNull;
34+
35+
public final class FabricPlatform extends CommonPlatformBuilder<ServerLevel, ServerPlayer, ItemStack, Object> {
36+
37+
@Override
38+
protected void prepareBuild() {
39+
// set the default task manager
40+
if (this.taskManager == null) {
41+
this.taskManager = FabricPlatformTaskManager.taskManager();
42+
}
43+
44+
// set the default version accessor
45+
if (this.versionAccessor == null) {
46+
this.versionAccessor = FabricVersionAccessor.versionNameBased();
47+
}
48+
49+
// set the default world accessor
50+
if (this.worldAccessor == null) {
51+
this.worldAccessor = FabricWorldAccessor.keyBased();
52+
}
53+
54+
// set the default packet adapter
55+
if (this.packetAdapter == null) {
56+
this.packetAdapter = FabricProtocolAdapter.fabricProtocolAdapter();
57+
}
58+
59+
// set the default logger if no logger was provided
60+
if (this.logger == null) {
61+
this.logger = FabricPlatformLogger.logger();
62+
}
63+
}
64+
65+
@Override
66+
protected @NotNull Platform<ServerLevel, ServerPlayer, ItemStack, Object> doBuild() {
67+
return null;
68+
}
69+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.juliarn.npclib.fabric;
26+
27+
import com.github.juliarn.npclib.api.log.PlatformLogger;
28+
import com.mojang.logging.LogUtils;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
import org.slf4j.Logger;
32+
33+
public final class FabricPlatformLogger implements PlatformLogger {
34+
35+
private static final Logger LOGGER = LogUtils.getLogger();
36+
private static final FabricPlatformLogger INSTANCE = new FabricPlatformLogger();
37+
38+
private FabricPlatformLogger() {
39+
}
40+
41+
public static @NotNull PlatformLogger logger() {
42+
return INSTANCE;
43+
}
44+
45+
@Override
46+
public void info(@NotNull String message) {
47+
LOGGER.info(message);
48+
}
49+
50+
@Override
51+
public void warning(@NotNull String message) {
52+
LOGGER.warn(message);
53+
}
54+
55+
@Override
56+
public void error(@NotNull String message) {
57+
LOGGER.error(message);
58+
}
59+
60+
@Override
61+
public void error(@NotNull String message, @Nullable Throwable exception) {
62+
LOGGER.error(message, exception);
63+
}
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.juliarn.npclib.fabric;
26+
27+
import com.github.juliarn.npclib.api.PlatformTaskManager;
28+
import com.github.juliarn.npclib.common.task.AsyncPlatformTaskManager;
29+
import org.jetbrains.annotations.NotNull;
30+
31+
public final class FabricPlatformTaskManager extends AsyncPlatformTaskManager {
32+
33+
private static final FabricPlatformTaskManager INSTANCE = new FabricPlatformTaskManager();
34+
35+
private FabricPlatformTaskManager() {
36+
super("Fabric");
37+
}
38+
39+
public static @NotNull PlatformTaskManager taskManager() {
40+
return INSTANCE;
41+
}
42+
43+
@Override
44+
public void scheduleSync(@NotNull Runnable task) {
45+
FabricModInitializer.theServer.execute(task);
46+
}
47+
48+
@Override
49+
public void scheduleDelayedSync(@NotNull Runnable task, int delayTicks) {
50+
throw new UnsupportedOperationException("not implemented on fabric platform");
51+
}
52+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* This file is part of npc-lib, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
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
14+
* all 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
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.juliarn.npclib.fabric;
26+
27+
import com.github.juliarn.npclib.api.PlatformVersionAccessor;
28+
import net.minecraft.SharedConstants;
29+
import org.jetbrains.annotations.NotNull;
30+
31+
public final class FabricVersionAccessor {
32+
33+
private static int[] extractServerVersionParts() {
34+
String[] parts = SharedConstants.getCurrentVersion().getName().split("\\.");
35+
if (parts.length == 2 || parts.length == 3) {
36+
// should be in the correct format, just to make sure
37+
try {
38+
int major = Integer.parseInt(parts[0]);
39+
int minor = Integer.parseInt(parts[1]);
40+
int patch = parts.length == 3 ? Integer.parseInt(parts[2]) : 0;
41+
42+
// return the version array from that
43+
return new int[]{major, minor, patch};
44+
} catch (NumberFormatException ignored) {
45+
}
46+
}
47+
48+
// unable to parse
49+
return new int[0];
50+
}
51+
52+
public static @NotNull PlatformVersionAccessor versionNameBased() {
53+
return FabricVersionNameAccessor.INSTANCE;
54+
}
55+
56+
private static final class FabricVersionNameAccessor implements PlatformVersionAccessor {
57+
58+
private static final int[] VERSION_NUMBER_PARTS = extractServerVersionParts();
59+
private static final FabricVersionNameAccessor INSTANCE = new FabricVersionNameAccessor();
60+
61+
private static int safeGetPart(int index, int def) {
62+
return VERSION_NUMBER_PARTS.length > index ? VERSION_NUMBER_PARTS[index] : def;
63+
}
64+
65+
@Override
66+
public int major() {
67+
return safeGetPart(0, 1);
68+
}
69+
70+
@Override
71+
public int minor() {
72+
return safeGetPart(1, 0);
73+
}
74+
75+
@Override
76+
public int patch() {
77+
return safeGetPart(2, 0);
78+
}
79+
80+
@Override
81+
public boolean atLeast(int major, int minor, int patch) {
82+
return this.major() >= major && this.minor() >= major && this.patch() >= patch;
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)