1.21.11
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package de.winniepat.voiceChatInteraction;
|
||||
|
||||
public class AudioUtils {
|
||||
public static double calculateAudioLevel(short[] samples) {
|
||||
double rms = 0D; // root mean square (RMS) amplitude
|
||||
|
||||
for (int i = 0; i < samples.length; i++) {
|
||||
double sample = (double) samples[i] / (double) Short.MAX_VALUE;
|
||||
rms += sample * sample;
|
||||
}
|
||||
|
||||
int sampleCount = samples.length / 2;
|
||||
|
||||
rms = (sampleCount == 0) ? 0 : Math.sqrt(rms / sampleCount);
|
||||
|
||||
double db;
|
||||
|
||||
if (rms > 0D) {
|
||||
db = Math.min(Math.max(20D * Math.log10(rms), -127D), 0D);
|
||||
} else {
|
||||
db = -127D;
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package de.winniepat.voiceChatInteraction;
|
||||
|
||||
import de.maxhenkel.voicechat.api.BukkitVoicechatService;import de.winniepat.voiceChatInteraction.config.ServerConfig;
|
||||
import org.bukkit.GameEvent;import org.bukkit.Server;import org.bukkit.configuration.file.FileConfiguration;import org.bukkit.plugin.java.JavaPlugin;
|
||||
import javax.annotation.Nullable;import java.util.logging.LogManager;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class VoiceChatInteraction extends JavaPlugin {
|
||||
|
||||
public static final String PLUGIN_ID = "voicechat_interaction";
|
||||
public static final Logger LOGGER = LogManager.getLogManager().getLogger(PLUGIN_ID);
|
||||
public static ServerConfig SERVER_CONFIG;
|
||||
public static Server SERVER;
|
||||
public static GameEvent VOICE_GAME_EVENT;
|
||||
public static VoiceChatInteraction INSTANCE;
|
||||
|
||||
@Nullable
|
||||
public static VoiceChatInteractionPlugin voicechatPlugin;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
SERVER = getServer();
|
||||
VOICE_GAME_EVENT = GameEvent.PRIME_FUSE;
|
||||
INSTANCE = this;
|
||||
|
||||
FileConfiguration config = this.getConfig();
|
||||
config.addDefault("group_interaction", false);
|
||||
config.addDefault("whisper_interaction", false);
|
||||
config.addDefault("sneak_interaction", false);
|
||||
config.addDefault("minimum_activation_threshold", -50);
|
||||
config.addDefault("default_interaction_toggle", true);
|
||||
config.options().copyDefaults(true);
|
||||
saveConfig();
|
||||
|
||||
SERVER_CONFIG = new ServerConfig(config);
|
||||
|
||||
BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);
|
||||
if (service != null) {
|
||||
voicechatPlugin = new VoiceChatInteractionPlugin();
|
||||
service.registerPlugin(voicechatPlugin);
|
||||
getLogger().info("Successfully registered voicechat_interaction plugin");
|
||||
} else {
|
||||
getLogger().info("Failed to register voicechat_interaction plugin");
|
||||
}
|
||||
|
||||
this.getCommand("voicechat_interaction").setExecutor(new VoiceChatInteractionCommand());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (voicechatPlugin != null) {
|
||||
getServer().getServicesManager().unregister(voicechatPlugin);
|
||||
getLogger().info("Successfully unregistered voicechat_interaction plugin");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package de.winniepat.voiceChatInteraction;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class VoiceChatInteractionCommand implements CommandExecutor {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
try {
|
||||
if (sender instanceof Player) {
|
||||
VoiceChatInteractionPlugin plugin = VoiceChatInteraction.voicechatPlugin;
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length == 0) {
|
||||
player.sendMessage("/voicechat_interaction toggle [<player>]");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equals("toggle")) {
|
||||
if (args.length >= 2 && player.hasPermission("voicechat_interaction.toggle.others")) {
|
||||
Player other = Bukkit.getPlayer(args[1]);
|
||||
plugin.setInteractionToggle(other, !plugin.getInteractionToggle(other));
|
||||
player.sendMessage("Interactions toggled to " + plugin.getInteractionToggle(other) + " for " + other.getName());
|
||||
other.sendMessage(player.getName() + " toggled your interactions to " + plugin.getInteractionToggle(other));
|
||||
} else if (player.hasPermission("voicechat_interaction.toggle")) {
|
||||
plugin.setInteractionToggle(player, !plugin.getInteractionToggle(player));
|
||||
player.sendMessage("Interactions toggled to " + plugin.getInteractionToggle(player) + " for yourself");
|
||||
} else {
|
||||
player.sendMessage("You do not have permission to use that command (voicechat_interaction.toggle)");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package de.winniepat.voiceChatInteraction;
|
||||
|
||||
import de.maxhenkel.voicechat.api.*;
|
||||
import de.maxhenkel.voicechat.api.events.EventRegistration;
|
||||
import de.maxhenkel.voicechat.api.events.MicrophonePacketEvent;
|
||||
import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent;
|
||||
import de.maxhenkel.voicechat.api.opus.OpusDecoder;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class VoiceChatInteractionPlugin implements VoicechatPlugin {
|
||||
public static VoicechatApi voicechatApi;
|
||||
private static ConcurrentHashMap<UUID, Long> cooldowns;
|
||||
|
||||
@Nullable
|
||||
public static VoicechatServerApi voicechatServerApi;
|
||||
|
||||
@Nullable
|
||||
private OpusDecoder decoder;
|
||||
|
||||
@Override
|
||||
public String getPluginId() {
|
||||
return VoiceChatInteraction.PLUGIN_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(VoicechatApi api) {
|
||||
voicechatApi = api;
|
||||
cooldowns = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerEvents(EventRegistration registration) {
|
||||
registration.registerEvent(VoicechatServerStartedEvent.class, this::onServerStarted);
|
||||
registration.registerEvent(MicrophonePacketEvent.class, this::onMicPacket);
|
||||
}
|
||||
|
||||
private void onServerStarted(VoicechatServerStartedEvent event) {
|
||||
voicechatServerApi = event.getVoicechat();
|
||||
}
|
||||
|
||||
private void onMicPacket(MicrophonePacketEvent event) {
|
||||
VoicechatConnection senderConnection = event.getSenderConnection();
|
||||
if (senderConnection == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getPacket().getOpusEncodedData().length <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!VoiceChatInteraction.SERVER_CONFIG.groupInteraction) {
|
||||
if (senderConnection.isInGroup()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!VoiceChatInteraction.SERVER_CONFIG.whisperInteraction) {
|
||||
if (event.getPacket().isWhispering()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ServerPlayer player = senderConnection.getPlayer();
|
||||
if (!(senderConnection.getPlayer().getPlayer() instanceof org.bukkit.entity.Player bukkitPlayer)) {
|
||||
VoiceChatInteraction.LOGGER.warning("Received microphone packet from non-player");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!VoiceChatInteraction.SERVER_CONFIG.sneakInteraction) {
|
||||
if (bukkitPlayer.isSneaking()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (decoder == null) {
|
||||
decoder = event.getVoicechat().createDecoder();
|
||||
}
|
||||
|
||||
decoder.resetState();
|
||||
short[] decoded = decoder.decode(event.getPacket().getOpusEncodedData());
|
||||
|
||||
if (AudioUtils.calculateAudioLevel(decoded) < VoiceChatInteraction.SERVER_CONFIG.minActivationThreshold) {
|
||||
return;
|
||||
}
|
||||
|
||||
bukkitPlayer.getServer().getScheduler().runTask(VoiceChatInteraction.INSTANCE, () -> {
|
||||
if (activate(player)) {
|
||||
bukkitPlayer.getWorld().sendGameEvent(null, VoiceChatInteraction.VOICE_GAME_EVENT, bukkitPlayer.getLocation().toVector());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean activate(ServerPlayer player) {
|
||||
Long lastTimestamp = cooldowns.get(player.getUuid());
|
||||
long currentTime = VoiceChatInteraction.SERVER.getPlayer(player.getUuid()).getWorld().getGameTime();
|
||||
if ((lastTimestamp == null || currentTime - lastTimestamp > 20L) && getInteractionToggle(Bukkit.getPlayer(player.getUuid()))) {
|
||||
cooldowns.put(player.getUuid(), currentTime);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getInteractionToggle(org.bukkit.entity.Player player) {
|
||||
PersistentDataContainer data = player.getPersistentDataContainer();
|
||||
NamespacedKey key = new NamespacedKey(VoiceChatInteraction.INSTANCE, "interaction_toggle");
|
||||
|
||||
if (!data.has(key)) {
|
||||
return VoiceChatInteraction.SERVER_CONFIG.defaultInteractionToggle;
|
||||
} else {
|
||||
return data.get(key, PersistentDataType.BYTE) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void setInteractionToggle(org.bukkit.entity.Player player, Boolean value) {
|
||||
PersistentDataContainer data = player.getPersistentDataContainer();
|
||||
NamespacedKey key = new NamespacedKey(VoiceChatInteraction.INSTANCE, "interaction_toggle");
|
||||
|
||||
data.set(key, PersistentDataType.BYTE, (byte) (value ? 1 : 0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package de.winniepat.voiceChatInteraction.config;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
public class ServerConfig {
|
||||
public boolean groupInteraction;
|
||||
public boolean whisperInteraction;
|
||||
public boolean sneakInteraction;
|
||||
public int minActivationThreshold;
|
||||
public boolean defaultInteractionToggle;
|
||||
|
||||
public ServerConfig(FileConfiguration config) {
|
||||
groupInteraction = config.getBoolean("group_interaction");
|
||||
whisperInteraction = config.getBoolean("whisper_interaction");
|
||||
sneakInteraction = config.getBoolean("sneak_interaction");
|
||||
minActivationThreshold = config.getInt("minimum_activation_threshold");
|
||||
defaultInteractionToggle = config.getBoolean("default_interaction_toggle");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
name: VoiceChatInteraction
|
||||
description: $description
|
||||
version: '${version}'
|
||||
|
||||
main: de.winniepat.voiceChatInteraction.VoiceChatInteraction
|
||||
api-version: '1.21.11'
|
||||
load: POSTWORLD
|
||||
|
||||
authors: [ WinniePatGG ]
|
||||
website: https://winniepat.de
|
||||
depend: [ voicechat ]
|
||||
commands:
|
||||
voicechat_interaction:
|
||||
description: "Manage Voice Chat Interaction settings"
|
||||
usage: "/voicechat_interaction toggle [<player>]"
|
||||
aliases: [voicechat_interaction]
|
||||
permission: voicechat_interaction
|
||||
Reference in New Issue
Block a user