下面列出了怎么用org.bukkit.util.BlockIterator的API类实例代码及写法,或者点击链接到github查看源代码。
private List<Block> getLineOfSight(Set<Material> transparent, int maxDistance, int maxLength) {
if (maxDistance > 120) {
maxDistance = 120;
}
ArrayList<Block> blocks = new ArrayList<Block>();
Iterator<Block> itr = new BlockIterator(this, maxDistance);
while (itr.hasNext()) {
Block block = itr.next();
blocks.add(block);
if (maxLength != 0 && blocks.size() > maxLength) {
blocks.remove(0);
}
Material material = block.getType();
if (transparent == null) {
if (!material.equals(Material.AIR)) {
break;
}
} else {
if (!transparent.contains(material)) {
break;
}
}
}
return blocks;
}
private boolean hasCleanShot(Location shootHere, Location targetHere) {
double x = shootHere.getX();
double y = shootHere.getY();
double z = shootHere.getZ();
double x1 = targetHere.getX();
double y1 = targetHere.getY();
double z1 = targetHere.getZ();
Vector start = new Vector(x, y, z);
Vector end = new Vector (x1, y1, z1);
BlockIterator bi = new BlockIterator(shootHere.getWorld(), start, end, 0, (int) shootHere.distance(targetHere));
while (bi.hasNext()) {
Block block = bi.next();
// System.out.println(Civs.getPrefix() + ((int) block.getLocation().getX()) +
// ":" + ((int) block.getLocation().getY()) + ":" +
// ((int) block.getLocation().getZ()) + " " + !Util.isSolidBlock(block.getType()));
if (!Util.isSolidBlock(block.getType())) {
return false;
}
}
return true;
}
private List<Block> getLineOfSight(HashSet<Byte> transparent, int maxDistance, int maxLength) {
if (maxDistance > 120) {
maxDistance = 120;
}
ArrayList<Block> blocks = new ArrayList<Block>();
Iterator<Block> itr = new BlockIterator(this, maxDistance);
while (itr.hasNext()) {
Block block = itr.next();
blocks.add(block);
if (maxLength != 0 && blocks.size() > maxLength) {
blocks.remove(0);
}
int id = block.getTypeId();
if (transparent == null) {
if (id != 0) {
break;
}
} else {
if (!transparent.contains((byte) id)) {
break;
}
}
}
return blocks;
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("Can't run command by Console", sender));
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop != null) {
if (shop.getModerator().isModerator(((Player) sender).getUniqueId()) || QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.control")) {
shop.setShopType(ShopType.BUYING);
// shop.setSignText();
shop.update();
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.now-buying", sender, Util.getItemStackName(shop.getItem())));
} else {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-managed-shop", sender));
}
return;
}
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Only player can run this command.");
return;
}
final BlockIterator bIt = new BlockIterator((Player) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) {
continue;
}
shop.setUnlimited(!shop.isUnlimited());
// shop.setSignText();
shop.update();
if (shop.isUnlimited()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.toggle-unlimited.unlimited", sender));
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.toggle-unlimited.limited", sender));
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, ChatColor.RED + "Only players may use that command.");
return;
}
final Player p = (Player) sender;
final BlockIterator bIt = new BlockIterator(p, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) {
continue;
}
if (shop.getModerator().isModerator(((Player) sender).getUniqueId())
|| QuickShop.getPermissionManager().hasPermission(p, "quickshop.other.destroy")) {
//shop.onUnload();
shop.delete();
plugin.log("Deleting shop "+shop+" request by /qs remove command.");
} else {
MsgUtil.sendMessage(sender, ChatColor.RED + MsgUtil.getMessage("no-permission", sender));
}
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("Can't run command by Console", sender));
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop != null) {
if (shop.getModerator().isModerator(((Player) sender).getUniqueId()) || QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.control")) {
shop.setShopType(ShopType.SELLING);
// shop.setSignText();
shop.update();
MsgUtil.sendMessage(sender,
MsgUtil.getMessage("command.now-selling", sender, Util.getItemStackName(shop.getItem())));
return;
} else {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-managed-shop", sender));
return;
}
}
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
protected void check(InteractWorldEvent e) {
HawkPlayer pp = e.getHawkPlayer();
Vector eyePos = pp.getPosition().clone().add(new Vector(0, pp.isSneaking() ? 1.54 : 1.62, 0));
Vector direction = MathPlus.getDirection(pp.getYaw(), pp.getPitch());
Location bLoc = e.getTargetedBlockLocation();
Block b = bLoc.getBlock();
WrappedBlock bNMS = WrappedBlock.getWrappedBlock(b, pp.getClientVersion());
AABB targetAABB = new AABB(bNMS.getHitBox().getMin(), bNMS.getHitBox().getMax());
double distance = targetAABB.distanceToPosition(eyePos);
BlockIterator iter = new BlockIterator(pp.getWorld(), eyePos, direction, 0, (int) distance + 2);
while (iter.hasNext()) {
Block bukkitBlock = iter.next();
if (bukkitBlock.getType() == Material.AIR || bukkitBlock.isLiquid())
continue;
if (bukkitBlock.getLocation().equals(bLoc))
break;
WrappedBlock iterBNMS = WrappedBlock.getWrappedBlock(bukkitBlock, pp.getClientVersion());
AABB checkIntersection = new AABB(iterBNMS.getHitBox().getMin(), iterBNMS.getHitBox().getMax());
Vector occludeIntersection = checkIntersection.intersectsRay(new Ray(eyePos, direction), 0, Float.MAX_VALUE);
if (occludeIntersection != null) {
if (occludeIntersection.distance(eyePos) < distance) {
Placeholder ph = new Placeholder("type", iterBNMS.getBukkitBlock().getType());
punishAndTryCancelAndBlockRespawn(pp, 1, e, ph);
return;
}
}
}
}
public Block getTarget(Location from, int distance, Set<Byte> transparentTypeIds) {
BlockIterator itr = new BlockIterator(from, 0, distance);
while (itr.hasNext()) {
Block block = itr.next();
int id = block.getType().getId();
if (transparentTypeIds == null) {
if (id == 0) continue;
} else if (transparentTypeIds.contains((byte) id)) {
continue;
}
return block;
}
return null;
}
@EventHandler
public void onProjectileHitEvent(ProjectileHitEvent event) {
final Projectile projectile = event.getEntity();
final ProjectileDefinition projectileDefinition = Projectiles.getProjectileDefinition(projectile);
if(projectileDefinition == null) return;
final Filter filter = projectileDefinition.destroyFilter();
if(filter == null) return;
final BlockIterator blockIterator = new BlockIterator(projectile.getWorld(), projectile.getLocation().toVector(), projectile.getVelocity().normalize(), 0d, 2);
Block hitBlock = null;
while(blockIterator.hasNext()) {
hitBlock = blockIterator.next();
if(hitBlock.getType() != Material.AIR) break;
}
if(hitBlock != null) {
final MatchPlayer shooter = projectile.getShooter() instanceof Player ? getMatch().getPlayer((Player) projectile.getShooter()) : null;
final IQuery query = shooter != null ? new PlayerBlockEventQuery(shooter, event, hitBlock.getState())
: new BlockEventQuery(event, hitBlock);
if(filter.query(query).isAllowed()) {
final BlockTransformEvent bte = new BlockTransformEvent(event, hitBlock, Material.AIR);
match.callEvent(bte);
if(!bte.isCancelled()) {
hitBlock.setType(Material.AIR);
projectile.remove();
}
}
}
}
/**
* @param start
* @param dir
* @param dist
* @throws IllegalStateException randomly (Bukkit bug)
*/
public BlockLineIterator(final Location start, final Vector dir, final double dist) throws IllegalStateException {
super(new BlockIterator(start.getWorld(), fitInWorld(start, dir), dir, 0, 0), new NullableChecker<Block>() {
private final double distSq = dist * dist;
@Override
public boolean check(final @Nullable Block b) {
return b != null && b.getLocation().add(0.5, 0.5, 0.5).distanceSquared(start) >= distSq;
}
}, false);
}
/**
* Get to know where the {@link Vector} intersects with a {@link org.bukkit.block.Block}.
* Non-Occluding {@link Block}s as defined in {@link BlockUtils#isReallyOccluding(Material)} are ignored.
*
* @param start the starting {@link Location}
* @param direction the {@link Vector} which should be checked
*
* @return The length when the {@link Vector} intersects or 0 if no intersection was found
*/
public static double getDistanceToFirstIntersectionWithBlock(final Location start, final Vector direction)
{
final int length = (int) Math.floor(direction.length());
Preconditions.checkNotNull(start.getWorld(), "RayTrace: Unknown start world.");
if (length >= 1) {
if (RAY_TRACING) {
RayTraceResult result = start.getWorld().rayTraceBlocks(start, direction, length, FluidCollisionMode.NEVER, true);
// Hit nothing or the other player
if (result == null || result.getHitBlock() == null) {
return 0;
}
return start.toVector().distance(result.getHitPosition());
} else {
try {
final BlockIterator blockIterator = new BlockIterator(start.getWorld(), start.toVector(), direction, 0, length);
Block block;
while (blockIterator.hasNext()) {
block = blockIterator.next();
// Account for a Spigot bug: BARRIER and MOB_SPAWNER are not occluding blocks
if (BlockUtils.isReallyOccluding(block.getType())) {
// Use the middle location of the Block instead of the simple location.
return block.getLocation().clone().add(0.5, 0.5, 0.5).distance(start);
}
}
} catch (IllegalStateException exception) {
// Just in case the start block could not be found for some reason or a chunk is loaded async.
return 0;
}
}
}
return 0;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public Block getHitBlock(ProjectileHitEvent event) {
BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
Block hitBlock = null;
while (iterator.hasNext()) {
hitBlock = iterator.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
return hitBlock;
}
@Override
public void execute(CommandSender sender, String[] args) {
if(!sender.hasPermission("areashop.delsign")) {
plugin.message(sender, "delsign-noPermission");
return;
}
if(!(sender instanceof Player)) {
plugin.message(sender, "cmd-onlyByPlayer");
return;
}
Player player = (Player)sender;
// Get the sign
Block block = null;
BlockIterator blockIterator = new BlockIterator(player, 100);
while(blockIterator.hasNext() && block == null) {
Block next = blockIterator.next();
if(next.getType() != Material.AIR) {
block = next;
}
}
if(block == null || !Materials.isSign(block.getType())) {
plugin.message(sender, "delsign-noSign");
return;
}
RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
if(regionSign == null) {
plugin.message(sender, "delsign-noRegion");
return;
}
plugin.message(sender, "delsign-success", regionSign.getRegion());
regionSign.remove();
}
@EventHandler
public void onProjectileHitEvent(ProjectileHitEvent event) {
Projectile projectile = event.getEntity();
ProjectileDefinition projectileDefinition = getProjectileDefinition(projectile);
if (projectileDefinition == null) return;
Filter filter = projectileDefinition.destroyFilter;
if (filter == null) return;
BlockIterator it =
new BlockIterator(
projectile.getWorld(),
projectile.getLocation().toVector(),
projectile.getVelocity().normalize(),
0d,
2);
Block hitBlock = null;
while (it.hasNext()) {
hitBlock = it.next();
if (hitBlock.getType() != Material.AIR) {
break;
}
}
if (hitBlock != null) {
MatchPlayer player =
projectile.getShooter() instanceof Player
? match.getPlayer((Player) projectile.getShooter())
: null;
Query query =
player != null
? new PlayerBlockQuery(event, player, hitBlock.getState())
: new BlockQuery(event, hitBlock);
if (filter.query(query).isAllowed()) {
BlockTransformEvent bte = new BlockTransformEvent(event, hitBlock, Material.AIR);
match.callEvent(bte);
if (!bte.isCancelled()) {
hitBlock.setType(Material.AIR);
projectile.remove();
}
}
}
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Can't run by Console");
return;
}
if (cmdArg.length < 1) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.no-amount-given", sender));
return;
}
final int add;
try {
add = Integer.parseInt(cmdArg[0]);
} catch (NumberFormatException e) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("thats-not-a-number", sender));
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) {
continue;
}
shop.add(shop.getItem(), add);
MsgUtil.sendMessage(sender, MsgUtil.getMessage("refill-success", sender));
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Only player can run this command");
return;
}
if (cmdArg.length < 1) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.no-owner-given", sender));
return;
}
final BlockIterator bIt = new BlockIterator((Player) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) {
continue;
}
@SuppressWarnings("deprecation") final OfflinePlayer newShopOwner = plugin.getServer().getOfflinePlayer(cmdArg[0]);
if (newShopOwner.getName() == null) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("unknown-player", null));
return;
}
shop.setOwner(newShopOwner.getUniqueId());
//shop.setSignText();
//shop.update();
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.new-owner", sender, newShopOwner.getName()));
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "This command can't be run by console");
return;
}
final Player p = (Player) sender;
final ItemStack item = p.getInventory().getItemInMainHand();
if (item.getType() == Material.AIR) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("no-anythings-in-your-hand", sender));
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
while (bIt.hasNext()) {
final Block b = bIt.next();
if (!Util.canBeShop(b)) {
continue;
}
BlockFace blockFace;
try {
blockFace = p.getFacing();
} catch (Throwable throwable) {
blockFace = Util.getYawFace(p.getLocation().getYaw());
}
if (!plugin.getShopManager().canBuildShop(p, b, blockFace)) {
// As of the new checking system, most plugins will tell the
// player why they can't create a shop there.
// So telling them a message would cause spam etc.
Util.debugLog("Util report you can't build shop there.");
return;
}
if (Util.getSecondHalf(b) != null
&& !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.create.double")) {
MsgUtil.sendMessage(p, MsgUtil.getMessage("no-double-chests", sender));
return;
}
if (Util.isBlacklisted(item)
&& !QuickShop.getPermissionManager()
.hasPermission(p, "quickshop.bypass." + item.getType().name())) {
MsgUtil.sendMessage(p, MsgUtil.getMessage("blacklisted-item", sender));
return;
}
if (cmdArg.length >= 1) {
plugin.getShopManager().handleChat(p, cmdArg[0], true);
return;
}
// Send creation menu.
final Info info =
new Info(
b.getLocation(),
ShopAction.CREATE,
p.getInventory().getItemInMainHand(),
b.getRelative(p.getFacing().getOppositeFace()));
plugin.getShopManager().getActions().put(p.getUniqueId(), info);
MsgUtil.sendMessage(p,
MsgUtil.getMessage("how-much-to-trade-for", sender, Util.getItemStackName(item), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(p, "quickshop.create.stacks") ? item.getAmount() : 1)));
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "This command can't be run by console");
return;
}
if (cmdArg.length < 1) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.bulk-size-not-set", sender));
return;
}
int amount;
try {
amount = Integer.parseInt(cmdArg[0]);
} catch (NumberFormatException e) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-a-integer", sender, cmdArg[0]));
return;
}
final BlockIterator bIt = new BlockIterator((Player) sender, 10);
// Loop through every block they're looking at upto 10 blocks away
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop != null) {
if (shop.getModerator().isModerator(((Player) sender).getUniqueId()) || sender.hasPermission("quickshop.other.amount")) {
if (amount <= 0 || amount > Util.getItemMaxStackSize(shop.getItem().getType())) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.invalid-bulk-amount", sender, Integer.toString(amount)));
return;
}
shop.getItem().setAmount(amount);
shop.refresh();
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.bulk-size-now", sender, Integer.toString(shop.getItem().getAmount()), Util.getItemStackName(shop.getItem())));
return;
} else {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-managed-shop", sender));
}
}
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Can't run this command from Console");
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) {
continue;
}
if (shop instanceof ContainerShop) {
final ContainerShop cs = (ContainerShop) shop;
final Inventory inventory = cs.getInventory();
if (inventory == null) {
// TODO: 24/11/2019 Send message about that issue.
return;
}
cs.getInventory().clear();
MsgUtil.sendMessage(sender, MsgUtil.getMessage("empty-success", sender));
} else {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
return;
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Only player can execute this command.");
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null || !shop.getModerator().isModerator(((Player) sender).getUniqueId())) {
continue;
}
switch (cmdArg.length) {
case 1:
switch (cmdArg[0]) {
case "clear":
shop.clearStaffs();
MsgUtil.sendMessage(sender, MsgUtil.getMessage("shop-staff-cleared", sender));
return;
case "list":
final List<UUID> staffs = shop.getStaffs();
if (staffs.isEmpty()) {
MsgUtil.sendMessage(sender,
ChatColor.GREEN
+ MsgUtil.getMessage("tableformat.left_begin", sender)
+ "Empty");
return;
}
for (UUID uuid : staffs) {
MsgUtil.sendMessage(sender,
ChatColor.GREEN
+ MsgUtil.getMessage("tableformat.left_begin", sender)
+ Bukkit.getOfflinePlayer(uuid).getName());
}
return;
case "add":
case "del":
default:
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.wrong-args", sender));
return;
}
case 2:
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(cmdArg[1]);
String offlinePlayerName = offlinePlayer.getName();
if (offlinePlayerName == null) {
offlinePlayerName = "null";
}
switch (cmdArg[0]) {
case "add":
shop.addStaff(offlinePlayer.getUniqueId());
MsgUtil.sendMessage(sender, MsgUtil.getMessage("shop-staff-added", sender, offlinePlayerName));
return;
case "del":
shop.delStaff(offlinePlayer.getUniqueId());
MsgUtil.sendMessage(sender,
MsgUtil.getMessage("shop-staff-deleted", sender, offlinePlayerName));
return;
default:
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.wrong-args", sender));
return;
}
default:
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.wrong-args", sender));
return;
}
}
//no match shop
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "Can't run command by Console");
return;
}
final BlockIterator bIt = new BlockIterator((Player) sender, 10);
// Loop through every block they're looking at upto 10 blocks away
if (!bIt.hasNext()) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
return;
}
while (bIt.hasNext()) {
final Block b = bIt.next();
final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop != null) {
if (!shop.getModerator().isModerator(((Player) sender).getUniqueId()) && !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.item")) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-managed-shop", sender));
return;
}
ItemStack itemStack = ((Player) sender).getInventory().getItemInMainHand().clone();
if (itemStack.getType() == Material.AIR) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.no-trade-item", sender));
return;
}
if (Util.isBlacklisted(itemStack) && !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.bypass." + itemStack.getType().name())) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("blacklisted-item", sender));
return;
}
if (!plugin.isAllowStack() && !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.create.stacks")) {
itemStack.setAmount(1);
}
shop.setItem(itemStack);
MsgUtil.sendItemholochat(shop, shop.getItem(), (Player) sender, MsgUtil.getMessage("command.trade-item-now", sender, Integer.toString(shop.getItem().getAmount()), Util.getItemStackName(shop.getItem())));
return;
}
// shop.setSignText();
}
MsgUtil.sendMessage(sender, MsgUtil.getMessage("not-looking-at-shop", sender));
}
@Override
public void onCommand(
@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!(sender instanceof Player)) {
MsgUtil.sendMessage(sender, "This command can't be run by console");
return;
}
final Player p = (Player) sender;
final ItemStack item = p.getInventory().getItemInMainHand();
if (item.getType() == Material.AIR) {
MsgUtil.sendMessage(sender, MsgUtil.getMessage("no-anythings-in-your-hand", sender));
return;
}
final BlockIterator bIt = new BlockIterator((LivingEntity) sender, 10);
while (bIt.hasNext()) {
final Block b = bIt.next();
if (!Util.canBeShop(b)) {
continue;
}
if (p.isOnline()) {
Result result = plugin.getPermissionChecker().canBuild(p, b);
if (!result.isSuccess()) {
MsgUtil.sendMessage(p, MsgUtil.getMessage("3rd-plugin-build-check-failed", p, result.getMessage()));
Util.debugLog("Failed to create shop because protection check failed, found:" + result.getMessage());
return;
}
}
BlockFace blockFace;
try {
blockFace = p.getFacing();
} catch (Throwable throwable) {
blockFace = Util.getYawFace(p.getLocation().getYaw());
}
if (!plugin.getShopManager().canBuildShop(p, b, blockFace)) {
// As of the new checking system, most plugins will tell the
// player why they can't create a shop there.
// So telling them a message would cause spam etc.
Util.debugLog("Util report you can't build shop there.");
return;
}
if (Util.getSecondHalf(b) != null
&& !QuickShop.getPermissionManager().hasPermission(p, "quickshop.create.double")) {
MsgUtil.sendMessage(p, MsgUtil.getMessage("no-double-chests", sender));
return;
}
if (Util.isBlacklisted(item)
&& !QuickShop.getPermissionManager()
.hasPermission(p, "quickshop.bypass." + item.getType().name())) {
MsgUtil.sendMessage(p, MsgUtil.getMessage("blacklisted-item", sender));
return;
}
// Send creation menu.
plugin
.getShopManager()
.getActions()
.put(
p.getUniqueId(),
new Info(
b.getLocation(),
ShopAction.CREATE,
p.getInventory().getItemInMainHand(),
b.getRelative(p.getFacing().getOppositeFace())));
if (cmdArg.length >= 1) {
plugin.getShopManager().handleChat(p, cmdArg[0]);
return;
}
MsgUtil.sendMessage(p,
MsgUtil.getMessage("how-much-to-trade-for", sender, Util.getItemStackName(item), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(p, "quickshop.create.stacks") ? item.getAmount() : 1)));
return;
}
}
private void processDirection(InteractEntityEvent e, float yaw, float pitch) {
Entity entity = e.getEntity();
if (!(entity instanceof Player) && !CHECK_OTHER_ENTITIES)
return;
Player attacker = e.getPlayer();
int ping = ServerUtils.getPing(attacker);
if (ping > PING_LIMIT && PING_LIMIT != -1)
return;
HawkPlayer att = e.getHawkPlayer();
Location attackerEyeLocation = att.getPosition().clone().add(new Vector(0, 1.62, 0)).toLocation(att.getWorld());
Vector attackerDirection = MathPlus.getDirection(yaw, pitch);
double maxReach = MAX_REACH;
if (attacker.getGameMode() == GameMode.CREATIVE)
maxReach += 1.9;
Vector victimLocation;
if (LAG_COMPENSATION)
//No need to add 50ms; the move and attack are already chronologically so close together
victimLocation = hawk.getLagCompensator().getHistoryLocation(ping, e.getEntity()).toVector();
else
victimLocation = e.getEntity().getLocation().toVector();
Vector eyePos = new Vector(attackerEyeLocation.getX(), attacker.isSneaking() ? attackerEyeLocation.getY() - 0.08 : attackerEyeLocation.getY(), attackerEyeLocation.getZ());
Vector direction = new Vector(attackerDirection.getX(), attackerDirection.getY(), attackerDirection.getZ());
Ray attackerRay = new Ray(eyePos, direction);
AABB victimAABB;
victimAABB = WrappedEntity.getWrappedEntity(entity).getHitbox(victimLocation);
victimAABB.expand(BOX_EPSILON, BOX_EPSILON, BOX_EPSILON);
Vector intersectVec3d = victimAABB.intersectsRay(attackerRay, 0, Float.MAX_VALUE);
if (DEBUG_HITBOX) {
victimAABB.highlight(hawk, attacker.getWorld(), 0.29);
}
if (DEBUG_RAY) {
attackerRay.highlight(hawk, attacker.getWorld(), maxReach, 0.1);
}
if (intersectVec3d != null) {
Location intersect = new Location(attacker.getWorld(), intersectVec3d.getX(), intersectVec3d.getY(), intersectVec3d.getZ());
double interDistance = intersect.distance(attackerEyeLocation);
if (interDistance > maxReach) {
punish(att, 1, true, e, new Placeholder("type", "Reach: " + MathPlus.round(interDistance, 2) + "m"));
return;
}
if (CHECK_OCCLUSION && interDistance > 1D) {
BlockIterator iter = new BlockIterator(attacker.getWorld(), eyePos, attackerDirection, 0, (int) interDistance + 1);
while (iter.hasNext()) {
Block bukkitBlock = iter.next();
if (bukkitBlock.getType() == Material.AIR || bukkitBlock.isLiquid())
continue;
WrappedBlock b = WrappedBlock.getWrappedBlock(bukkitBlock, att.getClientVersion());
Vector intersection = b.getHitBox().intersectsRay(new Ray(attackerEyeLocation.toVector(), attackerDirection), 0, Float.MAX_VALUE);
if (intersection != null) {
if (intersection.distance(eyePos) < interDistance) {
punish(att, 1, true, e, new Placeholder("type", "Interacted through " + b.getBukkitBlock().getType()));
return;
}
}
}
}
} else if (CHECK_BOX_INTERSECTION) {
punish(att, 1, true, e, new Placeholder("type", "Did not hit hitbox."));
return;
}
reward(att); //reward player
}
@Override
public void execute(CommandSender sender, String[] args) {
if(!sender.hasPermission("areashop.addsign")) {
plugin.message(sender, "addsign-noPermission");
return;
}
if(!(sender instanceof Player)) {
plugin.message(sender, "cmd-onlyByPlayer");
return;
}
Player player = (Player)sender;
// Get the sign
Block block = null;
BlockIterator blockIterator = new BlockIterator(player, 100);
while(blockIterator.hasNext() && block == null) {
Block next = blockIterator.next();
if(next.getType() != Material.AIR) {
block = next;
}
}
if(block == null || !Materials.isSign(block.getType())) {
plugin.message(sender, "addsign-noSign");
return;
}
GeneralRegion region;
if(args.length > 1) {
// Get region by argument
region = plugin.getFileManager().getRegion(args[1]);
if(region == null) {
plugin.message(sender, "cmd-notRegistered", args[1]);
return;
}
} else {
// Get region by sign position
List<GeneralRegion> regions = Utils.getImportantRegions(block.getLocation());
if(regions.isEmpty()) {
plugin.message(sender, "addsign-noRegions");
return;
} else if(regions.size() > 1) {
plugin.message(sender, "addsign-couldNotDetect", regions.get(0).getName(), regions.get(1).getName());
return;
}
region = regions.get(0);
}
String profile = null;
if(args.length > 2) {
profile = args[2];
Set<String> profiles = plugin.getConfig().getConfigurationSection("signProfiles").getKeys(false);
if(!profiles.contains(profile)) {
plugin.message(sender, "addsign-wrongProfile", Utils.createCommaSeparatedList(profiles), region);
return;
}
}
RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
if(regionSign != null) {
plugin.message(sender, "addsign-alreadyRegistered", regionSign.getRegion());
return;
}
region.getSignsFeature().addSign(block.getLocation(), block.getType(), plugin.getBukkitHandler().getSignFacing(block), profile);
if(profile == null) {
plugin.message(sender, "addsign-success", region);
} else {
plugin.message(sender, "addsign-successProfile", profile, region);
}
region.update();
}