-
Notifications
You must be signed in to change notification settings - Fork 9
/
VanishAPI.java
141 lines (126 loc) · 5.37 KB
/
VanishAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package me.drex.vanish.api;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.drex.vanish.util.VanishManager;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.UUID;
public interface VanishAPI {
/**
* Check if the specified entity is vanished and should be hidden.
* Use {@link VanishAPI#canSeePlayer(ServerPlayer, ServerPlayer)},
* to check which players should be able to see these actions.
*
* @param entity the entity to check the status
* @return the result of the check
*/
static boolean isVanished(@NotNull Entity entity) {
return VanishManager.isVanished(entity);
}
static boolean isVanished(@NotNull MinecraftServer server, @NotNull UUID uuid) {
return VanishManager.isVanished(server, uuid);
}
/**
* Set the vanish-status of a player
*
* @param player the player whose vanish status should be changed
* @param status the status to be set (true -> vanish | false -> un-vanish)
* @return true if the vanish-status changed
*/
static boolean setVanish(@NotNull ServerPlayer player, boolean status) {
return VanishManager.setVanished(player.getGameProfile(), player.server, status);
}
/**
* Check whether a player can see the action from another player.
*
* @param actor The player who executed the action
* @param observer Is the player that is viewing the action
* @return the result of the check
*/
static boolean canSeePlayer(@NotNull ServerPlayer actor, @NotNull ServerPlayer observer) {
return VanishManager.canSeePlayer(actor, observer.createCommandSourceStack());
}
static boolean canSeePlayer(@NotNull MinecraftServer server, @NotNull UUID uuid, @NotNull ServerPlayer observer) {
return canSeePlayer(server, uuid, observer.createCommandSourceStack());
}
static boolean canSeePlayer(@NotNull MinecraftServer server, @NotNull UUID uuid, @NotNull CommandSourceStack observer) {
return VanishManager.canSeePlayer(server, uuid, observer);
}
static boolean canViewVanished(SharedSuggestionProvider observer) {
return VanishManager.canViewVanished(observer);
}
/**
* Returns a list of players that the given {@link CommandSourceStack source} can see
*
* @param observer the viewing source context
* @return an immutable list of players that are visible to the source.
*/
@NotNull
static List<ServerPlayer> getVisiblePlayers(@NotNull CommandSourceStack observer) {
MinecraftServer server = observer.getServer();
ObjectArrayList<ServerPlayer> list = new ObjectArrayList<>();
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
if (VanishManager.canSeePlayer(player, observer)) {
list.add(player);
}
}
return list;
}
/**
* Returns a list of players that can view the actions of the given {@link ServerPlayer}
*
* @param actor the player who is observed
* @return an immutable list of players that can view the player
*/
@NotNull
@Deprecated(forRemoval = true)
static List<ServerPlayer> getViewingPlayers(@NotNull ServerPlayer actor) {
ObjectArrayList<ServerPlayer> list = new ObjectArrayList<>();
for (ServerPlayer observer : actor.server.getPlayerList().getPlayers()) {
if (canSeePlayer(actor, observer)) {
list.add(observer);
}
}
return list;
}
/**
* Broadcasts a message that would reveal players vanish status
* only to players who can see other vanished players
*
* @param actor the player who caused the message
* @param message the message that should be shown
*/
static void broadcastHiddenMessage(@NotNull ServerPlayer actor, @NotNull Component message) {
MutableComponent component = message.copy();
component.append(Component.translatable("text.vanish.chat.hidden")
.withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC));
for (ServerPlayer observer : actor.server.getPlayerList().getPlayers()) {
if (canSeePlayer(actor, observer)) {
observer.sendSystemMessage(component);
}
}
}
/**
* Conditionally send a message that would reveal players vanish status
* only if the observer can see other vanished players
*
* @param actor the player who caused the message
* @param observer the player who could receive the message
* @param message the message that should be shown
*/
static void sendHiddenMessage(@NotNull ServerPlayer actor, @NotNull ServerPlayer observer, @NotNull Component message) {
MutableComponent component = message.copy();
component.append(Component.translatable("text.vanish.chat.hidden")
.withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC));
if (canSeePlayer(actor, observer)) {
observer.sendSystemMessage(component);
}
}
}