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

455 lines
22 KiB
Java

package eu.univento.teamvento.plot;
import eu.univento.commons.player.rank.Rank;
import eu.univento.core.Core;
import eu.univento.core.api.player.CustomPlayer;
import eu.univento.core.api.schematic.Cuboid;
import eu.univento.teamvento.TeamVento;
import io.vertx.core.json.JsonObject;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
/**
* @author joethei
* @version 1.0
*/
public class PlotManager {
private static Queue<PlotAction> queue = new LinkedBlockingDeque<>();
private static final ArrayList<Plot> plots = new ArrayList<>();
private static Plot lastPlot;
public static ArrayList<Plot> getPlots() {
return plots;
}
public static Plot getLastPlot() {
return lastPlot;
}
public static void setLastPlot(Plot plot) {
lastPlot = plot;
}
public static void update() {
Core.getCommons().getDatabaseManager().getMongoDB().getClient().find("plots", null, res -> {
if(res.succeeded()) {
for(JsonObject json : res.result()) {
String uuid = json.getString("uuid");
Plot plot = new Plot(new Cuboid(getLocation(uuid, "minPos"), getLocation(uuid, "maxPos")), UUID.fromString(uuid), getLocation(uuid, "Spawn"));
plots.add(plot);
plot.setBiome(plot.getBiome());
}
}else res.cause().printStackTrace();
});
}
public static Plot getPlotByPlayer(CustomPlayer p) {
for (Plot plot : plots) {
if (plot.isOwner(p)) return plot;
}
return null;
}
public static boolean hasPlot(CustomPlayer p) {
return getPlotByPlayer(p) != null;
}
public static void createPlot(CustomPlayer p) {
if(queue.isEmpty()) {
new PlotAction.Create(p).execute();
}else {
queue.offer(new PlotAction.Create(p));
p.sendMessage("Du wurdest zur Warteschlange hinzugefügt: (" + queue.size() + ")");
}
}
public static void resetPlot(Plot plot) {
if(queue.isEmpty()) {
new PlotAction.Reset(plot).execute();
}else {
queue.offer(new PlotAction.Reset(plot));
}
}
static void clearPlot(Plot plot) {
Cuboid area = plot.getArea();
Location lower = new Location(Bukkit.getWorld("plots"), area.getLowerX(), area.getLowerY() + 17.0D, area.getLowerZ());
Location upper = new Location(Bukkit.getWorld("plots"), area.getUpperX(), area.getLowerY() + 20.0D, area.getUpperZ());
Cuboid blocks = new Cuboid(lower, upper);
Cuboid highest = new Cuboid(upper, new Location(Bukkit.getWorld("plots"), area.getLowerX(), 250.0D, area.getLowerZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : highest.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.AIR), i / 3000);
}
});
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : blocks.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.AIR), i / 45);
}
});
Cuboid bottom = new Cuboid(new Location(Bukkit.getWorld("plots"), area.getLowerX(), 0.0D, area.getLowerZ()), new Location(Bukkit.getWorld("plots"), area.getUpperX(), 0.0D, area.getUpperZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : bottom.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.AIR), i / 90);
}
});
Cuboid between = new Cuboid(new Location(Bukkit.getWorld("plots"), bottom.getLowerX(), bottom.getLowerY() + 1.0D, bottom.getLowerZ()), new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getLowerY() - 1.0D, blocks.getUpperZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : between.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.AIR), i / 200);
}
});
}
static void generatePlot(Plot plot) {
lastPlot = plot;
Cuboid area = plot.getArea();
Location lower = new Location(Bukkit.getWorld("plots"), area.getLowerX(), area.getLowerY() + 17.0D, area.getLowerZ());
Location upper = new Location(Bukkit.getWorld("plots"), area.getUpperX(), area.getLowerY() + 20.0D, area.getUpperZ());
Cuboid blocks = new Cuboid(lower, upper);
plot.setBiome(Biome.FOREST);
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : blocks.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.DIRT), i / 16);
}
});
Cuboid bottom = new Cuboid(new Location(Bukkit.getWorld("plots"), area.getLowerX(), 0.0D, area.getLowerZ()), new Location(Bukkit.getWorld("plots"), area.getUpperX(), 0.0D, area.getUpperZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : bottom.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.BEDROCK), i / 100);
}
});
Cuboid between = new Cuboid(new Location(Bukkit.getWorld("plots"), bottom.getLowerX(), bottom.getLowerY() + 1.0D, bottom.getLowerZ()), new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getLowerY() - 1.0D, blocks.getUpperZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : between.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.STONE), i / 90);
}
});
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> blocks.getUpperLocation().getBlock().setType(Material.GRASS), 35 * 20L));
Cuboid rand1 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getUpperY() + 1.0D, blocks.getUpperZ()), new Location(Bukkit.getWorld("plots"), blocks.getUpperX() - 50.0D, blocks.getUpperY() + 1.0D, blocks.getUpperZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : rand1.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> {
b.setType(Material.WOOD_STEP);
b.setData((byte) 1);
}, i);
}
});
Cuboid rand2 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getUpperY() + 1.0D, blocks.getUpperZ()), new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getUpperY() + 1.0D, blocks.getUpperZ() - 50.0D));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : rand2.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> {
b.setType(Material.WOOD_STEP);
b.setData((byte) 1);
}, i);
}
});
Cuboid rand3 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getLowerX(), blocks.getUpperY() + 1.0D, blocks.getLowerZ()), new Location(Bukkit.getWorld("plots"), blocks.getLowerX(), blocks.getUpperY() + 1.0D, blocks.getLowerZ() + 50.0D));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : rand3.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> {
b.setType(Material.WOOD_STEP);
b.setData((byte) 1);
}, i);
}
});
Cuboid rand4 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getLowerX(), blocks.getUpperY() + 1.0D, blocks.getLowerZ()), new Location(Bukkit.getWorld("plots"), blocks.getLowerX() + 50.D, blocks.getUpperY() + 1.0D, blocks.getLowerZ()));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : rand4.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> {
b.setType(Material.WOOD_STEP);
b.setData((byte) 1);
}, i);
}
});
Cuboid street1 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getUpperX() + 1.0D, blocks.getUpperY(), blocks.getUpperZ() + 4.0D), new Location(Bukkit.getWorld("plots"), blocks.getUpperX() + 4.0D, 0.0D, blocks.getUpperZ() - 54.0D));
Bukkit.getScheduler().runTaskLaterAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : street1.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.COAL_BLOCK), i / 16);
}
}, 35 * 20L);
Cuboid street2 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getLowerX() - 1.0D, blocks.getUpperY(), blocks.getLowerZ() - 4.0D), new Location(Bukkit.getWorld("plots"), blocks.getLowerX() - 4.0D, 0.0D, blocks.getLowerZ() + 54.0D));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : street2.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.COAL_BLOCK), i / 16);
}
});
Cuboid street3 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getUpperY(), blocks.getUpperZ() + 1.0D), new Location(Bukkit.getWorld("plots"), blocks.getLowerX(), 0.0D, blocks.getLowerZ() + 54.0D));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : street3.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.COAL_BLOCK), i / 16);
}
});
Cuboid street4 = new Cuboid(new Location(Bukkit.getWorld("plots"), blocks.getUpperX(), blocks.getUpperY(), blocks.getUpperZ() - 54.0D), new Location(Bukkit.getWorld("plots"), blocks.getLowerX(), 0.0D, blocks.getLowerZ() - 1.0D));
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> {
long i = 0;
for (Block b : street4.getBlocks()) {
i++;
Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> b.setType(Material.COAL_BLOCK), i / 16);
}
});
Bukkit.getScheduler().runTaskAsynchronously(TeamVento.getInstance(), () -> Bukkit.getScheduler().scheduleSyncDelayedTask(TeamVento.getInstance(), () -> {
ItemStack menu = new ItemStack(Material.WATCH);
ItemMeta menuMeta = menu.getItemMeta();
menuMeta.setDisplayName("§eErweiterungen");
menu.setItemMeta(menuMeta);
Bukkit.getPlayer(plot.getOwner()).getInventory().setItem(8, menu);
}, 35 * 20L));
if(!queue.isEmpty()) queue.peek().execute();
}
static Plot getPlotPosition(CustomPlayer p) {
if (lastPlot == null) {
Bukkit.broadcastMessage("Erstes Plot");
Location lower = new Location(Bukkit.getWorld("plots"), Bukkit.getWorld("plots").getSpawnLocation().getX(), Bukkit.getWorld("plots").getSpawnLocation().getY() - 20.0D, Bukkit.getWorld("plots").getSpawnLocation().getZ());
Location upper = new Location(Bukkit.getWorld("plots"), lower.getX() + 50.0D, lower.getY() + 250.0D, lower.getZ() + 50.0D);
Cuboid area = new Cuboid(upper, lower);
return new Plot(area, p.getUniqueId(), new Location(Bukkit.getWorld("plots"), upper.getX() + 1.0D, lower.getY() + 25.0D, lower.getZ() + 25.0D));
}
Location upper = lastPlot.getArea().getUpperLocation();
Location lower = lastPlot.getArea().getLowerLocation();
Location west = new Location(Bukkit.getWorld("plots"), lower.getX() - 25.0D, lower.getY() + 25.0D, lower.getZ() + 25.0D);
Location east = new Location(Bukkit.getWorld("plots"), upper.getX() + 25.0D, lower.getY() + 25.0D, lower.getZ() + 25.0D);
Location north = new Location(Bukkit.getWorld("plots"), lower.getX() + 25.0D, lower.getY() + 25.0D, lower.getZ() - 25.0D);
Location south = new Location(Bukkit.getWorld("plots"), upper.getX() - 25.0D, lower.getY() + 25.0D, lower.getZ() + 75.0D);
west.getBlock().setType(Material.GLASS);
east.getBlock().setType(Material.GRASS_PATH);
north.getBlock().setType(Material.STONE);
south.getBlock().setType(Material.ACACIA_STAIRS);
boolean westPlot = isPlot(west);
boolean eastPlot = isPlot(east);
boolean northPlot = isPlot(north);
boolean southPlot = isPlot(south);
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Westen");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Norden");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Osten");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Osten 2");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Süden");
upper.setZ(upper.getZ() + 55);
lower.setZ(lower.getZ() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Süden 2");
upper.setZ(upper.getZ() + 55);
lower.setZ(upper.getZ() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Westen 2");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Norden 2");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Norden 3");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Osten 3");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Osten 4");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Süden 3");
upper.setZ(upper.getZ() + 55);
lower.setZ(lower.getZ() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Westen 3");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
if (westPlot && eastPlot && northPlot && southPlot) {
Bukkit.broadcastMessage("Plot im Westen 4");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
return getPlot(p.getUniqueId(), upper, lower);
}
return null;
}
private static Plot getPlot(UUID uuid, Location upper, Location lower) {
Cuboid area = new Cuboid(upper, lower);
Location spawn = new Location(Bukkit.getWorld("plots"), lower.getX() + 52.0D, lower.getY() + 25.0D, lower.getZ() + 25.0D);
Plot plot = new Plot(area, uuid, spawn);
plots.add(plot);
return plot;
}
static void addPlotToDatabase(Plot plot) {
JsonObject json = new JsonObject().put("uuid", plot.getOwner().toString());
Date date = new Date();
HashMap<String, Object> maxPos = new HashMap<>();
maxPos.put("X", plot.getArea().getUpperX());
maxPos.put("Y", plot.getArea().getUpperY());
maxPos.put("Z", plot.getArea().getUpperZ());
HashMap<String, Object> minPos = new HashMap<>();
minPos.put("X", plot.getArea().getLowerX());
minPos.put("Y", plot.getArea().getLowerY());
minPos.put("Z", plot.getArea().getLowerZ());
HashMap<String, Object> spawn = new HashMap<>();
spawn.put("X", plot.getSpawn().getX());
spawn.put("Y", plot.getSpawn().getY());
spawn.put("Z", plot.getSpawn().getZ());
json.put("created", date).put("maxPos", new JsonObject(maxPos)).put("minPos", new JsonObject(minPos)).put("Spawn", new JsonObject(spawn)).put("time", Time.DAY.name())
.put("weather", Weather.CLEAR.name()).put("biome", Biome.PLAINS.name()).put("name", "§cunbenanntes Plot").put("contact", "").put("ready", false);
Core.getCommons().getDatabaseManager().getMongoDB().getClient().insert("plots", json, res -> {
if(res.failed()) res.cause().printStackTrace();
});
}
private static boolean isPlot(Location location) {
return getPlotByLocation(location) != null;
}
public static Plot getPlotByLocation(Location location) {
for (Plot plot : plots)
if (plot.isInPlot(location)) return plot;
return null;
}
public static Plot getPlotByOwner(String owner) {
for (Plot plot : plots)
if (plot.getOwner().equals(UUID.fromString(owner))) return plot;
return null;
}
public static CompletableFuture<Instant> getCreatedDate(String uuid) {
return getDateFromDatabase(uuid);
}
private static Location getLocation(String uuid, String name) {
Map<String, Object> map = getMapFromDatabase(uuid, name);
return new Location(Bukkit.getWorld("plots"), Double.valueOf(String.valueOf(map.get("X"))), Double.valueOf(String.valueOf(map.get("Y"))), Double.valueOf(String.valueOf(map.get("Z"))));
}
public static boolean isAllowedToBuild(CustomPlayer p, Location loc) {
if(loc.getWorld().getName().equalsIgnoreCase("plots")) {
if (p.getDatabasePlayer().isAllowed(Rank.SrDeveloper)) return true;
Plot plot = getPlotByLocation(loc);
return plot != null && plot.isOwner(p);
}else {
return true;
}
}
private static CompletableFuture<Instant> getDateFromDatabase(String uuid) {
CompletableFuture<Instant> future = new CompletableFuture<>();
Core.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("plots", new JsonObject().put("uuid", uuid), null, res -> {
if(res.succeeded()) future.complete(res.result().getInstant("created"));
else res.cause().printStackTrace();
});
return future;
}
private static CompletableFuture<Object> getObjectFromDatbase(String uuid, String name) {
CompletableFuture<Object> future = new CompletableFuture<>();
Core.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("plots", new JsonObject().put("uuid", uuid), null, res -> {
if(res.succeeded()) future.complete(res.result().getValue(name));
else res.cause().printStackTrace();
});
return future;
}
private static Map<String, Object> getMapFromDatabase(String uuid, String name) {
return (Map<String, Object>) getObjectFromDatbase(uuid, name);
}
}