Java Code Examples for org.bukkit.util.BlockIterator#next()

The following examples show how to use org.bukkit.util.BlockIterator#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrowTurret.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
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;
    }
 
Example 2
Source File: SubCommand_Buy.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@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));
}
 
Example 3
Source File: SubCommand_Remove.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@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));
}
 
Example 4
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: DelsignCommand.java    From AreaShop with GNU General Public License v3.0 5 votes vote down vote up
@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();
}
 
Example 6
Source File: SubCommand_Sell.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@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));
}
 
Example 7
Source File: ProjectileMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@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();
            }
        }
    }
}
 
Example 8
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 9
Source File: LoseCheckerTask.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
	for (Game game : gameManager.getGames()) {
		if (game.getGameState() != GameState.INGAME) {
			continue;
		}
		
		Set<SpleefPlayer> deathCandidates = null;
		final boolean isLiquidDeathzone = game.getPropertyValue(GameProperty.USE_LIQUID_DEATHZONE);
		
		for (SpleefPlayer player : game.getPlayers()) {
			Location playerLoc = player.getBukkitPlayer().getLocation();
			
			boolean isDeathCandidate = isInsideDeathzone(playerLoc, game, isLiquidDeathzone);
			if (!isDeathCandidate && recentLocations.containsKey(player)) {
				//Try to check every block the player has passed between the recent location and his location now
				Location recent = recentLocations.get(player);
				org.bukkit.util.Vector direction = playerLoc.clone().subtract(recent).toVector();
				int directionLength = (int) direction.length();
				
				if (directionLength > 0) {
					BlockIterator iterator = new BlockIterator(game.getWorld(), recent.toVector(), direction, 0D, directionLength);
					while (iterator.hasNext()) {
						Block passedBlock = iterator.next();
						
						if (isInsideDeathzone(passedBlock.getLocation(), game, isLiquidDeathzone)) {
							isDeathCandidate = true;
							break;
						}
					}
				}
			}
			
			if (isDeathCandidate) {
				//Lazy initialization for performance optimization
				if (deathCandidates == null) {
					deathCandidates = Sets.newHashSet();
				}
				
				deathCandidates.add(player);
			}
			
			recentLocations.put(player, playerLoc);
		}
		
		if (deathCandidates != null) {
			for (SpleefPlayer deathCandidate : deathCandidates) {
				game.requestLose(deathCandidate, QuitCause.LOSE);
			}
		}
	}
}
 
Example 10
Source File: ProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@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();
      }
    }
  }
}
 
Example 11
Source File: Util.java    From ObsidianDestroyer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isTargetsPathBlocked(Location tLoc, Location dLoc, boolean useOnlyMaterialListing) {

        // check world
        if (!dLoc.getWorld().getName().equalsIgnoreCase(tLoc.getWorld().getName())) {
            return false;
        }

        // if the distance is too close... the path is not blocked ;)
        if (dLoc.distance(tLoc) <= 0.9) {
            return false;
        }

        // try to iterate through blocks between dLoc and tLoc
        try {
            // Create a vector block trace from the detonator location to damaged block's location
            final BlockIterator blocksInPath = new BlockIterator(tLoc.getWorld(), dLoc.toVector(), tLoc.toVector().subtract(dLoc.toVector()).normalize(), 0.5, (int) dLoc.distance(tLoc));

            // iterate through the blocks in the path
            int over = 0; // prevents rare case of infinite loop and server crash
            while (blocksInPath.hasNext() && over < 128) {
                over++;
                // the next block
                final Block block = blocksInPath.next();
                if (block == null) {
                    continue;
                }
                // check if next block is the target block
                if (tLoc.getWorld().getName().equals(block.getWorld().getName()) &&
                        tLoc.getBlockX() == block.getX() &&
                        tLoc.getBlockY() == block.getY() &&
                        tLoc.getBlockZ() == block.getZ()) {
                    // ignore target block
                    continue;
                }

                // check if the block material is being handled
                if (useOnlyMaterialListing) {
                    // only handle for certain case as to not interfere with all explosions
                    if (MaterialManager.getInstance().contains(block.getType().name(), block.getData())) {
                        return true;
                    } else {
                        continue;
                    }
                }
                // check if the block material is a solid
                if (!isNonSolid(block.getType())) {
                    return true;
                }
            }
        } catch (Exception e) {
            // ignore the error and return no targets in path
        }
        return false;
    }
 
