Skip to content

Commit 4111de2

Browse files
committed
Merge remote-tracking branch 'refs/remotes/upstream/2.0' into grim/2.0
2 parents f04049e + 8669b0f commit 4111de2

File tree

6 files changed

+121
-17
lines changed

6 files changed

+121
-17
lines changed

api/src/main/java/com/github/retrooper/packetevents/protocol/world/states/defaulttags/BlockTags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public class BlockTags {
485485
BlockTags.COPPER_CHESTS.add(StateTypes.COPPER_CHEST, StateTypes.EXPOSED_COPPER_CHEST, StateTypes.WEATHERED_COPPER_CHEST, StateTypes.OXIDIZED_COPPER_CHEST, StateTypes.WAXED_COPPER_CHEST, StateTypes.WAXED_EXPOSED_COPPER_CHEST, StateTypes.WAXED_WEATHERED_COPPER_CHEST, StateTypes.WAXED_OXIDIZED_COPPER_CHEST);
486486
BlockTags.LIGHTNING_RODS.add(StateTypes.LIGHTNING_ROD, StateTypes.EXPOSED_LIGHTNING_ROD, StateTypes.WEATHERED_LIGHTNING_ROD, StateTypes.OXIDIZED_LIGHTNING_ROD, StateTypes.WAXED_LIGHTNING_ROD, StateTypes.WAXED_EXPOSED_LIGHTNING_ROD, StateTypes.WAXED_WEATHERED_LIGHTNING_ROD, StateTypes.WAXED_OXIDIZED_LIGHTNING_ROD);
487487
BlockTags.COPPER.add(StateTypes.COPPER_BLOCK, StateTypes.EXPOSED_COPPER, StateTypes.WEATHERED_COPPER, StateTypes.OXIDIZED_COPPER, StateTypes.WAXED_COPPER_BLOCK, StateTypes.WAXED_EXPOSED_COPPER, StateTypes.WAXED_WEATHERED_COPPER, StateTypes.WAXED_OXIDIZED_COPPER);
488-
BlockTags.CHAINS.add(StateTypes.IRON_CHAIN, StateTypes.COPPER_CHAIN, StateTypes.WAXED_COPPER_CHAIN, StateTypes.EXPOSED_COPPER_CHAIN, StateTypes.WAXED_EXPOSED_COPPER_CHAIN, StateTypes.WEATHERED_COPPER_CHAIN, StateTypes.WAXED_WEATHERED_COPPER_CHAIN, StateTypes.OXIDIZED_COPPER_CHAIN, StateTypes.WAXED_OXIDIZED_COPPER_CHAIN);
488+
BlockTags.CHAINS.add(StateTypes.CHAIN, StateTypes.IRON_CHAIN, StateTypes.COPPER_CHAIN, StateTypes.WAXED_COPPER_CHAIN, StateTypes.EXPOSED_COPPER_CHAIN, StateTypes.WAXED_EXPOSED_COPPER_CHAIN, StateTypes.WEATHERED_COPPER_CHAIN, StateTypes.WAXED_WEATHERED_COPPER_CHAIN, StateTypes.OXIDIZED_COPPER_CHAIN, StateTypes.WAXED_OXIDIZED_COPPER_CHAIN);
489489
BlockTags.COPPER_GOLEM_STATUES.add(StateTypes.COPPER_GOLEM_STATUE, StateTypes.EXPOSED_COPPER_GOLEM_STATUE, StateTypes.WEATHERED_COPPER_GOLEM_STATUE, StateTypes.OXIDIZED_COPPER_GOLEM_STATUE, StateTypes.WAXED_COPPER_GOLEM_STATUE, StateTypes.WAXED_EXPOSED_COPPER_GOLEM_STATUE, StateTypes.WAXED_WEATHERED_COPPER_GOLEM_STATUE, StateTypes.WAXED_OXIDIZED_COPPER_GOLEM_STATUE);
490490
BlockTags.LANTERNS.add(StateTypes.LANTERN, StateTypes.SOUL_LANTERN, StateTypes.COPPER_LANTERN, StateTypes.WAXED_COPPER_LANTERN, StateTypes.EXPOSED_COPPER_LANTERN, StateTypes.WAXED_EXPOSED_COPPER_LANTERN, StateTypes.WEATHERED_COPPER_LANTERN, StateTypes.WAXED_WEATHERED_COPPER_LANTERN, StateTypes.OXIDIZED_COPPER_LANTERN, StateTypes.WAXED_OXIDIZED_COPPER_LANTERN);
491491
BlockTags.BARS.add(StateTypes.IRON_BARS, StateTypes.COPPER_BARS, StateTypes.WAXED_COPPER_BARS, StateTypes.EXPOSED_COPPER_BARS, StateTypes.WAXED_EXPOSED_COPPER_BARS, StateTypes.WEATHERED_COPPER_BARS, StateTypes.WAXED_WEATHERED_COPPER_BARS, StateTypes.OXIDIZED_COPPER_BARS, StateTypes.WAXED_OXIDIZED_COPPER_BARS);

api/src/main/java/com/github/retrooper/packetevents/util/updatechecker/UpdateChecker.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
2525
import com.google.gson.JsonObject;
2626
import net.kyori.adventure.text.format.NamedTextColor;
27+
import org.jetbrains.annotations.ApiStatus;
2728
import org.jetbrains.annotations.Nullable;
2829

2930
import java.io.BufferedReader;
3031
import java.io.IOException;
3132
import java.io.InputStreamReader;
3233
import java.net.URL;
3334
import java.net.URLConnection;
35+
import java.util.concurrent.atomic.AtomicReference;
36+
import java.util.function.BiConsumer;
37+
import java.util.function.Consumer;
3438

3539
/**
3640
* PacketEvents update checker.
@@ -39,6 +43,7 @@
3943
* @since 1.6.9
4044
*/
4145
public class UpdateChecker {
46+
@ApiStatus.Internal
4247
public String checkLatestReleasedVersion() {
4348
try {
4449
URLConnection connection = new URL("https://api.github.com/repos/retrooper/packetevents/releases/latest").openConnection();
@@ -56,11 +61,15 @@ public String checkLatestReleasedVersion() {
5661
/**
5762
* Check for an update and log in the console (ALL DONE ON THE CURRENT THREAD).
5863
*/
59-
public UpdateCheckerStatus checkForUpdate() {
64+
@ApiStatus.Internal
65+
public UpdateCheckerStatus checkForUpdate(@Nullable Consumer<PEVersion> latestVersionHolder) {
6066
PEVersion localVersion = PacketEvents.getAPI().getVersion();
6167
PEVersion newVersion;
6268
try {
6369
newVersion = PEVersion.fromString(checkLatestReleasedVersion());
70+
if (latestVersionHolder != null) {
71+
latestVersionHolder.accept(newVersion);
72+
}
6473
} catch (Exception ex) {
6574
PacketEvents.getAPI().getLogManager().warn("Failed to check for updates. "
6675
+ (ex.getCause() != null ? ex.getCause().getClass().getName() + ": " + ex.getCause().getMessage() : ex.getMessage()));
@@ -91,18 +100,38 @@ public UpdateCheckerStatus checkForUpdate() {
91100
}
92101
}
93102

103+
@ApiStatus.Internal
104+
public UpdateCheckerStatus checkForUpdate() {
105+
return checkForUpdate(null);
106+
}
107+
108+
@Deprecated @ApiStatus.Internal
94109
public void handleUpdateCheck(@Nullable Runnable updateCheckCallback) {
95110
Thread thread = new Thread(() -> {
96111
PacketEvents.getAPI().getLogManager().info("Checking for updates, please wait...");
97-
UpdateChecker.UpdateCheckerStatus status = checkForUpdate();
112+
UpdateCheckerStatus status = checkForUpdate();
98113
if (updateCheckCallback != null)
99114
updateCheckCallback.run();
100115
}, "packetevents-update-check-thread");
101116
thread.start();
102117
}
103118

119+
@ApiStatus.Internal
120+
public void handleUpdateCheck(@Nullable BiConsumer<PEVersion, UpdateCheckerStatus> updateResultHolder) {
121+
Thread thread = new Thread(() -> {
122+
PacketEvents.getAPI().getLogManager().info("Checking for updates, please wait...");
123+
AtomicReference<PEVersion> latestVersion = new AtomicReference<>();
124+
Consumer<PEVersion> latestVersionHolder = latestVersion::set;
125+
UpdateCheckerStatus status = checkForUpdate(latestVersionHolder);
126+
if (updateResultHolder != null)
127+
updateResultHolder.accept(latestVersion.get(), status);
128+
}, "packetevents-update-check-thread");
129+
thread.start();
130+
}
131+
132+
@ApiStatus.Internal
104133
public void handleUpdateCheck() {
105-
handleUpdateCheck(null);
134+
handleUpdateCheck((Runnable) null);
106135
}
107136

108137
/**

spigot/src/main/java/io/github/retrooper/packetevents/factory/spigot/SpigotPacketEventsBuilder.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.github.retrooper.packetevents.settings.PacketEventsSettings;
3333
import com.github.retrooper.packetevents.util.LogManager;
3434
import com.github.retrooper.packetevents.util.PEVersion;
35+
import com.github.retrooper.packetevents.util.updatechecker.UpdateChecker;
3536
import io.github.retrooper.packetevents.bukkit.InternalBukkitListener;
3637
import io.github.retrooper.packetevents.bukkit.InternalBukkitLoginListener;
3738
import io.github.retrooper.packetevents.bukkit.InternalGlobalBukkitListener;
@@ -56,6 +57,9 @@
5657
import org.bukkit.entity.Player;
5758
import org.bukkit.plugin.Plugin;
5859

60+
import java.util.concurrent.atomic.AtomicBoolean;
61+
import java.util.function.BiConsumer;
62+
5963
public class SpigotPacketEventsBuilder {
6064
private static PacketEventsAPI<Plugin> API_INSTANCE;
6165

@@ -144,6 +148,50 @@ public void init() {
144148
Plugin plugin = (Plugin) PacketEvents.getAPI().getPlugin();
145149
String bukkitVersion = Bukkit.getBukkitVersion();
146150

151+
AtomicBoolean stopping = new AtomicBoolean(false);
152+
BiConsumer<PEVersion, UpdateChecker.UpdateCheckerStatus> unsupportedSoftwareLogic = (peVersion, status) -> {
153+
if (bukkitVersion.contains("Unknown")) {
154+
ServerVersion fallbackVersion = ServerVersion.V_1_8_8;
155+
String failureToDetectVersionMsg = "Your server software is preventing us from checking the Minecraft Server version. This is what we found: " + bukkitVersion + ". We will assume the Server version is " + fallbackVersion.name() + "...\n If you need assistance, join our Discord server: https://discord.gg/DVHxPPxHZc";
156+
plugin.getLogger().warning(failureToDetectVersionMsg);
157+
} else {
158+
// Our PEVersion class can parse this version and detect if it is a newer version than what is currently supported
159+
PEVersion bukkitServerVersion = PEVersion.fromString(bukkitVersion.substring(0, bukkitVersion.indexOf("-")));
160+
PEVersion latestSupportedVersion = PEVersion.fromString(ServerVersion.getLatest().getReleaseName());
161+
if (bukkitServerVersion.isNewerThan(latestSupportedVersion)) {
162+
// We do not support this version yet, so let us warn the user
163+
164+
String developmentBuildsMsg = "Please test the development builds, as they may already have support for your Minecraft version (hint: select the build that contains 'spigot'): https://ci.codemc.io/job/retrooper/job/packetevents";
165+
String releaseBuildsMsg = "Please test the latest stable release, as it should already have support for your Minecraft version: https://modrinth.com/plugin/packetevents";
166+
167+
/* Here's a breakdown of the logic:
168+
* If this build does not support the current Minecraft version and
169+
* the user is running an outdated version of PacketEvents
170+
* or PacketEvents somehow fails to check for an update,
171+
* we direct them toward the latest release.
172+
* If up-to-date, we direct them to development builds.
173+
* */
174+
String newBuildsMsg = (status == UpdateChecker.UpdateCheckerStatus.OUTDATED
175+
|| status == UpdateChecker.UpdateCheckerStatus.FAILED) ? releaseBuildsMsg : developmentBuildsMsg;
176+
177+
plugin.getLogger().warning("Your build of PacketEvents does not support the Minecraft version "
178+
+ bukkitServerVersion + "! The latest Minecraft version supported by your build of PacketEvents is " + latestSupportedVersion + ". "
179+
+ newBuildsMsg +
180+
" If you're in need of any help, join our Discord server: https://discord.gg/DVHxPPxHZc");
181+
Bukkit.getPluginManager().disablePlugin(plugin);
182+
stopping.set(true);
183+
}
184+
}
185+
};
186+
187+
if (settings.shouldCheckForUpdates()) {
188+
getUpdateChecker().handleUpdateCheck(unsupportedSoftwareLogic);
189+
}
190+
else {
191+
// We were not authorized to run a GitHub API call to check for the latest version.
192+
unsupportedSoftwareLogic.accept(null, null);
193+
}
194+
if (stopping.get()) return;
147195
if (bukkitVersion.contains("Unknown")) {
148196
ServerVersion fallbackVersion = ServerVersion.V_1_8_8;
149197
String failureToDetectVersionMsg = "Your server software is preventing us from checking the Minecraft Server version. This is what we found: " + bukkitVersion + ". We will assume the Server version is " + fallbackVersion.name() + "...\n If you need assistance, join our Discord server: https://discord.gg/DVHxPPxHZc";
@@ -153,17 +201,14 @@ public void init() {
153201
PEVersion bukkitServerVersion = PEVersion.fromString(bukkitVersion.substring(0, bukkitVersion.indexOf("-")));
154202
PEVersion latestSupportedVersion = PEVersion.fromString(ServerVersion.getLatest().getReleaseName());
155203
if (bukkitServerVersion.isNewerThan(latestSupportedVersion)) {
156-
//We do not support this version yet, so let us warn the user
204+
// We do not support this version yet, so let us warn the user
157205
plugin.getLogger().warning("Your build of PacketEvents does not support the Minecraft version "
158206
+ bukkitServerVersion + "! The latest Minecraft version supported by your build of PacketEvents is " + latestSupportedVersion
159207
+ ". Please test the development builds, as they may already have support for your Minecraft version (hint: select the build that contains 'spigot'): https://ci.codemc.io/job/retrooper/job/packetevents/ If you're in need of any help, join our Discord server: https://discord.gg/DVHxPPxHZc");
160208
Bukkit.getPluginManager().disablePlugin(plugin);
161209
return;
162210
}
163211
}
164-
if (settings.shouldCheckForUpdates()) {
165-
getUpdateChecker().handleUpdateCheck();
166-
}
167212

168213
Metrics metrics = new Metrics(plugin, 11327);
169214
//Just to have an idea of which versions of packetevents people use

velocity/src/main/java/io/github/retrooper/packetevents/injector/VelocityChannelInitializer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package io.github.retrooper.packetevents.injector;
2020

21+
import com.github.retrooper.packetevents.PacketEvents;
2122
import com.github.retrooper.packetevents.protocol.ConnectionState;
2223
import io.netty.channel.Channel;
2324
import io.netty.channel.ChannelInitializer;
@@ -42,6 +43,12 @@ protected void initChannel(@NotNull Channel channel) throws Exception {
4243
}
4344
INIT_CHANNEL.invoke(wrappedInitializer, channel);
4445

46+
if (PacketEvents.getAPI().isTerminated()) return;
4547
ServerConnectionInitializer.initChannel(channel, ConnectionState.HANDSHAKING);
4648
}
49+
50+
public ChannelInitializer<Channel> getWrappedInitializer() {
51+
return wrappedInitializer;
52+
}
53+
4754
}

velocity/src/main/java/io/github/retrooper/packetevents/injector/VelocityPipelineInjector.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,12 @@ public void inject() {
5555
SERVER_INITIALIZER_HOLDER_CLASS = Reflection.getClassByNameWithoutException("com.velocitypowered.proxy.network.ServerChannelInitializerHolder");
5656
BACKEND_INITIALIZER_HOLDER_CLASS = Reflection.getClassByNameWithoutException("com.velocitypowered.proxy.network.BackendChannelInitializerHolder");
5757
SET_SERVER_INTIIALIZER = Reflection.getMethod(SERVER_INITIALIZER_HOLDER_CLASS, 0, ChannelInitializer.class);
58-
SET_BACKEND_INITIALIZER = Reflection.getMethod(BACKEND_INITIALIZER_HOLDER_CLASS, 0, ChannelInitializer.class);
5958
}
60-
ReflectionObject reflectServer = new ReflectionObject(server);
61-
Object connectionManager = reflectServer.readObject(0, CONNECTION_MANAGER_CLASS);
62-
ReflectionObject reflectConnectionManager = new ReflectionObject(connectionManager);
63-
Object proxyInitializerHolder = reflectConnectionManager.readObject(0, SERVER_INITIALIZER_HOLDER_CLASS);
64-
ChannelInitializer<Channel> wrappedProxyInitializer = ((Supplier<ChannelInitializer<Channel>>) proxyInitializerHolder).get();
59+
Supplier<ChannelInitializer<Channel>> initializerHolder = getServerChannelInitializerHolder();
60+
ChannelInitializer<Channel> wrappedProxyInitializer = initializerHolder.get();
6561
VelocityChannelInitializer initializer = new VelocityChannelInitializer(wrappedProxyInitializer);
6662
try {
67-
SET_SERVER_INTIIALIZER.invoke(proxyInitializerHolder, initializer);
63+
SET_SERVER_INTIIALIZER.invoke(initializerHolder, initializer);
6864
} catch (IllegalAccessException | InvocationTargetException e) {
6965
e.printStackTrace();
7066
}
@@ -80,7 +76,16 @@ public void inject() {
8076

8177
@Override
8278
public void uninject() {
83-
79+
Supplier<ChannelInitializer<Channel>> initializerHolder = getServerChannelInitializerHolder();
80+
ChannelInitializer<Channel> wrapper = initializerHolder.get();
81+
// Check if it's our initializer, could be wrapped by other plugins
82+
if (wrapper instanceof VelocityChannelInitializer) {
83+
try {
84+
SET_SERVER_INTIIALIZER.invoke(initializerHolder, ((VelocityChannelInitializer) wrapper).getWrappedInitializer());
85+
} catch (IllegalAccessException | InvocationTargetException e) {
86+
e.printStackTrace();
87+
}
88+
}
8489
}
8590

8691
@Override
@@ -119,4 +124,11 @@ public void setPlayer(Object ch, Object p) {
119124
public boolean isProxy() {
120125
return true;
121126
}
127+
128+
private Supplier<ChannelInitializer<Channel>> getServerChannelInitializerHolder() {
129+
ReflectionObject reflectServer = new ReflectionObject(server);
130+
Object connectionManager = reflectServer.readObject(0, CONNECTION_MANAGER_CLASS);
131+
ReflectionObject reflectConnectionManager = new ReflectionObject(connectionManager);
132+
return (Supplier<ChannelInitializer<Channel>>) reflectConnectionManager.readObject(0, SERVER_INITIALIZER_HOLDER_CLASS);
133+
}
122134
}

velocity/src/main/java/io/github/retrooper/packetevents/velocity/factory/VelocityPacketEventsBuilder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import io.github.retrooper.packetevents.impl.netty.manager.server.ServerManagerAbstract;
4747
import io.github.retrooper.packetevents.injector.VelocityPipelineInjector;
4848
import io.github.retrooper.packetevents.manager.PlayerManagerImpl;
49+
import io.netty.channel.Channel;
4950
import net.kyori.adventure.text.format.NamedTextColor;
5051
import org.jetbrains.annotations.Nullable;
5152
import org.slf4j.Logger;
@@ -233,8 +234,18 @@ public boolean isInitialized() {
233234
@Override
234235
public void terminate() {
235236
if (initialized) {
236-
// Eject the injector if needed(depends on the injector implementation)
237+
// Uninject the injector
237238
injector.uninject();
239+
// Remove handlers for players
240+
for (User user : this.protocolManager.getUsers()) {
241+
Channel channel = (Channel) user.getChannel();
242+
if (channel.pipeline().get(PacketEvents.ENCODER_NAME) != null) {
243+
channel.pipeline().remove(PacketEvents.ENCODER_NAME);
244+
}
245+
if (channel.pipeline().get(PacketEvents.DECODER_NAME) != null) {
246+
channel.pipeline().remove(PacketEvents.DECODER_NAME);
247+
}
248+
}
238249
// Unregister all our listeners
239250
getEventManager().unregisterAllListeners();
240251
initialized = false;

0 commit comments

Comments
 (0)