Skip to content

Commit

Permalink
Fix bug where every plugin stop working if MineTrax fail to load :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Feb 21, 2024
1 parent 3d61e11 commit 456ba0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 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>Minetrax</groupId>
<artifactId>Minetrax</artifactId>
<version>4.0.0</version>
<version>4.0.1</version>
<packaging>jar</packaging>

<name>Minetrax</name>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/xinecraft/Minetrax.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.commons.lang.StringUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
Expand Down Expand Up @@ -365,7 +364,6 @@ public void onEnable() {
public void onDisable() {
// Plugin shutdown logic
getLogger().info("Minetrax Plugin Disabled!");
HandlerList.unregisterAll();
if (webQuerySocketServer != null) {
webQuerySocketServer.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.bukkit.scheduler.BukkitRunnable;
import io.netty.channel.Channel;

import java.util.concurrent.TimeUnit;

public class NettyWebQueryServer extends BukkitRunnable {
private int port;
private String host;

private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private Channel serverChannel;
private final EventLoopGroup bossGroup;
private final EventLoopGroup workerGroup;
private ChannelFuture serverChannelFuture;

public NettyWebQueryServer(String host, int port) {
this.host = host;
this.port = port;
this.bossGroup = new NioEventLoopGroup();
this.workerGroup = new NioEventLoopGroup();
}

@Override
public void run() {
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
Expand All @@ -37,45 +34,33 @@ public void run() {
.childOption(ChannelOption.SO_KEEPALIVE, true);

// Bind and start to accept incoming connections.
ChannelFuture f;
if (host != null && !host.isEmpty())
f = b.bind(host, port).sync();
serverChannelFuture = b.bind(host, port).sync();
else
f = b.bind(port).sync();
serverChannelFuture = b.bind(port).sync();

if(f.isSuccess()) {
if (serverChannelFuture.isSuccess()) {
Minetrax.getPlugin().getLogger().info("WebQuery Server started successfully on port " + port);
serverChannel = f.channel();
}

// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
serverChannelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
Minetrax.getPlugin().getLogger().warning("WebQuery Server interrupted: " + e.getMessage());
Thread.currentThread().interrupt();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}

public void shutdown() {
if (serverChannel != null) {
try {
serverChannel.close().syncUninterruptibly();
} catch (Exception e) {
Minetrax.getPlugin().getLogger().warning("Unable to shutdown webquery channel! " + e.getMessage());
}
}
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();

try {
bossGroup.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
workerGroup.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
if (serverChannelFuture != null) {
serverChannelFuture.channel().close().syncUninterruptibly();
}
workerGroup.shutdownGracefully().syncUninterruptibly();
bossGroup.shutdownGracefully().syncUninterruptibly();
}
}

0 comments on commit 456ba0d

Please sign in to comment.