mirror of
https://github.com/GeyserMC/Floodgate.git
synced 2025-12-19 14:59:20 +00:00
No longer use Adventure internally. Started working on adding Checkstyle
This commit is contained in:
@@ -14,7 +14,6 @@ relocate("io.leangen.geantyref")
|
||||
|
||||
|
||||
// these dependencies are already present on the platform
|
||||
provided("net.kyori", "adventure-api", Versions.adventureApiVersion, 0b100)
|
||||
provided("com.google.code.gson", "gson", gsonVersion)
|
||||
provided("com.google.guava", "guava", guavaVersion)
|
||||
provided("com.google.inject", "guice", Versions.guiceVersion)
|
||||
|
||||
@@ -57,7 +57,6 @@ import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
||||
import org.geysermc.floodgate.api.player.FloodgatePlayer;
|
||||
import org.geysermc.floodgate.config.ProxyFloodgateConfig;
|
||||
import org.geysermc.floodgate.util.LanguageManager;
|
||||
import org.geysermc.floodgate.util.VelocityCommandUtil;
|
||||
|
||||
public final class VelocityListener {
|
||||
private static final Field INITIAL_MINECRAFT_CONNECTION;
|
||||
@@ -168,7 +167,5 @@ public final class VelocityListener {
|
||||
@Subscribe(order = PostOrder.LAST)
|
||||
public void onDisconnect(DisconnectEvent event) {
|
||||
api.playerRemoved(event.getPlayer().getUniqueId());
|
||||
|
||||
VelocityCommandUtil.AUDIENCE_CACHE.remove(event.getPlayer().getUniqueId()); //todo
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,25 +66,24 @@ public final class VelocityPlatformModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
VelocityCommandUtil commandUtil = new VelocityCommandUtil();
|
||||
requestInjection(commandUtil);
|
||||
|
||||
bind(CommandUtil.class).to(VelocityCommandUtil.class);
|
||||
bind(VelocityCommandUtil.class).toInstance(commandUtil);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public CommandManager<UserAudience> commandManager(CommandUtil commandUtil) {
|
||||
Injector child = guice.createChildInjector(new CloudInjectionModule<>(
|
||||
UserAudience.class,
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
commandUtil::getAudience,
|
||||
commandUtil::getUserAudience,
|
||||
audience -> (CommandSource) audience.source()
|
||||
));
|
||||
|
||||
CommandManager<UserAudience> commandManager =
|
||||
child.getInstance(new Key<VelocityCommandManager<UserAudience>>() {});
|
||||
|
||||
bind(new Key<CommandManager<UserAudience>>() {}).toInstance(commandManager);
|
||||
|
||||
commandManager.registerCommandPreProcessor(new FloodgateCommandPreprocessor<>(commandUtil));
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -99,8 +98,9 @@ public final class VelocityPlatformModule extends AbstractModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public ListenerRegistration<Object> listenerRegistration(EventManager eventManager,
|
||||
VelocityPlugin plugin) {
|
||||
public ListenerRegistration<Object> listenerRegistration(
|
||||
EventManager eventManager,
|
||||
VelocityPlugin plugin) {
|
||||
return new VelocityListenerRegistration(eventManager, plugin);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Floodgate
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.player;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.audience.MessageType;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.geysermc.floodgate.platform.command.CommandUtil;
|
||||
import org.geysermc.floodgate.platform.command.TranslatableMessage;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public abstract class VelocityUserAudience implements UserAudience, ForwardingAudience.Single {
|
||||
private final UUID uuid;
|
||||
private final String username;
|
||||
private final String locale;
|
||||
private final CommandSource source;
|
||||
private final CommandUtil commandUtil;
|
||||
|
||||
@Override
|
||||
public @NonNull UUID uuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String username() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String locale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandSource source() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NonNull String permission) {
|
||||
return source.hasPermission(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(
|
||||
@NonNull Identity source,
|
||||
@NonNull Component message,
|
||||
@NonNull MessageType type) {
|
||||
// apparently the console doesn't implement sendMessage with MessageType,
|
||||
// so we'll just ignore it
|
||||
this.source.sendMessage(source, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(TranslatableMessage message, Object... args) {
|
||||
commandUtil.sendMessage(source(), locale(), message, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(@NonNull Component reason) {
|
||||
if (source instanceof Player) {
|
||||
((Player) source).disconnect(reason);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(TranslatableMessage message, Object... args) {
|
||||
commandUtil.kickPlayer(source(), locale(), message, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public static final class VelocityConsoleAudience extends VelocityUserAudience
|
||||
implements ConsoleAudience {
|
||||
|
||||
public VelocityConsoleAudience(CommandSource source, CommandUtil commandUtil) {
|
||||
super(new UUID(0, 0), "CONSOLE", "en_us", source, commandUtil);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class VelocityPlayerAudience extends VelocityUserAudience
|
||||
implements PlayerAudience {
|
||||
private final boolean online;
|
||||
|
||||
public VelocityPlayerAudience(
|
||||
UUID uuid,
|
||||
String username,
|
||||
String locale,
|
||||
CommandSource source,
|
||||
boolean online,
|
||||
CommandUtil commandUtil) {
|
||||
super(uuid, username, locale, source, commandUtil);
|
||||
this.online = online;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean online() {
|
||||
return online;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,35 +29,29 @@ import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.geysermc.floodgate.api.FloodgateApi;
|
||||
import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
||||
import org.geysermc.floodgate.platform.command.CommandUtil;
|
||||
import org.geysermc.floodgate.platform.command.TranslatableMessage;
|
||||
import org.geysermc.floodgate.player.UserAudience;
|
||||
import org.geysermc.floodgate.player.UserAudienceArgument.PlayerType;
|
||||
import org.geysermc.floodgate.player.VelocityUserAudience.VelocityConsoleAudience;
|
||||
import org.geysermc.floodgate.player.VelocityUserAudience.VelocityPlayerAudience;
|
||||
import org.geysermc.floodgate.player.UserAudience.ConsoleAudience;
|
||||
import org.geysermc.floodgate.player.UserAudience.PlayerAudience;
|
||||
|
||||
public final class VelocityCommandUtil implements CommandUtil {
|
||||
public static final @NonNull Map<UUID, UserAudience> AUDIENCE_CACHE = new HashMap<>();
|
||||
public final class VelocityCommandUtil extends CommandUtil {
|
||||
private static UserAudience console;
|
||||
|
||||
@Inject private ProxyServer server;
|
||||
@Inject private FloodgateApi api;
|
||||
@Inject private FloodgateLogger logger;
|
||||
@Inject private LanguageManager manager;
|
||||
|
||||
@Inject
|
||||
public VelocityCommandUtil(LanguageManager manager, FloodgateApi api) {
|
||||
super(manager, api);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserAudience getAudience(@NonNull Object sourceObj) {
|
||||
public @NonNull UserAudience getUserAudience(@NonNull Object sourceObj) {
|
||||
if (!(sourceObj instanceof CommandSource)) {
|
||||
throw new IllegalArgumentException("Can only work with CommandSource!");
|
||||
}
|
||||
@@ -67,7 +61,7 @@ public final class VelocityCommandUtil implements CommandUtil {
|
||||
if (console != null) {
|
||||
return console;
|
||||
}
|
||||
return console = new VelocityConsoleAudience(source, this);
|
||||
return console = new ConsoleAudience(source, this);
|
||||
}
|
||||
|
||||
Player player = (Player) source;
|
||||
@@ -75,84 +69,39 @@ public final class VelocityCommandUtil implements CommandUtil {
|
||||
String username = player.getUsername();
|
||||
String locale = Utils.getLocale(player.getPlayerSettings().getLocale());
|
||||
|
||||
return AUDIENCE_CACHE.computeIfAbsent(uuid,
|
||||
$ -> new VelocityPlayerAudience(uuid, username, locale, source, true, this));
|
||||
return new PlayerAudience(uuid, username, locale, source, this, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable UserAudience getAudienceByUsername(@NonNull String username) {
|
||||
return server.getPlayer(username)
|
||||
.map(this::getAudience)
|
||||
.orElse(null);
|
||||
protected String getUsernameFromSource(@NonNull Object source) {
|
||||
return ((Player) source).getUsername();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserAudience getOfflineAudienceByUsername(@NonNull String username) {
|
||||
return new VelocityPlayerAudience(null, username, null, null, false, this);
|
||||
protected UUID getUuidFromSource(@NonNull Object source) {
|
||||
return ((Player) source).getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable UserAudience getAudienceByUuid(@NonNull UUID uuid) {
|
||||
return server.getPlayer(uuid)
|
||||
.map(this::getAudience)
|
||||
.orElse(null);
|
||||
protected Collection<?> getOnlinePlayers() {
|
||||
return server.getAllPlayers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserAudience getOfflineAudienceByUuid(@NonNull UUID uuid) {
|
||||
return new VelocityPlayerAudience(uuid, null, null, null, false, this);
|
||||
public Object getPlayerByUuid(@NonNull UUID uuid) {
|
||||
Optional<Player> player = server.getPlayer(uuid);
|
||||
return player.isPresent() ? player : uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Collection<String> getOnlineUsernames(@NonNull PlayerType limitTo) {
|
||||
Collection<Player> players = server.getAllPlayers();
|
||||
|
||||
Collection<String> usernames = new ArrayList<>();
|
||||
switch (limitTo) {
|
||||
case ALL_PLAYERS:
|
||||
for (Player player : players) {
|
||||
usernames.add(player.getUsername());
|
||||
}
|
||||
break;
|
||||
case ONLY_JAVA:
|
||||
for (Player player : players) {
|
||||
if (!api.isFloodgatePlayer(player.getUniqueId())) {
|
||||
usernames.add(player.getUsername());
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ONLY_BEDROCK:
|
||||
for (Player player : players) {
|
||||
if (api.isFloodgatePlayer(player.getUniqueId())) {
|
||||
usernames.add(player.getUsername());
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Unknown PlayerType");
|
||||
}
|
||||
return usernames;
|
||||
public Object getPlayerByUsername(@NonNull String username) {
|
||||
Optional<Player> player = server.getPlayer(username);
|
||||
return player.isPresent() ? player : username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Object player, String permission) {
|
||||
return cast(player).hasPermission(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> getOnlinePlayersWithPermission(String permission) {
|
||||
List<Object> players = new ArrayList<>();
|
||||
for (Player player : server.getAllPlayers()) {
|
||||
if (hasPermission(player, permission)) {
|
||||
players.add(player);
|
||||
}
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Object target, String locale, TranslatableMessage message, Object... args) {
|
||||
((CommandSource) target).sendMessage(translateAndTransform(locale, message, args));
|
||||
return ((CommandSource) player).hasPermission(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -161,23 +110,9 @@ public final class VelocityCommandUtil implements CommandUtil {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kickPlayer(Object player, String locale, TranslatableMessage message, Object... args) {
|
||||
cast(player).disconnect(translateAndTransform(locale, message, args));
|
||||
}
|
||||
|
||||
public Component translateAndTransform(
|
||||
String locale,
|
||||
TranslatableMessage message,
|
||||
Object... args) {
|
||||
return Component.text(message.translateMessage(manager, locale, args));
|
||||
}
|
||||
|
||||
protected Player cast(Object instance) {
|
||||
try {
|
||||
return (Player) instance;
|
||||
} catch (ClassCastException exception) {
|
||||
logger.error("Failed to cast {} to Player", instance.getClass().getName());
|
||||
throw exception;
|
||||
public void kickPlayer(Object player, String message) {
|
||||
if (player instanceof Player) {
|
||||
((Player) player).disconnect(Component.text(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user