Skip to content

Commit

Permalink
refactor: rework caching, simplify TAB completion
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Mar 29, 2024
1 parent a9adcd0 commit 965b57b
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class BukkitHuskChat extends JavaPlugin implements HuskChat, BukkitEventP
private MorePaperLib morePaperLib;
private BukkitAudiences audiences;
private final List<ChatFilter> filtersAndReplacers = new ArrayList<>();
private final UserCache userCache = new UserCache();
private final List<PlaceholderReplacer> placeholderReplacers = new ArrayList<>();

@Setter
Expand All @@ -72,6 +71,8 @@ public class BukkitHuskChat extends JavaPlugin implements HuskChat, BukkitEventP
@Setter
private Filters filterSettings;
@Setter
private UserCache.Editor userCache;
@Setter
@Getter(AccessLevel.NONE)
private DiscordHook discordHook;
private DataGetter dataGetter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public final class BungeeHuskChat extends Plugin implements HuskChat, BungeeEven
private static final int METRICS_ID = 11882;

private final List<ChatFilter> filtersAndReplacers = new ArrayList<>();
private final UserCache userCache = new UserCache();
private final List<PlaceholderReplacer> placeholderReplacers = new ArrayList<>();

@Getter(AccessLevel.NONE)
Expand All @@ -78,6 +77,8 @@ public final class BungeeHuskChat extends Plugin implements HuskChat, BungeeEven
@Setter
private Filters filterSettings;
@Setter
private UserCache.Editor userCache;
@Setter
@Getter(AccessLevel.NONE)
private DiscordHook discordHook;
private DataGetter dataGetter;
Expand Down
4 changes: 0 additions & 4 deletions common/src/main/java/net/william278/huskchat/HuskChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.william278.huskchat.getter.DataGetter;
import net.william278.huskchat.placeholders.PlaceholderReplacer;
import net.william278.huskchat.user.OnlineUser;
import net.william278.huskchat.user.UserCache;
import net.william278.huskchat.util.AudiencesProvider;
import org.jetbrains.annotations.NotNull;

Expand All @@ -53,9 +52,6 @@ default void loadDiscordHook() {
}
}

@NotNull
UserCache getUserCache();

@NotNull
List<PlaceholderReplacer> getPlaceholderReplacers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String getPlayerChannel(@NotNull OnlineUser player) {
* @since 3.0
*/
public void setPlayerChannel(@NotNull OnlineUser player, @NotNull String channel) {
plugin.getUserCache().setPlayerChannel(player.getUuid(), channel);
plugin.editUserCache(c -> c.setPlayerChannel(player.getUuid(), channel));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.william278.huskchat.user.OnlineUser;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.StringJoiner;

public class BroadcastCommand extends CommandBase {
Expand All @@ -47,8 +46,4 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
}
}

@Override
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
return;
}
if (args.length == 1) {
plugin.getUserCache().switchPlayerChannel(player, args[0], plugin);
plugin.editUserCache(c -> c.switchPlayerChannel(player, args[0], plugin));
} else {
plugin.getLocales().sendMessage(player, "error_invalid_syntax", getUsage());
}
}

@Override
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length <= 1) {
return getUsableChannels(player).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public CommandBase(@NotNull List<String> aliases, @NotNull String usage, @NotNul
* @param args Current command arguments
* @return List of String arguments to offer TAB suggestions
*/
public abstract List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args);
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
}

