From e79145ee54f2929ff7d7b6b6a79e701c1d4fe3e0 Mon Sep 17 00:00:00 2001 From: speedxx <43330808+speedxx@users.noreply.github.com> Date: Fri, 14 Aug 2020 21:56:04 -0400 Subject: [PATCH] fix exceptions, properly teleport player in /ride - fixes ArrayIndexOutOfBoundsException in /ride - teleport sender properly if target is in different world for /ride - fixes IllegalArgumentException in /stopsound --- .../totalfreedommod/command/Command_ride.java | 40 +++++++++++-------- .../command/Command_stopsound.java | 34 +++++++++------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ride.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ride.java index c64707ce..7f41a5fb 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ride.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ride.java @@ -8,7 +8,6 @@ import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -17,7 +16,9 @@ import org.bukkit.entity.Player; @CommandParameters(description = "Ride on the top of the specified player.", usage = "/ >") public class Command_ride extends FreedomCommand { + private final Map RIDE_REQUESTS = new HashMap<>(); // requested, requester + @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { @@ -40,6 +41,7 @@ public class Command_ride extends FreedomCommand msg("You don't have a request currently."); return true; } + Player requester = RIDE_REQUESTS.get(playerSender); if (requester == null) { @@ -47,8 +49,15 @@ public class Command_ride extends FreedomCommand RIDE_REQUESTS.remove(playerSender); return true; } + msg("Request accepted."); requester.sendMessage(ChatColor.GRAY + "Your request has been accepted."); + + if (requester.getWorld() != playerSender.getWorld()) + { + requester.teleport(playerSender); + } + playerSender.addPassenger(requester); return true; } @@ -73,15 +82,18 @@ public class Command_ride extends FreedomCommand return true; } - if (args[0].equalsIgnoreCase("mode")) + if (args.length >= 2) { - if (args[1].equalsIgnoreCase("normal") || args[1].equalsIgnoreCase("off") || args[1].equalsIgnoreCase("ask")) + if (args[0].equalsIgnoreCase("mode")) { - PlayerData playerDataSender = plugin.pl.getData(playerSender); - playerDataSender.setRideMode(args[1].toLowerCase()); - plugin.pl.save(playerDataSender); - msg("Ride mode is now set to " + args[1].toLowerCase() + "."); - return true; + if (args[1].equalsIgnoreCase("normal") || args[1].equalsIgnoreCase("off") || args[1].equalsIgnoreCase("ask")) + { + PlayerData playerDataSender = plugin.pl.getData(playerSender); + playerDataSender.setRideMode(args[1].toLowerCase()); + plugin.pl.save(playerDataSender); + msg("Ride mode is now set to " + args[1].toLowerCase() + "."); + return true; + } } } @@ -133,17 +145,11 @@ public class Command_ride extends FreedomCommand if (player.getWorld() != playerSender.getWorld()) { - msg("Player is in another world. (" + player.getWorld().getName() + ")"); - return true; - } - else - { - Location loc = player.getLocation(); - playerSender.teleport(new Location(loc.getWorld(),loc.getX(), loc.getY(), loc.getZ())); + playerSender.teleport(player); } player.addPassenger(playerSender); - + msg(player, playerSender.getName() + " is now riding you, run /eject to eject them."); return true; } -} +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stopsound.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stopsound.java index 5ad72320..1e0acb92 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stopsound.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stopsound.java @@ -18,30 +18,36 @@ public class Command_stopsound extends FreedomCommand @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (args.length > 0) { - Sound sound = Sound.valueOf(args[0].toUpperCase()); - if (sound != null) + Sound sound = null; + + for (Sound loop : Sound.values()) { - playerSender.stopSound(sound); - msg("Stopped all " + sound.name() + " sounds", ChatColor.GREEN); + if (loop != null && loop.name().equalsIgnoreCase(args[0])) + { + sound = Sound.valueOf(args[0].toUpperCase()); + break; + } } - else + + if (sound == null) { msg(args[0] + " is not a valid sound.", ChatColor.RED); + return true; } + + playerSender.stopSound(sound); + msg("Stopped all " + sound.name() + " sounds", ChatColor.GREEN); + return true; } - else + + for (Sound sound : Sound.values()) { - for (Sound sound : Sound.values()) - { - playerSender.stopSound(sound); - } - msg("Stopped all sounds.", ChatColor.GREEN); - + playerSender.stopSound(sound); } + msg("Stopped all sounds.", ChatColor.GREEN); return true; } @@ -64,4 +70,4 @@ public class Command_stopsound extends FreedomCommand } return sounds; } -} +} \ No newline at end of file