diff --git a/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java b/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java index 30a893c5..3d32c0ea 100644 --- a/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java +++ b/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java @@ -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.
+ * Return of '0' means players must be in the same world/server.
+ * 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. * diff --git a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java index 0581bae3..3ee15149 100644 --- a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java +++ b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java @@ -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. * diff --git a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java index ff04f684..7de9e0ef 100644 --- a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java +++ b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkit.java @@ -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 diff --git a/bukkit/src/main/java/net/draycia/carbon/bukkit/users/CarbonPlayerBukkit.java b/bukkit/src/main/java/net/draycia/carbon/bukkit/users/CarbonPlayerBukkit.java index b805f43a..9b4c0b5f 100644 --- a/bukkit/src/main/java/net/draycia/carbon/bukkit/users/CarbonPlayerBukkit.java +++ b/bukkit/src/main/java/net/draycia/carbon/bukkit/users/CarbonPlayerBukkit.java @@ -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 = 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); diff --git a/common/src/main/java/net/draycia/carbon/common/channels/BasicChatChannel.java b/common/src/main/java/net/draycia/carbon/common/channels/BasicChatChannel.java index 32a3768c..24a462ab 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/BasicChatChannel.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/BasicChatChannel.java @@ -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(); diff --git a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java index 830c04de..539a7d57 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java @@ -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; + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/listeners/RadiusListener.java b/common/src/main/java/net/draycia/carbon/common/listeners/RadiusListener.java new file mode 100644 index 00000000..b614e501 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/listeners/RadiusListener.java @@ -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 . + */ +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; + }); + } + }); + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index 2ead562e..19a03d7c 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -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) { diff --git a/fabric/src/main/java/net/draycia/carbon/fabric/users/CarbonPlayerFabric.java b/fabric/src/main/java/net/draycia/carbon/fabric/users/CarbonPlayerFabric.java index 79adcc04..df5f66ac 100644 --- a/fabric/src/main/java/net/draycia/carbon/fabric/users/CarbonPlayerFabric.java +++ b/fabric/src/main/java/net/draycia/carbon/fabric/users/CarbonPlayerFabric.java @@ -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 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 playerOptional = this.player(); diff --git a/sponge/src/main/java/net/draycia/carbon/sponge/users/CarbonPlayerSponge.java b/sponge/src/main/java/net/draycia/carbon/sponge/users/CarbonPlayerSponge.java index c87d0024..63840f0b 100644 --- a/sponge/src/main/java/net/draycia/carbon/sponge/users/CarbonPlayerSponge.java +++ b/sponge/src/main/java/net/draycia/carbon/sponge/users/CarbonPlayerSponge.java @@ -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 player = this.player(); + final Optional 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); diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/users/CarbonPlayerVelocity.java b/velocity/src/main/java/net/draycia/carbon/velocity/users/CarbonPlayerVelocity.java index 96cfd688..9a217f99 100644 --- a/velocity/src/main/java/net/draycia/carbon/velocity/users/CarbonPlayerVelocity.java +++ b/velocity/src/main/java/net/draycia/carbon/velocity/users/CarbonPlayerVelocity.java @@ -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 = this.player(); + final Optional 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;