/**
* Get the primary command alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
}

@Override
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length <= 1) {
return Arrays.stream(COMMAND_TAB_ARGUMENTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import net.william278.huskchat.user.UserCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;

public class LocalSpyCommand extends CommandBase {

Expand All @@ -43,41 +42,38 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
plugin.getLocales().sendMessage(player, "error_in_game_only");
return;
}

// Set with color
if (args.length == 1) {
UserCache.SpyColor color;
Optional<UserCache.SpyColor> selectedColor = UserCache.SpyColor.getColor(args[0]);
if (selectedColor.isPresent()) {
try {
color = selectedColor.get();
plugin.getUserCache().setLocalSpy(player, color);
plugin.getLocales().sendMessage(player, "local_spy_toggled_on_color",
color.colorCode, color.name().toLowerCase().replaceAll("_", " "));
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save local spy state to spies file");
}
final Optional<UserCache.SpyColor> selectedColor = UserCache.SpyColor.getColor(args[0]);
if (selectedColor.isEmpty()) {
plugin.getLocales().sendMessage(player, "error_invalid_syntax", getUsage());
return;
}

final UserCache.SpyColor color = selectedColor.get();
plugin.editUserCache(c -> c.setLocalSpy(player, color));
plugin.getLocales().sendMessage(player, "local_spy_toggled_on_color",
color.colorCode, color.name().toLowerCase().replaceAll("_", " "));
return;
}

// Toggle without specifying color
if (!plugin.getUserCache().isLocalSpying(player)) {
try {
plugin.getUserCache().setLocalSpy(player);
plugin.getLocales().sendMessage(player, "local_spy_toggled_on");
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save local spy state to spies file");
}
} else {
try {
plugin.getUserCache().removeLocalSpy(player);
plugin.getLocales().sendMessage(player, "local_spy_toggled_off");
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save local spy state to spies file");
}
plugin.editUserCache(c -> c.setLocalSpy(player));
plugin.getLocales().sendMessage(player, "local_spy_toggled_on");
return;
}
plugin.editUserCache(c -> c.removeLocalSpy(player));
plugin.getLocales().sendMessage(player, "local_spy_toggled_off");
}

@Override
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
return Arrays.stream(UserCache.SpyColor.values())
.map(UserCache.SpyColor::name).map(String::toLowerCase)
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private List<String> getTargetPlayers(String playerList) {
}

@Override
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length <= 1) {
final ArrayList<String> userNames = new ArrayList<>();
Expand Down Expand Up @@ -95,9 +96,8 @@ public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[]
prependedUsernames.add(precursoryText + username);
}
return prependedUsernames;
} else {
return List.of();
}
return List.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import net.william278.huskchat.HuskChat;
import net.william278.huskchat.user.OnlineUser;
import net.william278.huskchat.user.UserCache;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -36,14 +35,14 @@ public OptOutMessageCommand(@NotNull HuskChat plugin) {

@Override
public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
UserCache.getLastMessengers(player.getUuid()).ifPresentOrElse(lastMessengers -> {
plugin.getUserCache().getLastMessengers(player.getUuid()).ifPresentOrElse(lastMessengers -> {
if (lastMessengers.size() <= 1) {
plugin.getLocales().sendMessage(player, "error_last_message_not_group");
return;
}

for (UUID uuid : lastMessengers) {
UserCache.getLastMessengers(uuid).ifPresent(last -> last.remove(player.getUuid()));
plugin.getUserCache().getLastMessengers(uuid).ifPresent(last -> last.remove(player.getUuid()));
}

String playerList = lastMessengers.stream().flatMap(u -> plugin.getPlayer(u).stream())
Expand All @@ -59,9 +58,4 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
}, () -> plugin.getLocales().sendMessage(player, "error_no_messages_opt_out"));
}

@Override
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.william278.huskchat.message.PrivateMessage;
import net.william278.huskchat.user.ConsoleUser;
import net.william278.huskchat.user.OnlineUser;
import net.william278.huskchat.user.UserCache;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -38,7 +37,7 @@ public ReplyCommand(@NotNull HuskChat plugin) {
@Override
public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
if (args.length >= 1) {
final Optional<Set<UUID>> lastMessengers = UserCache.getLastMessengers(player.getUuid());
final Optional<Set<UUID>> lastMessengers = plugin.getUserCache().getLastMessengers(player.getUuid());
if (lastMessengers.isEmpty()) {
plugin.getLocales().sendMessage(player, "error_reply_no_messages");
return;
Expand All @@ -49,7 +48,7 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
if (ConsoleUser.isConsolePlayer(lastMessenger)) {
lastPlayers.add(ConsoleUser.wrap(plugin).getName());
} else {
plugin.getPlayer(lastMessenger).ifPresent(onlineMessenger -> lastPlayers.add(onlineMessenger.getName()));
plugin.getPlayer(lastMessenger).ifPresent(online -> lastPlayers.add(online.getName()));
}
}

Expand Down Expand Up @@ -83,9 +82,4 @@ public String getPermission(@NotNull String... args) {
);
}

@Override
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
plugin.getLocales().sendMessage(player, "error_console_switch_channels");
return;
}
plugin.getUserCache().switchPlayerChannel(player, channelId, plugin);
plugin.editUserCache(c -> c.switchPlayerChannel(player, channelId, plugin));
} else {
StringJoiner message = new StringJoiner(" ");
for (String arg : args) {
Expand Down Expand Up @@ -79,9 +79,4 @@ public String getPermission(@NotNull String... args) {
.orElse(null);
}

@Override
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import net.william278.huskchat.user.UserCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;

public class SocialSpyCommand extends CommandBase {

Expand All @@ -43,41 +42,38 @@ public void onExecute(@NotNull OnlineUser player, @NotNull String[] args) {
plugin.getLocales().sendMessage(player, "error_in_game_only");
return;
}

// Set with color
if (args.length == 1) {
UserCache.SpyColor color;
Optional<UserCache.SpyColor> selectedColor = UserCache.SpyColor.getColor(args[0]);
if (selectedColor.isPresent()) {
try {
color = selectedColor.get();
plugin.getUserCache().setSocialSpy(player, color);
plugin.getLocales().sendMessage(player, "social_spy_toggled_on_color",
color.colorCode, color.name().toLowerCase().replaceAll("_", " "));
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save social spy state to spies file");
}
final Optional<UserCache.SpyColor> selectedColor = UserCache.SpyColor.getColor(args[0]);
if (selectedColor.isEmpty()) {
plugin.getLocales().sendMessage(player, "error_invalid_syntax", getUsage());
return;
}

final UserCache.SpyColor color = selectedColor.get();
plugin.editUserCache(c -> c.setSocialSpy(player, color));
plugin.getLocales().sendMessage(player, "social_spy_toggled_on_color",
color.colorCode, color.name().toLowerCase().replaceAll("_", " "));
return;
}

// Toggle without specifying color
if (!plugin.getUserCache().isSocialSpying(player)) {
try {
plugin.getUserCache().setSocialSpy(player);
plugin.getLocales().sendMessage(player, "social_spy_toggled_on");
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save social spy state to spies file");
}
} else {
try {
plugin.getUserCache().removeSocialSpy(player);
plugin.getLocales().sendMessage(player, "social_spy_toggled_off");
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to save social spy state to spies file");
}
plugin.editUserCache(c -> c.setSocialSpy(player));
plugin.getLocales().sendMessage(player, "social_spy_toggled_on");
return;
}
plugin.editUserCache(c -> c.removeSocialSpy(player));
plugin.getLocales().sendMessage(player, "social_spy_toggled_off");
}

@Override
@NotNull
public List<String> onTabComplete(@NotNull OnlineUser player, @NotNull String[] args) {
return List.of();
return Arrays.stream(UserCache.SpyColor.values())
.map(UserCache.SpyColor::name).map(String::toLowerCase)
.toList();
}

}
Loading

0 comments on commit 965b57b

Please sign in to comment.