PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java

390 lines
14 KiB
Java
Raw Normal View History

2015-02-19 07:08:15 +01:00
package com.intellectualcrafters.plot.util.bukkit;
2015-02-22 13:24:48 +01:00
import java.util.Arrays;
2015-02-19 07:08:15 +01:00
import java.util.HashMap;
2015-02-19 11:12:26 +01:00
import java.util.List;
2015-02-19 07:08:15 +01:00
import org.apache.commons.lang.StringUtils;
2015-02-19 07:08:15 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
2015-02-19 07:08:15 +01:00
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;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.bukkit.material.Sandstone;
import org.bukkit.material.Step;
import org.bukkit.material.Tree;
2015-07-14 20:53:06 +02:00
import org.bukkit.material.WoodenStep;
import org.bukkit.material.Wool;
2015-02-19 07:08:15 +01:00
2015-02-21 05:27:01 +01:00
import com.intellectualcrafters.plot.object.BukkitPlayer;
import com.intellectualcrafters.plot.object.ChunkLoc;
2015-02-19 11:12:26 +01:00
import com.intellectualcrafters.plot.object.Location;
2015-02-19 12:06:27 +01:00
import com.intellectualcrafters.plot.object.PlotBlock;
2015-02-21 05:27:01 +01:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.schematic.PlotItem;
2015-02-19 07:08:15 +01:00
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.StringComparison;
2015-02-19 07:08:15 +01:00
public class BukkitUtil extends BlockManager {
private static HashMap<String, World> worlds = new HashMap<>();
private static String lastString = null;
private static World lastWorld = null;
2015-02-23 02:32:27 +01:00
2015-02-21 05:27:01 +01:00
private static Player lastPlayer = null;
private static PlotPlayer lastPlotPlayer = null;
2015-02-23 02:32:27 +01:00
public static void removePlayer(final String plr) {
lastPlayer = null;
lastPlotPlayer = null;
2015-02-21 13:52:50 +01:00
UUIDHandler.players.remove(plr);
2015-02-21 05:32:01 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-22 07:30:58 +01:00
@Override
2015-02-23 02:32:27 +01:00
public boolean isWorld(final String world) {
2015-02-22 07:09:20 +01:00
return getWorld(world) != null;
}
2015-02-23 02:32:27 +01:00
public static PlotPlayer getPlayer(final OfflinePlayer op) {
if (op.isOnline()) {
return getPlayer(op.getPlayer());
}
Player player = OfflinePlayerUtil.loadPlayer(op);
player.loadData();
return new BukkitPlayer(player);
}
2015-02-23 02:32:27 +01:00
public static PlotPlayer getPlayer(final Player player) {
2015-02-21 05:27:01 +01:00
if (player == lastPlayer) {
return lastPlotPlayer;
}
String name = player.getName();
PlotPlayer pp = UUIDHandler.players.get(name);
if (pp != null) {
return pp;
}
2015-02-21 05:27:01 +01:00
lastPlotPlayer = new BukkitPlayer(player);
UUIDHandler.players.put(name, lastPlotPlayer);
2015-02-21 05:27:01 +01:00
lastPlayer = player;
return lastPlotPlayer;
}
2015-02-23 02:32:27 +01:00
2015-02-22 08:13:27 +01:00
@Override
public String getBiome(final Location loc) {
return getWorld(loc.getWorld()).getBiome(loc.getX(), loc.getZ()).name();
2015-02-20 03:37:46 +01:00
}
2015-02-23 02:32:27 +01:00
public static Location getLocation(final org.bukkit.Location loc) {
2015-07-14 16:46:03 +02:00
return new Location(loc.getWorld().getName(), (int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
2015-02-20 08:10:07 +01:00
}
2015-03-13 04:15:00 +01:00
public static org.bukkit.Location getLocation(final Location loc) {
return new org.bukkit.Location(getWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static World getWorld(final String string) {
2015-02-21 03:49:25 +01:00
if (string == lastString) {
2015-02-19 07:08:15 +01:00
return lastWorld;
}
World world = worlds.get(string);
if (world == null) {
world = Bukkit.getWorld(string);
worlds.put(string, world);
}
return world;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static int getMaxHeight(final String world) {
2015-02-19 12:06:27 +01:00
return getWorld(world).getMaxHeight();
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static int getHeighestBlock(final String world, final int x, final int z) {
2015-02-19 12:06:27 +01:00
return getWorld(world).getHighestBlockYAt(x, z);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static Chunk getChunkAt(final String worldname, final int x, final int z) {
final World world = getWorld(worldname);
2015-02-19 07:08:15 +01:00
return world.getChunkAt(x, z);
}
2015-02-23 02:32:27 +01:00
// public static void update(final String world, final int x, final int z) {
// final ArrayList<Chunk> chunks = new ArrayList<>();
// final int distance = Bukkit.getViewDistance();
// for (int cx = -distance; cx < distance; cx++) {
// for (int cz = -distance; cz < distance; cz++) {
// final Chunk chunk = getChunkAt(world, (x >> 4) + cx, (z >> 4) + cz);
// chunks.add(chunk);
// }
// }
// BukkitSetBlockManager.setBlockManager.update(chunks);
// }
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static String getWorld(final Entity entity) {
2015-02-19 11:12:26 +01:00
return entity.getWorld().getName();
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static void teleportPlayer(final Player player, final Location loc) {
final org.bukkit.Location bukkitLoc = new org.bukkit.Location(getWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ());
2015-02-19 11:12:26 +01:00
player.teleport(bukkitLoc);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static List<Entity> getEntities(final String worldname) {
2015-02-19 11:12:26 +01:00
return getWorld(worldname).getEntities();
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static void setBlock(final World world, final int x, final int y, final int z, final int id, final byte data) {
2015-02-19 07:08:15 +01:00
try {
2015-02-23 06:29:45 +01:00
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id, data);
2015-02-20 07:34:19 +01:00
} catch (final Throwable e) {
2015-02-23 06:29:45 +01:00
BukkitSetBlockManager.setBlockManager = new SetBlockSlow();
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id, data);
2015-02-19 07:08:15 +01:00
}
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static void setBiome(final String worldname, final int pos1_x, final int pos1_z, final int pos2_x, final int pos2_z, final String biome) {
final Biome b = Biome.valueOf(biome.toUpperCase());
final World world = getWorld(worldname);
for (int x = pos1_x; x <= pos2_x; x++) {
for (int z = pos1_z; z <= pos2_z; z++) {
2015-02-19 14:01:36 +01:00
final Block blk = world.getBlockAt(x, 0, z);
final Biome c = blk.getBiome();
if (c.equals(b)) {
x += 15;
continue;
}
blk.setBiome(b);
}
}
}
2015-02-23 02:32:27 +01:00
2015-03-15 01:49:22 +01:00
public static void refreshChunk(final String name, final int x, final int z) {
World world = getWorld(name);
world.refreshChunk(x, z);
2015-03-15 01:49:22 +01:00
world.loadChunk(x, z);
2015-02-19 15:47:15 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-21 04:43:08 +01:00
public static void regenerateChunk(final String world, final int x, final int z) {
2015-04-26 14:11:18 +02:00
World worldObj = getWorld(world);
Chunk chunk = worldObj.getChunkAt(x, z);
if (chunk.isLoaded() || chunk.load(false)) {
ChunkManager.manager.regenerateChunk(world, new ChunkLoc(x, z));
2015-04-26 14:11:18 +02:00
}
2015-02-21 04:43:08 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotBlock getBlock(final Location loc) {
final World world = getWorld(loc.getWorld());
final Block block = world.getBlockAt(loc.getX(), loc.getY(), loc.getZ());
2015-02-19 12:06:27 +01:00
if (block == null) {
2015-02-20 07:34:19 +01:00
return new PlotBlock((short) 0, (byte) 0);
2015-02-19 12:06:27 +01:00
}
return new PlotBlock((short) block.getTypeId(), block.getData());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static Location getLocation(final Entity entity) {
final org.bukkit.Location loc = entity.getLocation();
final String world = loc.getWorld().getName();
2015-02-19 11:12:26 +01:00
return new Location(world, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
2015-02-23 12:37:36 +01:00
public static Location getLocationFull(final Entity entity) {
final org.bukkit.Location loc = entity.getLocation();
final String world = loc.getWorld().getName();
return new Location(world, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), loc.getYaw(), loc.getPitch());
}
2015-02-23 02:32:27 +01:00
2015-02-19 07:08:15 +01:00
@Override
2015-02-20 07:34:19 +01:00
public void functionSetBlocks(final String worldname, final int[] x, final int[] y, final int[] z, final int[] id, final byte[] data) {
final World world = getWorld(worldname);
for (int i = 0; i < x.length; i++) {
BukkitUtil.setBlock(world, x[i], y[i], z[i], id[i], data[i]);
}
2015-02-19 07:08:15 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-19 07:08:15 +01:00
@Override
2015-02-20 07:34:19 +01:00
public void functionSetSign(final String worldname, final int x, final int y, final int z, final String[] lines) {
final World world = getWorld(worldname);
final Block block = world.getBlockAt(x, y, z);
2015-02-19 07:08:15 +01:00
block.setType(Material.AIR);
block.setTypeIdAndData(Material.WALL_SIGN.getId(), (byte) 2, false);
2015-02-20 07:34:19 +01:00
final BlockState blockstate = block.getState();
2015-02-19 07:08:15 +01:00
if ((blockstate instanceof Sign)) {
for (int i = 0; i < lines.length; i++) {
((Sign) blockstate).setLine(i, lines[i]);
}
2015-02-19 11:12:26 +01:00
((Sign) blockstate).update(true);
2015-02-19 07:08:15 +01:00
}
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:28:21 +01:00
public static int getViewDistance() {
return Bukkit.getViewDistance();
}
2015-02-23 02:32:27 +01:00
2015-02-20 17:17:34 +01:00
@Override
2015-02-23 02:32:27 +01:00
public void functionSetBiomes(final String worldname, final int[] x, final int[] z, final int[] biome) {
final World world = getWorld(worldname);
final Biome[] biomes = Biome.values();
2015-02-20 17:17:34 +01:00
for (int i = 0; i < x.length; i++) {
world.setBiome(x[i], z[i], biomes[biome[i]]);
}
}
2015-02-23 02:32:27 +01:00
2015-02-21 03:49:25 +01:00
@Override
2015-02-23 02:32:27 +01:00
public void functionSetBlock(final String worldname, final int x, final int y, final int z, final int id, final byte data) {
2015-02-21 03:49:25 +01:00
BukkitUtil.setBlock(getWorld(worldname), x, y, z, id, data);
}
2015-02-23 02:32:27 +01:00
2015-02-22 07:30:58 +01:00
@Override
2015-02-23 02:32:27 +01:00
public String[] getSign(final Location loc) {
final Block block = getWorld(loc.getWorld()).getBlockAt(loc.getX(), loc.getY(), loc.getZ());
2015-02-22 07:30:58 +01:00
if (block != null) {
if (block.getState() instanceof Sign) {
final Sign sign = (Sign) block.getState();
return sign.getLines();
}
}
return null;
}
2015-02-23 02:32:27 +01:00
2015-02-22 07:56:46 +01:00
@Override
2015-02-23 02:32:27 +01:00
public Location getSpawn(final String world) {
final org.bukkit.Location temp = getWorld(world).getSpawnLocation();
2015-02-22 07:56:46 +01:00
return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ());
}
2015-02-23 02:32:27 +01:00
2015-02-22 12:30:46 +01:00
@Override
2015-02-23 02:32:27 +01:00
public int getHeighestBlock(final Location loc) {
2015-02-22 12:30:46 +01:00
return getWorld(loc.getWorld()).getHighestBlockAt(loc.getX(), loc.getZ()).getY();
}
2015-02-23 02:32:27 +01:00
2015-02-22 13:24:48 +01:00
@Override
2015-02-23 02:32:27 +01:00
public int getBiomeFromString(final String biomeStr) {
2015-03-20 15:01:35 +01:00
try {
final Biome biome = Biome.valueOf(biomeStr.toUpperCase());
if (biome == null) {
return -1;
}
return Arrays.asList(Biome.values()).indexOf(biome);
}
catch (IllegalArgumentException e) {
2015-02-22 13:24:48 +01:00
return -1;
}
}
2015-02-23 02:32:27 +01:00
2015-02-22 13:24:48 +01:00
@Override
public String[] getBiomeList() {
2015-02-23 02:32:27 +01:00
final Biome[] biomes = Biome.values();
final String[] list = new String[biomes.length];
for (int i = 0; i < biomes.length; i++) {
2015-02-22 13:24:48 +01:00
list[i] = biomes[i].name();
}
return list;
}
2015-02-23 02:32:27 +01:00
2015-02-22 13:24:48 +01:00
@Override
2015-02-23 02:32:27 +01:00
public int getBlockIdFromString(final String block) {
final Material material = Material.valueOf(block.toUpperCase());
2015-02-22 13:24:48 +01:00
if (material == null) {
return -1;
}
return material.getId();
}
@Override
public boolean addItems(String worldname, PlotItem items) {
World world = getWorld(worldname);
Block block = world.getBlockAt(items.x, items.y, items.z);
if (block == null) {
return false;
}
BlockState state = block.getState();
if (state != null && state instanceof InventoryHolder) {
InventoryHolder holder = ((InventoryHolder) state);
Inventory inv = holder.getInventory();
for (int i = 0; i < items.id.length; i++) {
ItemStack item = new ItemStack(items.id[i], items.amount[i], items.data[i]);
inv.addItem(item);
}
state.update(true);
return true;
}
return false;
}
2015-04-14 15:55:00 +02:00
@Override
public boolean isBlockSolid(PlotBlock block) {
try {
Material material = Material.getMaterial(block.id);
if (material.isBlock() && material.isSolid() && !material.hasGravity()) {
Class<? extends MaterialData> data = material.getData();
2015-07-14 20:53:06 +02: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)) {
switch (material) {
case NOTE_BLOCK:
case MOB_SPAWNER: {
return false;
}
default:
return true;
}
}
}
return false;
2015-04-14 15:55:00 +02:00
}
catch (Exception e) {
return false;
}
}
@Override
public String getClosestMatchingName(PlotBlock block) {
try {
return Material.getMaterial(block.id).name();
}
catch (Exception e) {
return null;
}
}
@Override
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
try {
double match;
short id;
byte data;
String[] split = name.split(":");
if (split.length == 2) {
data = Byte.parseByte(split[1]);
name = split[0];
}
else {
data = 0;
}
if (StringUtils.isNumeric(split[0])) {
id = Short.parseShort(split[0]);
match = 0;
}
else {
StringComparison<Material>.ComparisonResult comparison = new StringComparison<Material>(name, Material.values()).getBestMatchAdvanced();
match = comparison.match;
id = (short) comparison.best.getId();
}
PlotBlock block = new PlotBlock(id, data);
StringComparison<PlotBlock> outer = new StringComparison<PlotBlock>();
return outer.new ComparisonResult(match, block);
}
catch (Exception e) {}
return null;
}
2015-02-19 07:08:15 +01:00
}