Add radius/same-world/same-server to channel options

This commit is contained in:
Draycia committed 2022-06-08 15:17:20 -07:00
1 parent ddb6998bc8
commit 60285830d4
11 files changed
+259

No files matched your search

@@ -121,6 +121,16 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
*/
@MonotonicNonNull String permission();
/**
* The distance from the sender players must be to receive chat messages.<br>
* Return of '0' means players must be in the same world/server.<br>
* Return of '-1' means there is no radius.
*
* @return the channel radius
* @since 2.0.8
*/
double radius();
/**
* Represents the result of a channel permission check.
*
@@ -57,6 +57,24 @@ public interface CarbonPlayer extends Audience, Identified {
}
}
/**
* Returns the distance from the other {@link CarbonPlayer}, or -1 if the players are not in the same world.
*
* @param other the other player
* @return the distance from the other player, or -1
* @since 2.0.8
*/
double distanceSquaredFrom(final CarbonPlayer other);
/**
* Returns if both players are in the same world or server.
*
* @param other the other player
* @return if both players are in the same world/server
* @since 2.0.8
*/
boolean sameWorldAs(final CarbonPlayer other);
/**
* Gets the player's username.
*
@@ -44,6 +44,7 @@ import net.draycia.carbon.bukkit.util.CarbonChatHook;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.listeners.RadiusListener;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ListenerUtils;
@@ -121,6 +122,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
// Listeners
ListenerUtils.registerCommonListeners(this.injector);
this.injector.getInstance(RadiusListener.class);
// Commands
// This is a bit awkward looking
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
@@ -65,6 +66,38 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
return this.player().map(player -> (Audience) player).orElse(Audience.empty());
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return -1;
}
final @Nullable Player player = this.player().orElse(null);
final @Nullable Player otherPlayer = Bukkit.getPlayer(other.uuid());
if (player == null || otherPlayer == null) {
return -1;
}
return player.getLocation().distanceSquared(otherPlayer.getLocation());
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return false;
}
final Optional<Player> player = this.player();
final @Nullable Player otherPlayer = Bukkit.getPlayer(other.uuid());
if (player.isEmpty() || otherPlayer == null) {
return false;
}
return player.get().getWorld().equals(otherPlayer.getWorld());
}
@Override
public void displayName(final @Nullable Component displayName) {
this.carbonPlayerCommon.displayName(displayName);
@@ -97,6 +97,11 @@ public final class BasicChatChannel implements ChatChannel {
return null;
}
@Override
public double radius() {
return -1;
}
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) {
return ChannelPermissionResult.allowed();
@@ -95,6 +95,11 @@ public final class ConfigChatChannel implements ChatChannel {
private transient @Nullable ConfigChannelMessages carbonMessages = null;
@Comment("""
The distance players must be within to see each other's messages.
A value of '0' requires that both players are in the same world.
On velocity, '0' requires that both players are in the same server.
""")
private int radius = -1;
private transient @Nullable ConfigChannelMessages messageService = null;
@@ -230,4 +235,9 @@ public final class ConfigChatChannel implements ChatChannel {
return this.permission;
}
@Override
public double radius() {
return this.radius;
}
}
@@ -0,0 +1,73 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.listeners;
import com.google.inject.Inject;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class RadiusListener {
@Inject
public RadiusListener(
final CarbonChat carbonChat
) {
carbonChat.eventHandler().subscribe(CarbonChatEvent.class, 0, false, event -> {
final double radius = event.chatChannel().radius();
if (radius < 0) {
return;
}
if (radius == 0) {
event.recipients().removeIf(audience -> {
if (audience.equals(event.sender())) {
return false;
}
if (audience instanceof CarbonPlayer carbonPlayer) {
return !carbonPlayer.sameWorldAs(event.sender());
}
return false;
});
} else {
event.recipients().removeIf(audience -> {
if (audience.equals(event.sender())) {
return false;
}
if (audience instanceof CarbonPlayer carbonPlayer) {
final double distance = carbonPlayer.distanceSquaredFrom(event.sender());
return distance > (radius * radius);
}
return false;
});
}
});
}
}
@@ -317,6 +317,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
}
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
return -1;
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
return false;
}
@Override
public String username() {
if (this.username == null) {
@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
@@ -84,6 +85,44 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
return this.carbonPlayerCommon;
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return -1;
}
final @Nullable ServerPlayer player = this.player().orElse(null);
final @Nullable ServerPlayer otherPlayer = this.carbonChatFabric.minecraftServer()
.getPlayerList().getPlayer(other.uuid());
if (player == null || otherPlayer == null) {
return -1;
}
final double deltaX = player.position().x() - otherPlayer.position().x();
final double deltaY = player.position().y() - otherPlayer.position().y();
final double deltaZ = player.position().z() - otherPlayer.position().z();
return (deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ);
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return false;
}
final Optional<ServerPlayer> player = this.player();
final @Nullable ServerPlayer otherPlayer = this.carbonChatFabric.minecraftServer()
.getPlayerList().getPlayer(other.uuid());
if (player.isEmpty() || otherPlayer == null) {
return false;
}
return player.get().getLevel().equals(otherPlayer.getLevel());
}
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
final Optional<ServerPlayer> playerOptional = this.player();
@@ -21,6 +21,7 @@ package net.draycia.carbon.sponge.users;
import java.util.Locale;
import java.util.Optional;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
@@ -83,6 +84,42 @@ public final class CarbonPlayerSponge extends WrappedCarbonPlayer implements For
return this.player().map(LocaleSource::locale).orElse(null);
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return -1;
}
final @Nullable ServerPlayer player = this.player().orElse(null);
final @Nullable ServerPlayer otherPlayer = Sponge.server().player(other.uuid()).orElse(null);
if (player == null || otherPlayer == null) {
return -1;
}
final double deltaX = player.position().x() - otherPlayer.position().x();
final double deltaY = player.position().y() - otherPlayer.position().y();
final double deltaZ = player.position().z() - otherPlayer.position().z();
return (deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ);
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
if (this.player().isEmpty()) {
return false;
}
final Optional<ServerPlayer> player = this.player();
final Optional<ServerPlayer> otherPlayer = Sponge.server().player(other.uuid());
if (player.isEmpty() || otherPlayer.isEmpty()) {
return false;
}
return player.get().world().equals(otherPlayer.get().world());
}
@Override
public void displayName(final @Nullable Component displayName) {
this.carbonPlayerCommon.displayName(displayName);
@@ -23,6 +23,7 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import java.util.Locale;
import java.util.Optional;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
@@ -69,6 +70,27 @@ public final class CarbonPlayerVelocity extends WrappedCarbonPlayer implements F
return this.player().map(value -> value.getPlayerSettings().getLocale()).orElse(null);
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
return -1;
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
final Optional<Player> player = this.player();
final Optional<Player> otherPlayer = this.server.getPlayer(other.uuid());
if (player.isEmpty() || otherPlayer.isEmpty()) {
return false;
}
if (player.get().getCurrentServer().isEmpty() || otherPlayer.get().getCurrentServer().isEmpty()) {
return false;
}
return player.get().getCurrentServer().get().equals(otherPlayer.get().getCurrentServer().get());
}
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
return null;