BlockPlaceListener and Replicator functions getBy...

This commit is contained in:
Mochaccino 2013-03-18 23:52:45 +01:00
parent 1335b437b7
commit eecd72782f
4 changed files with 122 additions and 2 deletions

View File

@ -0,0 +1,39 @@
/* Craft Inc. Replicator
Copyright (C) 2013 Paul Schulze, Maximilian Häckel, Moritz Kaltofen
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 org.bukkit.Location;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import java.util.ArrayList;
public class BlockPlaceListener implements Listener {
public void onBlockPlaced(BlockPlaceEvent event){
ArrayList<Location> replicators = Replicator.getReplicators(event.getBlockPlaced().getLocation());
if(!replicators.isEmpty()){
for(Location loc:replicators){
Replicator rep = Replicator.getOrCreate(loc,event.getPlayer().getName());
if(rep!=null){
event.getPlayer().sendMessage(Messages.newReplicator(rep));
}
}
}
}
}

View File

@ -124,4 +124,6 @@ public class Messages
}
return sb.toString();
}
}

View File

@ -27,7 +27,7 @@ public class Plugin extends JavaPlugin
@Override
public void onLoad()
{
ConfigurationSerialization.registerClass(Replicator.class);
//ConfigurationSerialization.registerClass(Replicator.class);
}
@Override

View File

@ -22,19 +22,23 @@ 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 spawn, Location center) {
public Replicator(String firstOwner, 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) {
@ -55,6 +59,33 @@ 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);
@ -76,6 +107,9 @@ 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++){
@ -102,4 +136,49 @@ 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;
}
}