TeamVento/src/main/java/eu/univento/teamvento/plot/Plot.java

156 lines
4.1 KiB
Java

/*
* Copyright (c) 2018 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.teamvento.plot;
import eu.univento.core.Core;
import eu.univento.core.api.player.CustomPlayer;
import eu.univento.core.api.schematic.Cuboid;
import io.vertx.core.json.JsonObject;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.WeatherType;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import java.util.UUID;
/**
* @author joethei
* @version 1.0
*/
public class Plot {
private JsonObject json;
private final Cuboid area;
private final UUID owner;
private final Location spawn;
public Plot(Cuboid area, UUID owner, Location spawn) {
this.area = area;
this.owner = owner;
this.spawn = spawn;
}
public Plot(JsonObject json) {
this.json = json;
this.area = new Cuboid(getLocation("minPos"), getLocation("maxPos"));
this.owner = UUID.fromString(json.getString("uuid"));
this.spawn = getLocation("Spawn");
}
private void update() {
Core.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("plots", new JsonObject().put("uuid", getOwner().toString()), null, res -> {
if(res.succeeded()) {
this.json = res.result();
}else res.cause().printStackTrace();
});
}
public boolean isInPlot(Location location) {
return area.containsLocation(location);
}
public boolean isOwner(CustomPlayer p) {
return p.getUniqueId().equals(owner);
}
public Cuboid getArea() {
return area;
}
public UUID getOwner() {
return owner;
}
public Location getSpawn() {
return spawn;
}
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(json.getString("biome"));
}
public String getName() {
return json.getString("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(json.getString("time"));
}
public void setWeather(Weather weather) {
setInDatabase("weather", weather.name());
}
public Weather getWeather() {
return Weather.valueOf(json.getString("weather"));
}
public void setContact(String contact) {
setInDatabase("contact", contact);
}
public String getContact() {
return json.getString("contact");
}
public boolean hasContact() {
return !getContact().equals("");
}
public void setReady() {
setInDatabase("ready", true);
}
public boolean isReady() {
return json.getBoolean("ready");
}
public WeatherType getWeatherType() {
if(getWeather() == Weather.THUNDER) return WeatherType.DOWNFALL;
return WeatherType.valueOf(getWeather().name());
}
private Location getLocation(String name) {
JsonObject location = json.getJsonObject(name);
return new Location(Bukkit.getWorld("plots"), location.getDouble("X"), location.getDouble("Y"), location.getDouble("Z"));
}
private void setInDatabase(String name, Object obj) {
Core.getCommons().getDatabaseManager().getMongoDB().getClient().findOneAndUpdate("plots", new JsonObject().put("uuid", getOwner().toString()), new JsonObject().put("$set", new JsonObject().put(name, obj)), event -> {
if(event.failed()) {
event.cause().printStackTrace();
}
});
update();
}
}