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

Commit

Permalink
Merge pull request #10 from V1nc3ntWasTaken/1.19.2
Browse files Browse the repository at this point in the history
Merge 1.19.2 support to the main branch
  • Loading branch information
V1nc3ntWasTaken committed Aug 9, 2022
2 parents c680091 + 839183c commit c22ef09
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 64 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.doclic</groupId>
<artifactId>NoEncryption</artifactId>
<version>3.2</version>
<version>4.0</version>
<packaging>jar</packaging>

<name>NoEncryption</name>
Expand Down
60 changes: 59 additions & 1 deletion src/main/java/me/doclic/noencryption/NoEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class NoEncryption extends JavaPlugin {

static NoEncryption plugin;
static boolean ready;

@Override
public void onEnable() {
plugin = this;

saveDefaultConfig();

PlayerListener.startup();

Compatibility.initialize(plugin);

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

ready = true;

} else {

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

Bukkit.getPluginManager().disablePlugin(this);
ready = false;

VersionDownloader.downloadVersion();

getLogger().info("Downloading the compatible version from https://github.com/V1nc3ntWasTaken/NoEncryption ...");
getLogger().info("Do not restart the server until the download success message is shown to prevent data loss");

}

}

public static File getJARFile() {

try {

JavaPlugin plugin = (JavaPlugin) getPlugin().getServer().getPluginManager().getPlugin(NoEncryption.getPlugin().getName());
Method getFileMethod = JavaPlugin.class.getDeclaredMethod("getFile");
getFileMethod.setAccessible(true);

return (File) getFileMethod.invoke(plugin);

} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {

e.printStackTrace();

return new File("plugins/NoEncryption-v" + getPlugin().getDescription().getVersion() + "--" + Compatibility.getCompatibleVersion() + "_only.jar");

}

}

public static NoEncryption getPlugin() {

return plugin;

}

public static boolean isReady() {

return ready;

}

@Override
public void onDisable() {

if (NoEncryption.isReady()) {

PlayerListener.shutdown();

}

VersionDownloader.shutdown();

}
}
100 changes: 80 additions & 20 deletions src/main/java/me/doclic/noencryption/PlayerListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package me.doclic.noencryption;

import io.netty.channel.*;
import me.doclic.noencryption.compatibility.CompatiblePacketListener;
import me.doclic.noencryption.compatibility.CompatiblePlayer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import me.doclic.noencryption.compatibility.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -12,39 +16,95 @@

public class PlayerListener implements Listener {

public static void startup() {

if (NoEncryption.isReady()) {

for (final Player player : Bukkit.getOnlinePlayers()) {

final ChannelPipeline pipeline = new CompatiblePlayer().getChannel(player).pipeline();
pipeline.addBefore("packet_handler", "no_encryption_interceptor", new ChannelDuplexHandler() {

@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {

Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
super.channelRead(channelHandlerContext, newPacket);

}

@Override
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {

Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
super.write(channelHandlerContext, newPacket, promise);

}

});

}

}

}

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin (PlayerJoinEvent e) {

final Player player = e.getPlayer();
final ChannelPipeline pipeline = new CompatiblePlayer().getChannel(player).pipeline();
pipeline.addBefore("packet_handler", "no_encryption", new ChannelDuplexHandler() {
if (NoEncryption.isReady()) {

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

Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
super.channelRead(channelHandlerContext, newPacket);
@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {

}
Object newPacket = new CompatiblePacketListener().readPacket(channelHandlerContext, packet);
super.channelRead(channelHandlerContext, newPacket);

@Override
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {
}

Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
super.write(channelHandlerContext, newPacket, promise);
@Override
public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise promise) throws Exception {

}
Object newPacket = new CompatiblePacketListener().writePacket(channelHandlerContext, packet, promise);
super.write(channelHandlerContext, newPacket, promise);

}

});

});
}

}

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

final Player player = e.getPlayer();
final Channel channel = new CompatiblePlayer().getChannel(player);
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption"));
if (NoEncryption.isReady()) {

final Player player = e.getPlayer();
final Channel channel = new CompatiblePlayer().getChannel(player);
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption_interceptor"));

}

}

public static void shutdown() {

if (NoEncryption.isReady()) {

for (final Player player : Bukkit.getOnlinePlayers()) {

final Channel channel = new CompatiblePlayer().getChannel(player);
channel.eventLoop().submit(() -> channel.pipeline().remove("no_encryption_interceptor"));

}

}

}

Expand Down
96 changes: 96 additions & 0 deletions src/main/java/me/doclic/noencryption/VersionDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package me.doclic.noencryption;

import me.doclic.noencryption.compatibility.Compatibility;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class VersionDownloader {

protected static NoEncryption plugin;

protected static boolean deleteOnShutdown;

public static void initialize(NoEncryption plugin) {

VersionDownloader.plugin = plugin;

}

public static void downloadVersion() {

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {

try {

File saveLocation = new File("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar");
String url = "https://github.com/V1nc3ntWasTaken/NoEncryption/releases/download/" + plugin.getDescription().getVersion() + "/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar";

ReadableByteChannel byteChannel = Channels.newChannel(new URL(url).openStream());
FileOutputStream outStream = new FileOutputStream(saveLocation);
FileChannel channel = outStream.getChannel();

channel.transferFrom(byteChannel, 0, Long.MAX_VALUE);

doneDownloading();

} catch (IOException e) {

plugin.getLogger().severe("Failed to reach URL for download.");
e.printStackTrace();

}

});

}

private static void doneDownloading() {
setDeleteOnShutdown();

plugin.getLogger().info("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar" + " successfully downloaded");
plugin.getLogger().info(NoEncryption.getJARFile().getName() + " (current version) will be deleted automatically upon restart");
plugin.getLogger().info("It is now safe to restart your server");

}

private static void setDeleteOnShutdown() {

deleteOnShutdown = true;

}

public static boolean getDeleteOnShutdown() {

return deleteOnShutdown;

}

public static void shutdown() {

try {

if (getDeleteOnShutdown()) {

if (NoEncryption.getJARFile().exists() && !NoEncryption.getJARFile().delete()) {
throw new IOException();
}

}

} catch (IOException e) {

plugin.getLogger().severe("Unable to delete " + NoEncryption.getJARFile().getName() + ". Manual deletion is required");
e.printStackTrace();

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Compatibility {

protected static String compatibleVersion;
protected static String bukkitVersion;
protected static String formattedBukkitVersion;
protected static String minecraftVersion;

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

try {
bukkitVersion = Bukkit.getBukkitVersion(); // Gets the server version displayable to a user
formattedBukkitVersion = bukkitVersion.split("-")[0];
minecraftVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; // Gets the server version
} catch (ArrayIndexOutOfBoundsException exception) {
// This should never happen
bukkitVersion = null;
formattedBukkitVersion = null;
minecraftVersion = null;
}

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

public static String getCompatibleVersion() {
return compatibleVersion;
}

public static boolean checkCompatibility() {
return compatible;
}

public static String getBukkitVersion() {
return bukkitVersion;
}

public static String getFormattedBukkitVersion() {
return formattedBukkitVersion;
}
}
Loading

0 comments on commit c22ef09

Please sign in to comment.