mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-12 20:29:20 +00:00
174 lines
4.4 KiB
Java
174 lines
4.4 KiB
Java
package com.earth2me.essentials.commands;
|
|
|
|
import static com.earth2me.essentials.I18n._;
|
|
import com.earth2me.essentials.User;
|
|
import java.util.List;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
public class Commandspeed extends EssentialsCommand
|
|
{
|
|
public Commandspeed()
|
|
{
|
|
super("speed");
|
|
}
|
|
|
|
@Override
|
|
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
|
|
{
|
|
if (args.length < 2)
|
|
{
|
|
throw new NotEnoughArgumentsException();
|
|
}
|
|
final boolean isFly = isFlyMode(args[0]);
|
|
final float speed = getMoveSpeed(args[1]);
|
|
speedOtherPlayers(server, sender, isFly, true, speed, args[2]);
|
|
}
|
|
|
|
@Override
|
|
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
|
{
|
|
if (args.length < 1)
|
|
{
|
|
throw new NotEnoughArgumentsException();
|
|
}
|
|
|
|
boolean isFly;
|
|
float speed;
|
|
boolean isBypass = user.isAuthorized("essentials.speed.bypass");
|
|
if (args.length == 1)
|
|
{
|
|
isFly = flyPermCheck(user, user.isFlying());
|
|
speed = getMoveSpeed(args[0]);
|
|
}
|
|
else
|
|
{
|
|
isFly = flyPermCheck(user, isFlyMode(args[0]));
|
|
speed = getMoveSpeed(args[1]);
|
|
if (args.length > 2 && user.isAuthorized("essentials.speed.others"))
|
|
{
|
|
speedOtherPlayers(server, user, isFly, isBypass, speed, args[2]);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (isFly)
|
|
{
|
|
user.setFlySpeed(getRealMoveSpeed(speed, isFly, isBypass));
|
|
user.sendMessage(_("moveSpeed", _("flying"), speed, user.getDisplayName()));
|
|
}
|
|
else
|
|
{
|
|
user.setWalkSpeed(getRealMoveSpeed(speed, isFly, isBypass));
|
|
user.sendMessage(_("moveSpeed", _("walking"), speed, user.getDisplayName()));
|
|
}
|
|
}
|
|
|
|
private void speedOtherPlayers(final Server server, final CommandSender sender, final boolean isFly, final boolean isBypass, final float speed, final String name) throws NotEnoughArgumentsException
|
|
{
|
|
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
|
|
boolean foundUser = false;
|
|
final List<Player> matchedPlayers = server.matchPlayer(name);
|
|
for (Player matchPlayer : matchedPlayers)
|
|
{
|
|
final User player = ess.getUser(matchPlayer);
|
|
if (skipHidden && player.isHidden())
|
|
{
|
|
continue;
|
|
}
|
|
foundUser = true;
|
|
if (isFly)
|
|
{
|
|
matchPlayer.setFlySpeed(getRealMoveSpeed(speed, isFly, isBypass));
|
|
sender.sendMessage(_("moveSpeed", _("flying"), speed, matchPlayer.getDisplayName()));
|
|
}
|
|
else
|
|
{
|
|
matchPlayer.setWalkSpeed(getRealMoveSpeed(speed, isFly, isBypass));
|
|
sender.sendMessage(_("moveSpeed", _("walking"), speed, matchPlayer.getDisplayName()));
|
|
}
|
|
}
|
|
if (!foundUser)
|
|
{
|
|
throw new NotEnoughArgumentsException(_("playerNotFound"));
|
|
}
|
|
}
|
|
|
|
private Boolean flyPermCheck(User user, boolean input) throws Exception
|
|
{
|
|
boolean canFly = user.isAuthorized("essentials.speed.fly");
|
|
boolean canWalk = user.isAuthorized("essentials.speed.walk");
|
|
if (input && canFly || !input && canWalk || !canFly && !canWalk)
|
|
{
|
|
return input;
|
|
}
|
|
else if (canWalk)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private boolean isFlyMode(final String modeString) throws NotEnoughArgumentsException
|
|
{
|
|
boolean isFlyMode;
|
|
if (modeString.contains("fly") || modeString.equalsIgnoreCase("f"))
|
|
{
|
|
isFlyMode = true;
|
|
}
|
|
else if (modeString.contains("walk") || modeString.contains("run")
|
|
|| modeString.equalsIgnoreCase("w") || modeString.equalsIgnoreCase("r"))
|
|
{
|
|
isFlyMode = false;
|
|
}
|
|
else
|
|
{
|
|
throw new NotEnoughArgumentsException();
|
|
}
|
|
return isFlyMode;
|
|
}
|
|
|
|
private float getMoveSpeed(final String moveSpeed) throws NotEnoughArgumentsException
|
|
{
|
|
float userSpeed;
|
|
try
|
|
{
|
|
userSpeed = Float.parseFloat(moveSpeed);
|
|
if (userSpeed > 10f)
|
|
{
|
|
userSpeed = 10f;
|
|
}
|
|
else if (userSpeed < 0f)
|
|
{
|
|
userSpeed = 0f;
|
|
}
|
|
}
|
|
catch (NumberFormatException e)
|
|
{
|
|
throw new NotEnoughArgumentsException();
|
|
}
|
|
return userSpeed;
|
|
}
|
|
|
|
private float getRealMoveSpeed(final float userSpeed, final boolean isFly, final boolean isBypass)
|
|
{
|
|
final float defaultSpeed = isFly ? 0.1f : 0.2f;
|
|
float maxSpeed = 1f;
|
|
if (!isBypass)
|
|
{
|
|
maxSpeed = (float)(isFly ? ess.getSettings().getMaxFlySpeed() : ess.getSettings().getMaxWalkSpeed());
|
|
}
|
|
|
|
if (userSpeed < 1f)
|
|
{
|
|
return defaultSpeed * userSpeed;
|
|
}
|
|
else
|
|
{
|
|
float ratio = ((userSpeed - 1) / 9) * (maxSpeed - defaultSpeed);
|
|
return ratio + defaultSpeed;
|
|
}
|
|
}
|
|
}
|