Untitled

 avatar
unknown
java
2 years ago
13 kB
9
Indexable
public static World generateFarmworld() {
        int count = 0;

        List<World> farmworlds = new ArrayList<>();

        for(World world : Bukkit.getWorlds()) {
            if(world.getName().startsWith("farmworld_")) {
                farmworlds.add(world);
            }
        }

        for(World world : farmworlds) {
            if(Integer.parseInt(world.getName().substring(10)) > count) {
                count = Integer.parseInt(world.getName().substring(10));
            }
        }

        //add 1 more, because it should be 1 more than previous
        count++;

        WorldCreator worldCreator = new WorldCreator("farmworld_" + count);
        worldCreator.type(WorldType.LARGE_BIOMES);
        World world = worldCreator.createWorld();

        if(world.isChunkLoaded(world.getSpawnLocation().getChunk())) {
            Location islandLocation = new Location(world.getSpawnLocation().getWorld(), world.getSpawnLocation().getX(), world.getSpawnLocation().getY() + 50, world.getSpawnLocation().getZ());
            if(islandLocation.getBlock().getType() == Material.AIR) {
                if(generateFarmworldIsland(islandLocation)) {
                    generateSpawnBiome(world);
                    return world;
                } else {
                    //rerun generator because island couldnt generate
                    deleteWorld(world);
                    return generateFarmworld();
                }
            } else {
                //rerun generator
                deleteWorld(world);
                return generateFarmworld();
            }
        } else {
            world.loadChunk(world.getSpawnLocation().getChunk());

            Location islandLocation = new Location(world.getSpawnLocation().getWorld(), world.getSpawnLocation().getX(), world.getSpawnLocation().getY() + 50, world.getSpawnLocation().getZ());
            if(islandLocation.getBlock().getType() == Material.AIR) {
                if(generateFarmworldIsland(islandLocation)) {
                    generateSpawnBiome(world);
                    return world;
                } else {
                    deleteWorld(world);
                    return generateFarmworld();
                }
            } else {
                //rerun generator
                deleteWorld(world);
                return generateFarmworld();
            }
        }
    }

    public static boolean generateFarmworldIsland(Location middleLocation) {
        try {
            Location pasteLocation = new Location(middleLocation.getWorld(), middleLocation.getX() - 28, middleLocation.getY() - 24, middleLocation.getZ() - 26);

            if(pasteLocation.getY() >= 220) {
                return false;
            }

            File schematicFile = new File(LaserServerManager.getPlugin(LaserServerManager.class).getDataFolder().getAbsolutePath() + "/farmworld_spawn.schem");
            Clipboard clipboard;

            ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
            try (ClipboardReader reader = format.getReader(new FileInputStream(schematicFile))) {
                clipboard = reader.read();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            com.sk89q.worldedit.world.World adaptedWorld = BukkitAdapter.adapt(middleLocation.getWorld());

                try (EditSession editSession = WorldEdit.getInstance().newEditSession(adaptedWorld)) {
                    Operation operation = new ClipboardHolder(clipboard)
                            .createPaste(editSession)
                            .to(BlockVector3.at(pasteLocation.getX(), pasteLocation.getY(), pasteLocation.getZ()))
                            .build();
                    Operations.complete(operation);

                    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
                    RegionManager regions = container.get(BukkitAdapter.adapt(middleLocation.getWorld()));
                    ProtectedCuboidRegion newRegion = new ProtectedCuboidRegion("farmworld_spawn_" + middleLocation.getWorld().getName().substring(10), BlockVector3.at(middleLocation.getBlockX() + 27, middleLocation.getBlockY() + 12, middleLocation.getBlockZ() -27), BlockVector3.at(middleLocation.getBlockX() -27, middleLocation.getBlockY() -25, middleLocation.getBlockZ() + 27));
                    regions.addRegion(newRegion);

                    newRegion.setFlag(Flags.BLOCK_BREAK, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.BUILD, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.DAMAGE_ANIMALS, StateFlag.State.ALLOW);
                    newRegion.setFlag(Flags.FROSTED_ICE_FORM, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.ICE_FORM, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.ICE_MELT, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.FROSTED_ICE_MELT, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.PVP, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.WITHER_DAMAGE, StateFlag.State.DENY);
                    newRegion.setFlag(Flags.TRAMPLE_BLOCKS, StateFlag.State.DENY);

                    middleLocation.getWorld().setSpawnLocation(middleLocation.add(0.5, 0, 0.5));
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void deleteWorld(World world) {
        List<Player> playerInWorld = world.getPlayers();

        for(Player player : playerInWorld) {
            player.sendMessage(prefix + ChatColor.RED + "You got teleported because the world you were in got deleted");
            Location spawn = new Location(Bukkit.getWorld("lobby"), 0, 65, 0);
            player.teleport(spawn);
        }

        MultiverseCore core = new MultiverseCore().getCore();
        MVWorldManager worldManager = core.getMVWorldManager();

        worldManager.deleteWorld(world.getName());
    }

    public static void generateSpawnBiome(World world) {
        RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
        RegionManager regions = container.get(BukkitAdapter.adapt(world));
        ProtectedRegion region = regions.getRegion("farmworld_spawn_" + world.getName().substring(10));

        int XminPoint = region.getMinimumPoint().getBlockX();
        int ZminPoint = region.getMinimumPoint().getBlockZ();
        int XmaxPoint = region.getMaximumPoint().getBlockX();
        int ZmaxPoint = region.getMaximumPoint().getBlockZ();

        Bukkit.getLogger().info("XMin: " + XminPoint + " XMax: " + XmaxPoint + " Zmin: " + ZminPoint + " ZMax: " + ZmaxPoint);

        int depthTop = region.getMaximumPoint().getBlockY();
        int depthBottom = region.getMinimumPoint().getBlockY();

        Bukkit.getLogger().info("depthTop: " + depthTop + " depthBottom: " + depthBottom);

        List<BlockVector3> topPointsList = new ArrayList<>();
        List<BlockVector3> bottomPointsList = new ArrayList<>();

        for(int x = XminPoint; x <= XmaxPoint; x++) {
            for(int z = ZminPoint; z <= ZmaxPoint; z++) {
                topPointsList.add(BlockVector3.at(x, depthTop, z));
                //Bukkit.getLogger().info("New Vector (TOP): X: " + x + " Y: " + depthTop + " Z: " + z);
            }
        }

        for(int x = XminPoint; x <= XmaxPoint; x++) {
            for(int z = ZminPoint; z <= ZmaxPoint; z++) {
                bottomPointsList.add(BlockVector3.at(x, depthBottom, z));
                //Bukkit.getLogger().info("New Vector (BOTTOM): X: " + x + " Y: " + depthBottom + " Z: " + z);
            }
        }

        for(int i = 0; i < topPointsList.size(); i++) {
            Bukkit.getLogger().info("X: " + topPointsList.get(i).getBlockX() + " Z: " + topPointsList.get(i).getBlockZ() + " World: " + world + " BiomeType: " + getBiomeType(topPointsList.get(i).getBlockX(), topPointsList.get(i).getBlockZ(), topPointsList.get(i).getBlockY(), world).toString());
            CuboidRegion cuboidRegion = new CuboidRegion(topPointsList.get(i), bottomPointsList.get(i));
            setBiome(world.getName(), cuboidRegion, getBiomeType(topPointsList.get(i).getBlockX(), topPointsList.get(i).getBlockZ(), topPointsList.get(i).getBlockY(), world));
        }
    }

    private static BiomeType getBiomeType(int x, int z, int topheight, World world) {
        int ground = topheight - 12;
        int e1 = topheight - 13;
        int e2 = topheight - 14;
        int e3 = topheight - 15;

        Block blockGround = world.getBlockAt(x, ground, z);
        Block blocke1 = world.getBlockAt(x, e1, z);
        Block blocke2 = world.getBlockAt(x, e2, z);
        Block blocke3 = world.getBlockAt(x, e3, z);

        Location worldSpawn = world.getSpawnLocation();

        if(blocke1.getType() == Material.GLASS) {
            return BiomeTypes.PLAINS;
        } else if(blocke1.getType() == Material.GRASS_BLOCK) {
            return BiomeTypes.PLAINS;
        } else if(blocke2.getType() == Material.GRASS_BLOCK) {
            return BiomeTypes.PLAINS;
        } else if(blocke3.getType() == Material.GRASS_BLOCK) {
            return BiomeTypes.PLAINS;
        } else if(blockGround.getType() == Material.RED_SAND) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blocke1.getType() == Material.RED_SAND) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blocke2.getType() == Material.RED_SAND) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blocke2.getType() == Material.LAVA && blocke3.getType() != Material.LAVA) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blockGround.getType() == Material.TERRACOTTA) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blockGround.getType() == Material.RED_SAND) {
            return BiomeTypes.SAVANNA_PLATEAU;
        } else if(blockGround.getType() == Material.STONE) {
            return BiomeTypes.MOUNTAIN_EDGE;
        } else if(blockGround.getType() == Material.ANDESITE) {
            return BiomeTypes.MOUNTAIN_EDGE;
        } else if(blockGround.getType() == Material.GRANITE) {
            return BiomeTypes.MOUNTAIN_EDGE;
        } else if(blockGround.getType() == Material.DIORITE) {
            return BiomeTypes.MOUNTAIN_EDGE;
        } else if(blockGround.getType() == Material.SCULK) {
            return BiomeTypes.MOUNTAIN_EDGE;
        } else if(blocke1.getType() == Material.DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke1.getType() == Material.COARSE_DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke2.getType() == Material.DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke2.getType() == Material.COARSE_DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke3.getType() == Material.DIRT && z < worldSpawn.getBlockZ() - 5) {
            return BiomeTypes.SWAMP;
        } else if(blocke3.getType() == Material.COARSE_DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke1.getType() == Material.MUD) {
            return BiomeTypes.SWAMP;
        } else if(blocke1.getType() == Material.DIRT) {
            return BiomeTypes.SWAMP;
        } else if(blocke2.getType() == Material.NETHERRACK) {
            return BiomeTypes.NETHER_WASTES;
        } else if(blockGround.getType() == Material.NETHER_BRICK) {
            return BiomeTypes.NETHER_WASTES;
        } else if(blocke2.getType() == Material.LAVA) {
            return BiomeTypes.NETHER_WASTES;
        } else if(blocke2.getType() == Material.SNOW_BLOCK) {
            return BiomeTypes.ICE_SPIKES;
        } else if(blocke1.getType() == Material.SNOW) {
            return BiomeTypes.ICE_SPIKES;
        } else if(blockGround.getType() == Material.PACKED_ICE) {
            return BiomeTypes.ICE_SPIKES;
        } else if(blocke2.getType() == Material.WATER) {
            return BiomeTypes.LUKEWARM_OCEAN;
        } else if(blocke3.getType() == Material.WATER) {
            return BiomeTypes.LUKEWARM_OCEAN;
        } else {
            return BiomeTypes.PLAINS;
        }
    }
Editor is loading...
Leave a Comment