Prettify BukkitUtil

This commit is contained in:
sauilitired 2018-08-10 20:54:17 +02:00
parent 724fb34b1f
commit 369ccb7c8d

View File

@ -26,10 +26,12 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.*; import org.bukkit.material.*;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@SuppressWarnings({"unused", "WeakerAccess"})
public class BukkitUtil extends WorldUtil { public class BukkitUtil extends WorldUtil {
private static String lastString = null; private static String lastString = null;
@ -43,21 +45,21 @@ public class BukkitUtil extends WorldUtil {
lastPlotPlayer = null; lastPlotPlayer = null;
} }
public static PlotPlayer getPlayer(OfflinePlayer op) { public static PlotPlayer getPlayer(@NonNull final OfflinePlayer op) {
if (op.isOnline()) { if (op.isOnline()) {
return getPlayer(op.getPlayer()); return getPlayer(op.getPlayer());
} }
Player player = OfflinePlayerUtil.loadPlayer(op); final Player player = OfflinePlayerUtil.loadPlayer(op);
player.loadData(); player.loadData();
return new BukkitPlayer(player, true); return new BukkitPlayer(player, true);
} }
public static PlotPlayer getPlayer(Player player) { public static PlotPlayer getPlayer(@NonNull final Player player) {
if (player == lastPlayer) { if (player == lastPlayer) {
return lastPlotPlayer; return lastPlotPlayer;
} }
String name = player.getName(); final String name = player.getName();
PlotPlayer plotPlayer = UUIDHandler.getPlayer(name); final PlotPlayer plotPlayer = UUIDHandler.getPlayer(name);
if (plotPlayer != null) { if (plotPlayer != null) {
return plotPlayer; return plotPlayer;
} }
@ -67,12 +69,12 @@ public class BukkitUtil extends WorldUtil {
return lastPlotPlayer; return lastPlotPlayer;
} }
public static Location getLocation(org.bukkit.Location location) { public static Location getLocation(@NonNull final org.bukkit.Location location) {
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()), return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()),
MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ())); MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()));
} }
public static org.bukkit.Location getLocation(Location location) { public static org.bukkit.Location getLocation(@NonNull final Location location) {
return new org.bukkit.Location(getWorld(location.getWorld()), location.getX(), return new org.bukkit.Location(getWorld(location.getWorld()), location.getX(),
location.getY(), location.getZ()); location.getY(), location.getZ());
} }
@ -81,30 +83,30 @@ public class BukkitUtil extends WorldUtil {
return Bukkit.getWorld(string); return Bukkit.getWorld(string);
} }
public static String getWorld(Entity entity) { public static String getWorld(@NonNull final Entity entity) {
return entity.getWorld().getName(); return entity.getWorld().getName();
} }
public static List<Entity> getEntities(String worldName) { public static List<Entity> getEntities(@NonNull final String worldName) {
World world = getWorld(worldName); World world = getWorld(worldName);
return world != null ? world.getEntities() : new ArrayList<Entity>(); return world != null ? world.getEntities() : new ArrayList<Entity>();
} }
public static Location getLocation(Entity entity) { public static Location getLocation(@NonNull final Entity entity) {
org.bukkit.Location location = entity.getLocation(); final org.bukkit.Location location = entity.getLocation();
String world = location.getWorld().getName(); String world = location.getWorld().getName();
return new Location(world, location.getBlockX(), location.getBlockY(), return new Location(world, location.getBlockX(), location.getBlockY(),
location.getBlockZ()); location.getBlockZ());
} }
public static Location getLocationFull(Entity entity) { public static Location getLocationFull(@NonNull final Entity entity) {
org.bukkit.Location location = entity.getLocation(); final org.bukkit.Location location = entity.getLocation();
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()), return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()),
MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()), location.getYaw(), MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()), location.getYaw(),
location.getPitch()); location.getPitch());
} }
@Override public boolean isWorld(String worldName) { @Override public boolean isWorld(@NonNull final String worldName) {
return getWorld(worldName) != null; return getWorld(worldName) != null;
} }
@ -112,11 +114,12 @@ public class BukkitUtil extends WorldUtil {
return getWorld(world).getBiome(x, z).name(); return getWorld(world).getBiome(x, z).name();
} }
@Override public void setSign(String worldName, int x, int y, int z, String[] lines) { @Override @SuppressWarnings("deprecation") public void setSign(@NonNull final String worldName,
World world = getWorld(worldName); final int x, final int y, final int z, @NonNull final String[] lines) {
Block block = world.getBlockAt(x, y, z); final World world = getWorld(worldName);
final Block block = world.getBlockAt(x, y, z);
// block.setType(Material.AIR); // block.setType(Material.AIR);
Material type = block.getType(); final Material type = block.getType();
if (type != Material.SIGN && type != Material.SIGN_POST) { if (type != Material.SIGN && type != Material.SIGN_POST) {
int data = 2; int data = 2;
if (world.getBlockAt(x, y, z + 1).getType().isSolid()) if (world.getBlockAt(x, y, z + 1).getType().isSolid())
@ -129,7 +132,7 @@ public class BukkitUtil extends WorldUtil {
data = 5; data = 5;
block.setTypeIdAndData(Material.WALL_SIGN.getId(), (byte) data, false); block.setTypeIdAndData(Material.WALL_SIGN.getId(), (byte) data, false);
} }
BlockState blockstate = block.getState(); final BlockState blockstate = block.getState();
if (blockstate instanceof Sign) { if (blockstate instanceof Sign) {
final Sign sign = (Sign) blockstate; final Sign sign = (Sign) blockstate;
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
@ -139,7 +142,7 @@ public class BukkitUtil extends WorldUtil {
} }
} }
@Override public String[] getSign(Location location) { @Override @Nullable public String[] getSign(@NonNull final Location location) {
Block block = getWorld(location.getWorld()) Block block = getWorld(location.getWorld())
.getBlockAt(location.getX(), location.getY(), location.getZ()); .getBlockAt(location.getX(), location.getY(), location.getZ());
if (block != null) { if (block != null) {
@ -151,32 +154,32 @@ public class BukkitUtil extends WorldUtil {
return null; return null;
} }
@Override public Location getSpawn(PlotPlayer player) { @Override public Location getSpawn(@NonNull final PlotPlayer player) {
return getLocation(((BukkitPlayer) player).player.getBedSpawnLocation()); return getLocation(((BukkitPlayer) player).player.getBedSpawnLocation());
} }
@Override public Location getSpawn(String world) { @Override public Location getSpawn(@NonNull final String world) {
org.bukkit.Location temp = getWorld(world).getSpawnLocation(); final org.bukkit.Location temp = getWorld(world).getSpawnLocation();
return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(), return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(),
temp.getYaw(), temp.getPitch()); temp.getYaw(), temp.getPitch());
} }
@Override public void setSpawn(Location location) { @Override public void setSpawn(@NonNull final Location location) {
World world = getWorld(location.getWorld()); final World world = getWorld(location.getWorld());
if (world != null) { if (world != null) {
world.setSpawnLocation(location.getX(), location.getY(), location.getZ()); world.setSpawnLocation(location.getX(), location.getY(), location.getZ());
} }
} }
@Override public void saveWorld(String worldName) { @Override public void saveWorld(@NonNull final String worldName) {
World world = getWorld(worldName); final World world = getWorld(worldName);
if (world != null) { if (world != null) {
world.save(); world.save();
} }
} }
@Override public int getHighestBlock(String world, int x, int z) { @Override public int getHighestBlock(@NonNull final String world, final int x, final int z) {
World bukkitWorld = getWorld(world); final World bukkitWorld = getWorld(world);
// Skip top and bottom block // Skip top and bottom block
int air = 1; int air = 1;
for (int y = bukkitWorld.getMaxHeight() - 1; y >= 0; y--) { for (int y = bukkitWorld.getMaxHeight() - 1; y >= 0; y--) {
@ -202,9 +205,9 @@ public class BukkitUtil extends WorldUtil {
return bukkitWorld.getMaxHeight() - 1; return bukkitWorld.getMaxHeight() - 1;
} }
@Override public int getBiomeFromString(String biomeString) { @Override public int getBiomeFromString(@NonNull final String biomeString) {
try { try {
Biome biome = Biome.valueOf(biomeString.toUpperCase()); final Biome biome = Biome.valueOf(biomeString.toUpperCase());
return Arrays.asList(Biome.values()).indexOf(biome); return Arrays.asList(Biome.values()).indexOf(biome);
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
return -1; return -1;
@ -212,21 +215,21 @@ public class BukkitUtil extends WorldUtil {
} }
@Override public String[] getBiomeList() { @Override public String[] getBiomeList() {
Biome[] biomes = Biome.values(); final Biome[] biomes = Biome.values();
String[] list = new String[biomes.length]; final String[] list = new String[biomes.length];
for (int i = 0; i < biomes.length; i++) { for (int i = 0; i < biomes.length; i++) {
list[i] = biomes[i].name(); list[i] = biomes[i].name();
} }
return list; return list;
} }
@Override public boolean addItems(String worldName, PlotItem items) { @Override public boolean addItems(@NonNull final String worldName, @NonNull final PlotItem items) {
World world = getWorld(worldName); final World world = getWorld(worldName);
Block block = world.getBlockAt(items.x, items.y, items.z); final Block block = world.getBlockAt(items.x, items.y, items.z);
if (block == null) { if (block == null) {
return false; return false;
} }
BlockState state = block.getState(); final BlockState state = block.getState();
if (state instanceof InventoryHolder) { if (state instanceof InventoryHolder) {
InventoryHolder holder = (InventoryHolder) state; InventoryHolder holder = (InventoryHolder) state;
Inventory inv = holder.getInventory(); Inventory inv = holder.getInventory();
@ -240,9 +243,9 @@ public class BukkitUtil extends WorldUtil {
return false; return false;
} }
@Override public boolean isBlockSolid(PlotBlock block) { @Override public boolean isBlockSolid(@NonNull final PlotBlock block) {
try { try {
Material material = Material.getMaterial(block.id); final Material material = Material.getMaterial(block.id);
if (material.isBlock() && material.isSolid() && !material.hasGravity()) { if (material.isBlock() && material.isSolid() && !material.hasGravity()) {
Class<? extends MaterialData> data = material.getData(); Class<? extends MaterialData> data = material.getData();
if (data.equals(MaterialData.class) && !material.isTransparent() && material if (data.equals(MaterialData.class) && !material.isTransparent() && material
@ -264,7 +267,7 @@ public class BukkitUtil extends WorldUtil {
} }
} }
@Override public String getClosestMatchingName(PlotBlock block) { @Override public String getClosestMatchingName(@NonNull final PlotBlock block) {
try { try {
return Material.getMaterial(block.id).name(); return Material.getMaterial(block.id).name();
} catch (Exception ignored) { } catch (Exception ignored) {
@ -272,9 +275,9 @@ public class BukkitUtil extends WorldUtil {
} }
} }
@Override public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) { @Override @Nullable public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
try { try {
Material material = Material.valueOf(name.toUpperCase()); final Material material = Material.valueOf(name.toUpperCase());
return new StringComparison<PlotBlock>().new ComparisonResult(0, return new StringComparison<PlotBlock>().new ComparisonResult(0,
PlotBlock.get((short) material.getId(), (byte) 0)); PlotBlock.get((short) material.getId(), (byte) 0));
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
@ -308,9 +311,10 @@ public class BukkitUtil extends WorldUtil {
return null; return null;
} }
@Override public void setBiomes(String worldName, RegionWrapper region, String biomeString) { @Override public void setBiomes(@NonNull final String worldName, @NonNull final RegionWrapper region,
World world = getWorld(worldName); @NonNull final String biomeString) {
Biome biome = Biome.valueOf(biomeString.toUpperCase()); final World world = getWorld(worldName);
final Biome biome = Biome.valueOf(biomeString.toUpperCase());
for (int x = region.minX; x <= region.maxX; x++) { for (int x = region.minX; x <= region.maxX; x++) {
for (int z = region.minZ; z <= region.maxZ; z++) { for (int z = region.minZ; z <= region.maxZ; z++) {
world.setBiome(x, z, biome); world.setBiome(x, z, biome);
@ -318,9 +322,9 @@ public class BukkitUtil extends WorldUtil {
} }
} }
@Override public PlotBlock getBlock(Location location) { @Override public PlotBlock getBlock(@NonNull final Location location) {
World world = getWorld(location.getWorld()); final World world = getWorld(location.getWorld());
Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ()); final Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ());
if (block == null) { if (block == null) {
return PlotBlock.EVERYTHING; return PlotBlock.EVERYTHING;
} }