Skip to content

Commit

Permalink
auto reload #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilin committed Jun 18, 2018
1 parent e4209d4 commit 873b40d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/main/java/cat/nyaa/bungeecordusercontrol/BUC.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class BUC extends net.md_5.bungee.api.plugin.Plugin {
public Config config;
public Messages msg;
public BungeeProxy bungeeProxy;
public boolean reloading = false;
public Long lastReload = 0L;
public FileWatcher fileWatcher;

@Override
public void onEnable() {
Expand All @@ -27,6 +30,8 @@ public void onEnable() {
userList.save();
config.save();
bungeeProxy = new BungeeProxy(this);
lastReload = System.currentTimeMillis();
fileWatcher = new FileWatcher(this);
}

@Override
Expand All @@ -44,8 +49,23 @@ public boolean kickPlayer(UUID uuid, BaseComponent... reason) {
}
}

public void save() {
public synchronized void save() {
lastReload = System.currentTimeMillis();
config.save();
userList.save();
}

public synchronized void reload() {
reloading = true;
Messages.load();
config.load();
userList.load();
getProxy().getPluginManager().registerCommand(this, new Commands(this));
reloading = false;
getLogger().info(Messages.get("messages.reload"));
}

public synchronized boolean isReloading() {
return reloading;
}
}
3 changes: 1 addition & 2 deletions src/main/java/cat/nyaa/bungeecordusercontrol/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public void run() {
sender.sendMessage(Messages.getTextComponent("messages.no_permission"));
return;
}
plugin.config.load();
plugin.config.save();
plugin.reload();
plugin.getLogger().info(Messages.get("log.reload", sender.getName()));
sender.sendMessage(Messages.getTextComponent("messages.reload"));
} else {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/cat/nyaa/bungeecordusercontrol/FileWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cat.nyaa.bungeecordusercontrol;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class FileWatcher extends Thread {

private BUC plugin;
private WatchService watchService;

public FileWatcher(BUC pl) {
plugin = pl;
try {
watchService = FileSystems.getDefault().newWatchService();
pl.getDataFolder().toPath().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
} catch (IOException e) {
e.printStackTrace();
}
start();
}

@Override
public void run() {
while (true) {
WatchKey watchKey = null;
try {
watchKey = watchService.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (watchKey != null) {
if (System.currentTimeMillis() - plugin.lastReload > 5000) {
plugin.lastReload = System.currentTimeMillis();
plugin.getLogger().info("reloading...");
watchKey.pollEvents();
try {
sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
plugin.reload();
plugin.getLogger().info(Messages.get("messages.reload"));
}
} else {
break;
}
if (!watchKey.reset()) {
break;
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public PlayerListener(BUC pl) {

@EventHandler
public void onPlayerLogin(LoginEvent event) {
if (plugin.isReloading()) {
event.setCancelled(true);
event.setCancelReason(Messages.getTextComponent("messages.login.reload"));
return;
}
UUID uuid = event.getConnection().getUniqueId();
String name = event.getConnection().getName();
if (plugin.userList.isEnableWhitelist() && !plugin.userList.isWhitelisted(uuid)) {
Expand All @@ -27,7 +32,7 @@ public void onPlayerLogin(LoginEvent event) {
User user = plugin.userList.getUserByUUID(uuid);
if (plugin.userList.banExpires(uuid)) {
plugin.getLogger().info(user.toString());
plugin.getLogger().info(Messages.get("log.unban", name,uuid,"[CONSOLE]"));
plugin.getLogger().info(Messages.get("log.unban", name, uuid, "[CONSOLE]"));
plugin.userList.unbanUser(user.getPlayerUUID());
} else if ("".equals(user.getBanExpires()) || user.getBanExpires().equalsIgnoreCase("forever")) {
event.setCancelReason(Messages.getTextComponent("messages.login.banned", user.getBanReason()));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cat/nyaa/bungeecordusercontrol/UserList.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void load() {
}

public void save() {
plugin.lastReload = System.currentTimeMillis();
userCacheFile = getFile("usercache.json");
bannedPlayersFile = getFile("banned-players.json");
whitelistFile = getFile("whitelist.json");
Expand Down Expand Up @@ -175,6 +176,7 @@ private void saveWhitelist() {

public void reloadWhitelist() {
loadWhitelist();
plugin.lastReload = System.currentTimeMillis();
saveWhitelist();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ messages:
whitelist: "&bYou are not whitelisted on this server"
banned: "&bYou have been banned.\n &fReason: %s"
tempban: "&bYou have been temporarily banned.\n &fReason: %s\n &fEnd in: %s"
reload: "&bBUC is reloading"
ban:
success: "&aPlayer %s banned "
already_banned: "&cPlayer %s is already banned"
Expand Down

0 comments on commit 873b40d

Please sign in to comment.