| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Random;
|
| 9 | import java.util.Set;
|
| 10 | import net.minecraft.block.material.Material;
|
| 11 | import net.minecraft.entity.Entity;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.scoreboard.Team;
|
| 14 | import net.minecraft.server.MinecraftServer;
|
| 15 | import net.minecraft.util.BlockPos;
|
| 16 | import net.minecraft.util.ChatComponentTranslation;
|
| 17 | import net.minecraft.util.MathHelper;
|
| 18 | import net.minecraft.world.World;
|
| 19 |
|
| 20 | public class CommandSpreadPlayers extends CommandBase
|
| 21 | {
|
| 22 | /**
|
| 23 | * Gets the name of the command
|
| 24 | */
|
| 25 | public String getCommandName()
|
| 26 | {
|
| 27 | return "spreadplayers";
|
| 28 | }
|
| 29 |
|
| 30 | /**
|
| 31 | * Return the required permission level for this command.
|
| 32 | */
|
| 33 | public int getRequiredPermissionLevel()
|
| 34 | {
|
| 35 | return 2;
|
| 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Gets the usage string for the command.
|
| 40 | */
|
| 41 | public String getCommandUsage(ICommandSender sender)
|
| 42 | {
|
| 43 | return "commands.spreadplayers.usage";
|
| 44 | }
|
| 45 |
|
| 46 | /**
|
| 47 | * Callback when the command is invoked
|
| 48 | */
|
| 49 | public void processCommand(ICommandSender sender, String[] args) throws CommandException
|
| 50 | {
|
| 51 | if (args.length < 6)
|
| 52 | {
|
| 53 | throw new WrongUsageException("commands.spreadplayers.usage", new Object[0]);
|
| 54 | }
|
| 55 | else
|
| 56 | {
|
| 57 | int i = 0;
|
| 58 | BlockPos blockpos = sender.getPosition();
|
| 59 | double d0 = parseDouble((double)blockpos.getX(), args[i++], true);
|
| 60 | double d1 = parseDouble((double)blockpos.getZ(), args[i++], true);
|
| 61 | double d2 = parseDouble(args[i++], 0.0D);
|
| 62 | double d3 = parseDouble(args[i++], d2 + 1.0D);
|
| 63 | boolean flag = parseBoolean(args[i++]);
|
| 64 | List<Entity> list = Lists.<Entity>newArrayList();
|
| 65 |
|
| 66 | while (i < args.length)
|
| 67 | {
|
| 68 | String s = args[i++];
|
| 69 |
|
| 70 | if (PlayerSelector.hasArguments(s))
|
| 71 | {
|
| 72 | List<Entity> list1 = PlayerSelector.<Entity>matchEntities(sender, s, Entity.class);
|
| 73 |
|
| 74 | if (list1.size() == 0)
|
| 75 | {
|
| 76 | throw new EntityNotFoundException();
|
| 77 | }
|
| 78 |
|
| 79 | list.addAll(list1);
|
| 80 | }
|
| 81 | else
|
| 82 | {
|
| 83 | EntityPlayer entityplayer = MinecraftServer.getServer().getConfigurationManager().getPlayerByUsername(s);
|
| 84 |
|
| 85 | if (entityplayer == null)
|
| 86 | {
|
| 87 | throw new PlayerNotFoundException();
|
| 88 | }
|
| 89 |
|
| 90 | list.add(entityplayer);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | sender.setCommandStat(CommandResultStats.Type.AFFECTED_ENTITIES, list.size());
|
| 95 |
|
| 96 | if (list.isEmpty())
|
| 97 | {
|
| 98 | throw new EntityNotFoundException();
|
| 99 | }
|
| 100 | else
|
| 101 | {
|
| 102 | sender.addChatMessage(new ChatComponentTranslation("commands.spreadplayers.spreading." + (flag ? "teams" : "players"), new Object[] {Integer.valueOf(list.size()), Double.valueOf(d3), Double.valueOf(d0), Double.valueOf(d1), Double.valueOf(d2)}));
|
| 103 | this.func_110669_a(sender, list, new CommandSpreadPlayers.Position(d0, d1), d2, d3, ((Entity)list.get(0)).worldObj, flag);
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private void func_110669_a(ICommandSender p_110669_1_, List<Entity> p_110669_2_, CommandSpreadPlayers.Position p_110669_3_, double p_110669_4_, double p_110669_6_, World worldIn, boolean p_110669_9_) throws CommandException
|
| 109 | {
|
| 110 | Random random = new Random();
|
| 111 | double d0 = p_110669_3_.field_111101_a - p_110669_6_;
|
| 112 | double d1 = p_110669_3_.field_111100_b - p_110669_6_;
|
| 113 | double d2 = p_110669_3_.field_111101_a + p_110669_6_;
|
| 114 | double d3 = p_110669_3_.field_111100_b + p_110669_6_;
|
| 115 | CommandSpreadPlayers.Position[] acommandspreadplayers$position = this.func_110670_a(random, p_110669_9_ ? this.func_110667_a(p_110669_2_) : p_110669_2_.size(), d0, d1, d2, d3);
|
| 116 | int i = this.func_110668_a(p_110669_3_, p_110669_4_, worldIn, random, d0, d1, d2, d3, acommandspreadplayers$position, p_110669_9_);
|
| 117 | double d4 = this.func_110671_a(p_110669_2_, worldIn, acommandspreadplayers$position, p_110669_9_);
|
| 118 | notifyOperators(p_110669_1_, this, "commands.spreadplayers.success." + (p_110669_9_ ? "teams" : "players"), new Object[] {Integer.valueOf(acommandspreadplayers$position.length), Double.valueOf(p_110669_3_.field_111101_a), Double.valueOf(p_110669_3_.field_111100_b)});
|
| 119 |
|
| 120 | if (acommandspreadplayers$position.length > 1)
|
| 121 | {
|
| 122 | p_110669_1_.addChatMessage(new ChatComponentTranslation("commands.spreadplayers.info." + (p_110669_9_ ? "teams" : "players"), new Object[] {String.format("%.2f", new Object[]{Double.valueOf(d4)}), Integer.valueOf(i)}));
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | private int func_110667_a(List<Entity> p_110667_1_)
|
| 127 | {
|
| 128 | Set<Team> set = Sets.<Team>newHashSet();
|
| 129 |
|
| 130 | for (Entity entity : p_110667_1_)
|
| 131 | {
|
| 132 | if (entity instanceof EntityPlayer)
|
| 133 | {
|
| 134 | set.add(((EntityPlayer)entity).getTeam());
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | set.add((Team)null);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | return set.size();
|
| 143 | }
|
| 144 |
|
| 145 | private int func_110668_a(CommandSpreadPlayers.Position p_110668_1_, double p_110668_2_, World worldIn, Random p_110668_5_, double p_110668_6_, double p_110668_8_, double p_110668_10_, double p_110668_12_, CommandSpreadPlayers.Position[] p_110668_14_, boolean p_110668_15_) throws CommandException
|
| 146 | {
|
| 147 | boolean flag = true;
|
| 148 | double d0 = 3.4028234663852886E38D;
|
| 149 | int i;
|
| 150 |
|
| 151 | for (i = 0; i < 10000 && flag; ++i)
|
| 152 | {
|
| 153 | flag = false;
|
| 154 | d0 = 3.4028234663852886E38D;
|
| 155 |
|
| 156 | for (int j = 0; j < p_110668_14_.length; ++j)
|
| 157 | {
|
| 158 | CommandSpreadPlayers.Position commandspreadplayers$position = p_110668_14_[j];
|
| 159 | int k = 0;
|
| 160 | CommandSpreadPlayers.Position commandspreadplayers$position1 = new CommandSpreadPlayers.Position();
|
| 161 |
|
| 162 | for (int l = 0; l < p_110668_14_.length; ++l)
|
| 163 | {
|
| 164 | if (j != l)
|
| 165 | {
|
| 166 | CommandSpreadPlayers.Position commandspreadplayers$position2 = p_110668_14_[l];
|
| 167 | double d1 = commandspreadplayers$position.func_111099_a(commandspreadplayers$position2);
|
| 168 | d0 = Math.min(d1, d0);
|
| 169 |
|
| 170 | if (d1 < p_110668_2_)
|
| 171 | {
|
| 172 | ++k;
|
| 173 | commandspreadplayers$position1.field_111101_a += commandspreadplayers$position2.field_111101_a - commandspreadplayers$position.field_111101_a;
|
| 174 | commandspreadplayers$position1.field_111100_b += commandspreadplayers$position2.field_111100_b - commandspreadplayers$position.field_111100_b;
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | if (k > 0)
|
| 180 | {
|
| 181 | commandspreadplayers$position1.field_111101_a /= (double)k;
|
| 182 | commandspreadplayers$position1.field_111100_b /= (double)k;
|
| 183 | double d2 = (double)commandspreadplayers$position1.func_111096_b();
|
| 184 |
|
| 185 | if (d2 > 0.0D)
|
| 186 | {
|
| 187 | commandspreadplayers$position1.func_111095_a();
|
| 188 | commandspreadplayers$position.func_111094_b(commandspreadplayers$position1);
|
| 189 | }
|
| 190 | else
|
| 191 | {
|
| 192 | commandspreadplayers$position.func_111097_a(p_110668_5_, p_110668_6_, p_110668_8_, p_110668_10_, p_110668_12_);
|
| 193 | }
|
| 194 |
|
| 195 | flag = true;
|
| 196 | }
|
| 197 |
|
| 198 | if (commandspreadplayers$position.func_111093_a(p_110668_6_, p_110668_8_, p_110668_10_, p_110668_12_))
|
| 199 | {
|
| 200 | flag = true;
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | if (!flag)
|
| 205 | {
|
| 206 | for (CommandSpreadPlayers.Position commandspreadplayers$position3 : p_110668_14_)
|
| 207 | {
|
| 208 | if (!commandspreadplayers$position3.func_111098_b(worldIn))
|
| 209 | {
|
| 210 | commandspreadplayers$position3.func_111097_a(p_110668_5_, p_110668_6_, p_110668_8_, p_110668_10_, p_110668_12_);
|
| 211 | flag = true;
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | if (i >= 10000)
|
| 218 | {
|
| 219 | throw new CommandException("commands.spreadplayers.failure." + (p_110668_15_ ? "teams" : "players"), new Object[] {Integer.valueOf(p_110668_14_.length), Double.valueOf(p_110668_1_.field_111101_a), Double.valueOf(p_110668_1_.field_111100_b), String.format("%.2f", new Object[]{Double.valueOf(d0)})});
|
| 220 | }
|
| 221 | else
|
| 222 | {
|
| 223 | return i;
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | private double func_110671_a(List<Entity> p_110671_1_, World worldIn, CommandSpreadPlayers.Position[] p_110671_3_, boolean p_110671_4_)
|
| 228 | {
|
| 229 | double d0 = 0.0D;
|
| 230 | int i = 0;
|
| 231 | Map<Team, CommandSpreadPlayers.Position> map = Maps.<Team, CommandSpreadPlayers.Position>newHashMap();
|
| 232 |
|
| 233 | for (int j = 0; j < p_110671_1_.size(); ++j)
|
| 234 | {
|
| 235 | Entity entity = (Entity)p_110671_1_.get(j);
|
| 236 | CommandSpreadPlayers.Position commandspreadplayers$position;
|
| 237 |
|
| 238 | if (p_110671_4_)
|
| 239 | {
|
| 240 | Team team = entity instanceof EntityPlayer ? ((EntityPlayer)entity).getTeam() : null;
|
| 241 |
|
| 242 | if (!map.containsKey(team))
|
| 243 | {
|
| 244 | map.put(team, p_110671_3_[i++]);
|
| 245 | }
|
| 246 |
|
| 247 | commandspreadplayers$position = (CommandSpreadPlayers.Position)map.get(team);
|
| 248 | }
|
| 249 | else
|
| 250 | {
|
| 251 | commandspreadplayers$position = p_110671_3_[i++];
|
| 252 | }
|
| 253 |
|
| 254 | entity.setPositionAndUpdate((double)((float)MathHelper.floor_double(commandspreadplayers$position.field_111101_a) + 0.5F), (double)commandspreadplayers$position.func_111092_a(worldIn), (double)MathHelper.floor_double(commandspreadplayers$position.field_111100_b) + 0.5D);
|
| 255 | double d2 = Double.MAX_VALUE;
|
| 256 |
|
| 257 | for (int k = 0; k < p_110671_3_.length; ++k)
|
| 258 | {
|
| 259 | if (commandspreadplayers$position != p_110671_3_[k])
|
| 260 | {
|
| 261 | double d1 = commandspreadplayers$position.func_111099_a(p_110671_3_[k]);
|
| 262 | d2 = Math.min(d1, d2);
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | d0 += d2;
|
| 267 | }
|
| 268 |
|
| 269 | d0 = d0 / (double)p_110671_1_.size();
|
| 270 | return d0;
|
| 271 | }
|
| 272 |
|
| 273 | private CommandSpreadPlayers.Position[] func_110670_a(Random p_110670_1_, int p_110670_2_, double p_110670_3_, double p_110670_5_, double p_110670_7_, double p_110670_9_)
|
| 274 | {
|
| 275 | CommandSpreadPlayers.Position[] acommandspreadplayers$position = new CommandSpreadPlayers.Position[p_110670_2_];
|
| 276 |
|
| 277 | for (int i = 0; i < acommandspreadplayers$position.length; ++i)
|
| 278 | {
|
| 279 | CommandSpreadPlayers.Position commandspreadplayers$position = new CommandSpreadPlayers.Position();
|
| 280 | commandspreadplayers$position.func_111097_a(p_110670_1_, p_110670_3_, p_110670_5_, p_110670_7_, p_110670_9_);
|
| 281 | acommandspreadplayers$position[i] = commandspreadplayers$position;
|
| 282 | }
|
| 283 |
|
| 284 | return acommandspreadplayers$position;
|
| 285 | }
|
| 286 |
|
| 287 | public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
|
| 288 | {
|
| 289 | return args.length >= 1 && args.length <= 2 ? func_181043_b(args, 0, pos) : null;
|
| 290 | }
|
| 291 |
|
| 292 | static class Position
|
| 293 | {
|
| 294 | double field_111101_a;
|
| 295 | double field_111100_b;
|
| 296 |
|
| 297 | Position()
|
| 298 | {
|
| 299 | }
|
| 300 |
|
| 301 | Position(double p_i1358_1_, double p_i1358_3_)
|
| 302 | {
|
| 303 | this.field_111101_a = p_i1358_1_;
|
| 304 | this.field_111100_b = p_i1358_3_;
|
| 305 | }
|
| 306 |
|
| 307 | double func_111099_a(CommandSpreadPlayers.Position p_111099_1_)
|
| 308 | {
|
| 309 | double d0 = this.field_111101_a - p_111099_1_.field_111101_a;
|
| 310 | double d1 = this.field_111100_b - p_111099_1_.field_111100_b;
|
| 311 | return Math.sqrt(d0 * d0 + d1 * d1);
|
| 312 | }
|
| 313 |
|
| 314 | void func_111095_a()
|
| 315 | {
|
| 316 | double d0 = (double)this.func_111096_b();
|
| 317 | this.field_111101_a /= d0;
|
| 318 | this.field_111100_b /= d0;
|
| 319 | }
|
| 320 |
|
| 321 | float func_111096_b()
|
| 322 | {
|
| 323 | return MathHelper.sqrt_double(this.field_111101_a * this.field_111101_a + this.field_111100_b * this.field_111100_b);
|
| 324 | }
|
| 325 |
|
| 326 | public void func_111094_b(CommandSpreadPlayers.Position p_111094_1_)
|
| 327 | {
|
| 328 | this.field_111101_a -= p_111094_1_.field_111101_a;
|
| 329 | this.field_111100_b -= p_111094_1_.field_111100_b;
|
| 330 | }
|
| 331 |
|
| 332 | public boolean func_111093_a(double p_111093_1_, double p_111093_3_, double p_111093_5_, double p_111093_7_)
|
| 333 | {
|
| 334 | boolean flag = false;
|
| 335 |
|
| 336 | if (this.field_111101_a < p_111093_1_)
|
| 337 | {
|
| 338 | this.field_111101_a = p_111093_1_;
|
| 339 | flag = true;
|
| 340 | }
|
| 341 | else if (this.field_111101_a > p_111093_5_)
|
| 342 | {
|
| 343 | this.field_111101_a = p_111093_5_;
|
| 344 | flag = true;
|
| 345 | }
|
| 346 |
|
| 347 | if (this.field_111100_b < p_111093_3_)
|
| 348 | {
|
| 349 | this.field_111100_b = p_111093_3_;
|
| 350 | flag = true;
|
| 351 | }
|
| 352 | else if (this.field_111100_b > p_111093_7_)
|
| 353 | {
|
| 354 | this.field_111100_b = p_111093_7_;
|
| 355 | flag = true;
|
| 356 | }
|
| 357 |
|
| 358 | return flag;
|
| 359 | }
|
| 360 |
|
| 361 | public int func_111092_a(World worldIn)
|
| 362 | {
|
| 363 | BlockPos blockpos = new BlockPos(this.field_111101_a, 256.0D, this.field_111100_b);
|
| 364 |
|
| 365 | while (blockpos.getY() > 0)
|
| 366 | {
|
| 367 | blockpos = blockpos.down();
|
| 368 |
|
| 369 | if (worldIn.getBlockState(blockpos).getBlock().getMaterial() != Material.air)
|
| 370 | {
|
| 371 | return blockpos.getY() + 1;
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | return 257;
|
| 376 | }
|
| 377 |
|
| 378 | public boolean func_111098_b(World worldIn)
|
| 379 | {
|
| 380 | BlockPos blockpos = new BlockPos(this.field_111101_a, 256.0D, this.field_111100_b);
|
| 381 |
|
| 382 | while (blockpos.getY() > 0)
|
| 383 | {
|
| 384 | blockpos = blockpos.down();
|
| 385 | Material material = worldIn.getBlockState(blockpos).getBlock().getMaterial();
|
| 386 |
|
| 387 | if (material != Material.air)
|
| 388 | {
|
| 389 | return !material.isLiquid() && material != Material.fire;
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | return false;
|
| 394 | }
|
| 395 |
|
| 396 | public void func_111097_a(Random p_111097_1_, double p_111097_2_, double p_111097_4_, double p_111097_6_, double p_111097_8_)
|
| 397 | {
|
| 398 | this.field_111101_a = MathHelper.getRandomDoubleInRange(p_111097_1_, p_111097_2_, p_111097_6_);
|
| 399 | this.field_111100_b = MathHelper.getRandomDoubleInRange(p_111097_1_, p_111097_4_, p_111097_8_);
|
| 400 | }
|
| 401 | }
|
| 402 | }
|
| 403 |
|