TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_cartsit.java

83 lines
2.6 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.NON_OP, source = SourceType.BOTH)
@CommandParameters(description = "Sit in nearest minecart. If target is in a minecart already, they will disembark.", usage = "/<command> [partialname]")
public class Command_cartsit extends FreedomCommand
{
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
Player targetPlayer = playerSender;
if (args.length == 1)
{
targetPlayer = getPlayer(args[0]);
if (targetPlayer == null || Command_vanish.VANISHED.contains(targetPlayer) && !plugin.al.isAdmin(sender))
{
sender.sendMessage(FreedomCommand.PLAYER_NOT_FOUND);
return true;
}
}
if (senderIsConsole)
{
2013-08-14 14:55:37 +00:00
if (targetPlayer == null)
{
sender.sendMessage("When used from the console, you must define a target player: /cartsit <player>");
return true;
}
}
else if (targetPlayer != playerSender && !isAdmin(sender))
{
2018-03-10 20:51:19 +00:00
sender.sendMessage("Only admins can select another player as a /cartsit target.");
return true;
}
2013-08-14 14:55:37 +00:00
if (targetPlayer.isInsideVehicle())
{
2013-08-14 14:55:37 +00:00
targetPlayer.getVehicle().eject();
}
else
{
Minecart nearest_cart = null;
2013-08-14 14:55:37 +00:00
for (Minecart cart : targetPlayer.getWorld().getEntitiesByClass(Minecart.class))
{
if (cart.isEmpty())
{
if (nearest_cart == null)
{
nearest_cart = cart;
}
else
{
2013-08-14 14:55:37 +00:00
if (cart.getLocation().distanceSquared(targetPlayer.getLocation()) < nearest_cart.getLocation().distanceSquared(targetPlayer.getLocation()))
{
nearest_cart = cart;
}
}
}
}
if (nearest_cart != null)
{
2018-03-10 20:51:19 +00:00
nearest_cart.addPassenger(targetPlayer);
}
else
{
2018-03-10 20:51:19 +00:00
msg("There are no empty minecarts in the target world.");
}
}
return true;
}
}