Fixed up the code a bit

This commit is contained in:
lukasabbe
2025-04-22 19:37:13 +02:00
parent 9dcededc87
commit dd8cf78b93
8 changed files with 145 additions and 140 deletions
@@ -1,56 +0,0 @@
package me.lukasabbe.voicechatgroupmsg;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.maxhenkel.voicechat.api.Group;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.MessageArgumentType;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.List;
public class Commands {
public static void CreateGroupMsgCommand(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(CommandManager
.literal("msgvc")
.then(CommandManager
.argument("message", MessageArgumentType.message())
.executes(Commands::runCommand)));
}
private static int runCommand(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
final ServerCommandSource source = ctx.getSource();
if(!source.isExecutedByPlayer()) {
source.sendError(Text.literal("You can't send msgvc as the console"));
return 0;
}
final ServerPlayerEntity player = source.getPlayer();
if(!VoiceChatGroupMsg.isPlayerInGroup(player)){
source.sendError(Text.literal("You need to be in a voice chat group to use this command"));
return 0;
}
Group group = VoiceChatGroupMsg.getPlayerGroup(player);
List<ServerPlayerEntity> players = VoiceChatGroupMsg.GroupPlayers(group.getId(), source.getWorld());
MessageArgumentType.getSignedMessage(ctx, "message", signedMessage -> {
player.sendChatMessage(
SentMessage.of(signedMessage),
true,
MessageType.params(MessageType.TEAM_MSG_COMMAND_OUTGOING,source).withTargetName(Text.of(group.getName())));
players.forEach(player1 -> {
if(!player1.getUuid().equals(player.getUuid())){
player1.sendChatMessage(
SentMessage.of(signedMessage),
true,
MessageType.params(MessageType.TEAM_MSG_COMMAND_INCOMING,source).withTargetName(Text.of(group.getName())));
}
});
});
return 1;
}
}
@@ -1,60 +1,19 @@
package me.lukasabbe.voicechatgroupmsg;
import de.maxhenkel.voicechat.api.Group;
import de.maxhenkel.voicechat.api.VoicechatPlugin;
import de.maxhenkel.voicechat.api.VoicechatServerApi;
import de.maxhenkel.voicechat.api.events.EventRegistration;
import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent;
import me.lukasabbe.voicechatgroupmsg.command.MsgVcCommand;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import java.util.List;
import java.util.UUID;
public class VoiceChatGroupMsg implements DedicatedServerModInitializer {
public class VoiceChatGroupMsg implements DedicatedServerModInitializer, VoicechatPlugin {
public final static String MOD_ID = "voicechatgroupmsg";
private static VoicechatServerApi API = null;
@Override
public void onInitializeServer() {
CommandRegistrationCallback.EVENT.register(Commands::CreateGroupMsgCommand);
registerCommands();
}
@Override
public String getPluginId() {
return MOD_ID;
}
@Override
public void registerEvents(EventRegistration registration) {
registration.registerEvent(VoicechatServerStartedEvent.class, this::onServerStarted);
}
public void onServerStarted(VoicechatServerStartedEvent event) {
API = event.getVoicechat();
}
public static boolean isPlayerInGroup(ServerPlayerEntity player){
try{
return API.getConnectionOf(player.getUuid()).isInGroup();
}catch (NullPointerException ignore){
return false;
}
}
public static boolean isPlayerInGroup(ServerPlayerEntity player, UUID groupID){
try{
if(!API.getConnectionOf(player.getUuid()).isInGroup()) return false;
return API.getConnectionOf(player.getUuid()).getGroup().getId().equals(groupID);
}catch (NullPointerException ignore){
return false;
}
}
public static Group getPlayerGroup(ServerPlayerEntity player){
return API.getConnectionOf(player.getUuid()).getGroup();
}
public static List<ServerPlayerEntity> GroupPlayers(UUID groupUUID, ServerWorld serverWorld){
return serverWorld.getPlayers(player -> isPlayerInGroup(player, groupUUID));
private static void registerCommands() {
CommandRegistrationCallback.EVENT.register(MsgVcCommand::CreateGroupMsgCommand);
}
}
@@ -0,0 +1,64 @@
package me.lukasabbe.voicechatgroupmsg.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.maxhenkel.voicechat.api.Group;
import me.lukasabbe.voicechatgroupmsg.util.VoiceChatUtil;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.MessageArgumentType;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
import net.minecraft.network.message.SignedMessage;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.List;
public class MsgVcCommand {
public static void CreateGroupMsgCommand(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
dispatcher.register(
CommandManager
.literal("msgvc")
.requires(ServerCommandSource::isExecutedByPlayer)
.then(
CommandManager
.argument("message", MessageArgumentType.message())
.executes(MsgVcCommand::runCommand)));
}
private static int runCommand(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
final ServerCommandSource source = ctx.getSource();
final ServerPlayerEntity player = source.getPlayer();
if(!VoiceChatUtil.isPlayerInGroup(player)){
source.sendError(Text.literal("You need to be in a voice chat group to use this command"));
return 0;
}
Group group = VoiceChatUtil.getPlayerGroup(player);
List<ServerPlayerEntity> players = VoiceChatUtil.GroupPlayers(group.getId(), source.getWorld());
MessageArgumentType.getSignedMessage(ctx, "message", signedMessage -> {
sendMessage(player, signedMessage, MessageType.TEAM_MSG_COMMAND_OUTGOING, source, group);
players.forEach(voiceChatMember -> {
if(voiceChatMember.getUuid().equals(player.getUuid())) return;
sendMessage(voiceChatMember, signedMessage, MessageType.TEAM_MSG_COMMAND_INCOMING, source, group);
});
});
return 1;
}
private static void sendMessage(ServerPlayerEntity player, SignedMessage signedMessage, RegistryKey<MessageType> teamMsgCommandOutgoing, ServerCommandSource source, Group group) {
player.sendChatMessage(
SentMessage.of(signedMessage),
true,
MessageType.params(teamMsgCommandOutgoing, source)
.withTargetName(Text.of(group.getName()))
);
}
}
@@ -0,0 +1,56 @@
package me.lukasabbe.voicechatgroupmsg.util;
import de.maxhenkel.voicechat.api.Group;
import de.maxhenkel.voicechat.api.VoicechatPlugin;
import de.maxhenkel.voicechat.api.VoicechatServerApi;
import de.maxhenkel.voicechat.api.events.EventRegistration;
import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import java.util.List;
import java.util.UUID;
import static me.lukasabbe.voicechatgroupmsg.VoiceChatGroupMsg.MOD_ID;
public class VoiceChatUtil implements VoicechatPlugin {
private static VoicechatServerApi API = null;
@Override
public String getPluginId() {
return MOD_ID;
}
@Override
public void registerEvents(EventRegistration registration) {
registration.registerEvent(VoicechatServerStartedEvent.class, this::onServerStarted);
}
public void onServerStarted(VoicechatServerStartedEvent event) {
API = event.getVoicechat();
}
public static boolean isPlayerInGroup(ServerPlayerEntity player){
try{
return API.getConnectionOf(player.getUuid()).isInGroup();
}catch (NullPointerException ignore){
return false;
}
}
public static boolean isPlayerInGroup(ServerPlayerEntity player, UUID groupID){
try{
if(!API.getConnectionOf(player.getUuid()).isInGroup()) return false;
return API.getConnectionOf(player.getUuid()).getGroup().getId().equals(groupID);
}catch (NullPointerException ignore){
return false;
}
}
public static Group getPlayerGroup(ServerPlayerEntity player){
return API.getConnectionOf(player.getUuid()).getGroup();
}
public static List<ServerPlayerEntity> GroupPlayers(UUID groupUUID, ServerWorld serverWorld){
return serverWorld.getPlayers(player -> isPlayerInGroup(player, groupUUID));
}
}
+1 -1
View File
@@ -16,7 +16,7 @@
"me.lukasabbe.voicechatgroupmsg.VoiceChatGroupMsg"
],
"voicechat": [
"me.lukasabbe.voicechatgroupmsg.VoiceChatGroupMsg"
"me.lukasabbe.voicechatgroupmsg.util.VoiceChatUtil"
]
},
"depends": {