Implement plugin message messenger service on Fabric

This commit is contained in:
Luck 2021-01-06 13:14:41 +00:00
parent 848c36928f
commit 443ea510bb
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 179 additions and 3 deletions

View File

@ -47,7 +47,7 @@ The project is split up into a few separate modules.
* **API** - The public, semantically versioned API used by other plugins wishing to integrate with and retrieve data from LuckPerms. This module (for the most part) does not contain any implementation itself, and is provided by the plugin. * **API** - The public, semantically versioned API used by other plugins wishing to integrate with and retrieve data from LuckPerms. This module (for the most part) does not contain any implementation itself, and is provided by the plugin.
* **Common** - The common module contains most of the code which implements the respective LuckPerms plugins. This abstract module reduces duplicated code throughout the project. * **Common** - The common module contains most of the code which implements the respective LuckPerms plugins. This abstract module reduces duplicated code throughout the project.
* **Bukkit, BungeeCord, Sponge, Nukkit & Velocity** - Each use the common module to implement plugins on the respective server platforms. * **Bukkit, BungeeCord, Sponge, Fabric, Nukkit & Velocity** - Each use the common module to implement plugins on the respective server platforms.
## License ## License
LuckPerms is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/lucko/LuckPerms/blob/master/LICENSE.txt) for more info. LuckPerms is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/lucko/LuckPerms/blob/master/LICENSE.txt) for more info.

View File

@ -46,6 +46,7 @@ import me.lucko.luckperms.fabric.context.FabricContextManager;
import me.lucko.luckperms.fabric.context.FabricPlayerCalculator; import me.lucko.luckperms.fabric.context.FabricPlayerCalculator;
import me.lucko.luckperms.fabric.listeners.FabricConnectionListener; import me.lucko.luckperms.fabric.listeners.FabricConnectionListener;
import me.lucko.luckperms.fabric.listeners.PermissionCheckListener; import me.lucko.luckperms.fabric.listeners.PermissionCheckListener;
import me.lucko.luckperms.fabric.messaging.FabricMessagingFactory;
import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.ModContainer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -133,8 +134,8 @@ public class LPFabricPlugin extends AbstractLuckPermsPlugin {
} }
@Override @Override
protected MessagingFactory<LPFabricPlugin> provideMessagingFactory() { protected MessagingFactory<?> provideMessagingFactory() {
return new MessagingFactory<>(this); return new FabricMessagingFactory(this);
} }
@Override @Override

View File

@ -0,0 +1,72 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) 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 me.lucko.luckperms.fabric.messaging;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.LuckPermsMessagingService;
import me.lucko.luckperms.common.messaging.MessagingFactory;
import me.lucko.luckperms.fabric.LPFabricPlugin;
import net.luckperms.api.messenger.IncomingMessageConsumer;
import net.luckperms.api.messenger.Messenger;
import net.luckperms.api.messenger.MessengerProvider;
import org.checkerframework.checker.nullness.qual.NonNull;
public class FabricMessagingFactory extends MessagingFactory<LPFabricPlugin> {
public FabricMessagingFactory(LPFabricPlugin plugin) {
super(plugin);
}
@Override
protected InternalMessagingService getServiceFor(String messagingType) {
if (messagingType.equals("pluginmsg") || messagingType.equals("bungee") || messagingType.equals("velocity")) {
try {
return new LuckPermsMessagingService(getPlugin(), new PluginMessageMessengerProvider());
} catch (Exception e) {
getPlugin().getLogger().severe("Exception occurred whilst enabling messaging", e);
}
}
return super.getServiceFor(messagingType);
}
private class PluginMessageMessengerProvider implements MessengerProvider {
@Override
public @NonNull String getName() {
return "PluginMessage";
}
@Override
public @NonNull Messenger obtain(@NonNull IncomingMessageConsumer incomingMessageConsumer) {
PluginMessageMessenger messenger = new PluginMessageMessenger(getPlugin(), incomingMessageConsumer);
messenger.init();
return messenger;
}
}
}

View File

@ -0,0 +1,103 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) 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 me.lucko.luckperms.fabric.messaging;
import com.google.common.collect.Iterables;
import me.lucko.luckperms.common.plugin.scheduler.SchedulerTask;
import me.lucko.luckperms.fabric.LPFabricPlugin;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.luckperms.api.messenger.IncomingMessageConsumer;
import net.luckperms.api.messenger.Messenger;
import net.luckperms.api.messenger.message.OutgoingMessage;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class PluginMessageMessenger implements Messenger, ServerPlayNetworking.PlayChannelHandler {
private static final Identifier CHANNEL = new Identifier("luckperms", "update");
private final LPFabricPlugin plugin;
private final IncomingMessageConsumer consumer;
public PluginMessageMessenger(LPFabricPlugin plugin, IncomingMessageConsumer consumer) {
this.plugin = plugin;
this.consumer = consumer;
}
public void init() {
ServerPlayNetworking.registerGlobalReceiver(CHANNEL, this);
}
@Override
public void close() {
ServerPlayNetworking.unregisterGlobalReceiver(CHANNEL);
}
@Override
public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
AtomicReference<SchedulerTask> taskRef = new AtomicReference<>();
SchedulerTask task = this.plugin.getBootstrap().getScheduler().asyncRepeating(() -> {
MinecraftServer server = this.plugin.getBootstrap().getServer().orElse(null);
if (server == null) {
return;
}
Collection<ServerPlayerEntity> players = server.getPlayerManager().getPlayerList();
ServerPlayerEntity p = Iterables.getFirst(players, null);
if (p == null) {
return;
}
PacketByteBuf buf = PacketByteBufs.create();
buf.writeString(outgoingMessage.asEncodedString());
ServerPlayNetworking.send(p, CHANNEL, buf);
SchedulerTask t = taskRef.getAndSet(null);
if (t != null) {
t.cancel();
}
}, 10, TimeUnit.SECONDS);
taskRef.set(task);
}
@Override
public void receive(MinecraftServer server, ServerPlayerEntity entity, ServerPlayNetworkHandler netHandler, PacketByteBuf buf, PacketSender packetSender) {
String msg = buf.readString();
this.consumer.consumeIncomingMessageAsString(msg);
}
}