2025-03-25 17:02:41 +01:00
|
|
|
package game.command;
|
|
|
|
|
2025-03-26 13:27:17 +01:00
|
|
|
import java.util.Collection;
|
2025-03-26 15:43:58 +01:00
|
|
|
import java.util.Collections;
|
2025-03-25 17:02:41 +01:00
|
|
|
import java.util.List;
|
2025-03-25 23:10:40 +01:00
|
|
|
import java.util.Set;
|
2025-03-25 17:02:41 +01:00
|
|
|
|
|
|
|
import game.collect.Lists;
|
2025-03-25 23:10:40 +01:00
|
|
|
import game.collect.Sets;
|
2025-03-25 17:02:41 +01:00
|
|
|
import game.entity.Entity;
|
2025-03-26 15:43:58 +01:00
|
|
|
import game.entity.EntityType;
|
2025-03-25 17:02:41 +01:00
|
|
|
import game.entity.types.EntityLiving;
|
|
|
|
import game.init.EntityRegistry;
|
2025-03-26 12:22:32 +01:00
|
|
|
import game.network.Player;
|
2025-03-25 17:02:41 +01:00
|
|
|
import game.world.WorldServer;
|
|
|
|
|
|
|
|
public class EntityListParser extends EntityParser {
|
|
|
|
public EntityListParser(String name, boolean useSender, boolean livingOnly) {
|
|
|
|
super(name, useSender, livingOnly);
|
|
|
|
}
|
|
|
|
|
2025-03-25 23:10:40 +01:00
|
|
|
public Object parse(CommandEnvironment env, String input) {
|
2025-03-25 17:02:41 +01:00
|
|
|
if(input.equals("**")) {
|
|
|
|
List<Entity> list = Lists.newArrayList();
|
|
|
|
for(WorldServer world : env.getServer().getWorlds()) {
|
|
|
|
if(this.livingOnly) {
|
|
|
|
for(Entity ent : world.getEntities()) {
|
|
|
|
if(ent instanceof EntityLiving)
|
|
|
|
list.add(ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list.addAll(world.getEntities());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(list.isEmpty())
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException(this.livingOnly ? "Keine lebendigen Objekte gefunden" : "Keine Objekte gefunden");
|
2025-03-25 17:02:41 +01:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
else if(input.equals("*")) {
|
|
|
|
List<Entity> list = Lists.newArrayList();
|
2025-03-26 12:22:32 +01:00
|
|
|
for(Player plr : env.getServer().getPlayers()) {
|
2025-03-27 17:54:03 +01:00
|
|
|
if(plr.getPresentEntity() != null)
|
|
|
|
list.add(plr.getPresentEntity());
|
2025-03-25 17:02:41 +01:00
|
|
|
}
|
|
|
|
if(list.isEmpty())
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Keine Spieler gefunden");
|
2025-03-25 17:02:41 +01:00
|
|
|
return list;
|
|
|
|
}
|
2025-03-26 15:43:58 +01:00
|
|
|
Set<Class<? extends Entity>> classes = Sets.newHashSet();
|
|
|
|
Set<EntityType> types = Sets.newHashSet();
|
|
|
|
Set<Entity> entities = Sets.newHashSet();
|
|
|
|
Set<Class<? extends Entity>> nclasses = Sets.newHashSet();
|
|
|
|
Set<EntityType> ntypes = Sets.newHashSet();
|
|
|
|
Set<Entity> nentities = Sets.newHashSet();
|
|
|
|
Boolean living = null;
|
|
|
|
Boolean player = null;
|
2025-03-25 23:10:40 +01:00
|
|
|
for(String tok : input.split(",", -1)) {
|
2025-03-26 15:43:58 +01:00
|
|
|
boolean negate = tok.startsWith("!");
|
|
|
|
tok = negate ? tok.substring(1) : tok;
|
2025-03-25 23:10:40 +01:00
|
|
|
Class<? extends Entity> clazz = EntityRegistry.getEntityClass(tok);
|
|
|
|
if(clazz != null) {
|
2025-03-26 15:43:58 +01:00
|
|
|
if(classes.contains(clazz) || nclasses.contains(clazz))
|
|
|
|
throw new RunException("Objekttyp %s mehrfach angegeben", EntityRegistry.getEntityName(EntityRegistry.getEntityString(clazz)));
|
2025-03-25 23:10:40 +01:00
|
|
|
if(this.livingOnly && !EntityLiving.class.isAssignableFrom(clazz))
|
|
|
|
throw new RunException("Objekttyp %s ist nicht lebendig", EntityRegistry.getEntityName(EntityRegistry.getEntityString(clazz)));
|
2025-03-26 15:43:58 +01:00
|
|
|
(negate ? nclasses : classes).add(clazz);
|
|
|
|
}
|
|
|
|
else if(tok.equals("Player")) {
|
|
|
|
if(player != null)
|
|
|
|
throw new RunException("'Player' mehrfach angegeben");
|
|
|
|
player = !negate;
|
|
|
|
}
|
|
|
|
else if(tok.equals("Living")) {
|
|
|
|
if(living != null)
|
|
|
|
throw new RunException("'Living' mehrfach angegeben");
|
|
|
|
if(this.livingOnly)
|
|
|
|
throw new RunException("Kann nicht 'Living' als Objekttypen angeben");
|
|
|
|
living = !negate;
|
2025-03-25 17:02:41 +01:00
|
|
|
}
|
2025-03-25 23:10:40 +01:00
|
|
|
else {
|
2025-03-26 15:43:58 +01:00
|
|
|
EntityType type = EntityType.getByName(tok);
|
|
|
|
if(type != null) {
|
|
|
|
if(types.contains(type) || ntypes.contains(type))
|
|
|
|
throw new RunException("Objekttyp %s mehrfach angegeben", type.getDisplay());
|
|
|
|
(negate ? ntypes : types).add(type);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Entity ent = (Entity)super.parse(env, tok);
|
|
|
|
if(entities.contains(ent) || nentities.contains(ent))
|
|
|
|
throw new RunException("Objekt '%s' mehrfach angegeben", tok);
|
|
|
|
(negate ? nentities : entities).add(ent);
|
|
|
|
}
|
2025-03-25 23:10:40 +01:00
|
|
|
}
|
2025-03-25 17:02:41 +01:00
|
|
|
}
|
2025-03-26 15:43:58 +01:00
|
|
|
List<Entity> filtered = Lists.newArrayList(entities);
|
|
|
|
boolean negateOnly = (living == null && player == null && types.isEmpty() && classes.isEmpty() && entities.isEmpty());
|
|
|
|
for(WorldServer world : env.getServer().getWorlds()) {
|
|
|
|
for(Entity ent : world.getEntities()) {
|
|
|
|
if((!this.livingOnly || ent instanceof EntityLiving) &&
|
2025-03-26 21:25:25 +01:00
|
|
|
(negateOnly || (living != null && types.isEmpty() && classes.isEmpty() && living == (ent instanceof EntityLiving)) || (player != null && types.isEmpty() && classes.isEmpty() && player == ent.isPlayer()) || types.contains(ent.getType()) ||
|
2025-03-26 15:43:58 +01:00
|
|
|
classes.contains(ent.getClass())) &&
|
|
|
|
(living == null || living == (ent instanceof EntityLiving)) && (player == null || player == ent.isPlayer()) &&
|
|
|
|
!ntypes.contains(ent.getType()) && !nclasses.contains(ent.getClass()) && !nentities.contains(ent) && !entities.contains(ent))
|
|
|
|
filtered.add(ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(filtered.isEmpty())
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Keine Objekte gefunden");
|
2025-03-26 15:43:58 +01:00
|
|
|
return filtered;
|
2025-03-25 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2025-03-25 23:10:40 +01:00
|
|
|
public Object getDefault(CommandEnvironment env) {
|
2025-03-25 17:02:41 +01:00
|
|
|
Entity entity = (Entity)super.getDefault(env);
|
|
|
|
return entity == null ? null : Lists.newArrayList(entity);
|
|
|
|
}
|
|
|
|
|
2025-03-26 13:27:17 +01:00
|
|
|
public Collection<String> getCompletions(CommandEnvironment env) {
|
|
|
|
Collection<String> comp = super.getCompletions(env);
|
2025-03-25 17:02:41 +01:00
|
|
|
comp.add("*");
|
|
|
|
for(Class<? extends Entity> clazz : EntityRegistry.getAllClasses()) {
|
|
|
|
if(!this.livingOnly || EntityLiving.class.isAssignableFrom(clazz))
|
|
|
|
comp.add(EntityRegistry.getEntityString(clazz));
|
|
|
|
}
|
2025-03-26 15:43:58 +01:00
|
|
|
for(EntityType type : EntityType.values()) {
|
|
|
|
comp.add(type.getName());
|
|
|
|
}
|
|
|
|
Collections.addAll(comp, "Player", "Living", "**");
|
2025-03-26 13:27:17 +01:00
|
|
|
return comp;
|
2025-03-25 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Class<?> getTypeClass() {
|
|
|
|
return List.class;
|
|
|
|
}
|
|
|
|
}
|