|
| 1 | +package com.solarrabbit.largeraids.command; |
| 2 | + |
| 3 | +import org.bukkit.ChatColor; |
| 4 | +import org.bukkit.Location; |
| 5 | +import org.bukkit.command.Command; |
| 6 | +import org.bukkit.command.CommandExecutor; |
| 7 | +import org.bukkit.command.CommandSender; |
| 8 | +import org.bukkit.entity.Player; |
| 9 | + |
| 10 | +import com.solarrabbit.largeraids.LargeRaids; |
| 11 | +import com.solarrabbit.largeraids.nms.AbstractBlockPositionWrapper; |
| 12 | +import com.solarrabbit.largeraids.nms.AbstractWorldServerWrapper; |
| 13 | +import com.solarrabbit.largeraids.util.VersionUtil; |
| 14 | + |
| 15 | +/** |
| 16 | + * Makes the entity type in range of the player path-find to a given location. |
| 17 | + */ |
| 18 | +public class MobControlCommand implements CommandExecutor { |
| 19 | + private final LargeRaids plugin; |
| 20 | + |
| 21 | + public MobControlCommand(LargeRaids plugin) { |
| 22 | + this.plugin = plugin; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
| 27 | + if (!(sender instanceof Player)) { |
| 28 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("sender-player")); |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + if (args.length < 3) |
| 33 | + return false; |
| 34 | + |
| 35 | + Class<?> entityClass = null; |
| 36 | + try { |
| 37 | + entityClass = Class.forName(args[0]); |
| 38 | + } catch (ClassNotFoundException e) { |
| 39 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.class-not-found")); |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + Location search = ((Player) sender).getLocation(); |
| 44 | + double range = 0; |
| 45 | + try { |
| 46 | + range = Double.parseDouble(args[1]); |
| 47 | + if (range <= 0) { |
| 48 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-range")); |
| 49 | + return false; |
| 50 | + } |
| 51 | + } catch (NumberFormatException e) { |
| 52 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-range")); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if (args.length == 3 && args[2].equalsIgnoreCase("clear")) { |
| 57 | + AbstractWorldServerWrapper world = VersionUtil.getCraftWorldWrapper(search.getWorld()).getHandle(); |
| 58 | + AbstractBlockPositionWrapper searchWrapper = VersionUtil.getBlockPositionWrapper(search); |
| 59 | + int result = world.clearMobTargets(entityClass, searchWrapper, range); |
| 60 | + if (result == -1) { |
| 61 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-entity-class")); |
| 62 | + } else if (result == 0) { |
| 63 | + sender.sendMessage(ChatColor.YELLOW + String.format(this.plugin.getMessage("mob-control.no-mobs-found"), |
| 64 | + entityClass.getSimpleName(), range, (int)Math.floor(search.getX()), |
| 65 | + (int)Math.floor(search.getY()), (int)Math.floor(search.getZ()))); |
| 66 | + } else { |
| 67 | + sender.sendMessage(ChatColor.GREEN + String.format(this.plugin.getMessage("mob-control.mobs-cleared"), result, |
| 68 | + entityClass.getSimpleName())); |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + if (args.length != 9) |
| 74 | + return false; |
| 75 | + |
| 76 | + Location target; |
| 77 | + try { |
| 78 | + double x = parseDoubleOrRelative(args[2], search, 0); |
| 79 | + double y = parseDoubleOrRelative(args[3], search, 1); |
| 80 | + double z = parseDoubleOrRelative(args[4], search, 2); |
| 81 | + target = new Location(search.getWorld(), x, y, z); |
| 82 | + } catch (NumberFormatException e) { |
| 83 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-coordinates")); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + double radius; |
| 88 | + try { |
| 89 | + radius = Double.parseDouble(args[5]); |
| 90 | + if (radius < 0) { |
| 91 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-radius")); |
| 92 | + return false; |
| 93 | + } |
| 94 | + } catch (NumberFormatException e) { |
| 95 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-radius")); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + double navSpeed; |
| 100 | + try { |
| 101 | + navSpeed = Double.parseDouble(args[6]); |
| 102 | + if (navSpeed <= 0) { |
| 103 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-navspeed")); |
| 104 | + return false; |
| 105 | + } |
| 106 | + } catch (NumberFormatException e) { |
| 107 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-navspeed")); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + int prio; |
| 112 | + try { |
| 113 | + prio = Integer.parseInt(args[7]); |
| 114 | + if (prio < 0) { |
| 115 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-prio")); |
| 116 | + return false; |
| 117 | + } |
| 118 | + } catch (NumberFormatException e) { |
| 119 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-prio")); |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + boolean pathfindOnce; |
| 124 | + if (!args[8].equalsIgnoreCase("true") && !args[8].equalsIgnoreCase("false")) { |
| 125 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.pathfind-once-boolean")); |
| 126 | + return false; |
| 127 | + } |
| 128 | + pathfindOnce = Boolean.parseBoolean(args[8]); |
| 129 | + |
| 130 | + AbstractWorldServerWrapper world = VersionUtil.getCraftWorldWrapper(search.getWorld()).getHandle(); |
| 131 | + AbstractBlockPositionWrapper searchWrapper = VersionUtil.getBlockPositionWrapper(search); |
| 132 | + AbstractBlockPositionWrapper targetWrapper = VersionUtil.getBlockPositionWrapper(target); |
| 133 | + int result = world.setMobTargets(entityClass, searchWrapper, range, targetWrapper, radius, navSpeed, prio, pathfindOnce); |
| 134 | + |
| 135 | + if (result == -1) { |
| 136 | + sender.sendMessage(ChatColor.RED + this.plugin.getMessage("mob-control.invalid-entity-class")); |
| 137 | + } else if (result == 0) { |
| 138 | + sender.sendMessage(ChatColor.YELLOW + String.format(this.plugin.getMessage("mob-control.no-mobs-found"), |
| 139 | + entityClass.getSimpleName(), range, (int)Math.floor(search.getX()), |
| 140 | + (int)Math.floor(search.getY()), (int)Math.floor(search.getZ()))); |
| 141 | + } else { |
| 142 | + sender.sendMessage(ChatColor.GREEN + String.format(this.plugin.getMessage("mob-control.mobs-set"), result, |
| 143 | + entityClass.getSimpleName(), radius, (int)Math.floor(target.getX()), |
| 144 | + (int)Math.floor(target.getY()), (int)Math.floor(target.getZ()), navSpeed)); |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + private double parseDoubleOrRelative(String pos, Location loc, int type) { |
| 151 | + if (loc == null || pos.length() == 0 || pos.charAt(0) != '~') |
| 152 | + return Double.parseDouble(pos); |
| 153 | + double relative = pos.length() == 1 ? 0 : Double.parseDouble(pos.substring(1)); |
| 154 | + switch (type) { |
| 155 | + case 0: |
| 156 | + return relative + loc.getX(); |
| 157 | + case 1: |
| 158 | + return relative + loc.getY(); |
| 159 | + case 2: |
| 160 | + return relative + loc.getZ(); |
| 161 | + default: |
| 162 | + return 0; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments