User/Owner Buggs fixed, first final version

This commit is contained in:
Mochaccino 2013-03-19 19:18:41 +01:00
parent 28d9e89ab6
commit 8b5e3a4342
5 changed files with 155 additions and 98 deletions

View File

@ -30,15 +30,15 @@ public class BlockPlaceListener implements Listener
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onBlockPlaced( BlockPlaceEvent event ) public void onBlockPlaced( BlockPlaceEvent event )
{ {
ArrayList<Location> replicators = Replicator.getReplicators(event.getBlockPlaced().getLocation()); ArrayList<Location> replicators = Replicator.getReplicatorLocations(event.getBlockPlaced().getLocation());
if ( !replicators.isEmpty() ) if ( !replicators.isEmpty() )
{ {
for ( Location loc : replicators ) for ( Location loc : replicators )
{ {
Replicator rep = Replicator.getOrCreate(loc, event.getPlayer().getName()); Replicator rep = Replicator.getOrCreate(loc, event.getPlayer());
if ( rep != null ) if ( rep != null )
{ {
event.getPlayer().sendMessage(Messages.newReplicator(rep));
} }
} }
} }

View File

@ -79,23 +79,16 @@ public class Commands implements CommandExecutor
if ( args.length == 1 ) if ( args.length == 1 )
{ {
// get block where the player is looking at // get block where the player is looking at
Block potentialReplicatorBlock = player.getTargetBlock(BlockUtil.transparentBlocks, 100); Block potentialReplicatorBlock = player.getTargetBlock(null, 100);
Replicator.mochaccino.sendMessage("You are looking at "+potentialReplicatorBlock.getLocation().getBlockX()+","+potentialReplicatorBlock.getLocation().getBlockY()+","+potentialReplicatorBlock.getLocation().getBlockZ());
// get zero or more valid replicator centers // get zero or more valid replicator centers
ArrayList<Location> replicatorCenters = Replicator ArrayList<Replicator> replicators = Replicator.getReplicators(potentialReplicatorBlock.getLocation());
.getReplicators(potentialReplicatorBlock.getLocation());
if ( replicatorCenters.size() == 0 ) if ( replicators.size() == 0 )
{ {
sender.sendMessage(Messages.noReplicatorInSight); sender.sendMessage(Messages.noReplicatorInSight);
return true; return true;
} }
ArrayList<Replicator> replicators = new ArrayList<Replicator>();
for ( Location replicatorCenter : replicatorCenters )
{
replicators.add(Replicator.getOrCreate(replicatorCenter, player.getName()));
}
sender.sendMessage(Messages.info(replicators)); sender.sendMessage(Messages.info(replicators));
return true; return true;
} }
@ -131,44 +124,36 @@ public class Commands implements CommandExecutor
if ( args.length == 2 ) if ( args.length == 2 )
{ {
// get block where the player is looking at // get block where the player is looking at
Block potentialReplicatorBlock = player.getTargetBlock(BlockUtil.transparentBlocks, 100); Block potentialReplicatorBlock = player.getTargetBlock(null, 100);
// get zero or more valid replicator centers // get zero or more valid replicator centers
ArrayList<Location> replicatorCenters = Replicator ArrayList<Replicator> replicators = Replicator
.getReplicators(potentialReplicatorBlock.getLocation()); .getOwnReplicators(potentialReplicatorBlock.getLocation(),player.getName());
// no replicator in sight // no replicator in sight
if ( replicatorCenters.size() == 0 ) if ( replicators.isEmpty() )
{ {
sender.sendMessage(Messages.noReplicatorInSight); sender.sendMessage(Messages.noReplicatorInSight);
return true; return true;
} }
for ( Replicator replicator : replicators )
for ( Location replicatorCenter : replicatorCenters )
{ {
Replicator replicator = Replicator.getOrCreate(replicatorCenter, player.getName());
if ( replicator == null )
{
sender.sendMessage(Messages.noReplicatorInSight);
continue;
}
if ( args[0].equalsIgnoreCase("addowner") ) if ( args[0].equalsIgnoreCase("addowner") )
{ {
replicator.addOwner(args[1]); replicator.addOwner(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("delowner") ) else if ( args[0].equalsIgnoreCase("delowner") )
{ {
replicator.rmOwner(args[1]); replicator.rmOwner(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("adduser") ) else if ( args[0].equalsIgnoreCase("adduser") )
{ {
replicator.addUser(args[1]); replicator.addUser(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("deluser") ) else if ( args[0].equalsIgnoreCase("deluser") )
{ {
replicator.rmUser(args[1]); replicator.rmUser(args[1],player.getName());
} }
sender.sendMessage(Messages.addedOwner(args[1], replicator)); sender.sendMessage(Messages.addedOwner(args[1], replicator));
} }
return true; return true;
@ -186,19 +171,19 @@ public class Commands implements CommandExecutor
if ( args[0].equalsIgnoreCase("addowner") ) if ( args[0].equalsIgnoreCase("addowner") )
{ {
replicator.addOwner(args[1]); replicator.addOwner(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("delowner") ) else if ( args[0].equalsIgnoreCase("delowner") )
{ {
replicator.rmOwner(args[1]); replicator.rmOwner(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("adduser") ) else if ( args[0].equalsIgnoreCase("adduser") )
{ {
replicator.addUser(args[1]); replicator.addUser(args[1],player.getName());
} }
else if ( args[0].equalsIgnoreCase("deluser") ) else if ( args[0].equalsIgnoreCase("deluser") )
{ {
replicator.rmUser(args[1]); replicator.rmUser(args[1],player.getName());
} }
sender.sendMessage(Messages.addedOwner(player.getName(), replicator)); sender.sendMessage(Messages.addedOwner(player.getName(), replicator));

View File

@ -116,6 +116,8 @@ public class Messages
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(ChatColor.YELLOW + "The following replicators have been found:" + NEWLINE); sb.append(ChatColor.YELLOW + "The following replicators have been found:" + NEWLINE);
if(replicators.size()>0)
{
for ( Replicator r : replicators ) for ( Replicator r : replicators )
{ {
sb.append(ChatColor.GOLD + r.getName() + ":" + NEWLINE); sb.append(ChatColor.GOLD + r.getName() + ":" + NEWLINE);
@ -131,6 +133,7 @@ public class Messages
} }
sb.append(NEWLINE); sb.append(NEWLINE);
} }
}
return sb.toString(); return sb.toString();
} }

View File

@ -25,6 +25,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
@ -51,7 +52,7 @@ public class PlayerInteractEntityListener implements Listener
break; break;
} }
} }
//Replicator.mochaccino.sendMessage("ItemFrame found "+itemFrame.getEntityId());
// if no item frame was found: return! (strange and should NEVER happen as long as the world is not upside down!) // if no item frame was found: return! (strange and should NEVER happen as long as the world is not upside down!)
if ( itemFrame == null ) if ( itemFrame == null )
{ {
@ -65,33 +66,30 @@ public class PlayerInteractEntityListener implements Listener
} }
// find replicator centers which are suitable for this item frame // find replicator centers which are suitable for this item frame
ArrayList<Location> replicatorCenters = Replicator.getReplicators(event.getRightClicked().getLocation()); //Replicator.mochaccino.sendMessage(itemFrame.getAttachedFace() + "");
ArrayList<Replicator> replicators = Replicator.getUsableReplicators(event.getRightClicked().getLocation().getBlock().getRelative(itemFrame.getAttachedFace()).getLocation(),event.getPlayer().getName());
// do nothing if no replicator centers have been found // do nothing if no replicator centers have been found
if ( replicatorCenters.isEmpty() ) if ( replicators.isEmpty() )
{ {
//Replicator.mochaccino.sendMessage("No replicator found.");
return; return;
} }
// get the replicators from database for these centers // get the replicators from database for these centers
for ( Location replicatorCenter : replicatorCenters ) for ( Replicator replicator : replicators )
{ {
Replicator replicator = Replicator.getOrCreate(replicatorCenter, event.getPlayer().getName());
// replicator can be null if the player is not user and not owner
if ( replicator == null )
{
continue;
}
// from here on we have a valid replicator and a valid item // from here on we have a valid replicator and a valid item
// that's why we cancel the event // that's why we cancel the event
event.setCancelled(true); event.setCancelled(true);
// and spawn the items at the center location of the replicator // and spawn the items at the center location of the replicator
ItemStack stack = itemFrame.getItem();
stack.setAmount(64);
event.getPlayer().getWorld().dropItemNaturally( event.getPlayer().getWorld().dropItemNaturally(
replicatorCenter.getBlock().getRelative(Replicator.getDirection(replicatorCenter)).getLocation(), replicator.getCenter().getBlock().getRelative(Replicator.getDirection(replicator.getCenter())).getLocation(),
itemFrame.getItem()); stack);
} }
} }
} }

View File

@ -29,6 +29,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
public class Replicator implements ConfigurationSerializable public class Replicator implements ConfigurationSerializable
@ -43,23 +44,28 @@ public class Replicator implements ConfigurationSerializable
private static final String keyCenter = "center"; private static final String keyCenter = "center";
private static final String keyOwners = "owners"; private static final String keyOwners = "owners";
private static final String keyUsers = "users"; private static final String keyUsers = "users";
private static final Player mochaccino = Plugin.instance.getServer().getPlayer("Mochaccino"); //public static final Player mochaccino = Plugin.instance.getServer().getPlayer("Mochaccino");
/** /**
* List of owners. An owner is able to use the replicator and is able to add other users/owners. * List of owners. An owner is able to use the replicator and is able to add other users/owners.
*/ */
private ArrayList<String> owners; private HashSet<String> owners;
/** /**
* List of users. A user is able to use the replicator. * List of users. A user is able to use the replicator.
*/ */
private ArrayList<String> users; private HashSet<String> users;
/** /**
* Name of the replicator. It will always be in the format "world,x,y,z". * Name of the replicator. It will always be in the format "world,x,y,z".
*/ */
private String name; private String name;
public Location getCenter()
{
return center;
}
/** /**
* Center location of the replicator. * Center location of the replicator.
*/ */
@ -76,11 +82,11 @@ public class Replicator implements ConfigurationSerializable
public Replicator( String firstOwner, Location center ) public Replicator( String firstOwner, Location center )
{ {
this.owners = new ArrayList<String>(); this.owners = new HashSet<String>();
this.users = new ArrayList<String>(); this.users = new HashSet<String>();
this.owners.add(firstOwner); this.owners.add(firstOwner);
this.center = center; this.center = center;
name = center.getWorld() + "," + center.getBlockX() + "," + center.getBlockY() + "," + center.getBlockZ(); name = center.getWorld().getName() + "," + center.getBlockX() + "," + center.getBlockY() + "," + center.getBlockZ();
} }
@SuppressWarnings("unchecked unused") @SuppressWarnings("unchecked unused")
@ -90,8 +96,8 @@ public class Replicator implements ConfigurationSerializable
{ {
name = (String) map.get(keyName); name = (String) map.get(keyName);
center = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(keyCenter)); center = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(keyCenter));
owners = (ArrayList<String>) map.get(keyOwners); owners = (HashSet<String>) map.get(keyOwners);
users = (ArrayList<String>) map.get(keyUsers); users = (HashSet<String>) map.get(keyUsers);
allReplicators.put(center, this); allReplicators.put(center, this);
} }
@ -101,38 +107,50 @@ public class Replicator implements ConfigurationSerializable
} }
} }
public ArrayList<String> getOwners() public HashSet<String> getOwners()
{ {
return owners; return owners;
} }
public ArrayList<String> getUsers() public HashSet<String> getUsers()
{ {
return users; return users;
} }
public void addUser( String user ) public void addUser( String user, String player )
{ {
this.users.add(user); this.users.add(user);
Replicator.saveReplicators(player);
} }
public void addOwner( String owner ) public void addOwner( String owner, String player )
{ {
this.users.add(owner); this.owners.add(owner);
Replicator.saveReplicators(player);
} }
public boolean rmUser( String user ) public boolean rmUser( String user, String player )
{ {
if ( this.users.remove(user) ) if ( this.users.remove(user) )
{
Replicator.saveReplicators(player);
return true; return true;
}
else else
return false; return false;
} }
public boolean rmOwner( String owner ) public boolean rmOwner( String owner, String player )
{ {
if ( this.owners.remove(owner) ) if ( this.owners.remove(owner) )
{
if(owners.isEmpty())
{
allReplicators.remove(this.center);
}
Replicator.saveReplicators(player);
return true; return true;
}
else else
return false; return false;
} }
@ -141,7 +159,7 @@ public class Replicator implements ConfigurationSerializable
{ {
for ( String owner : owners ) for ( String owner : owners )
{ {
if ( owner.equals(player) ) if ( owner.equalsIgnoreCase(player) )
{ {
return true; return true;
} }
@ -153,7 +171,7 @@ public class Replicator implements ConfigurationSerializable
{ {
for ( String user : users ) for ( String user : users )
{ {
if ( user.equals(player) ) if ( user.equalsIgnoreCase(player) )
{ {
return true; return true;
} }
@ -172,7 +190,16 @@ public class Replicator implements ConfigurationSerializable
return name; return name;
} }
public static ArrayList<Location> getReplicators( Location currentBlock ) public boolean isUsable(String player)
{
if ( this.isOwner(player) || this.isUser(player) )
{
return true;
}
return false;
}
public static ArrayList<Location> getReplicatorLocations(Location currentBlock)
{ {
//mochaccino.sendMessage("Hello Mochaccino!"); //mochaccino.sendMessage("Hello Mochaccino!");
ArrayList<Location> replicators = new ArrayList<Location>(); ArrayList<Location> replicators = new ArrayList<Location>();
@ -216,7 +243,7 @@ public class Replicator implements ConfigurationSerializable
private static boolean isValid( Location center ) private static boolean isValid( Location center )
{ {
Material[][][] pattern = getPattern(center); Material[][][] pattern = getPattern(center);
mochaccino.sendMessage("Direction: "+getDirection(center)); //mochaccino.sendMessage("Direction: "+getDirection(center));
if ( pattern == null ) if ( pattern == null )
{ {
return false; return false;
@ -227,17 +254,17 @@ public class Replicator implements ConfigurationSerializable
{ {
for ( int z = 0; z <= 2; z++ ) for ( int z = 0; z <= 2; z++ )
{ {
mochaccino.sendMessage("Expected "+pattern[x][y][z]+", found "+center.getBlock().getRelative(x - 1, y - 1, z - 1).getType()+" at "+x+","+y+","+z+"."); //mochaccino.sendMessage("Expected "+pattern[x][y][z]+", found "+center.getBlock().getRelative(x - 1, y - 1, z - 1).getType()+" at "+x+","+y+","+z+".");
if ( ( pattern[x][y][z] != center.getBlock().getRelative(x - 1, y - 1, z - 1).getType() ) && if ( ( pattern[x][y][z] != center.getBlock().getRelative(x - 1, y - 1, z - 1).getType() ) &&
( ( pattern[x][y][z] != null ) ) ) ( ( pattern[x][y][z] != null ) ) )
{ {
mochaccino.sendMessage("Pattern don't match."); //mochaccino.sendMessage("Pattern don't match.");
return false; return false;
} }
} }
} }
} }
mochaccino.sendMessage("Pattern matched."); //mochaccino.sendMessage("Pattern matched.");
return true; return true;
} }
@ -255,7 +282,7 @@ public class Replicator implements ConfigurationSerializable
if ( nextBlock.getBlock().getType().equals(Pattern.getCenter()) ) if ( nextBlock.getBlock().getType().equals(Pattern.getCenter()) )
{ {
centers.add(nextBlock); centers.add(nextBlock);
mochaccino.sendMessage(nextBlock.getBlock().getType()+" found at "+nextBlock.getBlockX()+","+nextBlock.getBlockY()+","+nextBlock.getBlockZ()); //mochaccino.sendMessage(nextBlock.getBlock().getType()+" found at "+nextBlock.getBlockX()+","+nextBlock.getBlockY()+","+nextBlock.getBlockZ());
} }
} }
} }
@ -268,17 +295,17 @@ public class Replicator implements ConfigurationSerializable
* Returns null if player is not owner or user of the replicator. * Returns null if player is not owner or user of the replicator.
* *
* @param loc center of the replicator * @param loc center of the replicator
* @param playerName name of the player * @param player the player entity
* @return Replicator * @return Replicator
*/ */
public static Replicator getOrCreate( Location loc, String playerName ) public static Replicator getOrCreate( Location loc, Player player )
{ {
Replicator rep = allReplicators.get(loc); Replicator rep = allReplicators.get(loc);
// replicator already exists // replicator already exists
if ( rep != null ) if ( rep != null )
{ {
if ( rep.isOwner(playerName) || rep.isUser(playerName) ) if ( rep.isOwner(player.getName()) || rep.isUser(player.getName()) )
{ {
return rep; return rep;
} }
@ -290,20 +317,56 @@ public class Replicator implements ConfigurationSerializable
// replicator does not exist, create one // replicator does not exist, create one
else else
{ {
rep = new Replicator(playerName, loc); rep = new Replicator(player.getName(), loc);
allReplicators.put(loc, rep); allReplicators.put(loc, rep);
try { player.sendMessage(Messages.newReplicator(rep));
Replicator.saveReplicators(); Replicator.saveReplicators(player.getName());
}
catch ( IOException e )
{
Plugin.instance.getServer().getPlayer(playerName).sendMessage(Messages.couldNotSave);
Plugin.instance.getLogger().severe("Could not save replicators to file: " + e.getMessage());
}
return rep; return rep;
} }
} }
/**
* Returns all Replicators, which includes the Block at Location loc.
* @param loc Location of the current Block
* @return ArrayList of Replicators
*/
public static ArrayList<Replicator> getReplicators(Location loc)
{
ArrayList<Location> locs = getReplicatorLocations(loc);
ArrayList<Replicator> reps = new ArrayList<Replicator>();
for(Location center:locs)
{
if(allReplicators.get(center)!=null)
reps.add(allReplicators.get(center));
}
return reps;
}
public static ArrayList<Replicator> getUsableReplicators(Location loc, String player)
{
ArrayList<Location> locs = getReplicatorLocations(loc);
ArrayList<Replicator> reps = new ArrayList<Replicator>();
for(Location center:locs)
{
Replicator rep = allReplicators.get(center);
if(rep!=null&&rep.isUsable(player))
reps.add(rep);
}
return reps;
}
public static ArrayList<Replicator> getOwnReplicators(Location loc, String player)
{
ArrayList<Location> locs = getReplicatorLocations(loc);
ArrayList<Replicator> reps = new ArrayList<Replicator>();
for(Location center:locs)
{
Replicator rep = allReplicators.get(center);
if(rep!=null&&rep.isOwner(player))
reps.add(rep);
}
return reps;
}
/** /**
* Get a replicator with the specified name. Returns null if player * Get a replicator with the specified name. Returns null if player
* is not owner or user or if replicator does not exist. * is not owner or user or if replicator does not exist.
@ -318,7 +381,7 @@ public class Replicator implements ConfigurationSerializable
{ {
if ( rep.getName().equals(repName) ) if ( rep.getName().equals(repName) )
{ {
if ( rep.isOwner(playerName) || rep.isUser(playerName) ) if(rep.isUsable(playerName))
{ {
return rep; return rep;
} }
@ -347,6 +410,7 @@ public class Replicator implements ConfigurationSerializable
{ {
if ( rep.isUser(playerName) ) if ( rep.isUser(playerName) )
{ {
//mochaccino.sendMessage("You are User for "+rep.getName());
reps.add(rep); reps.add(rep);
} }
} }
@ -371,9 +435,16 @@ public class Replicator implements ConfigurationSerializable
replicatorsFileConf.getList(keyReplicators); replicatorsFileConf.getList(keyReplicators);
} }
public static void saveReplicators() throws IOException public static void saveReplicators(String playerName)
{ {
try {
replicatorsFileConf.set(keyReplicators, new ArrayList<Object>(allReplicators.values())); replicatorsFileConf.set(keyReplicators, new ArrayList<Object>(allReplicators.values()));
replicatorsFileConf.save(replicatorsFile); replicatorsFileConf.save(replicatorsFile);
} }
catch ( IOException e )
{
Plugin.instance.getServer().getPlayer(playerName).sendMessage(Messages.couldNotSave);
Plugin.instance.getLogger().severe("Could not save replicators to file: " + e.getMessage());
}
}
} }