use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.
the class BaseSpell method cast.
@Override
public boolean cast(ConfigurationSection extraParameters, Location defaultLocation) {
if (mage.isPlayer() && mage.getPlayer().getGameMode() == GameMode.SPECTATOR) {
if (mage.getDebugLevel() > 0 && extraParameters != null) {
mage.sendDebugMessage("Cannot cast in spectator mode.");
}
return false;
}
if (toggle != ToggleType.NONE && isActive()) {
mage.sendDebugMessage(ChatColor.DARK_BLUE + "Deactivating " + ChatColor.GOLD + getName());
deactivate(true, true);
processResult(SpellResult.DEACTIVATE, parameters);
return true;
}
if (mage.getDebugLevel() > 5 && extraParameters != null) {
Collection<String> keys = extraParameters.getKeys(false);
if (keys.size() > 0) {
mage.sendDebugMessage(ChatColor.BLUE + "Cast " + ChatColor.GOLD + getName() + " " + ChatColor.GREEN + ConfigurationUtils.getParameters(extraParameters));
}
}
this.reset();
Location location = mage.getLocation();
if (location != null) {
Set<String> overrides = controller.getSpellOverrides(mage, location);
if (overrides != null && !overrides.isEmpty()) {
if (extraParameters == null) {
extraParameters = new MemoryConfiguration();
}
for (String entry : overrides) {
String[] pieces = StringUtils.split(entry, ' ');
if (pieces.length < 2)
continue;
String fullKey = pieces[0];
String[] key = StringUtils.split(fullKey, '.');
if (key.length == 0)
continue;
if (key.length == 2 && !key[0].equals("default") && !key[0].equals(spellKey.getBaseKey()) && !key[0].equals(spellKey.getKey())) {
continue;
}
fullKey = key.length == 2 ? key[1] : key[0];
ConfigurationUtils.set(extraParameters, fullKey, pieces[1]);
}
}
}
if (this.currentCast == null) {
this.currentCast = new CastContext();
this.currentCast.setSpell(this);
}
this.location = defaultLocation;
workingParameters = new SpellParameters(this);
ConfigurationUtils.addConfigurations(workingParameters, this.parameters);
ConfigurationUtils.addConfigurations(workingParameters, extraParameters);
processParameters(workingParameters);
// Check to see if this is allowed to be cast by a command block
if (!commandBlockAllowed) {
CommandSender sender = mage.getCommandSender();
if (sender != null && sender instanceof BlockCommandSender) {
Block block = mage.getLocation().getBlock();
if (block.getType() == Material.COMMAND) {
block.setType(Material.AIR);
}
return false;
}
}
// Allow other plugins to cancel this cast
PreCastEvent preCast = new PreCastEvent(mage, this);
Bukkit.getPluginManager().callEvent(preCast);
if (preCast.isCancelled()) {
processResult(SpellResult.CANCELLED, workingParameters);
sendCastMessage(SpellResult.CANCELLED, " (no cast)");
return false;
}
// Don't allow casting if the player is confused or weakened
bypassConfusion = workingParameters.getBoolean("bypass_confusion", bypassConfusion);
bypassWeakness = workingParameters.getBoolean("bypass_weakness", bypassWeakness);
LivingEntity livingEntity = mage.getLivingEntity();
if (livingEntity != null && !mage.isSuperPowered()) {
if (!bypassConfusion && livingEntity.hasPotionEffect(PotionEffectType.CONFUSION)) {
processResult(SpellResult.CURSED, workingParameters);
sendCastMessage(SpellResult.CURSED, " (no cast)");
return false;
}
// Don't allow casting if the player is weakened
if (!bypassWeakness && livingEntity.hasPotionEffect(PotionEffectType.WEAKNESS)) {
processResult(SpellResult.CURSED, workingParameters);
sendCastMessage(SpellResult.CURSED, " (no cast)");
return false;
}
}
// Don't perform permission check until after processing parameters, in case of overrides
if (!canCast(getLocation())) {
processResult(SpellResult.INSUFFICIENT_PERMISSION, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_PERMISSION, " (no cast)");
if (mage.getDebugLevel() > 1) {
CommandSender messageTarget = mage.getDebugger();
if (messageTarget == null) {
messageTarget = mage.getCommandSender();
}
if (messageTarget != null) {
mage.debugPermissions(messageTarget, this);
}
}
return false;
}
this.preCast();
// PVP override settings
bypassPvpRestriction = workingParameters.getBoolean("bypass_pvp", false);
bypassPvpRestriction = workingParameters.getBoolean("bp", bypassPvpRestriction);
bypassPermissions = workingParameters.getBoolean("bypass_permissions", bypassPermissions);
bypassFriendlyFire = workingParameters.getBoolean("bypass_friendly_fire", false);
onlyFriendlyFire = workingParameters.getBoolean("only_friendly", false);
// Check cooldowns
cooldown = workingParameters.getInt("cooldown", cooldown);
cooldown = workingParameters.getInt("cool", cooldown);
mageCooldown = workingParameters.getInt("cooldown_mage", mageCooldown);
// Color override
color = ConfigurationUtils.getColor(workingParameters, "color", color);
particle = workingParameters.getString("particle", null);
double cooldownRemaining = getRemainingCooldown() / 1000.0;
String timeDescription = "";
if (cooldownRemaining > 0) {
if (cooldownRemaining > 60 * 60) {
long hours = (long) Math.ceil(cooldownRemaining / (60 * 60));
if (hours == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_hour");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_hours").replace("$hours", Long.toString(hours));
}
} else if (cooldownRemaining > 60) {
long minutes = (long) Math.ceil(cooldownRemaining / 60);
if (minutes == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_minute");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_minutes").replace("$minutes", Long.toString(minutes));
}
} else if (cooldownRemaining >= 1) {
long seconds = (long) Math.ceil(cooldownRemaining);
if (seconds == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_second");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_seconds").replace("$seconds", Long.toString(seconds));
}
} else {
timeDescription = controller.getMessages().get("cooldown.wait_moment");
if (timeDescription.contains("$seconds")) {
timeDescription = timeDescription.replace("$seconds", SECONDS_FORMATTER.format(cooldownRemaining));
}
}
castMessage(getMessage("cooldown").replace("$time", timeDescription));
processResult(SpellResult.COOLDOWN, workingParameters);
sendCastMessage(SpellResult.COOLDOWN, " (no cast)");
return false;
}
CastingCost required = getRequiredCost();
if (required != null) {
String baseMessage = getMessage("insufficient_resources");
String costDescription = required.getDescription(controller.getMessages(), mage);
// Send loud messages when items are required.
if (required.isItem()) {
sendMessage(baseMessage.replace("$cost", costDescription));
} else {
castMessage(baseMessage.replace("$cost", costDescription));
}
processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
return false;
}
if (requiredHealth > 0) {
LivingEntity li = mage.getLivingEntity();
double healthPercentage = li == null ? 0 : 100 * li.getHealth() / li.getMaxHealth();
if (healthPercentage < requiredHealth) {
processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
return false;
}
}
if (controller.isSpellProgressionEnabled()) {
long progressLevel = getProgressLevel();
for (Entry<String, EquationTransform> entry : progressLevelEquations.entrySet()) {
workingParameters.set(entry.getKey(), entry.getValue().get(progressLevel));
}
}
// Check for cancel-on-cast-other spells, after we have determined that this spell can really be cast.
if (!passive) {
for (Iterator<Batch> iterator = mage.getPendingBatches().iterator(); iterator.hasNext(); ) {
Batch batch = iterator.next();
if (!(batch instanceof SpellBatch))
continue;
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
if (spell.cancelOnCastOther()) {
spell.cancel();
batch.finish();
iterator.remove();
}
}
}
return finalizeCast(workingParameters);
}
use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.
the class MagicMobCommandExecutor method onClearMobs.
protected void onClearMobs(CommandSender sender, String[] args) {
String mobType = args.length > 1 ? args[1] : null;
if (mobType != null && mobType.equalsIgnoreCase("all")) {
mobType = null;
}
String worldName = null;
Integer radiusSquared = null;
Location targetLocation = null;
Player player = (sender instanceof Player) ? (Player) sender : null;
BlockCommandSender commandBlock = (sender instanceof BlockCommandSender) ? (BlockCommandSender) sender : null;
if (args.length == 3) {
if (!(sender instanceof ConsoleCommandSender) && Bukkit.getWorld(args[2]) == null) {
try {
int radius = Integer.parseInt(args[2]);
radiusSquared = radius * radius;
} catch (Exception ignored) {
}
}
if (radiusSquared == null) {
worldName = args[2];
} else {
if (player != null) {
targetLocation = player.getLocation();
} else if (commandBlock != null) {
targetLocation = commandBlock.getBlock().getLocation();
} else {
sender.sendMessage(ChatColor.RED + "Invalid world: " + args[2]);
}
}
} else if (args.length > 5) {
World world = null;
if (args.length > 6) {
worldName = args[6];
world = Bukkit.getWorld(worldName);
}
if (world == null) {
sender.sendMessage(ChatColor.RED + "Invalid world: " + worldName);
}
try {
int radius = Integer.parseInt(args[2]);
radiusSquared = radius * radius;
double currentX = 0;
double currentY = 0;
double currentZ = 0;
if (player != null) {
targetLocation = player.getLocation();
} else if (commandBlock != null) {
targetLocation = commandBlock.getBlock().getLocation();
}
if (targetLocation != null) {
currentX = targetLocation.getX();
currentY = targetLocation.getY();
currentZ = targetLocation.getZ();
if (world == null) {
world = targetLocation.getWorld();
worldName = world.getName();
}
}
if (world == null) {
sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
return;
}
targetLocation = new Location(world, ConfigurationUtils.overrideDouble(args[3], currentX), ConfigurationUtils.overrideDouble(args[4], currentY), ConfigurationUtils.overrideDouble(args[5], currentZ));
} catch (Exception ex) {
sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
return;
}
}
Collection<Mage> mages = new ArrayList<>(api.getController().getMobMages());
int removed = 0;
for (Mage mage : mages) {
EntityData entityData = mage.getEntityData();
if (entityData == null)
continue;
if (worldName != null && !mage.getLocation().getWorld().getName().equals(worldName))
continue;
if (mobType != null && !entityData.getKey().equals(mobType))
continue;
if (radiusSquared != null && targetLocation != null && mage.getLocation().distanceSquared(targetLocation) > radiusSquared)
continue;
Entity entity = mage.getEntity();
mage.undoScheduled();
api.getController().removeMage(mage);
if (entity != null) {
entity.remove();
}
removed++;
}
List<World> worlds = new ArrayList<>();
if (worldName != null) {
World world = Bukkit.getWorld(worldName);
if (world == null) {
sender.sendMessage(ChatColor.RED + "Unknown world: " + ChatColor.WHITE + worldName);
return;
}
worlds.add(world);
} else {
worlds.addAll(Bukkit.getWorlds());
}
Set<String> mobNames = new HashSet<>();
if (mobType != null) {
EntityData mob = api.getController().getMob(mobType);
if (mob == null) {
sender.sendMessage(ChatColor.RED + "Unknown mob type: " + ChatColor.WHITE + mobType);
return;
}
mobNames.add(mob.getName());
} else {
Set<String> allKeys = api.getController().getMobKeys();
for (String key : allKeys) {
EntityData mob = api.getController().getMob(key);
mobNames.add(mob.getName());
}
}
for (World world : worlds) {
List<Entity> entities = world.getEntities();
for (Entity entity : entities) {
String customName = entity.getCustomName();
if (entity.isValid() && customName != null && mobNames.contains(customName)) {
if (radiusSquared != null && targetLocation != null && entity.getLocation().distanceSquared(targetLocation) > radiusSquared)
continue;
entity.remove();
removed++;
}
}
}
sender.sendMessage("Removed " + removed + " magic mobs");
}
use of org.bukkit.command.BlockCommandSender in project Glowstone by GlowstoneMC.
the class SetWorldSpawnCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
Location spawnLocation;
final World world = CommandUtils.getWorld(sender);
if (args.length == 0) {
// Get the player current location
if (CommandUtils.isPhysical(sender)) {
spawnLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
} else {
commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
return false;
}
} else if (args.length >= 3) {
// manage arguments
final Location senderLocation;
// Get the sender coordinates if relative is used
if (args[0].startsWith("~") || args[1].startsWith("~") || args[2].startsWith("~")) {
if (!CommandUtils.isPhysical(sender)) {
commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
return false;
} else {
senderLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
}
} else {
// Otherwise, the current location can be set to 0/0/0 (since it's absolute)
senderLocation = new Location(world, 0, 0, 0);
}
spawnLocation = CommandUtils.getLocation(senderLocation, args[0], args[1], args[2]);
} else {
sendUsageMessage(sender, commandMessages);
return false;
}
int newY = spawnLocation.getBlockY();
if (newY < 0) {
commandMessages.getGeneric(GenericMessage.TOO_LOW).sendInColor(ChatColor.RED, sender);
return false;
} else if (newY > world.getMaxHeight()) {
commandMessages.getGeneric(GenericMessage.TOO_HIGH).sendInColor(ChatColor.RED, sender, world.getMaxHeight());
return false;
}
int newX = spawnLocation.getBlockX();
int newZ = spawnLocation.getBlockZ();
world.setSpawnLocation(newX, newY, newZ);
new LocalizedStringImpl("setworldspawn.done", commandMessages.getResourceBundle()).send(sender, newX, newY, newZ);
return true;
}
use of org.bukkit.command.BlockCommandSender in project Glowstone by GlowstoneMC.
the class GiveCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 2) {
sendUsageMessage(sender, commandMessages);
return false;
}
String itemName = CommandUtils.toNamespaced(args[1]);
Material type = ItemIds.getItem(itemName);
if (type == null) {
new LocalizedStringImpl("give.unknown", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, itemName);
return false;
}
ItemStack stack = new ItemStack(type);
if (args.length >= 3) {
String amountString = args[2];
try {
int amount = Integer.valueOf(amountString);
if (amount > 64) {
new LocalizedStringImpl("give.too-many", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, amount);
return false;
} else if (amount < 1) {
new LocalizedStringImpl("give.too-few", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, amount);
return false;
}
stack.setAmount(amount);
} catch (NumberFormatException ex) {
commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, amountString);
return false;
}
}
String name = args[0];
if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
CommandTarget target = new CommandTarget(sender, name);
Entity[] matched = target.getMatched(location);
for (Entity entity : matched) {
if (entity instanceof Player) {
Player player = (Player) entity;
giveItem(sender, player, stack, commandMessages.getResourceBundle());
}
}
} else {
Player player = Bukkit.getPlayerExact(name);
if (player == null) {
commandMessages.getGeneric(GenericMessage.OFFLINE).sendInColor(ChatColor.RED, sender, name);
return false;
} else {
giveItem(sender, player, stack, commandMessages.getResourceBundle());
}
}
return true;
}
use of org.bukkit.command.BlockCommandSender in project MyMaid2 by jaoafa.
the class Event_CommandBlockLogger method onCommandBlockCall.
@EventHandler
public void onCommandBlockCall(ServerCommandEvent event) {
if (event.getSender() instanceof ConsoleCommandSender) {
Bukkit.getLogger().info("CONSOLE COMMAND: " + event.getCommand());
}
if (!(event.getSender() instanceof BlockCommandSender))
return;
BlockCommandSender sender = (BlockCommandSender) event.getSender();
if (sender.getBlock() == null || !(sender.getBlock().getState() instanceof CommandBlock))
return;
CommandBlock cmdb = (CommandBlock) sender.getBlock().getState();
String command = cmdb.getCommand();
if (command.startsWith("/testfor")) {
return;
} else if (command.startsWith("testfor")) {
return;
} else if (command.startsWith("/testforblock")) {
return;
} else if (command.startsWith("testforblock")) {
return;
} else if (command.startsWith("/testforblocks")) {
return;
} else if (command.startsWith("testforblocks")) {
return;
} else if (command.equals("")) {
return;
}
if (cmdb.getWorld().getName().equalsIgnoreCase("Jao_Afa_Old")) {
return;
}
Player nearestPlayer = getNearestPlayer(cmdb.getLocation());
String name, uuid;
if (nearestPlayer == null) {
name = uuid = "null";
} else {
name = nearestPlayer.getName();
uuid = nearestPlayer.getUniqueId().toString();
}
double nearestDistance = getNearestPlayerDistance(cmdb.getLocation());
String world = cmdb.getWorld().getName();
int x = cmdb.getX();
int y = cmdb.getY();
int z = cmdb.getZ();
try {
String sql = "INSERT INTO cmdb_log (nearplayer, nearplayer_uuid, distance, command, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
PreparedStatement statement = MySQL.getNewPreparedStatement(sql);
statement.setString(1, name);
statement.setString(2, uuid);
statement.setDouble(3, nearestDistance);
statement.setString(4, command);
statement.setString(5, world);
statement.setInt(6, x);
statement.setInt(7, y);
statement.setInt(8, z);
statement.executeUpdate();
} catch (SQLException | ClassNotFoundException e) {
BugReporter(e);
}
}
Aggregations