/** order the minions by id and return the previous one, or the first if none were active! */ public static void getPreviousMinion(boolean deleteactive) { EntityManager entman = CoreRegistry.get(EntityManager.class); List<Integer> sortedlist = new ArrayList<Integer>(); for (EntityRef minion : entman.iteratorEntities(MinionComponent.class)) { if (!minion.getComponent(MinionComponent.class).dying) { sortedlist.add(minion.getId()); } } if (sortedlist.size() == 0) { return; } else if (deleteactive && sortedlist.size() == 1) { activeminion = null; return; } Collections.sort(sortedlist); int index = 0; if (activeminion != null) { index = sortedlist.indexOf(activeminion.getId()); } if (index == 0) { index = sortedlist.size() - 1; } else { index--; } index = sortedlist.get(index); for (EntityRef minion : entman.iteratorEntities(MinionComponent.class)) { if (minion.getId() == index) { setActiveMinion(minion); } } }