Skip to content

Commit 5b591d4

Browse files
committed
Initial implementation for the fabric loader
0 parents  commit 5b591d4

File tree

11 files changed

+448
-0
lines changed

11 files changed

+448
-0
lines changed

.gitignore

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
# mpeltonen/sbt-idea plugin
11+
.idea_modules/
12+
13+
# JIRA plugin
14+
atlassian-ide-plugin.xml
15+
16+
# Compiled class file
17+
*.class
18+
19+
# Log file
20+
*.log
21+
22+
# BlueJ files
23+
*.ctxt
24+
25+
# Package Files #
26+
*.jar
27+
*.war
28+
*.nar
29+
*.ear
30+
*.zip
31+
*.tar.gz
32+
*.rar
33+
34+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
35+
hs_err_pid*
36+
37+
*~
38+
39+
# temporary files which can be created if a process still has a handle open of a deleted file
40+
.fuse_hidden*
41+
42+
# KDE directory preferences
43+
.directory
44+
45+
# Linux trash folder which might appear on any partition or disk
46+
.Trash-*
47+
48+
# .nfs files are created when an open file is removed but is still being accessed
49+
.nfs*
50+
51+
# General
52+
.DS_Store
53+
.AppleDouble
54+
.LSOverride
55+
56+
# Icon must end with two \r
57+
Icon
58+
59+
# Thumbnails
60+
._*
61+
62+
# Files that might appear in the root of a volume
63+
.DocumentRevisions-V100
64+
.fseventsd
65+
.Spotlight-V100
66+
.TemporaryItems
67+
.Trashes
68+
.VolumeIcon.icns
69+
.com.apple.timemachine.donotpresent
70+
71+
# Directories potentially created on remote AFP share
72+
.AppleDB
73+
.AppleDesktop
74+
Network Trash Folder
75+
Temporary Items
76+
.apdisk
77+
78+
# Windows thumbnail cache files
79+
Thumbs.db
80+
Thumbs.db:encryptable
81+
ehthumbs.db
82+
ehthumbs_vista.db
83+
84+
# Dump file
85+
*.stackdump
86+
87+
# Folder config file
88+
[Dd]esktop.ini
89+
90+
# Recycle Bin used on file shares
91+
$RECYCLE.BIN/
92+
93+
# Windows Installer files
94+
*.cab
95+
*.msi
96+
*.msix
97+
*.msm
98+
*.msp
99+
100+
# Windows shortcuts
101+
*.lnk
102+
103+
.gradle
104+
build/
105+
106+
# Ignore Gradle GUI config
107+
gradle-app.setting
108+
109+
# Cache of project
110+
.gradletasknamecache
111+
112+
**/build/
113+
114+
# Common working directory
115+
run/
116+
117+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
118+
!gradle-wrapper.jar

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

