2016-04-30 07:38:37 +02:00
|
|
|
package eu.univento.teamvento.utils;
|
|
|
|
|
2016-07-09 13:19:56 +02:00
|
|
|
import com.mongodb.CursorType;
|
|
|
|
import com.mongodb.client.FindIterable;
|
|
|
|
import eu.univento.core.Core;
|
2016-04-30 07:38:37 +02:00
|
|
|
import eu.univento.core.api.map.Cuboid;
|
|
|
|
import eu.univento.core.api.player.CustomPlayer;
|
2016-07-09 13:19:56 +02:00
|
|
|
import eu.univento.teamvento.TeamVento;
|
|
|
|
import org.bson.Document;
|
|
|
|
import org.bukkit.*;
|
|
|
|
import org.bukkit.block.Biome;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
2016-04-30 07:38:37 +02:00
|
|
|
|
2016-07-09 13:19:56 +02:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
2016-04-30 07:38:37 +02:00
|
|
|
|
|
|
|
public class Plot {
|
|
|
|
|
|
|
|
private Cuboid area;
|
|
|
|
private UUID owner;
|
|
|
|
private Location spawn;
|
|
|
|
|
|
|
|
public Plot(Cuboid area, UUID owner, Location spawn) {
|
|
|
|
this.area = area;
|
|
|
|
this.owner = owner;
|
|
|
|
this.spawn = spawn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isInPlot(Location location) {
|
|
|
|
return area.containsLocation(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOwner(CustomPlayer p) {
|
2016-07-09 13:19:56 +02:00
|
|
|
return p.getUniqueId().toString().equals(owner.toString());
|
2016-04-30 07:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Cuboid getArea() {
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
|
|
|
public UUID getOwner() {
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Location getSpawn() {
|
|
|
|
return spawn;
|
|
|
|
}
|
|
|
|
|
2016-07-09 13:19:56 +02:00
|
|
|
public void setBiome(Biome biome) {
|
|
|
|
setInDatabase("biome", biome.name());
|
|
|
|
for(Block blocks : getArea())
|
|
|
|
blocks.setBiome(biome);
|
|
|
|
for(Chunk chunk : Bukkit.getWorld("plots").getLoadedChunks())
|
|
|
|
Bukkit.getWorld("plots").refreshChunk(chunk.getX(), chunk.getZ());
|
|
|
|
for(LivingEntity entity : Bukkit.getWorld("plots").getLivingEntities())
|
|
|
|
entity.teleport(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Biome getBiome() {
|
|
|
|
return Biome.valueOf(getStringFromDatabase("biome"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return getStringFromDatabase("name");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
setInDatabase("name", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasName() {
|
|
|
|
return !getName().equals("");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTime(Time time) {
|
|
|
|
setInDatabase("time", time.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Time getTime() {
|
|
|
|
return Time.valueOf(getStringFromDatabase("time"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setWeather(Weather weather) {
|
|
|
|
setInDatabase("weather", weather.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Weather getWeather() {
|
|
|
|
return Weather.valueOf(getStringFromDatabase("weather"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setContact(String contact) {
|
|
|
|
setInDatabase("contact", contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getContact() {
|
|
|
|
return getStringFromDatabase("contact");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasContact() {
|
|
|
|
return !getContact().equals("");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setResourcePack(ResourcePack pack) {
|
|
|
|
setInDatabase("pack", pack.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourcePack getResourcePack() {
|
|
|
|
return ResourcePack.valueOf(getStringFromDatabase("pack"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addAnimal(Entity animal) {
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("type", animal.getType().name());
|
|
|
|
map.put("X", String.valueOf(animal.getLocation().getX()));
|
|
|
|
map.put("Y", String.valueOf(animal.getLocation().getY()));
|
|
|
|
map.put("Z", String.valueOf(animal.getLocation().getZ()));
|
|
|
|
ArrayList<Map<String, String>> animals = (ArrayList<Map<String, String>>) getObjectFromDatbase("animals");
|
|
|
|
animals.add(map);
|
|
|
|
setInDatabase("animals", animals);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAnimal(Entity animal) {
|
|
|
|
Map<Location, EntityType> animals = getAnimalsMap();
|
|
|
|
|
|
|
|
//ArrayList<HashMap<String, String>> animals = (ArrayList<HashMap<String, String>>) getObjectFromDatbase("animals");
|
|
|
|
if(animals.containsKey(animal.getLocation())) {
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("type", animal.getType().name());
|
|
|
|
map.put("X", String.valueOf(animal.getLocation().getX()));
|
|
|
|
map.put("Y", String.valueOf(animal.getLocation().getY()));
|
|
|
|
map.put("Z", String.valueOf(animal.getLocation().getZ()));
|
|
|
|
ArrayList<Map<String, String>> list = (ArrayList<Map<String, String>>) getObjectFromDatbase("animals");
|
|
|
|
list.remove(map);
|
|
|
|
setInDatabase("animals", list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Entity> getAnimals() {
|
|
|
|
ArrayList<Entity> animals = new ArrayList<>();
|
|
|
|
Map<Location, EntityType> map = getAnimalsMap();
|
|
|
|
for(Map.Entry<Location, EntityType> entry : map.entrySet())
|
|
|
|
animals.addAll(Bukkit.getWorld("plots").getLivingEntities().stream().filter(entity -> entity.getLocation().equals(entry.getKey())).collect(Collectors.toList()));
|
|
|
|
return animals;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<Location, EntityType> getAnimalsMap() {
|
|
|
|
Map<Location, EntityType> map = new HashMap<>();
|
|
|
|
ArrayList<Map<String, String>> list = (ArrayList<Map<String, String>>) getObjectFromDatbase("animals");
|
|
|
|
assert list != null;
|
|
|
|
for(Map<String, String> stringlist : list) {
|
|
|
|
double x = Double.parseDouble(stringlist.get("X"));
|
|
|
|
double y = Double.parseDouble(stringlist.get("Y"));
|
|
|
|
double z = Double.parseDouble(stringlist.get("Z"));
|
|
|
|
Location loc = new Location(Bukkit.getWorld("plots"), x, y , z);
|
|
|
|
map.put(loc, EntityType.valueOf(String.valueOf(stringlist.get("type"))));
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public WeatherType getWeatherType() {
|
|
|
|
if(getWeather() == Weather.THUNDER) return WeatherType.DOWNFALL;
|
|
|
|
return WeatherType.valueOf(getWeather().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, Object> getMapFromDatabase(String name) {
|
|
|
|
return (Map<String, Object>) getObjectFromDatbase(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<Object> getArrayListFromDatabase(String name) {
|
|
|
|
return (ArrayList<Object>) getObjectFromDatbase(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Object getObjectFromDatbase(String name) {
|
|
|
|
FindIterable<Document> cursor = Core.getMongoDB().getDatabase().getCollection("plots").find(new Document("uuid", owner.toString()));
|
|
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
|
|
|
|
Document doc = cursor.first();
|
|
|
|
if (doc == null) return null;
|
|
|
|
|
|
|
|
return doc.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getStringFromDatabase(String name) {
|
|
|
|
FindIterable<Document> cursor = Core.getMongoDB().getDatabase().getCollection("plots").find(new Document("uuid", owner.toString()));
|
|
|
|
cursor.cursorType(CursorType.NonTailable);
|
|
|
|
|
|
|
|
Document doc = cursor.first();
|
|
|
|
if (doc == null) return null;
|
|
|
|
|
|
|
|
return doc.getString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setInDatabase(String name, String obj) {
|
|
|
|
Core.getMongoDB().getDatabase().getCollection("plots").updateOne(new Document("uuid", owner.toString()), new Document("$set", new Document(name, obj)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setInDatabase(String name, ArrayList<Map<String, String>> obj) {
|
|
|
|
Core.getMongoDB().getDatabase().getCollection("plots").updateOne(new Document("uuid", owner.toString()), new Document("$set", new Document(name, obj)));
|
|
|
|
}
|
2016-04-30 07:38:37 +02:00
|
|
|
}
|