diff --git a/CraftincBorderProtection.jar b/CraftincBorderProtection.jar index 95127d0..7301897 100644 Binary files a/CraftincBorderProtection.jar and b/CraftincBorderProtection.jar differ diff --git a/pom.xml b/pom.xml index b344b5b..fa478a9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ de.craftinc CraftincBorderProtection jar - 1.0.3-1 + 1.1 UTF-8 diff --git a/src/main/java/de/craftinc/borderprotection/Messages.java b/src/main/java/de/craftinc/borderprotection/Messages.java index 1bc7bc9..8e35632 100644 --- a/src/main/java/de/craftinc/borderprotection/Messages.java +++ b/src/main/java/de/craftinc/borderprotection/Messages.java @@ -116,4 +116,14 @@ public class Messages public static String borderEnableDisableException = ChatColor.RED + "Error: Could not save border state on server. After the next reload this border state will be lost!"; + + public static String UpdateMessage( String newVersion, String curVersion ) + { + return ChatColor.RED + "Craft Inc. BorderProtection: New version available!" + NEWLINE + + ChatColor.YELLOW + "Current version: " + ChatColor.WHITE + curVersion + NEWLINE + + ChatColor.YELLOW + "New version: " + ChatColor.WHITE + newVersion + NEWLINE + + ChatColor.YELLOW + "Please visit:" + NEWLINE + + ChatColor.AQUA + "http://dev.bukkit.org/server-mods/craftinc-borderprotection" + NEWLINE + + ChatColor.YELLOW + "to get the latest version!"; + } } diff --git a/src/main/java/de/craftinc/borderprotection/PlayerLoginListener.java b/src/main/java/de/craftinc/borderprotection/PlayerLoginListener.java new file mode 100644 index 0000000..0286300 --- /dev/null +++ b/src/main/java/de/craftinc/borderprotection/PlayerLoginListener.java @@ -0,0 +1,50 @@ +/* Craft Inc. BorderProtection + Copyright (C) 2013 Paul Schulze + + 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 3 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, see . +*/ +package de.craftinc.borderprotection; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; + +public class PlayerLoginListener implements Listener +{ + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerLogin( PlayerLoginEvent e ) + { + final Player player = e.getPlayer(); + + if ( e.getPlayer().hasPermission("craftinc.borderprotection.update") ) + { + if ( UpdateHelper.newVersionAvailable() ) + { + // Schedule a task which delays 20 ticks (1 second) and then sends a message to the player + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable() + { + @Override + public void run() + { + player.sendMessage(Messages.UpdateMessage(UpdateHelper.cachedLatestVersion, + UpdateHelper.getCurrentVersion())); + } + }, 20L); + } + } + } +} diff --git a/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java b/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java index a3a5992..b237065 100644 --- a/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java +++ b/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java @@ -56,9 +56,15 @@ public class PlayerMoveListener implements Listener } @SuppressWarnings("unused") - @EventHandler(priority = EventPriority.NORMAL) + @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerMove( PlayerMoveEvent e ) { + // do nothing if the event is already cancelled + if (e.isCancelled()) + { + return; + } + // do nothing if player has the ignoreborders permission if ( e.getPlayer().hasPermission("craftinc.borderprotection.ignoreborders") ) { diff --git a/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java b/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java index 679e200..5be1e9a 100644 --- a/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java +++ b/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java @@ -33,9 +33,15 @@ public class PlayerTeleportListener implements Listener } @SuppressWarnings("unused") - @EventHandler(priority = EventPriority.LOWEST) + @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerMove( PlayerTeleportEvent e ) { + // do nothing if the event is already cancelled + if (e.isCancelled()) + { + return; + } + // do nothing if player has the ignoreborders permission if ( e.getPlayer().hasPermission("craftinc.borderprotection.ignoreborders") ) { diff --git a/src/main/java/de/craftinc/borderprotection/Plugin.java b/src/main/java/de/craftinc/borderprotection/Plugin.java index 7e9a948..5510721 100644 --- a/src/main/java/de/craftinc/borderprotection/Plugin.java +++ b/src/main/java/de/craftinc/borderprotection/Plugin.java @@ -48,6 +48,7 @@ public class Plugin extends JavaPlugin BorderManager borderManager = new BorderManager(); PlayerMoveListener playerMoveListener = new PlayerMoveListener(borderManager); PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener(borderManager); + PlayerLoginListener playerLoginListener = new PlayerLoginListener(); // commands Commands commandExecutor = new Commands(borderManager); @@ -57,5 +58,6 @@ public class Plugin extends JavaPlugin PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(playerMoveListener, this); pm.registerEvents(playerTeleportListener, this); + pm.registerEvents(playerLoginListener, this); } } diff --git a/src/main/java/de/craftinc/borderprotection/UpdateHelper.java b/src/main/java/de/craftinc/borderprotection/UpdateHelper.java new file mode 100644 index 0000000..37f9952 --- /dev/null +++ b/src/main/java/de/craftinc/borderprotection/UpdateHelper.java @@ -0,0 +1,98 @@ +/* Craft Inc. BorderProtection + Copyright (C) 2013 Paul Schulze + + 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 3 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, see . +*/ +package de.craftinc.borderprotection; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +public class UpdateHelper +{ + /** + * The URL from which the Plugin tries to get the latest version. + */ + @SuppressWarnings("FieldCanBeLocal") + private static final String updateUrl = "http://www.craftinc.de/plugins/update/craftinc-borderprotection"; + + /** + * The latest version which was seen on last check. + */ + public static String cachedLatestVersion = null; + + + /** + * Gets the latest version from the updateURL and returns it as String. + * + * @return latest version as String. + */ + @SuppressWarnings("StringBufferMayBeStringBuilder") + public static String getLatestVersion() + { + // StringBuffer is thread-safe. Don't know if this is really important here, but safe is safe :). + StringBuffer s = new StringBuffer(); + try + { + URLConnection c = new URL(updateUrl).openConnection(); + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); + String inputLine; + while ( ( inputLine = br.readLine() ) != null ) + { + s.append(inputLine); + } + br.close(); + } + catch ( MalformedURLException e ) + { + Plugin.getPlugin().getLogger().warning("Could not check for latest version. Update URL is malformed."); + } + catch ( IOException e ) + { + Plugin.getPlugin().getLogger().warning("Could not check for latest version. Update URL was not readable."); + } + + // update cached latest version + cachedLatestVersion = s.toString(); + + return s.toString(); + } + + + /** + * Gets the current version of this plugin directly from the plugin.yml version entry. + * + * @return current version as String. + */ + public static String getCurrentVersion() + { + return Plugin.getPlugin().getDescription().getVersion(); + } + + + /** + * Checks if a newer version is available. + * + * @return Boolean + */ + public static Boolean newVersionAvailable() + { + return !getCurrentVersion().equals(getLatestVersion()); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6adfe4f..4642d1c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -33,3 +33,6 @@ permissions: craftinc.borderprotection.ignoreborders: default: op description: Allows to be everywhere on the map (ignoring the borders). + craftinc.borderprotection.update: + default: op + description: Allows to get notified on login, when a new update is available. \ No newline at end of file