Don't sync data over network

This commit is contained in:
Draycia committed 2021-05-07 04:02:53 -07:00
1 parent d7b3b01c2b
commit 4131f811b7
14 files changed
+55 -298

No files matched your search

@@ -25,11 +25,11 @@ public interface ChatChannel extends Audience {
@NonNull TextColor channelColor(@NonNull CarbonUser user);
@NonNull
Map<CarbonUser, Component> parseMessage(@NonNull PlayerUser user, @NonNull String message, boolean fromRemote);
Map<CarbonUser, Component> parseMessage(@NonNull PlayerUser user, @NonNull String message);
@NonNull
Map<CarbonUser, Component> parseMessage(@NonNull PlayerUser user, @NonNull Collection<@NonNull PlayerUser> recipients,
@NonNull String message, boolean fromRemote);
@NonNull String message);
boolean canPlayerUse(@NonNull PlayerUser user);
@@ -106,12 +106,12 @@ public class EmptyTextChannel implements TextChannel {
}
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message, final boolean fromRemote) {
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message) {
return Collections.emptyMap();
}
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull Collection<@NonNull PlayerUser> recipients, final @NonNull String message, final boolean fromRemote) {
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull Collection<@NonNull PlayerUser> recipients, final @NonNull String message) {
return Collections.emptyMap();
}
@@ -19,11 +19,7 @@ public interface PlayerUser extends CarbonUser {
@NonNull String nickname();
default void nickname(final @Nullable String nickname) {
this.nickname(nickname, false);
}
void nickname(@Nullable String nickname, boolean fromRemote);
void nickname(@Nullable String nickname);
@NonNull String displayName();
@@ -33,7 +29,7 @@ public interface PlayerUser extends CarbonUser {
@Nullable TextColor customChatColor();
void customChatColor(@Nullable TextColor customChatColor, boolean fromRemote);
void customChatColor(@Nullable TextColor customChatColor);
@NonNull String parsePlaceholders(@NonNull String input);
@@ -43,70 +39,38 @@ public interface PlayerUser extends CarbonUser {
boolean spyingWhispers();
default void spyingWhispers(final boolean spyingWhispers) {
this.spyingWhispers(spyingWhispers, false);
}
void spyingWhispers(boolean spyingWhispers, boolean fromRemote);
void spyingWhispers(boolean spyingWhispers);
boolean muted();
default void muted(final boolean muted) {
this.muted(muted, false);
}
void muted(boolean muted, boolean fromRemote);
void muted(boolean muted);
boolean shadowMuted();
default void shadowMuted(final boolean shadowMuted) {
this.shadowMuted(shadowMuted, false);
}
void shadowMuted(boolean shadowMuted, boolean fromRemote);
void shadowMuted(boolean shadowMuted);
@Nullable UUID replyTarget();
default void replyTarget(final @Nullable UUID target) {
this.replyTarget(target, false);
}
default void replyTarget(final @Nullable PlayerUser user) {
if (user == null) {
this.replyTarget((UUID) null, false);
this.replyTarget((UUID) null);
} else {
this.replyTarget(user.uuid(), false);
this.replyTarget(user.uuid());
}
}
void replyTarget(@Nullable UUID target, boolean fromRemote);
default void replyTarget(final @Nullable PlayerUser user, final boolean fromRemote) {
if (user == null) {
this.replyTarget((UUID) null, fromRemote);
} else {
this.replyTarget(user.uuid(), fromRemote);
}
}
void replyTarget(@Nullable UUID target);
boolean ignoringUser(@NonNull UUID uuid);
void ignoringUser(@NonNull UUID uuid, boolean ignoring, boolean fromRemote);
default void ignoringUser(final @NonNull UUID uuid, final boolean ignoring) {
this.ignoringUser(uuid, ignoring, false);
}
void ignoringUser(@NonNull UUID uuid, boolean ignoring);
default boolean ignoringUser(final @NonNull PlayerUser user) {
return this.ignoringUser(user.uuid());
}
default void ignoringUser(final @NonNull PlayerUser user, final boolean ignoring, final boolean fromRemote) {
this.ignoringUser(user.uuid(), ignoring, fromRemote);
}
default void ignoringUser(final @NonNull PlayerUser user, final boolean ignoring) {
this.ignoringUser(user.uuid(), ignoring, false);
default void ignoringUser(final @NonNull PlayerUser user, boolean ignoring) {
this.ignoringUser(user.uuid(), ignoring);
}
@NonNull Iterable<CarbonUser> ignoredChatUsers();
@@ -125,11 +89,7 @@ public interface PlayerUser extends CarbonUser {
@Nullable ChatChannel selectedChannel();
default void selectedChannel(final @NonNull ChatChannel channel) {
this.selectedChannel(channel, false);
}
void selectedChannel(@NonNull ChatChannel channel, boolean fromRemote);
void selectedChannel(@NonNull ChatChannel channel);
void clearSelectedChannel();
@@ -7,27 +7,15 @@ public interface UserChannelSettings {
boolean spying();
default void spying(final boolean spying) {
this.spying(spying, false);
}
void spying(boolean spying, boolean fromRemote);
void spying(boolean spying);
boolean ignored();
void ignoring(boolean ignored, boolean fromRemote);
default void ignoring(final boolean ignored) {
this.ignoring(ignored, false);
}
void ignoring(boolean ignored);
@Nullable
TextColor color();
default void color(final @Nullable TextColor color) {
this.color(color, false);
}
void color(@Nullable TextColor color, boolean fromRemote);
void color(@Nullable TextColor color);
}
@@ -70,7 +70,7 @@ public class BukkitChatHandler implements Listener {
if (event.isAsynchronous()) {
final Map<CarbonUser, Component> messages =
selectedChannel.parseMessage(user, event.getMessage(), false);
selectedChannel.parseMessage(user, event.getMessage());
for (final Map.Entry<CarbonUser, Component> entry : messages.entrySet()) {
if (entry.getValue().equals(Component.empty())) {
@@ -87,7 +87,7 @@ public class BukkitChatHandler implements Listener {
} else {
Bukkit.getScheduler().runTaskAsynchronously(this.carbonChat, () -> {
selectedChannel.sendComponentsAndLog(user.identity(),
selectedChannel.parseMessage(user, event.getMessage(), false));
selectedChannel.parseMessage(user, event.getMessage()));
// if (this.carbonChat.getConfig().getBoolean("show-tips")) {
// this.carbonChat.logger().info("Tip: Sync chat event! I cannot set the message format due to this. :(");
@@ -142,7 +142,7 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void nickname(@Nullable String newNickname, final boolean fromRemote) {
public void nickname(@Nullable String newNickname) {
this.nickname = newNickname;
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
@@ -160,19 +160,6 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
onlinePlayer.setPlayerListName(newNickname);
}
}
if (!fromRemote) {
final String nick = this.nickname;
if (nick == null) {
this.carbonChat.messageManager().sendMessage("nickname-reset", this.uuid(), byteArray -> {
});
} else {
this.carbonChat.messageManager().sendMessage("nickname", this.uuid(), byteArray -> {
byteArray.writeUTF(nick);
});
}
}
}
@Override
@@ -204,19 +191,8 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void customChatColor(final @Nullable TextColor customChatColor, final boolean fromRemote) {
public void customChatColor(final @Nullable TextColor customChatColor) {
this.customChatColor = customChatColor;
if (!fromRemote) {
if (customChatColor == null) {
this.carbonChat.messageManager().sendMessage("custom-chat-color-reset", this.uuid(), byteArray -> {
});
} else {
this.carbonChat.messageManager().sendMessage("custom-chat-color", this.uuid(), byteArray -> {
byteArray.writeUTF(customChatColor.asHexString());
});
}
}
}
@Override
@@ -241,7 +217,7 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void selectedChannel(final @NonNull ChatChannel chatChannel, final boolean fromRemote) {
public void selectedChannel(final @NonNull ChatChannel chatChannel) {
final String failureMessage = chatChannel.switchFailureMessage();
final ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
@@ -258,12 +234,6 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
this.selectedChannelKey = chatChannel.key();
this.selectedChannel = chatChannel;
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("selected-channel", this.uuid(), byteArray -> {
byteArray.writeUTF(chatChannel.key());
});
}
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
if (player.isOnline()) {
@@ -284,20 +254,12 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void ignoringUser(final @NonNull UUID uuid, final boolean ignoring, final boolean fromRemote) {
public void ignoringUser(final @NonNull UUID uuid, final boolean ignoring) {
if (ignoring) {
this.ignoredUsers.add(uuid);
} else {
this.ignoredUsers.remove(uuid);
}
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("ignoring-user", this.uuid(), byteArray -> {
byteArray.writeLong(uuid.getMostSignificantBits());
byteArray.writeLong(uuid.getLeastSignificantBits());
byteArray.writeBoolean(ignoring);
});
}
}
@Override
@@ -357,14 +319,8 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void muted(final boolean muted, final boolean fromRemote) {
public void muted(final boolean muted) {
this.muted = muted;
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("muted", this.uuid(), byteArray -> {
byteArray.writeBoolean(muted);
});
}
}
@Override
@@ -373,14 +329,8 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void shadowMuted(final boolean shadowMuted, final boolean fromRemote) {
public void shadowMuted(final boolean shadowMuted) {
this.shadowMuted = shadowMuted;
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("shadow-muted", this.uuid(), byteArray -> {
byteArray.writeBoolean(shadowMuted);
});
}
}
@Override
@@ -394,26 +344,14 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void replyTarget(final @Nullable UUID target, final boolean fromRemote) {
public void replyTarget(final @Nullable UUID target) {
this.replyTarget = target;
if (!fromRemote) {
if (target != null) {
this.carbonChat.messageManager().sendMessage("reply-target", this.uuid(), byteArray -> {
byteArray.writeLong(target.getMostSignificantBits());
byteArray.writeLong(target.getLeastSignificantBits());
});
} else {
this.carbonChat.messageManager().sendMessage("reply-target-reset", this.uuid(), byteArray -> {
});
}
}
}
@Override
public @NonNull UserChannelSettings channelSettings(final @NonNull ChatChannel channel) {
return this.channelSettings.computeIfAbsent(channel.key(), name -> {
return new SimpleUserChannelSettings(this.uuid, channel.key());
return new SimpleUserChannelSettings(this.uuid);
});
}
@@ -422,14 +360,8 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
@Override
public void spyingWhispers(final boolean spyingWhispers, final boolean fromRemote) {
public void spyingWhispers(final boolean spyingWhispers) {
this.spyingWhispers = spyingWhispers;
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("spying-whispers", this.uuid(), byteArray -> {
byteArray.writeBoolean(spyingWhispers);
});
}
}
@Override
@@ -17,12 +17,10 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
private boolean ignored;
private @Nullable String color;
private @NonNull UUID uuid;
private @NonNull String channel;
@SuppressWarnings("initialization.fields.uninitialized")
public SimpleUserChannelSettings(final @NonNull UUID uuid, final @NonNull String channel) {
public SimpleUserChannelSettings(final @NonNull UUID uuid) {
this.uuid = uuid;
this.channel = channel;
}
private @NonNull CarbonUser user() {
@@ -35,15 +33,8 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
}
@Override
public void spying(final boolean spying, final boolean fromRemote) {
public void spying(final boolean spying) {
this.spying = spying;
if (!fromRemote) {
this.carbonChat.messageService().sendMessage("spying-channel", this.uuid, byteArray -> {
byteArray.writeUTF(this.channel);
byteArray.writeBoolean(spying);
});
}
}
@Override
@@ -52,15 +43,8 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
}
@Override
public void ignoring(final boolean ignored, final boolean fromRemote) {
public void ignoring(final boolean ignored) {
this.ignored = ignored;
if (!fromRemote) {
this.carbonChat.messageService().sendMessage("ignoring-channel", this.uuid, byteArray -> {
byteArray.writeUTF(this.channel);
byteArray.writeBoolean(ignored);
});
}
}
@Override
@@ -73,25 +57,12 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
}
@Override
public void color(final @Nullable TextColor color, final boolean fromRemote) {
public void color(final @Nullable TextColor color) {
if (color == null) {
this.color = null;
} else {
this.color = color.asHexString();
}
if (!fromRemote) {
final String newColor = this.color;
if (newColor == null || newColor.isEmpty()) {
this.carbonChat.messageService().sendMessage("channel-color-reset", this.uuid, byteArray -> {
});
} else {
this.carbonChat.messageService().sendMessage("channel-color", this.uuid, byteArray -> {
byteArray.writeUTF(newColor);
});
}
}
}
}
@@ -106,7 +106,7 @@ public class CarbonChatChannel implements TextChannel {
// }
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull Collection<@NonNull PlayerUser> recipients, final @NonNull String message, final boolean fromRemote) {
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull Collection<@NonNull PlayerUser> recipients, final @NonNull String message) {
//this.updateUserNickname(user);
final MessageContextEvent event = new MessageContextEvent(this, user);
@@ -226,7 +226,8 @@ public class CarbonChatChannel implements TextChannel {
}
}
if (user.online() && !fromRemote && this.crossServer()) {
// TODO: Rethink how cross server messaging works. There should be no recursion inherent in the design.
if (user.online() && this.crossServer()) {
this.sendMessageToBungee(user.uuid(), consoleEvent.component());
}
@@ -263,8 +264,8 @@ public class CarbonChatChannel implements TextChannel {
}
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message, final boolean fromRemote) {
return this.parseMessage(user, this.audiences(), message, fromRemote);
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message) {
return this.parseMessage(user, this.audiences(), message);
}
public @NonNull String format(final @NonNull PlayerUser user) {
@@ -64,15 +64,14 @@ public class CarbonWhisperChannel implements WhisperChannel {
}
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message,
final boolean fromRemote) {
return this.parseMessage(user, Collections.singleton(this.audience()), message, fromRemote);
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user, final @NonNull String message) {
return this.parseMessage(user, Collections.singleton(this.audience()), message);
}
@Override
public @NonNull Map<CarbonUser, Component> parseMessage(final @NonNull PlayerUser user,
final @NonNull Collection<@NonNull PlayerUser> recipients,
final @NonNull String message, final boolean fromRemote) {
final @NonNull String message) {
final Map<CarbonUser, Component> result = new HashMap<>();
// Formats
@@ -61,7 +61,7 @@ public class AliasedChannelCommand {
context.<String>getOptional("message").ifPresent(message -> {
this.chatChannel().sendComponentsAndLog(
context.getSender().identity(),
this.chatChannel().parseMessage(user, message, false));
this.chatChannel().parseMessage(user, message));
});
}
@@ -66,8 +66,7 @@ public class ChannelCommand {
if (!this.canUse(user, channel)) return;
channel.sendComponentsAndLog(context.getSender().identity(), channel.parseMessage(user,
message, false));
channel.sendComponentsAndLog(context.getSender().identity(), channel.parseMessage(user, message));
}
private boolean canUse(final PlayerUser user, final ChatChannel channel) {
@@ -76,7 +76,7 @@ public class SudoChannelCommand {
final ChatChannel channel = context.get("channel");
final String message = context.get("message");
channel.sendComponentsAndLog(user.identity(), channel.parseMessage(user, message, false));
channel.sendComponentsAndLog(user.identity(), channel.parseMessage(user, message));
}
}
@@ -8,7 +8,6 @@ import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.PlayerUser;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
@@ -29,90 +28,6 @@ public class MessageManager {
}
private void registerDefaultListeners() {
this.messageService().registerUserMessageListener("nickname", (user, byteArray) -> {
final String nickname = byteArray.readUTF();
final String message = this.carbonChat.translations().nicknameSet();
user.nickname(nickname, true);
user.sendMessage(Identity.nil(), this.carbonChat.messageProcessor()
.processMessage(message, "nickname", nickname));
});
this.messageService().registerUserMessageListener("nickname-reset", (user, byteArray) -> {
final String message = this.carbonChat.translations().nicknameReset();
user.nickname(null, true);
user.sendMessage(Identity.nil(), this.carbonChat.messageProcessor().processMessage(message));
});
this.messageService().registerUserMessageListener("selected-channel", (user, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
if (channel != null) {
user.selectedChannel(channel, true);
}
});
this.messageService().registerUserMessageListener("spying-whispers", (user, byteArray) -> {
user.spyingWhispers(byteArray.readBoolean(), true);
});
this.messageService().registerUserMessageListener("muted", (user, byteArray) -> {
user.muted(byteArray.readBoolean(), true);
});
this.messageService().registerUserMessageListener("shadow-muted", (user, byteArray) -> {
user.shadowMuted(byteArray.readBoolean(), true);
});
this.messageService().registerUserMessageListener("reply-target", (user, byteArray) -> {
user.replyTarget(new UUID(byteArray.readLong(), byteArray.readLong()), true);
});
this.messageService().registerUserMessageListener("reply-target-reset", (user, byteArray) -> {
user.replyTarget((UUID) null, true);
});
this.messageService().registerUserMessageListener("ignoring-user", (user, byteArray) -> {
user.ignoringUser(new UUID(byteArray.readLong(), byteArray.readLong()), byteArray.readBoolean(), true);
});
this.messageService().registerUserMessageListener("ignoring-channel", (user, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
if (channel != null) {
user.channelSettings(channel)
.ignoring(byteArray.readBoolean(), true);
}
});
this.messageService().registerUserMessageListener("spying-channel", (user, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
if (channel != null) {
user.channelSettings(channel)
.spying(byteArray.readBoolean(), true);
}
});
this.messageService().registerUserMessageListener("channel-color", (user, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
if (channel != null) {
user.channelSettings(channel)
.color(TextColor.fromHexString(byteArray.readUTF()), true);
}
});
this.messageService().registerUserMessageListener("channel-color-reset", (user, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
if (channel != null) {
user.channelSettings(channel)
.color(null, true);
}
});
this.messageService().registerUUIDMessageListener("channel-component", (uuid, byteArray) -> {
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
final PlayerUser user = this.carbonChat.userService().wrap(uuid);
@@ -152,14 +67,6 @@ public class MessageManager {
"target", target.displayName(), "sender", sender.displayName()));
}
});
this.messageService().registerUserMessageListener("custom-chat-color-reset", (user, byteArray) -> {
user.customChatColor(null, true);
});
this.messageService().registerUserMessageListener("custom-chat-color", (user, byteArray) -> {
user.customChatColor(TextColor.fromHexString(byteArray.readUTF()), true);
});
}
public @NonNull MessageService messageService() {
@@ -189,18 +189,18 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
final ChatChannel channel = this.carbonChat.channelRegistry().getOrDefault(users.get("channel"));
user.selectedChannel(channel, true);
user.selectedChannel(channel);
final String nickname = users.get("nickname");
if (nickname != null) {
user.nickname(nickname, true);
user.nickname(nickname);
}
user.muted(users.get("muted"), true);
user.shadowMuted(users.get("shadowmuted"), true);
user.spyingWhispers(users.get("spyingwhispers"), true);
user.customChatColor(users.get("customchatcolor"), true);
user.muted(users.get("muted"));
user.shadowMuted(users.get("shadowmuted"));
user.spyingWhispers(users.get("spyingwhispers"));
user.customChatColor(users.get("customchatcolor"));
final String whisperPingKey = users.get("whisperpingkey");
final Float whisperPingVolume = users.get("whisperpingvolume");
@@ -234,19 +234,19 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
if (chatChannel != null) {
final UserChannelSettings settings = user.channelSettings(chatChannel);
settings.spying(channelSetting.<Boolean>get("spying"), true);
settings.ignoring(channelSetting.<Boolean>get("ignored"), true);
settings.spying(channelSetting.<Boolean>get("spying"));
settings.ignoring(channelSetting.<Boolean>get("ignored"));
final String color = channelSetting.get("color");
if (color != null) {
settings.color(TextColor.fromHexString(color), true);
settings.color(TextColor.fromHexString(color));
}
}
}
for (final DbRow ignoredUser : ignoredUsers) {
user.ignoringUser(UUID.fromString(ignoredUser.get("user")), true, true);
user.ignoringUser(UUID.fromString(ignoredUser.get("user")), true);
}
} catch (final SQLException exception) {
exception.printStackTrace();