unstable
This commit is contained in:
parent
eecd72782f
commit
abec8e11ef
@ -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<Replicator>(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<Location> replicatorCenters = Replicator
|
||||
.getReplicators(potentialReplicatorBlock.getLocation());
|
||||
|
||||
// no replicator in sight
|
||||
if ( replicatorCenters.size() == 0 )
|
||||
{
|
||||
sender.sendMessage(Messages.noReplicatorInSight);
|
||||
return true;
|
||||
}
|
||||
|
||||
ArrayList<Replicator> replicators = new ArrayList<Replicator>();
|
||||
for ( Location replicatorCenter : replicatorCenters )
|
||||
{
|
||||
Replicator replicator = Replicator.getOrCreate();
|
||||
replicator.addOwner(args[1]);
|
||||
sender.sendMessage(Messages.addedOwner(args[1], replicator));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<Replicator> repByOwner, ArrayList<Replicator> 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 +
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class Plugin extends JavaPlugin
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
//ConfigurationSerialization.registerClass(Replicator.class);
|
||||
ConfigurationSerialization.registerClass(Replicator.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<String> owners;
|
||||
private ArrayList<String> users;
|
||||
private String name;
|
||||
private static HashMap<Location,Replicator> allReplicators;
|
||||
|
||||
private Location center;
|
||||
|
||||
public Replicator(String firstOwner, Location center) {
|
||||
public Replicator(String firstOwner, Location spawn, Location center) {
|
||||
this.owners = new ArrayList<String>();
|
||||
this.users = new ArrayList<String>();
|
||||
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<Location> getReplicators(Location currentBlock){
|
||||
ArrayList<Location> replicators = new ArrayList<Location>();
|
||||
ArrayList<Location> 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<Replicator> getReplicatorsByOwner(String playerName){
|
||||
ArrayList<Replicator> reps = new ArrayList<Replicator>();
|
||||
for(Replicator rep: allReplicators.values()){
|
||||
if(rep.isOwner(playerName)){
|
||||
reps.add(rep);
|
||||
}
|
||||
}
|
||||
return reps;
|
||||
}
|
||||
|
||||
public ArrayList<Replicator> getReplicatorsByUser(String playerName){
|
||||
ArrayList<Replicator> reps = new ArrayList<Replicator>();
|
||||
for(Replicator rep: allReplicators.values()){
|
||||
if(rep.isUser(playerName)){
|
||||
reps.add(rep);
|
||||
}
|
||||
}
|
||||
return reps;
|
||||
}
|
||||
}
|
||||
|
97
src/main/java/de/craftinc/replicator/UpdateHelper.java
Normal file
97
src/main/java/de/craftinc/replicator/UpdateHelper.java
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 <code>String</code>.
|
||||
*
|
||||
* @return latest version as <code>String</code>.
|
||||
*/
|
||||
@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 <code>String</code>.
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user