Files
src
build.gradle
Code
3 187
src/main/java/com/kodari/distort/DistortPlugin.java
package com.kodari.distort;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.noise.SimplexNoiseGenerator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public final class DistortPlugin extends JavaPlugin {
private final List<Material> materials = new ArrayList<>();
private boolean generatingFarlands;
@Override
public void onEnable() {
for (String name : new String[]{"STONE", "DIRT", "COBBLESTONE", "SAND", "GRAVEL",
"OAK_PLANKS", "GLASS", "NETHERRACK", "OBSIDIAN", "GOLD_BLOCK", "DIAMOND_BLOCK"}) {
XMaterial.matchXMaterial(name).map(XMaterial::parseMaterial).ifPresent(materials::add);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("farlands")) {
return generateFarlands(sender, args);
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can distort the area around them.");
return true;
}
if (args.length > 1) {
return false;
}
int count = 100;
if (args.length == 1) {
try {
count = Integer.parseInt(args[0]);
} catch (NumberFormatException exception) {
sender.sendMessage(ChatColor.RED + "Enter a number from 1 to 10000.");
return true;
}
}
if (count < 1 || count > 10000) {
sender.sendMessage(ChatColor.RED + "Enter a number from 1 to 10000.");
return true;
}
if (materials.isEmpty()) {
sender.sendMessage(ChatColor.RED + "No distortion blocks are available.");
return true;
}
Player player = (Player) sender;
Location center = player.getLocation();
World world = player.getWorld();
int radius = 1;
while ((2 * radius + 1) * (2 * radius + 1) * (2 * radius + 1) < count * 2) {
radius++;
}
List<Location> locations = new ArrayList<>();
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
int blockY = center.getBlockY() + y;
if (blockY < world.getMinHeight() || blockY >= world.getMaxHeight()) {
continue;
}
for (int z = -radius; z <= radius; z++) {
locations.add(new Location(world, center.getBlockX() + x, blockY, center.getBlockZ() + z));
}
}
}
Collections.shuffle(locations);
int changed = Math.min(count, locations.size());
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < changed; i++) {
locations.get(i).getBlock().setType(materials.get(random.nextInt(materials.size())), false);
}
sender.sendMessage(ChatColor.GREEN + "Distorted " + changed + " blocks around you.");
return true;
}
private boolean generateFarlands(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can generate Far Lands.");
return true;
}
if (args.length != 1 || !args[0].equalsIgnoreCase("confirm")) {
sender.sendMessage(ChatColor.RED + "This replaces terrain and containers in a 1000x1000 area around you. Move somewhere safe, back up your world, and use /farlands confirm to start.");
return true;
}
if (generatingFarlands) {
sender.sendMessage(ChatColor.RED + "Far Lands generation is already running.");
return true;
}
Material stone = XMaterial.matchXMaterial("STONE").map(XMaterial::parseMaterial).orElse(null);
Material air = XMaterial.matchXMaterial("AIR").map(XMaterial::parseMaterial).orElse(null);
if (stone == null || air == null) {
sender.sendMessage(ChatColor.RED + "The required terrain blocks are unavailable.");
return true;
}
Player player = (Player) sender;
Location center = player.getLocation();
World world = player.getWorld();
int size = 1000;
int startX = center.getBlockX() - size / 2;
int startZ = center.getBlockZ() - size / 2;
int base = Math.max(world.getMinHeight(), Math.min(world.getMaxHeight() - 1, center.getBlockY() - 1));
int minY = Math.max(world.getMinHeight(), base - 16);
int maxY = Math.min(world.getMaxHeight() - 1, base + 80);
SimplexNoiseGenerator noise = new SimplexNoiseGenerator(new Random(world.getSeed()));
generatingFarlands = true;
sender.sendMessage(ChatColor.YELLOW + "Generating Far Lands over 1000x1000 blocks. Existing terrain will be replaced; stay clear of the area.");
new BukkitRunnable() {
private int column;
@Override
public void run() {
for (int n = 0; n < 8 && column < size * size; n++, column++) {
int x = startX + column / size;
int z = startZ + column % size;
double ridge = Math.abs(noise.noise(x * 0.018, z * 0.018));
double height = base + 15 + ridge * 90;
for (int y = minY; y <= maxY; y++) {
double warp = noise.noise(x * 0.035, y * 0.035, z * 0.035) * 27;
boolean solid = y < base - 8 || y < height + warp;
world.getBlockAt(x, y, z).setType(solid ? stone : air, false);
}
}
if (column >= size * size) {
generatingFarlands = false;
sender.sendMessage(ChatColor.GREEN + "Far Lands generation complete.");
cancel();
}
}
}.runTaskTimer(this, 1L, 1L);
return true;
}
}