Skip to content

Commit

Permalink
Resource Pack Status Event
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Feb 5, 2024
1 parent ffdac2b commit c1a59c6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ kotlin_version=1.9.22
fabric_kotlin_version=1.10.17+kotlin.1.9.22

# Mod Properties
mod_version=1.0.0
mod_version=1.1.0
maven_group=net.mcbrawls
mod_id=packmanager
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.mcbrawls.packmanager;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.UUID;

/**
* An event invoked when the server receives a resource pack status packet.
* This is only invoked during the play phase, not configuration.
*/
public interface ResourcePackStatusEvent {
Event<ResourcePackStatusEvent> EVENT = EventFactory.createArrayBacked(
ResourcePackStatusEvent.class,
callbacks -> (player, id, status) -> {
for (ResourcePackStatusEvent callback : callbacks) {
callback.onStatus(player, id, status);
}
}
);

/**
* Called on resource pack status receipt from the client.
* @param id the id of the resource pack
* @param status the status of the client
*/
void onStatus(ServerPlayerEntity player, UUID id, ResourcePackStatusC2SPacket.Status status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.mcbrawls.packmanager.mixin;

import net.mcbrawls.packmanager.ResourcePackStatusEvent;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(ServerPlayNetworkHandler.class)
public abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler {
@Shadow public ServerPlayerEntity player;

private ServerPlayNetworkHandlerMixin(MinecraftServer server, ClientConnection connection, ConnectedClientData clientData) {
super(server, connection, clientData);
}

@Override
public void onResourcePackStatus(ResourcePackStatusC2SPacket packet) {
super.onResourcePackStatus(packet);

// invoke event if not disconnected
if (!connection.disconnected) {
ResourcePackStatusEvent.EVENT.invoker().onStatus(this.player, packet.id(), packet.status());
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/packmanager.accesswidener
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
accessWidener v2 named

accessible field net/minecraft/network/ClientConnection disconnected Z
1 change: 1 addition & 0 deletions src/main/resources/packmanager.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"MinecraftServerMixin",
"ServerPlayNetworkHandlerMixin",
"ServerWorldMixin"
],
"injectors": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.mcbrawls.packmanager.test
import net.fabricmc.api.DedicatedServerModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.mcbrawls.packmanager.ResourcePackEnvironment
import net.mcbrawls.packmanager.ResourcePackStatusEvent
import net.minecraft.server.MinecraftServer.ServerResourcePackProperties
import net.minecraft.world.World
import java.util.UUID
Expand Down Expand Up @@ -59,5 +60,9 @@ object PackManagerTest : DedicatedServerModInitializer {
)
}
}

ResourcePackStatusEvent.EVENT.register { player, id, status ->
println("${player.gameProfile.name}: $id, $status")
}
}
}

0 comments on commit c1a59c6

Please sign in to comment.