Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit c22ef09

Browse files
Merge pull request #10 from V1nc3ntWasTaken/1.19.2
Merge 1.19.2 support to the main branch
2 parents c680091 + 839183c commit c22ef09

File tree

8 files changed

+318
-64
lines changed

8 files changed

+318
-64
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.doclic</groupId>
88
<artifactId>NoEncryption</artifactId>
9-
<version>3.2</version>
9+
<version>4.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>NoEncryption</name>

src/main/java/me/doclic/noencryption/NoEncryption.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
import org.bukkit.Bukkit;
55
import org.bukkit.plugin.java.JavaPlugin;
66

7+
import java.io.File;
8+
import java.lang.reflect.InvocationTargetException;
9+
import java.lang.reflect.Method;
10+
711
public final class NoEncryption extends JavaPlugin {
812

913
static NoEncryption plugin;
14+
static boolean ready;
1015

1116
@Override
1217
public void onEnable() {
1318
plugin = this;
1419

20+
saveDefaultConfig();
21+
22+
PlayerListener.startup();
23+
1524
Compatibility.initialize(plugin);
1625

1726
if (Compatibility.checkCompatibility()) {
@@ -23,17 +32,66 @@ public void onEnable() {
2332
getLogger().info("If you used /reload to update NoEncryption, your players need to");
2433
getLogger().info("disconnect and join back");
2534

35+
ready = true;
36+
2637
} else {
2738

2839
getLogger().severe("Failed to setup NoEncryption's compatibility!");
2940
getLogger().severe("Your server version (" + Compatibility.getBukkitVersion() + ") is not compatible with this plugin!");
3041

31-
Bukkit.getPluginManager().disablePlugin(this);
42+
ready = false;
43+
44+
VersionDownloader.downloadVersion();
45+
46+
getLogger().info("Downloading the compatible version from https://github.com/V1nc3ntWasTaken/NoEncryption ...");
47+
getLogger().info("Do not restart the server until the download success message is shown to prevent data loss");
48+
49+
}
50+
51+
}
52+
53+
public static File getJARFile() {
54+
55+
try {
56+
57+
JavaPlugin plugin = (JavaPlugin) getPlugin().getServer().getPluginManager().getPlugin(NoEncryption.getPlugin().getName());
58+
Method getFileMethod = JavaPlugin.class.getDeclaredMethod("getFile");
59+
getFileMethod.setAccessible(true);
60+
61+
return (File) getFileMethod.invoke(plugin);
62+
63+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
64+
65+
e.printStackTrace();
66+
67+
return new File("plugins/NoEncryption-v" + getPlugin().getDescription().getVersion() + "--" + Compatibility.getCompatibleVersion() + "_only.jar");
68+
3269
}
3370

3471
}
3572

3673
public static NoEncryption getPlugin() {
74+
3775
return plugin;
76+
77+
}
78+
79+
public static boolean isReady() {
80+
81+
return ready;
82+
83+
}
84+
85+
@Override
86+
public void onDisable() {
87+
88+
if (NoEncryption.isReady()) {
89+
90+
PlayerListener.shutdown();
91+
92+
}
93+
94+
VersionDownloader.shutdown();
95+
3896
}
3997
}

src/main/java/me/doclic/noencryption/PlayerListener.java

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package me.doclic.noencryption;
22

3-
import io.netty.channel.*;
4-
import me.doclic.noencryption.compatibility.CompatiblePacketListener;
5-
import me.doclic.noencryption.compatibility.CompatiblePlayer;
3+
import io.netty.channel.Channel;
4+
import io.netty.channel.ChannelDuplexHandler;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.ChannelPipeline;
7+
import io.netty.channel.ChannelPromise;
8+
import me.doclic.noencryption.compatibility.*;
9+
import org.bukkit.Bukkit;
610
import org.bukkit.entity.Player;
711
import org.bukkit.event.EventHandler;
812
import org.bukkit.event.EventPriority;
@@ -12,39 +16,95 @@
1216

1317
public class PlayerListener implements Listener {
1418

19+
public static void startup() {
20+
21+
if (NoEncryption.isReady()) {
22+
23+
for (final Player player : Bukkit.getOnlinePlayers()) {
24+
25+
final ChannelPipeline pipeline = new CompatiblePlayer().getChannel(player).pipeline();
26+
pipeline.addBefore("packet_handler", "no_encryption_interceptor", new ChannelDuplexHandler() {
27+
28+
@Override
29+
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {
30+
31+
Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
32+
super.channelRead(channelHandlerContext, newPacket);
33+
34+
}
35+
36+
@Override
37+
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {
38+
39+
Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
40+
super.write(channelHandlerContext, newPacket, promise);
41+
42+
}
43+
44+
});
45+
46+
}
47+
48+
}
49+
50+
}
51+
1552
@EventHandler(priority = EventPriority.LOWEST)
1653
public void onPlayerJoin (PlayerJoinEvent e) {
1754

18-
final Player player = e.getPlayer();
19-
final ChannelPipeline pipeline = new CompatiblePlayer().getChannel(player).pipeline();
20-
pipeline.addBefore("packet_handler", "no_encryption", new ChannelDuplexHandler() {
55+
if (NoEncryption.isReady()) {
2156

22-
@Override
23-
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {
57+
final Player player = e.getPlayer();
58+
final ChannelPipeline pipeline = new CompatiblePlayer().getChannel(player).pipeline();
59+
pipeline.addBefore("packet_handler", "no_encryption_interceptor", new ChannelDuplexHandler() {
2460

25-
Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
26-
super.channelRead(channelHandlerContext, newPacket);
61+
@Override
62+
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {
2763

28-
}
64+
Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
65+
super.channelRead(channelHandlerContext, newPacket);
2966

30-
@Override
31-
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {
67+
}
3268

33-
Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
34-
super.write(channelHandlerContext, newPacket, promise);
69+
@Override
70+
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {
3571

36-
}
72+
Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
73+
super.write(channelHandlerContext, newPacket, promise);
74+
75+
}
76+
77+
});
3778

38-
});
79+
}
3980

4081
}
4182

4283
@EventHandler(priority = EventPriority.LOWEST)
4384
public void onPlayerQuit (PlayerQuitEvent e) {
4485

45-
final Player player = e.getPlayer();
46-
final Channel channel = new CompatiblePlayer().getChannel(player);
47-
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption"));
86+
if (NoEncryption.isReady()) {
87+
88+
final Player player = e.getPlayer();
89+
final Channel channel = new CompatiblePlayer().getChannel(player);
90+
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption_interceptor"));
91+
92+
}
93+
94+
}
95+
96+
public static void shutdown() {
97+
98+
if (NoEncryption.isReady()) {
99+
100+
for (final Player player : Bukkit.getOnlinePlayers()) {
101+
102+
final Channel channel = new CompatiblePlayer().getChannel(player);
103+
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption_interceptor"));
104+
105+
}
106+
107+
}
48108

49109
}
50110

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package me.doclic.noencryption;
2+
3+
import me.doclic.noencryption.compatibility.Compatibility;
4+
import org.bukkit.Bukkit;
5+
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.net.URL;
10+
import java.nio.channels.Channels;
11+
import java.nio.channels.FileChannel;
12+
import java.nio.channels.ReadableByteChannel;
13+
14+
public class VersionDownloader {
15+
16+
protected static NoEncryption plugin;
17+
18+
protected static boolean deleteOnShutdown;
19+
20+
public static void initialize(NoEncryption plugin) {
21+
22+
VersionDownloader.plugin = plugin;
23+
24+
}
25+
26+
public static void downloadVersion() {
27+
28+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
29+
30+
try {
31+
32+
File saveLocation = new File("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar");
33+
String url = "https://github.com/V1nc3ntWasTaken/NoEncryption/releases/download/" + plugin.getDescription().getVersion() + "/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar";
34+
35+
ReadableByteChannel byteChannel = Channels.newChannel(new URL(url).openStream());
36+
FileOutputStream outStream = new FileOutputStream(saveLocation);
37+
FileChannel channel = outStream.getChannel();
38+
39+
channel.transferFrom(byteChannel, 0, Long.MAX_VALUE);
40+
41+
doneDownloading();
42+
43+
} catch (IOException e) {
44+
45+
plugin.getLogger().severe("Failed to reach URL for download.");
46+
e.printStackTrace();
47+
48+
}
49+
50+
});
51+
52+
}
53+
54+
private static void doneDownloading() {
55+
setDeleteOnShutdown();
56+
57+
plugin.getLogger().info("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar" + " successfully downloaded");
58+
plugin.getLogger().info(NoEncryption.getJARFile().getName() + " (current version) will be deleted automatically upon restart");
59+
plugin.getLogger().info("It is now safe to restart your server");
60+
61+
}
62+
63+
private static void setDeleteOnShutdown() {
64+
65+
deleteOnShutdown = true;
66+
67+
}
68+
69+
public static boolean getDeleteOnShutdown() {
70+
71+
return deleteOnShutdown;
72+
73+
}
74+
75+
public static void shutdown() {
76+
77+
try {
78+
79+
if (getDeleteOnShutdown()) {
80+
81+
if (NoEncryption.getJARFile().exists() && !NoEncryption.getJARFile().delete()) {
82+
throw new IOException();
83+
}
84+
85+
}
86+
87+
} catch (IOException e) {
88+
89+
plugin.getLogger().severe("Unable to delete " + NoEncryption.getJARFile().getName() + ". Manual deletion is required");
90+
e.printStackTrace();
91+
92+
}
93+
94+
}
95+
96+
}