build.gradle

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
plugins {
2+
id 'fabric-loom' version '1.6-SNAPSHOT'
3+
id 'maven-publish'
4+
id 'com.github.johnrengelman.shadow' version '7.0.0'
5+
}
6+
7+
version = "${project.mod_version}+mc${project.minecraft_version}"
8+
group = project.maven_group
9+
10+
base {
11+
archivesName = project.archives_base_name
12+
}
13+
14+
repositories {
15+
// Add repositories to retrieve artifacts from in here.
16+
// You should only use this when depending on other mods because
17+
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
18+
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
19+
// for more information about repositories.
20+
maven {
21+
url "https://repo.hypixel.net/repository/Hypixel/"
22+
}
23+
mavenLocal()
24+
}
25+
26+
dependencies {
27+
// To change the versions see the gradle.properties file
28+
minecraft "com.mojang:minecraft:${project.minecraft_version}"
29+
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
30+
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
31+
32+
// Fabric API. This is technically optional, but you probably want it anyway.
33+
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
34+
35+
shadow "net.hypixel:mod-api:${project.mod_api_version}"
36+
}
37+
38+
shadowJar {
39+
// Exclude all dependencies by default
40+
configurations = [project.configurations.shadow]
41+
42+
// Rename the shaded JAR
43+
archiveClassifier = 'shaded'
44+
}
45+
46+
remapJar {
47+
dependsOn(shadowJar)
48+
inputFile = tasks.shadowJar.archiveFile
49+
}
50+
51+
processResources {
52+
inputs.property "version", project.version
53+
inputs.property "minecraft_version", project.minecraft_version
54+
inputs.property "loader_version", project.loader_version
55+
filteringCharset "UTF-8"
56+
57+
filesMatching("fabric.mod.json") {
58+
expand "version": project.version,
59+
"minecraft_version": project.minecraft_version,
60+
"loader_version": project.loader_version
61+
}
62+
}
63+
64+
def targetJavaVersion = 17
65+
tasks.withType(JavaCompile).configureEach {
66+
// ensure that the encoding is set to UTF-8, no matter what the system default is
67+
// this fixes some edge cases with special characters not displaying correctly
68+
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
69+
// If Javadoc is generated, this must be specified in that task too.
70+
it.options.encoding = "UTF-8"
71+
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
72+
it.options.release.set(targetJavaVersion)
73+
}
74+
}
75+
76+
java {
77+
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
78+
if (JavaVersion.current() < javaVersion) {
79+
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
80+
}
81+
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
82+
// if it is present.
83+
// If you remove this line, sources will not be generated.
84+
withSourcesJar()
85+
}
86+
87+
jar {
88+
from("LICENSE") {
89+
rename { "${it}_${project.archivesBaseName}"}
90+
}
91+
}
92+
93+
// configure the maven publication
94+
publishing {
95+
publications {
96+
mavenJava(MavenPublication) {
97+
from components.java
98+
}
99+
}
100+
101+
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
102+
repositories {
103+
// Add repositories to publish to here.
104+
// Notice: This block does NOT have the same function as the block in the top level.
105+
// The repositories here will be used for publishing your artifact, not for
106+
// retrieving dependencies.
107+
}
108+
}

gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Done to increase the memory available to gradle.
2+
org.gradle.jvmargs=-Xmx1G
3+
4+
# Fabric Properties
5+
# check these on https://modmuss50.me/fabric.html
6+
minecraft_version=1.20.5
7+
yarn_mappings=1.20.5+build.1
8+
loader_version=0.15.10
9+
10+
# Mod Properties
11+
mod_version = 0.3.2
12+
maven_group = net.hypixel
13+
archives_base_name = HypixelModAPI
14+
mod_api_version = 0.3.2
15+
16+
# Dependencies
17+
# check this on https://modmuss50.me/fabric.html
18+
fabric_version=0.97.8+1.20.6
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip

settings.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pluginManagement {
2+
repositories {
3+
maven {
4+
name = 'Fabric'
5+
url = 'https://maven.fabricmc.net/'
6+
}
7+
gradlePluginPortal()
8+
}
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package net.hypixel.modapi.fabric;
2+
3+
import com.mojang.logging.LogUtils;
4+
import net.fabricmc.api.ClientModInitializer;
5+
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
6+
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
7+
import net.hypixel.modapi.HypixelModAPI;
8+
import net.hypixel.modapi.fabric.payload.ClientboundHypixelPayload;
9+
import net.hypixel.modapi.fabric.payload.ServerboundHypixelPayload;
10+
import net.minecraft.network.PacketByteBuf;
11+
import net.minecraft.network.codec.PacketCodec;
12+
import net.minecraft.network.packet.CustomPayload;
13+
import org.apache.commons.lang3.Validate;
14+
import org.slf4j.Logger;
15+
16+
public class FabricModAPI implements ClientModInitializer {
17+
private static final Logger LOGGER = LogUtils.getLogger();
18+
19+
@Override
20+
public void onInitializeClient() {
21+
registerPayloads();
22+
registerPacketSender();
23+
}
24+
25+
private void registerPayloads() {
26+
for (String identifier : HypixelModAPI.getInstance().getRegistry().getIdentifiers()) {
27+
registerClientbound(identifier);
28+
registerServerbound(identifier);
29+
}
30+
}
31+
32+
private void registerPacketSender() {
33+
HypixelModAPI.getInstance().setPacketSender((packet) -> {
34+
ServerboundHypixelPayload payload = new ServerboundHypixelPayload(packet);
35+
ClientPlayNetworking.send(payload);
36+
});
37+
}
38+
39+
public void registerClientbound(String identifier) {
40+
Validate.isTrue(HypixelModAPI.getInstance().getRegistry().isRegistered(identifier), "Identifier %s is not registered", identifier);
41+
42+
CustomPayload.Id<ClientboundHypixelPayload> clientboundId = CustomPayload.id(identifier);
43+
PacketCodec<PacketByteBuf, ClientboundHypixelPayload> codec = ClientboundHypixelPayload.buildCodec(clientboundId);
44+
PayloadTypeRegistry.playS2C().register(clientboundId, codec);
45+
46+
// Also register the global receiver for handling incoming packets
47+
ClientPlayNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> {
48+
if (!payload.isSuccess()) {
49+
LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason());
50+
return;
51+
}
52+
53+
HypixelModAPI.getInstance().handle(payload.getPacket());
54+
});
55+
}
56+
57+
public void registerServerbound(String identifier) {
58+
Validate.isTrue(HypixelModAPI.getInstance().getRegistry().isRegistered(identifier), "Identifier %s is not registered", identifier);
59+
60+
CustomPayload.Id<ServerboundHypixelPayload> serverboundId = CustomPayload.id(identifier);
61+
PacketCodec<PacketByteBuf, ServerboundHypixelPayload> codec = ServerboundHypixelPayload.buildCodec(serverboundId);
62+
PayloadTypeRegistry.playC2S().register(serverboundId, codec);
63+
}
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package net.hypixel.modapi.fabric.payload;
2+
3+
import net.hypixel.modapi.HypixelModAPI;
4+
import net.hypixel.modapi.error.ErrorReason;
5+
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
6+
import net.hypixel.modapi.serializer.PacketSerializer;
7+
import net.minecraft.network.PacketByteBuf;
8+
import net.minecraft.network.codec.PacketCodec;
9+
import net.minecraft.network.packet.CustomPayload;
10+
11+
public class ClientboundHypixelPayload implements CustomPayload {
12+
private final Id<ClientboundHypixelPayload> id;
13+
14+
private ClientboundHypixelPacket packet;
15+
private ErrorReason errorReason;
16+
17+
private ClientboundHypixelPayload(Id<ClientboundHypixelPayload> id, PacketByteBuf buf) {
18+
this.id = id;
19+
20+
PacketSerializer serializer = new PacketSerializer(buf);
21+
boolean success = serializer.readBoolean();
22+
if (!success) {
23+
this.errorReason = ErrorReason.getById(serializer.readVarInt());
24+
return;
25+
}
26+
27+
this.packet = HypixelModAPI.getInstance().getRegistry().createClientboundPacket(id.id().toString(), serializer);
28+
}
29+
30+
@Override
31+
public Id<? extends CustomPayload> getId() {
32+
return id;
33+
}
34+
35+
public boolean isSuccess() {
36+
return packet != null;
37+
}
38+
39+
public ClientboundHypixelPacket getPacket() {
40+
return packet;
41+
}
42+
43+
public ErrorReason getErrorReason() {
44+
return errorReason;
45+
}
46+
47+
public static PacketCodec<PacketByteBuf, ClientboundHypixelPayload> buildCodec(Id<ClientboundHypixelPayload> id) {
48+
return CustomPayload.codecOf((value, buf) -> {
49+
throw new UnsupportedOperationException("Cannot write ClientboundHypixelPayload");
50+
}, buf -> new ClientboundHypixelPayload(id, buf));
51+
}
52+
}

0 commit comments

Comments
 (0)