Skip to content

Commit

Permalink
Whitelist is now watching for changes in whitelist.txt and reload whe…
Browse files Browse the repository at this point in the history
…n needed
  • Loading branch information
Styr1x committed Jan 19, 2011
1 parent cceb66d commit 8401cda
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/com/bukkit/silence/whitelist/FileWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2011 <[email protected]>
*
* This file is part of the Bukkit plugin Whitelist.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA.
*/

package com.bukkit.silence.whitelist;

import java.io.File;
import java.util.TimerTask;

public class FileWatcher extends TimerTask
{
private File m_File;
private long m_LastModified;
private volatile boolean m_WasChanged;

FileWatcher(File file)
{
m_File = file;
m_LastModified = m_File.lastModified();
}

@Override
public void run()
{
if ( m_LastModified != m_File.lastModified() )
{
m_LastModified = m_File.lastModified();
if ( !m_WasChanged )
{
m_WasChanged = true;
System.out.println("Whitelist: Whitelist.txt was updated. Whitelist was scheduled for reloading.");
}
}
}

public boolean wasFileModified()
{
return m_WasChanged;
}

public void resetFileModifiedState()
{
m_WasChanged = false;
}

}
8 changes: 8 additions & 0 deletions src/com/bukkit/silence/whitelist/WLPlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void onPlayerLogin(PlayerLoginEvent event)
{
if ( m_Plugin.isWhitelistActive() )
{
//Check if whitelist.txt needs to be reloaded
if ( m_Plugin.needReloadWhitelist() )
{
System.out.println("Whitelist: Executing scheduled whitelist reload.");
m_Plugin.reloadSettings();
m_Plugin.resetNeedReloadWhitelist();
}

String playerName = event.getPlayer().getName();
System.out.print("Whitelist: Player " + playerName + " is trying to join...");
if ( m_Plugin.isOnWhitelist(playerName) )
Expand Down
21 changes: 21 additions & 0 deletions src/com/bukkit/silence/whitelist/Whitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.util.Timer;
import org.bukkit.Server;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
Expand All @@ -53,6 +54,8 @@ public class Whitelist extends JavaPlugin

//Attributes
private final WLPlayerListener m_PlayerListner = new WLPlayerListener(this);
private FileWatcher m_Watcher;
private Timer m_Timer = new Timer(true);
private File m_Folder;
private ArrayList<String> m_WhitelistAdmins;
private ArrayList<String> m_WhitelistAllow;
Expand Down Expand Up @@ -100,6 +103,10 @@ public void onEnable()
System.out.println("failed.");
}
}
//Start file watcher
m_Watcher = new FileWatcher(fWhitelist);
m_Timer.schedule(m_Watcher, 0, 1000);

File fConfig = new File(m_Folder.getAbsolutePath() + File.separator + FILE_CONFIG);
if (!fConfig.exists())
{
Expand Down Expand Up @@ -127,6 +134,7 @@ public void onEnable()

public void onDisable()
{
m_Timer.cancel();
System.out.println("Goodbye world!");
}

Expand Down Expand Up @@ -283,4 +291,17 @@ public boolean isListCommandDisabled()
{
return m_IsListCommandDisabled;
}

public boolean needReloadWhitelist()
{
if ( m_Watcher != null )
return m_Watcher.wasFileModified();
return false;
}

public void resetNeedReloadWhitelist()
{
if ( m_Watcher != null )
m_Watcher.resetFileModifiedState();
}
}
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: Whitelist
main: com.bukkit.silence.whitelist.Whitelist
version: 1.3
version: 1.4

0 comments on commit 8401cda

Please sign in to comment.