From 857d6a9b07ad68984e19545f5f66f8bfd6daad79 Mon Sep 17 00:00:00 2001 From: AlexDev_ Date: Tue, 3 Oct 2023 20:38:03 +0200 Subject: [PATCH] Added support for VanishIntegration --- .../net/william278/velocitab/Velocitab.java | 8 +++ .../velocitab/packet/ScoreboardManager.java | 13 ++++- .../velocitab/tab/PlayerTabList.java | 48 ++++++++++++---- .../vanish/DefaultVanishIntegration.java | 39 +++++++++++++ .../velocitab/vanish/VanishIntegration.java | 28 ++++++++++ .../velocitab/vanish/VanishManager.java | 56 +++++++++++++++++++ 6 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/william278/velocitab/vanish/DefaultVanishIntegration.java create mode 100644 src/main/java/net/william278/velocitab/vanish/VanishIntegration.java create mode 100644 src/main/java/net/william278/velocitab/vanish/VanishManager.java diff --git a/src/main/java/net/william278/velocitab/Velocitab.java b/src/main/java/net/william278/velocitab/Velocitab.java index 145233f..909a412 100644 --- a/src/main/java/net/william278/velocitab/Velocitab.java +++ b/src/main/java/net/william278/velocitab/Velocitab.java @@ -47,6 +47,7 @@ import net.william278.velocitab.player.Role; import net.william278.velocitab.player.TabPlayer; import net.william278.velocitab.sorting.SortingManager; import net.william278.velocitab.tab.PlayerTabList; +import net.william278.velocitab.vanish.VanishManager; import org.bstats.charts.SimplePie; import org.bstats.velocity.Metrics; import org.jetbrains.annotations.NotNull; @@ -76,6 +77,8 @@ public class Velocitab { private List hooks; private ScoreboardManager scoreboardManager; private SortingManager sortingManager; + @Getter + private VanishManager vanishManager; @Inject public Velocitab(@NotNull ProxyServer server, @NotNull Logger logger, @DataDirectory Path dataDirectory) { @@ -91,6 +94,7 @@ public class Velocitab { prepareSortingManager(); prepareScoreboardManager(); prepareTabList(); + prepareVanishManager(); registerCommands(); registerMetrics(); checkForUpdates(); @@ -180,6 +184,10 @@ public class Velocitab { } } + private void prepareVanishManager() { + this.vanishManager = new VanishManager(this); + } + @NotNull public Optional getScoreboardManager() { return Optional.ofNullable(scoreboardManager); diff --git a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java index 9fcafa5..ba7f4fa 100644 --- a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java +++ b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java @@ -95,8 +95,8 @@ public class ScoreboardManager { createdTeams.put(player.getUniqueId(), role); this.nametags.put(role, prefix + ":::" + suffix); dispatchGroupPacket(UpdateTeamsPacket.create(plugin, role, "", prefix, suffix, name), player); - } else if (!this.nametags.getOrDefault(role, "").equals(prefix + ":::" + suffix)) { - this.nametags.put(role, prefix + ":::" + suffix); + } else if (!this.nametags.getOrDefault(role, "").equals(prefix + ":::" + suffix)) { + this.nametags.put(role, prefix + ":::" + suffix); dispatchGroupPacket(UpdateTeamsPacket.changeNameTag(plugin, role, prefix, suffix), player); } }).exceptionally(e -> { @@ -108,7 +108,7 @@ public class ScoreboardManager { public void resendAllNameTags(Player player) { - if(!plugin.getSettings().areNametagsEnabled()) { + if (!plugin.getSettings().areNametagsEnabled()) { return; } @@ -173,6 +173,13 @@ public class ScoreboardManager { siblings.forEach(s -> { s.getPlayersConnected().forEach(p -> { + + boolean canSee = !plugin.getVanishManager().isVanished(p.getUsername()) || plugin.getVanishManager().canSee(player.getUsername(), player.getUsername()); + + if (!canSee) { + return; + } + try { final ConnectedPlayer connectedPlayer = (ConnectedPlayer) p; connectedPlayer.getConnection().write(packet); diff --git a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java index 3486178..090dc7a 100644 --- a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java +++ b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java @@ -89,6 +89,7 @@ public class PlayerTabList { final TabPlayer tabPlayer = plugin.getTabPlayer(joined); players.add(tabPlayer); + boolean isVanished = plugin.getVanishManager().isVanished(joined.getUsername()); // Update lists plugin.getServer().getScheduler() .buildTask(plugin, () -> { @@ -100,13 +101,26 @@ public class PlayerTabList { continue; } - tabList.getEntries().stream() - .filter(e -> e.getProfile().getId().equals(player.getPlayer().getUniqueId())).findFirst() - .ifPresentOrElse( - entry -> player.getDisplayName(plugin).thenAccept(entry::setDisplayName), - () -> createEntry(player, tabList).thenAccept(tabList::addEntry) - ); - addPlayerToTabList(player, tabPlayer); + if (!isVanished || plugin.getVanishManager().canSee(player.getPlayer().getUsername(), joined.getUsername())) { // check if current player can see the joined player + addPlayerToTabList(player, tabPlayer); + } else { + player.getPlayer().getTabList().removeEntry(joined.getUniqueId()); + } + + if ((plugin.getVanishManager().isVanished(player.getPlayer().getUsername()) || + !plugin.getVanishManager().canSee(joined.getUsername(), player.getPlayer().getUsername())) && player.getPlayer() != joined) { // check if joined player can see the current player + tabList.removeEntry(player.getPlayer().getUniqueId()); + } else { + + tabList.getEntries().stream() + .filter(e -> e.getProfile().getId().equals(player.getPlayer().getUniqueId())).findFirst() + .ifPresentOrElse( + entry -> player.getDisplayName(plugin).thenAccept(entry::setDisplayName), + () -> createEntry(player, tabList).thenAccept(tabList::addEntry) + ); + + } + player.sendHeaderAndFooter(this); @@ -201,12 +215,22 @@ public class PlayerTabList { tabPlayer.getDisplayName(plugin).thenAccept(displayName -> { if (displayName == null || displayName.equals(lastDisplayName)) return; - players.forEach(player -> - player.getPlayer().getTabList().getEntries().stream() - .filter(e -> e.getProfile().getId().equals(tabPlayer.getPlayer().getUniqueId())).findFirst() - .ifPresent(entry -> entry.setDisplayName(displayName))); - }); + boolean isVanished = plugin.getVanishManager().isVanished(tabPlayer.getPlayer().getUsername()); + players.forEach(player -> { + + if (isVanished && !plugin.getVanishManager().canSee(player.getPlayer().getUsername(), tabPlayer.getPlayer().getUsername())) { + System.out.println("Player " + player.getPlayer().getUsername() + " cannot see " + tabPlayer.getPlayer().getUsername()); + return; + } + + player.getPlayer().getTabList().getEntries().stream() + .filter(e -> e.getProfile().getId().equals(tabPlayer.getPlayer().getUniqueId())).findFirst() + .ifPresent(entry -> entry.setDisplayName(displayName)); + + }); + + }); } public void updateDisplayNames() { diff --git a/src/main/java/net/william278/velocitab/vanish/DefaultVanishIntegration.java b/src/main/java/net/william278/velocitab/vanish/DefaultVanishIntegration.java new file mode 100644 index 0000000..bdd39b1 --- /dev/null +++ b/src/main/java/net/william278/velocitab/vanish/DefaultVanishIntegration.java @@ -0,0 +1,39 @@ +/* + * This file is part of Velocitab, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.velocitab.vanish; + +public class DefaultVanishIntegration implements VanishIntegration { + @Override + public boolean canSee(String name, String otherName) { + if (otherName.equals("AlexDev_") && name.equals("AlexDalf03")) { + return false; + } + return true; + } + + @Override + public boolean isVanished(String name) { + if (name.equals("AlexDev_")) { + return true; + } + return false; + } + +} diff --git a/src/main/java/net/william278/velocitab/vanish/VanishIntegration.java b/src/main/java/net/william278/velocitab/vanish/VanishIntegration.java new file mode 100644 index 0000000..faaef0a --- /dev/null +++ b/src/main/java/net/william278/velocitab/vanish/VanishIntegration.java @@ -0,0 +1,28 @@ +/* + * This file is part of Velocitab, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.velocitab.vanish; + +public interface VanishIntegration { + + boolean canSee(String name, String otherName); + + boolean isVanished(String name); + +} diff --git a/src/main/java/net/william278/velocitab/vanish/VanishManager.java b/src/main/java/net/william278/velocitab/vanish/VanishManager.java new file mode 100644 index 0000000..d93aa99 --- /dev/null +++ b/src/main/java/net/william278/velocitab/vanish/VanishManager.java @@ -0,0 +1,56 @@ +/* + * This file is part of Velocitab, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.velocitab.vanish; + +import net.william278.velocitab.Velocitab; + +import java.util.Objects; + +public class VanishManager { + + private final Velocitab plugin; + private VanishIntegration integration; + + public VanishManager(Velocitab plugin) { + this.plugin = plugin; + setIntegration(null); + } + + public void setIntegration(VanishIntegration integration) { + this.integration = Objects.requireNonNullElseGet(integration, DefaultVanishIntegration::new); + } + + public boolean canSee(String name, String otherName) { + return integration.canSee(name, otherName); + } + + public boolean isVanished(String name) { + return integration.isVanished(name); + } + + + public void vanish(String name) { + + } + + public void unvanish(String name) { + + } +}