From abec8e11ef138e47b65fec83b7ed7255b8b50893 Mon Sep 17 00:00:00 2001 From: Paul Schulze Date: Tue, 19 Mar 2013 00:03:47 +0100 Subject: [PATCH] unstable --- .../java/de/craftinc/replicator/Commands.java | 71 +++++++++++++- .../java/de/craftinc/replicator/Messages.java | 30 +++++- .../java/de/craftinc/replicator/Plugin.java | 2 +- .../de/craftinc/replicator/Replicator.java | 81 +--------------- .../de/craftinc/replicator/UpdateHelper.java | 97 +++++++++++++++++++ 5 files changed, 198 insertions(+), 83 deletions(-) create mode 100644 src/main/java/de/craftinc/replicator/UpdateHelper.java diff --git a/src/main/java/de/craftinc/replicator/Commands.java b/src/main/java/de/craftinc/replicator/Commands.java index d4a0ab8..3074b3e 100644 --- a/src/main/java/de/craftinc/replicator/Commands.java +++ b/src/main/java/de/craftinc/replicator/Commands.java @@ -24,6 +24,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; public class Commands implements CommandExecutor { @@ -49,6 +50,28 @@ public class Commands implements CommandExecutor return true; } + // checkversion + if ( args.length > 0 && args[0].equalsIgnoreCase("checkversion") ) + { + if ( !sender.hasPermission("craftinc.replicator.update") ) + { + sender.sendMessage(Messages.noPermissionCheckversion); + return false; + } + + if ( UpdateHelper.newVersionAvailable() ) + { + sender.sendMessage( + Messages.updateMessage(UpdateHelper.cachedLatestVersion, UpdateHelper.getCurrentVersion())); + return true; + } + else + { + sender.sendMessage(Messages.noUpdateAvailable); + return true; + } + } + // info if ( args.length > 0 && args[0].equalsIgnoreCase("info") ) { @@ -79,9 +102,55 @@ public class Commands implements CommandExecutor // replicator specified as argument else if ( args.length == 2 ) { - + Replicator rep = Replicator.getByName(args[1], player); + if (rep == null) + { + sender.sendMessage(Messages.noReplicatorWithName(args[1])); + return true; + } + sender.sendMessage(Messages.info(new ArrayList(Arrays.asList(new Replicator[]{rep})))); + return true; } } + + // list + if (args.length == 1 && args[0].equalsIgnoreCase("list")) + { + sender.sendMessage(Messages.list(Replicator.getReplicatorsByOwner(), Replicator.getReplicatorsByUser())); + return true; + } + + // addowner + if (args.length > 1 && args[0].equalsIgnoreCase("addowner")) + { + // looking at replicator + if (args.length == 2) + { + // get block where the player is looking at + Block potentialReplicatorBlock = player.getTargetBlock(BlockUtil.transparentBlocks, 100); + + // get zero or more valid replicator centers + ArrayList replicatorCenters = Replicator + .getReplicators(potentialReplicatorBlock.getLocation()); + + // no replicator in sight + if ( replicatorCenters.size() == 0 ) + { + sender.sendMessage(Messages.noReplicatorInSight); + return true; + } + + ArrayList replicators = new ArrayList(); + for ( Location replicatorCenter : replicatorCenters ) + { + Replicator replicator = Replicator.getOrCreate(); + replicator.addOwner(args[1]); + sender.sendMessage(Messages.addedOwner(args[1], replicator)); + } + return true; + } + } + } diff --git a/src/main/java/de/craftinc/replicator/Messages.java b/src/main/java/de/craftinc/replicator/Messages.java index c7cf7f5..c6c1ac7 100644 --- a/src/main/java/de/craftinc/replicator/Messages.java +++ b/src/main/java/de/craftinc/replicator/Messages.java @@ -109,7 +109,7 @@ public class Messages sb.append(ChatColor.YELLOW + "The following replicators have been found:" + NEWLINE); for (Replicator r: replicators) { - sb.append(ChatColor.GOLD + r.id + " @ " + r.center.getX() + "," + r.center.getZ() + NEWLINE); + sb.append(ChatColor.GOLD + r.id + " @ " + r.center.getX() + "," + r.center.getZ() + ":" + NEWLINE); sb.append(ChatColor.GREEN + "Owners:" + NEWLINE); for (String owner: r.getOwners()) { @@ -125,5 +125,33 @@ public class Messages return sb.toString(); } + public static String list(ArrayList repByOwner, ArrayList repByUser) + { + StringBuilder sb = new StringBuilder(); + sb.append(ChatColor.YELLOW + "Replicators where you are owner:" + NEWLINE); + for (Replicator r: repByOwner) + { + sb.append(ChatColor.WHITE + r.id + " @ " + r.center.getX() + "," + r.center.getZ() + NEWLINE); + } + sb.append(NEWLINE); + + sb.append(ChatColor.YELLOW + "Replicators where you are user:" + NEWLINE); + for (Replicator r: repByUser) + { + sb.append(ChatColor.WHITE + r.id + " @ " + r.center.getX() + "," + r.center.getZ() + NEWLINE); + } + + return sb.toString(); + } + + public static String noReplicatorWithName( String replicatorName ) + { + return ChatColor.RED + "No replicator with name: " + replicatorName + " found or you don't have permission for that replicator."; + } + + public static String addedOwner( String newOwner, Replicator replicator ) + { + return ChatColor.GREEN + "New owner for " + replicator.id + + } } diff --git a/src/main/java/de/craftinc/replicator/Plugin.java b/src/main/java/de/craftinc/replicator/Plugin.java index a0fe719..bab8642 100644 --- a/src/main/java/de/craftinc/replicator/Plugin.java +++ b/src/main/java/de/craftinc/replicator/Plugin.java @@ -27,7 +27,7 @@ public class Plugin extends JavaPlugin @Override public void onLoad() { - //ConfigurationSerialization.registerClass(Replicator.class); + ConfigurationSerialization.registerClass(Replicator.class); } @Override diff --git a/src/main/java/de/craftinc/replicator/Replicator.java b/src/main/java/de/craftinc/replicator/Replicator.java index 8a175ae..e35142d 100644 --- a/src/main/java/de/craftinc/replicator/Replicator.java +++ b/src/main/java/de/craftinc/replicator/Replicator.java @@ -22,23 +22,19 @@ import org.bukkit.block.BlockFace; import org.bukkit.Material; import java.util.ArrayList; -import java.util.HashMap; public class Replicator { private ArrayList owners; private ArrayList users; - private String name; - private static HashMap allReplicators; private Location center; - public Replicator(String firstOwner, Location center) { + public Replicator(String firstOwner, Location spawn, Location center) { this.owners = new ArrayList(); this.users = new ArrayList(); this.owners.add(firstOwner); this.center = center; - name = center.getWorld()+","+center.getBlockX()+","+center.getBlockY()+","+center.getBlockZ(); } public void addUser(String user) { @@ -59,33 +55,6 @@ public class Replicator { else return false; } - public boolean isOwner(String player){ - for(String owner:owners){ - if(owner.equals(player)){ - return true; - } - } - return false; - } - - public boolean isUser(String player){ - for(String user:users){ - if(user.equals(player)){ - return true; - } - } - return false; - } - - public void setName(String newName){ - this.name=newName; - //TODO: Save List - } - - public String getName(){ - return name; - } - public static ArrayList getReplicators(Location currentBlock){ ArrayList replicators = new ArrayList(); ArrayList centers = getCenters(currentBlock); @@ -107,9 +76,6 @@ public class Replicator { private static boolean isValid(Location center){ Material[][][] pattern = getPattern(center); - if(pattern==null){ - return false; - } for(int x=0;x<=2;x++){ for(int y=0;y<=2;y++){ for(int z=0;z<=2;z++){ @@ -136,49 +102,4 @@ public class Replicator { return centers; } - public static Replicator getOrCreate(Location loc, String playerName) { //Returns null, if player is not owner or user of the replicator - Replicator rep = allReplicators.get(loc); - if(rep!=null){ - if(rep.isOwner(playerName)||rep.isUser(playerName)){ - return allReplicators.get(loc); - } - else return null; - } - else { - rep = new Replicator(playerName,loc); - allReplicators.put(loc, rep); - return rep; - } - } - - public static Replicator getByName(String repName, String playerName){ //Returns null, if player is not owner or user or if replicator does not exist - for(Replicator rep: allReplicators.values()){ - if(rep.getName().equals(repName)){ - if(rep.isOwner(playerName)||rep.isUser(playerName)){ - return rep; - } - } - } - return null; - } - - public ArrayList getReplicatorsByOwner(String playerName){ - ArrayList reps = new ArrayList(); - for(Replicator rep: allReplicators.values()){ - if(rep.isOwner(playerName)){ - reps.add(rep); - } - } - return reps; - } - - public ArrayList getReplicatorsByUser(String playerName){ - ArrayList reps = new ArrayList(); - for(Replicator rep: allReplicators.values()){ - if(rep.isUser(playerName)){ - reps.add(rep); - } - } - return reps; - } } diff --git a/src/main/java/de/craftinc/replicator/UpdateHelper.java b/src/main/java/de/craftinc/replicator/UpdateHelper.java new file mode 100644 index 0000000..233f0e0 --- /dev/null +++ b/src/main/java/de/craftinc/replicator/UpdateHelper.java @@ -0,0 +1,97 @@ +/* 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.replicator; + +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.instance.getLogger().warning("Could not check for latest version. Update URL is malformed."); + } + catch ( IOException e ) + { + Plugin.instance.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.instance.getDescription().getVersion(); + } + + + /** + * Checks if a newer version is available. + * + * @return Boolean + */ + public static Boolean newVersionAvailable() + { + return !getCurrentVersion().equals(getLatestVersion()); + } +}