Untitled
unknown
java
a year ago
5.3 kB
3
Indexable
Never
package net.shmit.courses.testplugin; import net.kyori.adventure.text.Component; import org.apache.commons.lang3.tuple.Pair; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.*; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; public class CommandKit implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (sender instanceof Player){ World world = ((Player) sender).getWorld(); Endermite endermite = world.spawn(new Location(world, -25.000, 63.000, -127.000), Endermite.class); Chicken chicken = world.spawn(new Location(world, -4.000, 63.000, -127.000), Chicken.class); Axolotl axolotl = world.spawn(new Location(world, -32.000, 63.000, -127.000), Axolotl.class); Pig pig = world.spawn(new Location(world, 10.000, 63.000, -127.000), Pig.class); Goat goat = world.spawn(new Location(world, -53.000, 63.000, -127.000), Goat.class); Villager villager = world.spawn(new Location(world, -46.000, 63.000, -127.000), Villager.class); Horse horse = world.spawn(new Location(world, -18.000, 63.000, -127.000), Horse.class); Cat cat = world.spawn(new Location(world, 3.000, 63.000, -127.000), Cat.class); Ravager ravager = world.spawn(new Location(world, -11.000, 63.000, -127.000), Ravager.class); ElderGuardian guardian = world.spawn(new Location(world, -39.000, 63.000, -127.000), ElderGuardian.class); List <SortMob> arr = new ArrayList<>(List.of( new SortMob(goat, goat.getLocation(), 5), new SortMob(villager, villager.getLocation(), 6), new SortMob(guardian, guardian.getLocation(), 10), new SortMob(axolotl, axolotl.getLocation(), 3), new SortMob(endermite, endermite.getLocation(), 1), new SortMob(horse, horse.getLocation(), 7), new SortMob(ravager, ravager.getLocation(), 9), new SortMob(chicken, chicken.getLocation(), 2), new SortMob(cat, cat.getLocation(), 8), new SortMob(pig, pig.getLocation(), 4) )); for (SortMob sortMob : arr) { sortMob.entity.setAI(false); } List <SortMob> arrCopy = arr.subList(0, arr.size()); List<Pair <Integer, Integer>> swaps = new ArrayList<>(); final int[] currentIndex = {0}; BukkitRunnable sortTask = new BukkitRunnable() { @Override public void run() { if (currentIndex[0] == swaps.size()) { this.cancel(); return; } int indexA = swaps.get(currentIndex[0]).getLeft(); int indexB = swaps.get(currentIndex[0]).getRight(); SortMob mobA = arr.get(indexA); SortMob mobB = arr.get(indexB); Location locationA = mobA.entity.getLocation().clone(); Location locationB = mobB.entity.getLocation().clone(); mobA.entity.teleport(locationB); mobB.entity.teleport(locationA); arr.set(indexA, mobB); arr.set(indexB, mobA); plugin.getLogger().info("Swapping " + indexA + " and " + indexB); currentIndex[0]++; } }; sortTask.runTaskTimer(plugin, 0, 10); for(int i = 0; i < 10; i++){ for(int j = 0; j < 9; j++) { if (arrCopy.get(j).weight > arrCopy.get(j + 1).weight) { // arr.get(j).entity.teleport(location2); // arr.get(j + 1).entity.teleport(location1); SortMob mobA = arrCopy.get(j); SortMob mobB = arrCopy.get(j + 1); arrCopy.set(j, mobB); arrCopy.set(j + 1, mobA); swaps.add(Pair.of(j, j + 1)); plugin.getLogger().info("Add " + j + " " + (j + 1) + " indexes to array"); } } } } return true; } private TestPlugin plugin; public CommandKit(TestPlugin plugin) { this.plugin = plugin; } } class SortMob { LivingEntity entity; int weight; public SortMob(LivingEntity entity, Location location, int weight){ this.entity = entity; this.entity.teleport(location); this.weight = weight; } }