2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-02-19 07:08:15 +01:00
|
|
|
|
2016-02-14 02:01:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotBlock;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.RegionWrapper;
|
|
|
|
import com.intellectualcrafters.plot.object.schematic.PlotItem;
|
2016-03-14 03:44:59 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MathMan;
|
|
|
|
import com.intellectualcrafters.plot.util.StringComparison;
|
|
|
|
import com.intellectualcrafters.plot.util.StringMan;
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
|
|
|
import com.intellectualcrafters.plot.util.WorldUtil;
|
2016-02-14 02:01:18 +01:00
|
|
|
import com.plotsquared.bukkit.object.BukkitPlayer;
|
2016-03-14 03:44:59 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.World;
|
2015-02-19 14:01:36 +01:00
|
|
|
import org.bukkit.block.Biome;
|
2015-02-19 07:08:15 +01:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockState;
|
|
|
|
import org.bukkit.block.Sign;
|
2015-02-19 11:12:26 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.Player;
|
2015-02-26 05:16:52 +01:00
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2016-03-14 03:44:59 +01:00
|
|
|
import org.bukkit.material.MaterialData;
|
|
|
|
import org.bukkit.material.Sandstone;
|
|
|
|
import org.bukkit.material.Step;
|
|
|
|
import org.bukkit.material.Tree;
|
|
|
|
import org.bukkit.material.WoodenStep;
|
|
|
|
import org.bukkit.material.Wool;
|
2016-03-23 02:41:37 +01:00
|
|
|
|
2016-04-26 16:14:22 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public class BukkitUtil extends WorldUtil {
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-19 07:08:15 +01:00
|
|
|
private static String lastString = null;
|
|
|
|
private static World lastWorld = null;
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-21 05:27:01 +01:00
|
|
|
private static Player lastPlayer = null;
|
|
|
|
private static PlotPlayer lastPlotPlayer = null;
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-06-02 17:38:47 +02:00
|
|
|
public static void removePlayer(String player) {
|
2015-05-28 17:42:17 +02:00
|
|
|
lastPlayer = null;
|
|
|
|
lastPlotPlayer = null;
|
2015-02-21 05:32:01 +01:00
|
|
|
}
|
2015-10-07 08:33:33 +02:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static PlotPlayer getPlayer(OfflinePlayer op) {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (op.isOnline()) {
|
|
|
|
return getPlayer(op.getPlayer());
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
Player player = OfflinePlayerUtil.loadPlayer(op);
|
2015-06-23 13:13:48 +02:00
|
|
|
player.loadData();
|
2015-08-01 22:11:28 +02:00
|
|
|
return new BukkitPlayer(player, true);
|
2015-06-23 13:13:48 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static PlotPlayer getPlayer(Player player) {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (player == lastPlayer) {
|
|
|
|
return lastPlotPlayer;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
String name = player.getName();
|
2016-06-02 17:38:47 +02:00
|
|
|
PlotPlayer plotPlayer = UUIDHandler.getPlayer(name);
|
|
|
|
if (plotPlayer != null) {
|
|
|
|
return plotPlayer;
|
2015-09-13 06:04:31 +02:00
|
|
|
}
|
2015-02-21 05:27:01 +01:00
|
|
|
lastPlotPlayer = new BukkitPlayer(player);
|
2015-07-24 16:06:58 +02:00
|
|
|
UUIDHandler.getPlayers().put(name, lastPlotPlayer);
|
2015-02-21 05:27:01 +01:00
|
|
|
lastPlayer = player;
|
|
|
|
return lastPlotPlayer;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static Location getLocation(org.bukkit.Location location) {
|
|
|
|
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()),
|
|
|
|
MathMan.roundInt(location.getZ()));
|
2015-02-20 08:10:07 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static org.bukkit.Location getLocation(Location location) {
|
|
|
|
return new org.bukkit.Location(getWorld(location.getWorld()), location.getX(), location.getY(), location.getZ());
|
2015-03-13 04:15:00 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static World getWorld(String string) {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (StringMan.isEqual(string, lastString)) {
|
|
|
|
if (lastWorld != null) {
|
|
|
|
return lastWorld;
|
|
|
|
}
|
2015-02-19 07:08:15 +01:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
World world = Bukkit.getWorld(string);
|
2015-09-05 03:35:43 +02:00
|
|
|
lastString = string;
|
|
|
|
lastWorld = world;
|
2015-02-19 07:08:15 +01:00
|
|
|
return world;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static String getWorld(Entity entity) {
|
2015-02-19 11:12:26 +01:00
|
|
|
return entity.getWorld().getName();
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-29 21:47:59 +02:00
|
|
|
public static List<Entity> getEntities(String worldName) {
|
|
|
|
return getWorld(worldName).getEntities();
|
2015-02-19 11:12:26 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static Location getLocation(Entity entity) {
|
2016-06-02 17:38:47 +02:00
|
|
|
org.bukkit.Location location = entity.getLocation();
|
|
|
|
String world = location.getWorld().getName();
|
|
|
|
return new Location(world, location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
2015-07-28 08:06:19 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public static Location getLocationFull(Entity entity) {
|
2016-06-02 17:38:47 +02:00
|
|
|
org.bukkit.Location location = entity.getLocation();
|
|
|
|
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()),
|
|
|
|
location.getYaw(), location.getPitch());
|
2015-07-28 08:06:19 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-07-28 08:06:19 +02:00
|
|
|
@Override
|
2016-03-29 21:47:59 +02:00
|
|
|
public boolean isWorld(String worldName) {
|
|
|
|
return getWorld(worldName) != null;
|
2015-07-20 07:14:46 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-07-28 08:06:19 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public String getBiome(String world, int x, int z) {
|
2015-07-28 08:06:19 +02:00
|
|
|
return getWorld(world).getBiome(x, z).name();
|
2015-02-23 12:37:36 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-19 07:08:15 +01:00
|
|
|
@Override
|
2016-03-29 21:47:59 +02:00
|
|
|
public void setSign(String worldName, int x, int y, int z, String[] lines) {
|
|
|
|
World world = getWorld(worldName);
|
2016-03-23 02:41:37 +01:00
|
|
|
Block block = world.getBlockAt(x, y, z);
|
2015-10-24 12:55:34 +02:00
|
|
|
// block.setType(Material.AIR);
|
2015-02-19 07:08:15 +01:00
|
|
|
block.setTypeIdAndData(Material.WALL_SIGN.getId(), (byte) 2, false);
|
2016-03-23 02:41:37 +01:00
|
|
|
BlockState blockstate = block.getState();
|
2016-02-14 02:01:18 +01:00
|
|
|
if (blockstate instanceof Sign) {
|
2015-10-24 12:55:34 +02:00
|
|
|
final Sign sign = (Sign) blockstate;
|
|
|
|
for (int i = 0; i < lines.length; i++) {
|
|
|
|
sign.setLine(i, lines[i]);
|
|
|
|
}
|
|
|
|
sign.update(true);
|
2015-10-18 13:20:12 +02:00
|
|
|
TaskManager.runTaskLater(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-10-24 12:55:34 +02:00
|
|
|
sign.update(true);
|
2015-10-18 13:20:12 +02:00
|
|
|
}
|
2015-10-24 12:55:34 +02:00
|
|
|
}, 20);
|
2015-02-19 07:08:15 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-22 07:30:58 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public String[] getSign(Location location) {
|
|
|
|
Block block = getWorld(location.getWorld()).getBlockAt(location.getX(), location.getY(), location.getZ());
|
2015-09-13 06:04:31 +02:00
|
|
|
if (block != null) {
|
|
|
|
if (block.getState() instanceof Sign) {
|
2016-03-23 02:41:37 +01:00
|
|
|
Sign sign = (Sign) block.getState();
|
2015-02-22 07:30:58 +01:00
|
|
|
return sign.getLines();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-31 11:48:46 +02:00
|
|
|
@Override
|
2016-06-02 17:38:47 +02:00
|
|
|
public Location getSpawn(PlotPlayer player) {
|
|
|
|
return getLocation(((BukkitPlayer) player).player.getBedSpawnLocation());
|
2016-03-31 11:48:46 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 07:56:46 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public Location getSpawn(String world) {
|
|
|
|
org.bukkit.Location temp = getWorld(world).getSpawnLocation();
|
2015-07-20 07:14:46 +02:00
|
|
|
return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(), temp.getYaw(), temp.getPitch());
|
2015-02-22 07:56:46 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-03-17 10:11:07 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setSpawn(Location location) {
|
|
|
|
World world = getWorld(location.getWorld());
|
2016-03-17 10:11:07 +01:00
|
|
|
if (world != null) {
|
2016-03-23 02:41:37 +01:00
|
|
|
world.setSpawnLocation(location.getX(), location.getY(), location.getZ());
|
2016-03-17 10:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-29 21:47:59 +02:00
|
|
|
public void saveWorld(String worldName) {
|
|
|
|
World world = getWorld(worldName);
|
2016-03-17 10:11:07 +01:00
|
|
|
if (world != null) {
|
|
|
|
world.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 12:30:46 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public int getHighestBlock(String world, int x, int z) {
|
2015-07-28 08:06:19 +02:00
|
|
|
return getWorld(world).getHighestBlockAt(x, z).getY();
|
2015-02-22 12:30:46 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-22 13:24:48 +01:00
|
|
|
@Override
|
2016-06-02 17:38:47 +02:00
|
|
|
public int getBiomeFromString(String biomeString) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2016-06-02 17:38:47 +02:00
|
|
|
Biome biome = Biome.valueOf(biomeString.toUpperCase());
|
2015-03-20 15:01:35 +01:00
|
|
|
return Arrays.asList(Biome.values()).indexOf(biome);
|
2016-04-28 22:38:51 +02:00
|
|
|
} catch (IllegalArgumentException ignored) {
|
2015-02-22 13:24:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-22 13:24:48 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public String[] getBiomeList() {
|
2016-03-23 02:41:37 +01:00
|
|
|
Biome[] biomes = Biome.values();
|
|
|
|
String[] list = new String[biomes.length];
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 0; i < biomes.length; i++) {
|
2015-02-22 13:24:48 +01:00
|
|
|
list[i] = biomes[i].name();
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-02-26 05:16:52 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean addItems(String worldName, PlotItem items) {
|
|
|
|
World world = getWorld(worldName);
|
|
|
|
Block block = world.getBlockAt(items.x, items.y, items.z);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (block == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
BlockState state = block.getState();
|
2016-02-14 02:01:18 +01:00
|
|
|
if (state instanceof InventoryHolder) {
|
2016-03-23 02:41:37 +01:00
|
|
|
InventoryHolder holder = (InventoryHolder) state;
|
|
|
|
Inventory inv = holder.getInventory();
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 0; i < items.id.length; i++) {
|
2016-03-23 02:41:37 +01:00
|
|
|
ItemStack item = new ItemStack(items.id[i], items.amount[i], items.data[i]);
|
2015-02-26 05:16:52 +01:00
|
|
|
inv.addItem(item);
|
|
|
|
}
|
|
|
|
state.update(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-04-14 15:55:00 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean isBlockSolid(PlotBlock block) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2016-03-23 02:41:37 +01:00
|
|
|
Material material = Material.getMaterial(block.id);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (material.isBlock() && material.isSolid() && !material.hasGravity()) {
|
2016-03-23 02:41:37 +01:00
|
|
|
Class<? extends MaterialData> data = material.getData();
|
2016-02-14 02:01:18 +01:00
|
|
|
if (data.equals(MaterialData.class) && !material.isTransparent() && material.isOccluding()
|
|
|
|
|| data.equals(Tree.class)
|
|
|
|
|| data.equals(Sandstone.class)
|
|
|
|
|| data.equals(Wool.class)
|
|
|
|
|| data.equals(Step.class)
|
|
|
|
|| data.equals(WoodenStep.class)) {
|
2015-09-13 06:04:31 +02:00
|
|
|
switch (material) {
|
2015-07-12 21:08:41 +02:00
|
|
|
case NOTE_BLOCK:
|
2016-02-14 02:01:18 +01:00
|
|
|
case MOB_SPAWNER:
|
2015-07-12 21:08:41 +02:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-29 21:30:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-04-28 22:38:51 +02:00
|
|
|
} catch (Exception ignored) {
|
2015-04-14 15:55:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-29 21:30:36 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public String getClosestMatchingName(PlotBlock block) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2015-06-29 21:30:36 +02:00
|
|
|
return Material.getMaterial(block.id).name();
|
2016-04-26 16:14:22 +02:00
|
|
|
} catch (Exception ignored) {
|
2015-06-29 21:30:36 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-29 21:30:36 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
|
2016-02-10 19:59:51 +01:00
|
|
|
try {
|
2016-03-23 02:41:37 +01:00
|
|
|
Material material = Material.valueOf(name.toUpperCase());
|
2016-06-03 22:20:13 +02:00
|
|
|
return new StringComparison<PlotBlock>().new ComparisonResult(0, PlotBlock.get((short) material.getId(), (byte) 0));
|
2016-04-30 00:14:12 +02:00
|
|
|
} catch (IllegalArgumentException ignored) {}
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2015-06-29 21:30:36 +02:00
|
|
|
byte data;
|
2016-03-23 02:41:37 +01:00
|
|
|
String[] split = name.split(":");
|
2015-09-13 06:04:31 +02:00
|
|
|
if (split.length == 2) {
|
2015-06-29 21:30:36 +02:00
|
|
|
data = Byte.parseByte(split[1]);
|
|
|
|
name = split[0];
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2015-06-29 21:30:36 +02:00
|
|
|
data = 0;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
double match;
|
|
|
|
short id;
|
2015-09-13 06:04:31 +02:00
|
|
|
if (MathMan.isInteger(split[0])) {
|
2015-06-29 21:30:36 +02:00
|
|
|
id = Short.parseShort(split[0]);
|
|
|
|
match = 0;
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2016-03-23 02:41:37 +01:00
|
|
|
StringComparison<Material>.ComparisonResult comparison = new StringComparison<>(name, Material.values()).getBestMatchAdvanced();
|
2015-06-29 21:30:36 +02:00
|
|
|
match = comparison.match;
|
2015-09-11 12:09:22 +02:00
|
|
|
id = (short) comparison.best.getId();
|
2015-06-29 21:30:36 +02:00
|
|
|
}
|
2016-06-03 22:20:13 +02:00
|
|
|
PlotBlock block = PlotBlock.get(id, data);
|
2016-03-23 02:41:37 +01:00
|
|
|
StringComparison<PlotBlock> outer = new StringComparison<>();
|
2015-06-29 21:30:36 +02:00
|
|
|
return outer.new ComparisonResult(match, block);
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-04-30 00:14:12 +02:00
|
|
|
} catch (NumberFormatException ignored) {}
|
2015-06-29 21:30:36 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
@Override
|
2016-06-02 17:38:47 +02:00
|
|
|
public void setBiomes(String worldName, RegionWrapper region, String biomeString) {
|
2016-03-23 02:41:37 +01:00
|
|
|
World world = getWorld(worldName);
|
2016-06-02 17:38:47 +02:00
|
|
|
Biome biome = Biome.valueOf(biomeString.toUpperCase());
|
2016-02-10 19:59:51 +01:00
|
|
|
for (int x = region.minX; x <= region.maxX; x++) {
|
|
|
|
for (int z = region.minZ; z <= region.maxZ; z++) {
|
|
|
|
world.setBiome(x, z, biome);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-07-28 08:06:19 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public PlotBlock getBlock(Location location) {
|
|
|
|
World world = getWorld(location.getWorld());
|
|
|
|
Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ());
|
2015-09-13 06:04:31 +02:00
|
|
|
if (block == null) {
|
2016-03-14 03:44:59 +01:00
|
|
|
return PlotBlock.EVERYTHING;
|
2015-09-13 06:04:31 +02:00
|
|
|
}
|
2016-06-03 22:20:13 +02:00
|
|
|
return PlotBlock.get((short) block.getTypeId(), block.getData());
|
2015-07-28 08:06:19 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
@Override
|
|
|
|
public String getMainWorld() {
|
|
|
|
return Bukkit.getWorlds().get(0).getName();
|
|
|
|
}
|
2015-02-19 07:08:15 +01:00
|
|
|
}
|