Example 12
Source File: FlagSplegg.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onProjectileHit(ProjectileHitEvent event) {
	Projectile projectile = event.getEntity();
	if (!(projectile instanceof Egg)) {
		return;
	}
	
	ProjectileSource source = projectile.getShooter();
	
	if (!(source instanceof Player)) {
		return;
	}
	
	SpleefPlayer shooter = getHeavySpleef().getSpleefPlayer(source);
	Game game = getHeavySpleef().getGameManager().getGame(shooter);
	if (game != this.game) {
		return;
	}
	
	projectile.remove();
	
	if (game == null || game.getGameState() != GameState.INGAME) {
		return;
	}
	
	// Use a BlockIterator to determine where the arrow has hit the ground
	BlockIterator blockIter = new BlockIterator(projectile.getWorld(), 
			projectile.getLocation().toVector(), 
			projectile.getVelocity().normalize(), 
			0, 4);
	
	Block blockHit = null;
	
	while (blockIter.hasNext()) {
		blockHit = blockIter.next();
		
		if (blockHit.getType() != Material.AIR) {
			break;
		}
	}
	
	if (!game.canSpleef(blockHit)) {
		//Cannot remove this block
		return;
	}
	
	game.addBlockBroken(shooter, blockHit);
	Material type = blockHit.getType();
	blockHit.setType(Material.AIR);
	
	World world = blockHit.getWorld();
	
	if (type == Material.TNT) {
		Location spawnLocation = blockHit.getLocation().add(0.5, 0, 0.5);
		TNTPrimed tnt = (TNTPrimed) world.spawnEntity(spawnLocation, EntityType.PRIMED_TNT);
		tnt.setMetadata(TNT_METADATA_KEY, new FixedMetadataValue(getHeavySpleef().getPlugin(), game));
		tnt.setYield(3);
		tnt.setFuseTicks(0);
	} else {
           Sound chickenEggPopSound = Game.getSoundEnumType("CHICKEN_EGG_POP", "CHICKEN_EGG");
           if (chickenEggPopSound != null) {
               projectile.getWorld().playSound(blockHit.getLocation(), chickenEggPopSound, 1.0f, 0.7f);
           }
	}
}
 
Example 13
Source File: FightHitbox.java    From Hawk with GNU General Public License v3.0 4 votes vote down vote up
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
}
 
Example 14
Source File: ExtensionLobbyWall.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
public void loopSigns(SignLooper looper) {
	Vector startVec = new Vector(start.getBlockX(), start.getBlockY(), start.getBlockZ());
	Vector endVec = new Vector(end.getBlockX(), end.getBlockY(), end.getBlockZ());
	
	Vector directionVec = direction.mod();
	
	int maxDistance = Math.abs(direction == BlockFace2D.NORTH || direction == BlockFace2D.SOUTH ? endVec.getBlockZ() - startVec.getBlockZ()
			: endVec.getBlockX() - startVec.getBlockX());
          if (maxDistance == 0) {
              maxDistance = 1;
          }

	BlockIterator iterator = new BlockIterator(world, startVec, directionVec, 0, maxDistance);
	
	for (int i = 0; iterator.hasNext(); i++) {
		Block block = iterator.next();
		
		if (block.getType() != Material.WALL_SIGN) {
			continue;
		}

              Vector blockVec = block.getLocation().toVector();
              Map<String, Object> preMeta = metadata == null ? null : metadata.get(blockVec);

		Sign sign = (Sign) block.getState();
		WrappedMetadataSign wrappedMetadataSign = new WrappedMetadataSign(sign, preMeta);

		SignLooper.LoopReturn loopReturn = looper.loop(i, wrappedMetadataSign);
              if (preMeta == null && wrappedMetadataSign.metadataValues != null) {
                  if (metadata == null) {
                      enableSignMetadata();
                  }

                  metadata.put(blockVec, wrappedMetadataSign.metadataValues);
              }

		if (loopReturn == SignLooper.LoopReturn.CONTINUE) {
			continue;
		} else if (loopReturn == SignLooper.LoopReturn.BREAK) {
			break;
		} else if (loopReturn == SignLooper.LoopReturn.RETURN) {
			return;
		}
	}
}
 
Example 15
Source File: SubCommand_Item.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@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));
}
 
Example 16
Source File: SubCommand_Staff.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@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));
}
 
Example 17
Source File: SubCommand_Empty.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@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));
}
 
Example 18
Source File: SubCommand_Size.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@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));


}
 
Example 19
Source File: AddsignCommand.java    From AreaShop with GNU General Public License v3.0 4 votes vote down vote up
@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();
}
 
Example 20
Source File: SubCommand_Refill.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@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));
}