src/main/java/me/doclic/noencryption/compatibility/Compatibility.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Compatibility {
99

1010
protected static String compatibleVersion;
1111
protected static String bukkitVersion;
12+
protected static String formattedBukkitVersion;
1213
protected static String minecraftVersion;
1314

1415
protected static boolean compatible;
@@ -21,10 +22,12 @@ public static void initialize(NoEncryption plugin) {
2122

2223
try {
2324
bukkitVersion = Bukkit.getBukkitVersion(); // Gets the server version displayable to a user
25+
formattedBukkitVersion = bukkitVersion.split("-")[0];
2426
minecraftVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; // Gets the server version
2527
} catch (ArrayIndexOutOfBoundsException exception) {
2628
// This should never happen
2729
bukkitVersion = null;
30+
formattedBukkitVersion = null;
2831
minecraftVersion = null;
2932
}
3033

@@ -33,11 +36,19 @@ public static void initialize(NoEncryption plugin) {
3336
compatible = bukkitVersion.equals(compatibleVersion);
3437
}
3538

39+
public static String getCompatibleVersion() {
40+
return compatibleVersion;
41+
}
42+
3643
public static boolean checkCompatibility() {
3744
return compatible;
3845
}
3946

4047
public static String getBukkitVersion() {
4148
return bukkitVersion;
4249
}
50+
51+
public static String getFormattedBukkitVersion() {
52+
return formattedBukkitVersion;
53+
}
4354
}

0 commit comments

Comments
 (0)