-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGlobalLinkServer.java
199 lines (172 loc) · 7.44 KB
/
GlobalLinkServer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2021-2025 GeyserMC
* Licensed under the MIT license
* @link https://github.com/GeyserMC/GlobalLinkServer
*/
package org.geysermc.globallinkserver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import io.papermc.paper.event.player.AsyncChatEvent;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRecipeDiscoverEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.globallinkserver.config.Config;
import org.geysermc.globallinkserver.config.ConfigReader;
import org.geysermc.globallinkserver.link.LinkManager;
import org.geysermc.globallinkserver.util.CommandUtils;
import org.geysermc.globallinkserver.util.Utils;
@SuppressWarnings("UnstableApiUsage")
public class GlobalLinkServer extends JavaPlugin implements Listener {
public static Logger LOGGER;
public static LinkManager linkManager;
public static Config config;
public final static Component LINK_INSTRUCTIONS = Component.text("Run the ").color(NamedTextColor.AQUA)
.append(Component.text("`/link`", NamedTextColor.GREEN))
.append(Component.text(" command to link your accounts.", NamedTextColor.AQUA));
public final static Component UNLINK_INSTRUCTIONS = Component.text("To unlink, use ").color(NamedTextColor.AQUA)
.append(Component.text("`/unlink`", NamedTextColor.RED))
.append(Component.text("."));
@Override
public void onEnable() {
LOGGER = getLogger();
config = ConfigReader.readConfig(this);
linkManager = new LinkManager(config);
CommandUtils commandUtils = new CommandUtils(linkManager);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, linkManager::cleanupTempLinks, 0, 1);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
Bukkit.getOnlinePlayers().forEach(player -> {
if (Utils.isLinked(player)) {
player.sendActionBar(UNLINK_INSTRUCTIONS);
} else {
player.sendActionBar(LINK_INSTRUCTIONS);
}
});
}, 10, 15);
getServer().getPluginManager().registerEvents(this, this);
LifecycleEventManager<@NonNull Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
final Commands commands = event.registrar();
commands.register(
Commands.literal("link")
.requires(ctx -> ctx.getSender() instanceof Player)
.executes(commandUtils::startLink)
.then(Commands.argument("code", ArgumentTypes.integerRange())
.executes(commandUtils::linkWithCode)
)
.build(),
"Use this command to link your Java and Bedrock account.",
List.of("linkaccount")
);
commands.register(
Commands.literal("unlink")
.requires(ctx -> ctx.getSender() instanceof Player)
.executes(commandUtils::unlink)
.build(),
"Use this command to unlink your Java and Bedrock account.",
List.of("unlinkaccount")
);
commands.register(
Commands.literal("linkinfo")
.requires(ctx -> ctx.getSender() instanceof Player)
.executes(ctx -> {
Utils.sendCurrentLinkInfo(Utils.getPlayer(ctx));
return 1;
})
.build(),
"Use this command to show information whether you are currently linked.",
List.of("info")
);
});
LOGGER.info("Started Global Linking Server plugin!");
}
@EventHandler
public void onCommands(PlayerCommandSendEvent event) {
Collection<String> toRemove = new ArrayList<>();
for (String command : event.getCommands()) {
if (command.startsWith("link")) {
continue;
}
if (command.startsWith("unlink")) {
continue;
}
if (command.contains("info")) {
continue;
}
toRemove.add(command);
}
event.getCommands().removeAll(toRemove);
}
@EventHandler
public void onPlayerLoad(PlayerJoinEvent event) {
if (config.util().hideJoinLeaveMessages()) event.joinMessage(null);
Utils.processJoin(event.getPlayer());
if (config.util().hidePlayers()) {
Bukkit.getOnlinePlayers().forEach(player -> {
event.getPlayer().hidePlayer(this, player);
player.hidePlayer(this, event.getPlayer());
});
}
if (config.util().respawnOnJoin()) Utils.fakeRespawn(event.getPlayer());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
if (config.util().hideJoinLeaveMessages()) event.quitMessage(null);
Utils.processLeave(event.getPlayer());
}
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (config.util().voidTeleport()
&& event.getEntity() instanceof Player player
&& event.getCause() == EntityDamageEvent.DamageCause.VOID) {
event.setCancelled(true);
Utils.fakeRespawn(player);
}
}
@EventHandler
public void onFoodLevelChange(FoodLevelChangeEvent event) {
if (config.util().preventHunger()) {
if (event.getFoodLevel() < event.getEntity().getFoodLevel()) {
event.setCancelled(true);
}
}
}
@EventHandler
public void onDeath(PlayerDeathEvent event) {
if (config.util().hideDeathMessages()) event.deathMessage(null);
}
@EventHandler
public void onPlayerChat(AsyncChatEvent event) {
if (config.util().disableChat()) event.setCancelled(true);
}
@EventHandler
public void onServerListPing(PaperServerListPingEvent event) {
if (!config.util().hidePlayerCount()) return;
event.getListedPlayers().clear();
event.setNumPlayers(0);
event.setMaxPlayers(1);
}
@EventHandler
public void onPlayerRecipeDiscover(PlayerRecipeDiscoverEvent event) {
if (config.util().disableRecipeDiscover()) event.setCancelled(true);
}
}