PlotSquared/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java

393 lines
15 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.util;
2015-02-19 07:08:15 +01:00
2015-07-28 08:06:19 +02:00
import java.util.Arrays;
import java.util.List;
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;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
2015-07-28 08:06:19 +02: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;
2015-02-19 07:08:15 +01:00
2015-07-28 08:06:19 +02:00
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.schematic.PlotItem;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.StringComparison;
import com.intellectualcrafters.plot.util.StringMan;
2015-07-28 08:06:19 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.bukkit.object.BukkitPlayer;
2015-02-19 07:08:15 +01:00
2015-09-13 06:04:31 +02:00
public class BukkitUtil extends BlockManager {
2015-02-19 07:08:15 +01:00
private static String lastString = null;
private static World lastWorld = null;
2015-09-13 06:04:31 +02:00
2015-02-21 05:27:01 +01:00
private static Player lastPlayer = null;
private static PlotPlayer lastPlotPlayer = null;
2015-09-13 06:04:31 +02:00
public static void removePlayer(final String plr) {
lastPlayer = null;
lastPlotPlayer = null;
2015-07-24 16:06:58 +02:00
UUIDHandler.getPlayers().remove(plr);
2015-02-21 05:32:01 +01:00
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
// These weren't being used, but they might be useful later, so I'm just commenting them out
// private static int getMaxHeight(final String world) {
// return getWorld(world).getMaxHeight();
// }
//
// private static void unloadChunkAt(String worldname, int X, int Z, boolean save, boolean safe) {
// final World world = getWorld(worldname);
// world.unloadChunk(X, Z, save, safe);
// }
//
// private static void loadChunkAt(final String worldname, int X, int Z, boolean force) {
// final World world = getWorld(worldname);
// world.loadChunk(X, Z, force);
// }
//
// private static Chunk getChunkAt(final String worldname, final int x, final int z) {
// final World world = getWorld(worldname);
// return world.getChunkAt(x, z);
// }
//
// private 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());
// player.teleport(bukkitLoc);
// }
//
// private 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++) {
// if (world.getBiome(x, z) == b) {
// continue;
// }
// world.setBiome(x, z, b);
// }
// }
// }
//
// private static void refreshChunk(final String name, final int x, final int z) {
// World world = getWorld(name);
// world.refreshChunk(x, z);
// world.loadChunk(x, z);
// }
//
// private static void regenerateChunk(final String world, final int x, final int z) {
// World worldObj = getWorld(world);
// Chunk chunk = worldObj.getChunkAt(x, z);
// if (chunk.isLoaded() || chunk.load(false)) {
// ChunkManager.manager.regenerateChunk(world, new ChunkLoc(x, z));
// }
// }
//
// private static Location getLocationFull(final org.bukkit.Location loc) {
// final String world = loc.getWorld().getName();
// return new Location(world, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), loc.getYaw(), loc.getPitch());
// }
//
// private static int getViewDistance() {
// return Bukkit.getViewDistance();
// }
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
////////////////////////////////////////////////////////////////////////
/////////////////// USED BY EVENT SYSTEM AND SUCH //////////////////////
////////////////////////////////////////////////////////////////////////
2015-09-13 06:04:31 +02:00
public static PlotPlayer getPlayer(final OfflinePlayer op) {
if (op.isOnline()) {
return getPlayer(op.getPlayer());
}
2015-09-11 12:09:22 +02:00
final Player player = OfflinePlayerUtil.loadPlayer(op);
player.loadData();
2015-08-01 22:11:28 +02:00
return new BukkitPlayer(player, true);
}
2015-09-13 06:04:31 +02:00
public static PlotPlayer getPlayer(final Player player) {
if (player == lastPlayer) {
return lastPlotPlayer;
}
2015-09-11 12:09:22 +02:00
final String name = player.getName();
final PlotPlayer pp = UUIDHandler.getPlayers().get(name);
2015-09-13 06:04:31 +02:00
if (pp != null) {
return pp;
}
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;
}
2015-09-13 06:04:31 +02:00
public static Location getLocation(final org.bukkit.Location loc) {
2015-08-09 11:58:29 +02:00
return new Location(loc.getWorld().getName(), MathMan.roundInt(loc.getX()), MathMan.roundInt(loc.getY()), MathMan.roundInt(loc.getZ()));
2015-02-20 08:10:07 +01:00
}
2015-09-13 06:04:31 +02:00
public static org.bukkit.Location getLocation(final Location loc) {
2015-03-13 04:15:00 +01:00
return new org.bukkit.Location(getWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ());
}
2015-09-13 06:04:31 +02:00
public static World getWorld(final String string) {
if (StringMan.isEqual(string, lastString)) {
if (lastWorld != null) {
return lastWorld;
}
2015-02-19 07:08:15 +01:00
}
2015-09-11 12:09:22 +02:00
final World world = Bukkit.getWorld(string);
lastString = string;
lastWorld = world;
2015-02-19 07:08:15 +01:00
return world;
}
2015-09-13 06:04:31 +02:00
public static String getWorld(final Entity entity) {
2015-02-19 11:12:26 +01:00
return entity.getWorld().getName();
}
2015-09-13 06:04:31 +02:00
public static List<Entity> getEntities(final String worldname) {
2015-02-19 11:12:26 +01:00
return getWorld(worldname).getEntities();
}
2015-09-13 06:04:31 +02:00
public static Location getLocation(final Entity entity) {
2015-07-28 08:06:19 +02:00
final org.bukkit.Location loc = entity.getLocation();
final String world = loc.getWorld().getName();
return new Location(world, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
2015-09-13 06:04:31 +02:00
public static Location getLocationFull(final Entity entity) {
2015-09-11 12:09:22 +02:00
final org.bukkit.Location loc = entity.getLocation();
2015-08-09 11:58:29 +02:00
return new Location(loc.getWorld().getName(), MathMan.roundInt(loc.getX()), MathMan.roundInt(loc.getY()), MathMan.roundInt(loc.getZ()), loc.getYaw(), loc.getPitch());
2015-07-28 08:06:19 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
////////////////////////////////////////////////////////////////////////
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
////////////////////////////////////////////////////////////////////////
////////////////////// CLASS ONLY METHODS //////////////////////////////
////////////////////////////////////////////////////////////////////////
2015-09-13 06:04:31 +02:00
private static void setBlock(final World world, final int x, final int y, final int z, final int id, final byte data) {
try {
2015-02-23 06:29:45 +01:00
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id, data);
2015-09-13 06:04:31 +02: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-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
////////////////////////////////////////////////////////////////////////
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean isWorld(final String world) {
2015-07-28 08:06:19 +02:00
return getWorld(world) != null;
2015-07-20 07:14:46 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getBiome(final String world, final int x, final 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
}
2015-09-13 06:04:31 +02:00
2015-02-19 07:08:15 +01:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetBlocks(final String worldname, final int[] x, final int[] y, final int[] z, final int[] id, final byte[] data) {
2015-02-20 07:34:19 +01:00
final World world = getWorld(worldname);
2015-09-13 06:04:31 +02:00
for (int i = 0; i < x.length; i++) {
2015-02-20 07:34:19 +01:00
BukkitUtil.setBlock(world, x[i], y[i], z[i], id[i], data[i]);
}
2015-02-19 07:08:15 +01:00
}
2015-09-13 06:04:31 +02:00
2015-02-19 07:08:15 +01:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetSign(final String worldname, final int x, final int y, final int z, final String[] lines) {
2015-02-20 07:34:19 +01:00
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-09-13 06:04:31 +02:00
if ((blockstate instanceof Sign)) {
for (int i = 0; i < lines.length; i++) {
2015-02-19 07:08:15 +01:00
((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-09-13 06:04:31 +02:00
2015-02-20 17:17:34 +01:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetBiomes(final String worldname, final int[] x, final int[] z, final String biomeStr) {
2015-02-23 02:32:27 +01:00
final World world = getWorld(worldname);
2015-07-28 08:06:19 +02:00
final Biome biome = Biome.valueOf(biomeStr.toUpperCase());
2015-09-13 06:04:31 +02:00
for (int i = 0; i < x.length; i++) {
2015-07-28 08:06:19 +02:00
world.setBiome(x[i], z[i], biome);
2015-02-20 17:17:34 +01:00
}
}
2015-09-13 06:04:31 +02:00
2015-02-21 03:49:25 +01:00
@Override
2015-09-13 06:04:31 +02: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-09-13 06:04:31 +02:00
2015-02-22 07:30:58 +01:00
@Override
2015-09-13 06:04:31 +02:00
public String[] getSign(final Location loc) {
2015-02-23 02:32:27 +01:00
final Block block = getWorld(loc.getWorld()).getBlockAt(loc.getX(), loc.getY(), loc.getZ());
2015-09-13 06:04:31 +02:00
if (block != null) {
if (block.getState() instanceof Sign) {
2015-02-22 07:30:58 +01:00
final Sign sign = (Sign) block.getState();
return sign.getLines();
}
}
return null;
}
2015-09-13 06:04:31 +02:00
2015-02-22 07:56:46 +01:00
@Override
2015-09-13 06:04:31 +02:00
public Location getSpawn(final String world) {
2015-02-23 02:32:27 +01:00
final 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
}
2015-09-13 06:04:31 +02:00
2015-02-22 12:30:46 +01:00
@Override
2015-09-13 06:04:31 +02:00
public int getHeighestBlock(final String world, final int x, final 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
}
2015-09-13 06:04:31 +02:00
2015-02-22 13:24:48 +01:00
@Override
2015-09-13 06:04:31 +02:00
public int getBiomeFromString(final String biomeStr) {
try {
2015-03-20 15:01:35 +01:00
final Biome biome = Biome.valueOf(biomeStr.toUpperCase());
2015-09-13 06:04:31 +02:00
if (biome == null) {
return -1;
}
2015-03-20 15:01:35 +01:00
return Arrays.asList(Biome.values()).indexOf(biome);
2015-09-13 06:04:31 +02:00
} catch (final IllegalArgumentException e) {
2015-02-22 13:24:48 +01:00
return -1;
}
}
2015-09-13 06:04:31 +02:00
2015-02-22 13:24:48 +01:00
@Override
2015-09-13 06:04:31 +02:00
public String[] getBiomeList() {
2015-02-23 02:32:27 +01:00
final Biome[] biomes = Biome.values();
final 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;
}
2015-09-13 06:04:31 +02:00
2015-02-22 13:24:48 +01:00
@Override
2015-09-13 06:04:31 +02:00
public PlotBlock getPlotBlockFromString(final String block) {
2015-02-23 02:32:27 +01:00
final Material material = Material.valueOf(block.toUpperCase());
2015-09-13 06:04:31 +02:00
if (material == null) {
return new PlotBlock((short) -1, (byte) 0);
}
2015-07-30 16:25:16 +02:00
return new PlotBlock((short) material.getId(), (byte) 0);
2015-02-22 13:24:48 +01:00
}
2015-09-13 06:04:31 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean addItems(final String worldname, final PlotItem items) {
2015-09-11 12:09:22 +02:00
final World world = getWorld(worldname);
final Block block = world.getBlockAt(items.x, items.y, items.z);
2015-09-13 06:04:31 +02:00
if (block == null) {
return false;
}
2015-09-11 12:09:22 +02:00
final BlockState state = block.getState();
2015-09-13 06:04:31 +02:00
if ((state != null) && (state instanceof InventoryHolder)) {
2015-09-11 12:09:22 +02:00
final InventoryHolder holder = ((InventoryHolder) state);
final Inventory inv = holder.getInventory();
2015-09-13 06:04:31 +02:00
for (int i = 0; i < items.id.length; i++) {
2015-09-11 12:09:22 +02:00
final ItemStack item = new ItemStack(items.id[i], items.amount[i], items.data[i]);
inv.addItem(item);
}
state.update(true);
return true;
}
return false;
2015-09-13 06:04:31 +02:00
}
2015-09-13 06:04:31 +02:00
2015-04-14 15:55:00 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean isBlockSolid(final PlotBlock block) {
try {
2015-09-11 12:09:22 +02:00
final Material material = Material.getMaterial(block.id);
2015-09-13 06:04:31 +02:00
if (material.isBlock() && material.isSolid() && !material.hasGravity()) {
2015-09-11 12:09:22 +02:00
final Class<? extends MaterialData> data = material.getData();
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)
2015-09-13 06:04:31 +02:00
|| data.equals(WoodenStep.class)) {
switch (material) {
case NOTE_BLOCK:
2015-09-13 06:04:31 +02:00
case MOB_SPAWNER: {
return false;
}
default:
return true;
}
}
}
return false;
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-04-14 15:55:00 +02:00
return false;
}
}
2015-09-13 06:04:31 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getClosestMatchingName(final PlotBlock block) {
try {
return Material.getMaterial(block.id).name();
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
return null;
}
}
2015-09-13 06:04:31 +02:00
@Override
2015-09-13 06:04:31 +02:00
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
try {
double match;
short id;
byte data;
2015-09-11 12:09:22 +02:00
final String[] split = name.split(":");
2015-09-13 06:04:31 +02:00
if (split.length == 2) {
data = Byte.parseByte(split[1]);
name = split[0];
2015-09-13 06:04:31 +02:00
} else {
data = 0;
}
2015-09-13 06:04:31 +02:00
if (MathMan.isInteger(split[0])) {
id = Short.parseShort(split[0]);
match = 0;
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
final StringComparison<Material>.ComparisonResult comparison = new StringComparison<Material>(name, Material.values()).getBestMatchAdvanced();
match = comparison.match;
2015-09-11 12:09:22 +02:00
id = (short) comparison.best.getId();
}
2015-09-11 12:09:22 +02:00
final PlotBlock block = new PlotBlock(id, data);
final StringComparison<PlotBlock> outer = new StringComparison<PlotBlock>();
return outer.new ComparisonResult(match, block);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {}
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-28 08:06:19 +02:00
@Override
2015-09-13 06:04:31 +02:00
public PlotBlock getBlock(final Location loc) {
2015-07-28 08:06:19 +02:00
final World world = getWorld(loc.getWorld());
final Block block = world.getBlockAt(loc.getX(), loc.getY(), loc.getZ());
2015-09-13 06:04:31 +02:00
if (block == null) {
return new PlotBlock((short) 0, (byte) 0);
}
2015-07-28 08:06:19 +02:00
return new PlotBlock((short) block.getTypeId(), block.getData());
}
2015-02-19 07:08:15 +01:00
}