refactor: use FabricPluginMessaging API by Stampede for better compat

This commit is contained in:
William278 committed 2025-03-16 13:32:56 +00:00
1 parent 18cda2da3c
commit 1d306c2e23
4 files changed
+16 -88

No files matched your search

+1
View File
@@ -17,6 +17,7 @@ dependencies {
modImplementation include("eu.pb4:placeholder-api:${fabric_placeholder_api_version}")
modImplementation include("net.impactdev.impactor.api:economy:${fabric_impactor_api_version}")
modImplementation include("net.william278.toilet:toilet-fabric:1.0.12+$project.name")
modImplementation include("com.github.PokeSkies:FabricPluginMessaging:f621c4fcf6")
modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_api_version}"
// Manually include config deps due to the way including api deps works
@@ -22,14 +22,13 @@ package net.william278.huskhomes;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.pokeskies.fabricpluginmessaging.PluginMessageEvent;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.kyori.adventure.audience.Audience;
@@ -63,7 +62,6 @@ import net.william278.huskhomes.listener.EventListener;
import net.william278.huskhomes.listener.FabricEventListener;
import net.william278.huskhomes.manager.Manager;
import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.FabricPluginMessage;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
@@ -93,8 +91,7 @@ import java.util.logging.Level;
@Getter
@NoArgsConstructor
public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes, FabricTask.Supplier,
FabricEventDispatcher, FabricSavePositionProvider, FabricUserProvider, FabricHookProvider,
ServerPlayNetworking.PlayPayloadHandler<FabricPluginMessage> {
FabricEventDispatcher, FabricSavePositionProvider, FabricUserProvider, FabricHookProvider {
public static final Logger LOGGER = LoggerFactory.getLogger("HuskHomes");
private final ModContainer modContainer = FabricLoader.getInstance().getModContainer("huskhomes")
@@ -290,22 +287,16 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
@Override
public void setupPluginMessagingChannels() {
PayloadTypeRegistry.playC2S().register(FabricPluginMessage.CHANNEL_ID, FabricPluginMessage.CODEC);
PayloadTypeRegistry.playS2C().register(FabricPluginMessage.CHANNEL_ID, FabricPluginMessage.CODEC);
ServerPlayNetworking.registerGlobalReceiver(FabricPluginMessage.CHANNEL_ID, this);
}
// When the server receives a plugin message
@Override
public void receive(@NotNull FabricPluginMessage payload, @NotNull ServerPlayNetworking.Context context) {
if (broker instanceof PluginMessageBroker messenger
&& getSettings().getCrossServer().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
messenger.onReceive(
PluginMessageBroker.BUNGEE_CHANNEL_ID,
getOnlineUser(context.player()),
payload.getData()
);
}
PluginMessageEvent.EVENT.register((payload, context) -> {
if (broker instanceof PluginMessageBroker messenger
&& getSettings().getCrossServer().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
messenger.onReceive(
PluginMessageBroker.BUNGEE_CHANNEL_ID,
getOnlineUser(context.player()),
payload.getData()
);
}
});
}
@Override
@@ -1,64 +0,0 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.network;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
@Getter
@AllArgsConstructor
public class FabricPluginMessage implements CustomPayload {
public static final PacketCodec<RegistryByteBuf, FabricPluginMessage> CODEC = PacketCodec.of(
(value, buf) -> writeBytes(buf, value.getData()),
FabricPluginMessage::new
);
public static final Id<FabricPluginMessage> CHANNEL_ID = new Id<>(
Identifier.of("bungeecord", "main")
);
private byte[] data;
private FabricPluginMessage(@NotNull PacketByteBuf buf) {
this(getWrittenBytes(buf));
}
private static byte[] getWrittenBytes(@NotNull PacketByteBuf buf) {
byte[] bs = new byte[buf.readableBytes()];
buf.readBytes(bs);
return bs;
}
private static void writeBytes(@NotNull PacketByteBuf buf, byte[] v) {
buf.writeBytes(v);
}
@Override
public Id<FabricPluginMessage> getId() {
return CHANNEL_ID;
}
}
@@ -19,17 +19,17 @@
package net.william278.huskhomes.user;
import com.pokeskies.fabricpluginmessaging.PluginMessagePacket;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.util.TriState;
import net.kyori.adventure.audience.Audience;
import net.minecraft.entity.Entity;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.network.FabricPluginMessage;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
@@ -170,7 +170,7 @@ public class FabricUser extends OnlineUser {
@Override
public void sendPluginMessage(byte[] message) {
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(new FabricPluginMessage(message)));
ServerPlayNetworking.send(player, new PluginMessagePacket(message));
}
@Override