command entities

This commit is contained in:
Sen 2025-03-26 15:43:58 +01:00
parent e88958e2f4
commit 460a58e062
32 changed files with 293 additions and 37 deletions

View file

@ -1,12 +1,14 @@
package game.command;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import game.collect.Lists;
import game.collect.Sets;
import game.entity.Entity;
import game.entity.EntityType;
import game.entity.types.EntityLiving;
import game.init.EntityRegistry;
import game.network.Player;
@ -45,26 +47,67 @@ public class EntityListParser extends EntityParser {
throw new RunException("Keine Spieler gefunden");
return list;
}
Set<Entity> set = Sets.newHashSet();
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;
for(String tok : input.split(",", -1)) {
boolean negate = tok.startsWith("!");
tok = negate ? tok.substring(1) : tok;
Class<? extends Entity> clazz = EntityRegistry.getEntityClass(tok);
if(clazz != null) {
if(classes.contains(clazz) || nclasses.contains(clazz))
throw new RunException("Objekttyp %s mehrfach angegeben", EntityRegistry.getEntityName(EntityRegistry.getEntityString(clazz)));
if(this.livingOnly && !EntityLiving.class.isAssignableFrom(clazz))
throw new RunException("Objekttyp %s ist nicht lebendig", EntityRegistry.getEntityName(EntityRegistry.getEntityString(clazz)));
for(WorldServer world : env.getServer().getWorlds()) {
for(Entity ent : world.getEntities()) {
if(clazz.isAssignableFrom(ent.getClass()))
set.add(ent);
}
}
(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;
}
else {
set.add((Entity)super.parse(env, tok));
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);
}
}
}
if(set.isEmpty())
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) &&
(negateOnly || (living != null && living == (ent instanceof EntityLiving)) || (player != null && player == ent.isPlayer()) || types.contains(ent.getType()) ||
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())
throw new RunException("Keine Objekte gefunden");
return Lists.newArrayList(set);
return filtered;
}
public Object getDefault(CommandEnvironment env) {
@ -79,7 +122,10 @@ public class EntityListParser extends EntityParser {
if(!this.livingOnly || EntityLiving.class.isAssignableFrom(clazz))
comp.add(EntityRegistry.getEntityString(clazz));
}
comp.add("**");
for(EntityType type : EntityType.values()) {
comp.add(type.getName());
}
Collections.addAll(comp, "Player", "Living", "**");
return comp;
}