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

@@ -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) {