Skip to content

Commit

Permalink
begin work on fabric implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Jan 21, 2025
1 parent 60af3ff commit 63f3147
Show file tree
Hide file tree
Showing 19 changed files with 1,022 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.diffplug.gradle.spotless.SpotlessExtension
plugins {
alias(libs.plugins.spotless)
alias(libs.plugins.nexusPublish)
alias(libs.plugins.fabricLoom) apply false
}

defaultTasks("clean", "build")
Expand Down
57 changes: 57 additions & 0 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

plugins {
alias(libs.plugins.fabricLoom)
}

dependencies {
minecraft(libs.minecraft)
modImplementation(libs.fabricLoader)
mappings(loom.officialMojangMappings())

modImplementation(platform(libs.fabricApiBom))
modImplementation(libs.fabricApiNetworkingV1)

api(projects.npcLibApi)
api(projects.npcLibCommon)

implementation(libs.geantyref)
}

tasks.withType<JavaCompile> {
options.release.set(21)
}

tasks.withType<ProcessResources> {
val props = mapOf("version" to project.version)
inputs.properties(props)
filesMatching("fabric.mod.json") {
expand(props)
}
}

loom {
accessWidenerPath.set(project.file("src/main/resources/npc_lib.accesswidener"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.fabric;

import net.fabricmc.api.ModInitializer;
import net.minecraft.server.MinecraftServer;

public final class FabricModInitializer implements ModInitializer {

public static MinecraftServer theServer;

@Override
public void onInitialize() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.fabric;

import com.github.juliarn.npclib.api.Platform;
import com.github.juliarn.npclib.common.platform.CommonPlatformBuilder;
import com.github.juliarn.npclib.fabric.protocol.FabricProtocolAdapter;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public final class FabricPlatform extends CommonPlatformBuilder<ServerLevel, ServerPlayer, ItemStack, Object> {

@Override
protected void prepareBuild() {
// set the default task manager
if (this.taskManager == null) {
this.taskManager = FabricPlatformTaskManager.taskManager();
}

// set the default version accessor
if (this.versionAccessor == null) {
this.versionAccessor = FabricVersionAccessor.versionNameBased();
}

// set the default world accessor
if (this.worldAccessor == null) {
this.worldAccessor = FabricWorldAccessor.keyBased();
}

// set the default packet adapter
if (this.packetAdapter == null) {
this.packetAdapter = FabricProtocolAdapter.fabricProtocolAdapter();
}

// set the default logger if no logger was provided
if (this.logger == null) {
this.logger = FabricPlatformLogger.logger();
}
}

@Override
protected @NotNull Platform<ServerLevel, ServerPlayer, ItemStack, Object> doBuild() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.fabric;

import com.github.juliarn.npclib.api.log.PlatformLogger;
import com.mojang.logging.LogUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

public final class FabricPlatformLogger implements PlatformLogger {

private static final Logger LOGGER = LogUtils.getLogger();
private static final FabricPlatformLogger INSTANCE = new FabricPlatformLogger();

private FabricPlatformLogger() {
}

public static @NotNull PlatformLogger logger() {
return INSTANCE;
}

@Override
public void info(@NotNull String message) {
LOGGER.info(message);
}

@Override
public void warning(@NotNull String message) {
LOGGER.warn(message);
}

@Override
public void error(@NotNull String message) {
LOGGER.error(message);
}

@Override
public void error(@NotNull String message, @Nullable Throwable exception) {
LOGGER.error(message, exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.fabric;

import com.github.juliarn.npclib.api.PlatformTaskManager;
import com.github.juliarn.npclib.common.task.AsyncPlatformTaskManager;
import org.jetbrains.annotations.NotNull;

public final class FabricPlatformTaskManager extends AsyncPlatformTaskManager {

private static final FabricPlatformTaskManager INSTANCE = new FabricPlatformTaskManager();

private FabricPlatformTaskManager() {
super("Fabric");
}

public static @NotNull PlatformTaskManager taskManager() {
return INSTANCE;
}

@Override
public void scheduleSync(@NotNull Runnable task) {
FabricModInitializer.theServer.execute(task);
}

@Override
public void scheduleDelayedSync(@NotNull Runnable task, int delayTicks) {
throw new UnsupportedOperationException("not implemented on fabric platform");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.fabric;

import com.github.juliarn.npclib.api.PlatformVersionAccessor;
import net.minecraft.SharedConstants;
import org.jetbrains.annotations.NotNull;

public final class FabricVersionAccessor {

private static int[] extractServerVersionParts() {
String[] parts = SharedConstants.getCurrentVersion().getName().split("\\.");
if (parts.length == 2 || parts.length == 3) {
// should be in the correct format, just to make sure
try {
int major = Integer.parseInt(parts[0]);
int minor = Integer.parseInt(parts[1]);
int patch = parts.length == 3 ? Integer.parseInt(parts[2]) : 0;

// return the version array from that
return new int[]{major, minor, patch};
} catch (NumberFormatException ignored) {
}
}

// unable to parse
return new int[0];
}

public static @NotNull PlatformVersionAccessor versionNameBased() {
return FabricVersionNameAccessor.INSTANCE;
}

private static final class FabricVersionNameAccessor implements PlatformVersionAccessor {

private static final int[] VERSION_NUMBER_PARTS = extractServerVersionParts();
private static final FabricVersionNameAccessor INSTANCE = new FabricVersionNameAccessor();

private static int safeGetPart(int index, int def) {
return VERSION_NUMBER_PARTS.length > index ? VERSION_NUMBER_PARTS[index] : def;
}

@Override
public int major() {
return safeGetPart(0, 1);
}

@Override
public int minor() {
return safeGetPart(1, 0);
}

@Override
public int patch() {
return safeGetPart(2, 0);
}

@Override
public boolean atLeast(int major, int minor, int patch) {
return this.major() >= major && this.minor() >= major && this.patch() >= patch;
}
}
}
Loading

0 comments on commit 63f3147

Please sign in to comment.