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

416 lines
20 KiB
Java

package eu.univento.teamvento.utils;
import com.mongodb.BasicDBObject;
import com.mongodb.CursorType;
import com.mongodb.client.FindIterable;
import eu.univento.core.Core;
import eu.univento.core.api.map.Cuboid;
import eu.univento.core.api.player.CustomPlayer;
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.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
public class PlotManager {
private static boolean reset = false;
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() {
FindIterable<Document> cursor = Core.getCommons().getDatabaseManager().getMongoDB().getDatabase().getCollection("plots").find();
for (Document doc : cursor) {
String uuid = doc.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());
}
}
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 clearPlot(Plot plot) {
CustomPlayer p = CustomPlayer.getPlayer(plot.getOwner());
if (reset) {
p.sendMessage("§cEs wird bereits grade ein Plot bearbeitet.");
p.sendMessage("§cBitte versuche es später noch einmal.");
return;
}
reset = true;
plot.getAnimals().forEach(plot::removeAnimal);
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);
}
});
Bukkit.getScheduler().runTaskLaterAsynchronously(TeamVento.getInstance(), () -> reset = false, 13 * 20L);
}
public 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);
plots.add(plot);
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));
}
public static Plot createPlot(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);
Plot westPlot = getPlotByLocation(west);
Plot eastPlot = getPlotByLocation(east);
Plot northPlot = getPlotByLocation(north);
Plot southPlot = getPlotByLocation(south);
if (westPlot == null && eastPlot == null && northPlot == null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Westen");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
}
if (westPlot == null && eastPlot != null && northPlot == null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Norden");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
}
if (westPlot == null && eastPlot == null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Osten");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
}
if (westPlot != null && eastPlot == null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Osten 2");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
}
if (westPlot != null && eastPlot == null && northPlot == null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Süden");
upper.setZ(upper.getZ() + 55);
lower.setZ(lower.getZ() + 55);
}
if (westPlot != null && eastPlot == null && northPlot != null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Süden 2");
upper.setZ(upper.getZ() + 55);
lower.setZ(upper.getZ() + 55);
}
if (westPlot == null && eastPlot == null && northPlot != null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Westen 2");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
}
if (westPlot == null && eastPlot != null && northPlot == null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Norden 2");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
}
if (westPlot == null && eastPlot != null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Norden 3");
upper.setZ(upper.getZ() - 55);
lower.setZ(lower.getZ() - 55);
}
if (westPlot == null && eastPlot == null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Osten 3");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
}
if (westPlot != null && eastPlot == null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Osten 4");
upper.setX(upper.getX() - 55);
lower.setX(lower.getX() - 55);
}
if (westPlot != null && eastPlot == null && northPlot == null && southPlot != null) {
Bukkit.broadcastMessage("Plot im Süden 3");
upper.setZ(upper.getZ() + 55);
lower.setZ(lower.getZ() + 55);
}
if (westPlot == null && eastPlot == null && northPlot != null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Westen 3");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
}
if (westPlot == null && eastPlot != null && northPlot != null && southPlot == null) {
Bukkit.broadcastMessage("Plot im Westen 4");
upper.setX(upper.getX() + 55);
lower.setX(lower.getX() + 55);
}
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);
return new Plot(area, p.getUniqueId(), spawn);
}
public static void createPlot(Plot plot) {
Document doc = new Document("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());
ArrayList<Map<String, String>> animals = new ArrayList<>();
doc.put("created", date);
doc.put("maxPos", new BasicDBObject(maxPos));
doc.put("minPos", new BasicDBObject(minPos));
doc.put("Spawn", new BasicDBObject(spawn));
doc.put("time", "DAY");
doc.put("weather", "CLEAR");
doc.put("biome", "PLAINS");
doc.put("pack", "Default");
doc.put("name", "");
doc.put("contact", "");
doc.put("animals", animals);
Core.getCommons().getDatabaseManager().getMongoDB().getDatabase().getCollection("plots").insertOne(doc);
}
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 Date 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"))));
}
private static Date getDateFromDatabase(String uuid) {
FindIterable<Document> cursor = Core.getCommons().getDatabaseManager().getMongoDB().getDatabase().getCollection("plots").find(new Document("uuid", uuid));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) return null;
return doc.getDate("created");
}
private static Object getObjectFromDatbase(String uuid, String name) {
FindIterable<Document> cursor = Core.getCommons().getDatabaseManager().getMongoDB().getDatabase().getCollection("plots").find(new Document("uuid", uuid));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) return null;
return doc.get(name);
}
private static Map<String, Object> getMapFromDatabase(String uuid, String name) {
return (Map<String, Object>) getObjectFromDatbase(uuid, name);
}
}