mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
refactor: fix plugin messaging on Fabric
This commit is contained in:
7 files changed
+43
-49
No files matched your search
@@ -21,6 +21,7 @@ package net.william278.huskhomes.user;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.network.PluginMessageBroker;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.teleport.TeleportationException;
|
||||
@@ -50,22 +51,11 @@ public class BukkitUser extends OnlineUser {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapt a {@link Player} to a {@link OnlineUser}.
|
||||
*
|
||||
* @param player the online {@link Player} to adapt
|
||||
* @return the adapted {@link OnlineUser}
|
||||
*/
|
||||
@NotNull
|
||||
public static BukkitUser adapt(@NotNull Player player, @NotNull BukkitHuskHomes plugin) {
|
||||
return new BukkitUser(player, plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Player} wrapped by this {@link BukkitUser}.
|
||||
*
|
||||
* @return the {@link Player} wrapped by this {@link BukkitUser}
|
||||
*/
|
||||
@NotNull
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
@@ -154,21 +144,16 @@ public class BukkitUser extends OnlineUser {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the player momentum and return if they are moving.
|
||||
*
|
||||
* @return {@code true} if the player is moving, {@code false} otherwise
|
||||
**/
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
return player.getVelocity().length() >= 0.1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the player's "vanished" metadata tag if they have it.
|
||||
*
|
||||
* @return {@code true} if the player is vanished, {@code false} otherwise
|
||||
*/
|
||||
@Override
|
||||
public void sendPluginMessage(byte[] message) {
|
||||
player.sendPluginMessage((BukkitHuskHomes) plugin, PluginMessageBroker.BUNGEE_CHANNEL_ID, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVanished() {
|
||||
return player.getMetadata("vanished")
|
||||
@@ -178,9 +163,6 @@ public class BukkitUser extends OnlineUser {
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles player invulnerability after teleporting.
|
||||
*/
|
||||
@Override
|
||||
public void handleInvulnerability() {
|
||||
if (plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime() <= 0) {
|
||||
@@ -192,11 +174,4 @@ public class BukkitUser extends OnlineUser {
|
||||
plugin.runSyncDelayed(() -> player.setInvulnerable(false), invulnerabilityTimeInTicks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Bukkit plugin message to the player.
|
||||
*/
|
||||
public void sendPluginMessage(@NotNull String channel, final byte[] message) {
|
||||
player.sendPluginMessage((BukkitHuskHomes) plugin, channel, message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -93,7 +93,7 @@ public class PluginMessageBroker extends Broker {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendPluginMessage(BUNGEE_CHANNEL_ID, messageWriter.toByteArray());
|
||||
sender.sendPluginMessage(messageWriter.toByteArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +104,7 @@ public class PluginMessageBroker extends Broker {
|
||||
outputStream.writeUTF("Connect");
|
||||
outputStream.writeUTF(server);
|
||||
|
||||
user.sendPluginMessage(BUNGEE_CHANNEL_ID, outputStream.toByteArray());
|
||||
user.sendPluginMessage(outputStream.toByteArray());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -193,12 +193,11 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
|
||||
public abstract void teleportLocally(@NotNull Location location, boolean async) throws TeleportationException;
|
||||
|
||||
/**
|
||||
* Send a plugin message to the user.
|
||||
* Send a plugin message to the user on the bungee channel.
|
||||
*
|
||||
* @param channel channel to send it on
|
||||
* @param message byte array of message data
|
||||
*/
|
||||
public abstract void sendPluginMessage(@NotNull String channel, byte[] message);
|
||||
public abstract void sendPluginMessage(byte[] message);
|
||||
|
||||
/**
|
||||
* Returns if a player is moving (i.e., they have momentum).
|
||||
|
||||
@@ -27,6 +27,7 @@ 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;
|
||||
@@ -321,7 +322,9 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
|
||||
@Override
|
||||
public void initializePluginChannels() {
|
||||
ServerPlayNetworking.registerGlobalReceiver(FabricPluginMessage.BUNGEE_CHANNEL_ID, this);
|
||||
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
|
||||
|
||||
@@ -20,27 +20,44 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import lombok.*;
|
||||
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(access = AccessLevel.PRIVATE)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FabricPluginMessage implements CustomPayload {
|
||||
|
||||
public static final Id<FabricPluginMessage> BUNGEE_CHANNEL_ID = new Id<>(
|
||||
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 Id<FabricPluginMessage> id;
|
||||
private byte[] data;
|
||||
|
||||
@NotNull
|
||||
public static FabricPluginMessage of(@NotNull String id, byte[] data) {
|
||||
return new FabricPluginMessage(
|
||||
id.equals("BungeeCord") ? BUNGEE_CHANNEL_ID : new Id<>(Identifier.tryParse(id)), 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -161,8 +161,8 @@ public class FabricUser extends OnlineUser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPluginMessage(@NotNull String channel, byte[] message) {
|
||||
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(FabricPluginMessage.of(channel, message)));
|
||||
public void sendPluginMessage(byte[] message) {
|
||||
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(new FabricPluginMessage(message)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -130,7 +130,7 @@ public class SpongeUser extends OnlineUser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPluginMessage(@NotNull String channel, byte[] message) {
|
||||
public void sendPluginMessage(byte[] message) {
|
||||
((SpongeHuskHomes) plugin).getPluginMessageChannel().sendTo(player, buf -> buf.writeBytes(message));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user