fixes, cmds

This commit is contained in:
Sen 2025-03-19 02:08:41 +01:00
parent 868a5ed9ea
commit c906760bd4
13 changed files with 295 additions and 134 deletions

View file

@ -14,6 +14,7 @@ import game.entity.npc.EntityNPC;
import game.entity.types.EntityLiving;
import game.item.Item;
import game.item.ItemStack;
import game.nbt.NBTTagCompound;
import game.world.BlockPos;
import game.world.State;
import game.world.Vec3;
@ -403,4 +404,8 @@ public class ScriptArgs {
public WorldServer getWorld(String name) {
return this.getUnchecked(name);
}
public NBTTagCompound getTag(String name) {
return this.getUnchecked(name);
}
}

View file

@ -1,10 +1,14 @@
package game.command;
import java.util.Collection;
import java.util.Map;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import game.command.DoubleParser.DefType;
import game.world.World;
public abstract class ScriptExecutable implements Executable {
private final String name;
private final Map<String, Parameter> parameters = Maps.newHashMap();
@ -31,6 +35,62 @@ public abstract class ScriptExecutable implements Executable {
return this;
}
protected ScriptExecutable addParameter(ArgumentParser parser) {
return this.addParameter(parser.getName(), parser);
}
protected ScriptExecutable addParameter(char shortName, ArgumentParser parser) {
return this.addParameter(parser.getName(), shortName, parser);
}
protected ScriptExecutable addVector(String name, boolean defaulted) {
return this.addParameter(name, new DoubleParser("x", defaulted ? DefType.X : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE), new DoubleParser("y", defaulted ? DefType.Y : null, 0.0, (double)World.HEIGHT), new DoubleParser("z", defaulted ? DefType.Z : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE));
}
protected ScriptExecutable addWorld(String name, boolean defaulted) {
return this.addParameter(new WorldParser(name, false, defaulted));
}
protected ScriptExecutable addInt(String name, char shortName, int min, int max, int def) {
return this.addParameter(shortName, new IntParser(name, false, def, min, max, def));
}
protected ScriptExecutable addInt(String name, int min, int max, int def) {
return this.addParameter(new IntParser(name, false, def, min, max, def));
}
protected ScriptExecutable addFlag(String name, char shortName) {
return this.addParameter(name, shortName);
}
protected ScriptExecutable addFlag(String name) {
return this.addParameter(name, name.charAt(0));
}
protected ScriptExecutable addEnum(String name, Object ... values) {
return this.addParameter(new EnumParser(name, null, values));
}
protected ScriptExecutable addEnumDef(String name, Object def, Object ... values) {
return this.addParameter(new EnumParser(name, null, values));
}
protected ScriptExecutable addEnum(String name, Collection<?> values) {
return this.addEnum(name, (Object[])values.toArray(new String[values.size()]));
}
protected ScriptExecutable addEnumDef(String name, Object def, Collection<?> values) {
return this.addEnumDef(name, def, (Object[])values.toArray(new String[values.size()]));
}
protected ScriptExecutable addTag(String name) {
return this.addParameter(new TagParser(name, null));
}
protected ScriptExecutable addTag(String name, char shortName) {
return this.addParameter(shortName, new TagParser(name, null));
}
public Object exec(ScriptEnvironment env, ScriptArgs args) {
return null;
}

View file

@ -0,0 +1,22 @@
package game.command;
import game.nbt.NBTException;
import game.nbt.NBTParser;
import game.nbt.NBTTagCompound;
public class TagParser extends DefaultingParser {
public TagParser(String name, NBTTagCompound def, Object ... completions) {
super(name, def, completions);
}
public NBTTagCompound parse(ScriptEnvironment env, String input) {
NBTTagCompound value;
try {
value = NBTParser.parseTag(input);
}
catch(NBTException e) {
throw new ScriptException(e, "Ungültiger NBT-Tag '%s'", input);
}
return value;
}
}

View file

@ -4,20 +4,15 @@ import java.util.Set;
import com.google.common.collect.Sets;
import game.command.DoubleParser;
import game.command.DoubleParser.DefType;
import game.command.EnumParser;
import game.command.IntParser;
import game.command.ScriptArgs;
import game.command.ScriptEnvironment;
import game.command.ScriptException;
import game.command.ScriptExecutable;
import game.command.WorldParser;
import game.entity.Entity;
import game.entity.types.EntityLiving;
import game.init.EntityRegistry;
import game.nbt.NBTTagCompound;
import game.world.Vec3;
import game.world.World;
import game.world.WorldServer;
public class CommandSpawn extends ScriptExecutable {
@ -27,13 +22,14 @@ public class CommandSpawn extends ScriptExecutable {
for(Class<? extends Entity> clazz : EntityRegistry.getAllClasses()) {
names.add(EntityRegistry.getEntityString(clazz));
}
this.addParameter("type", new EnumParser("type", null, (Object[])names.toArray(new String[names.size()])));
this.addEnum("type", names);
this.setParamsOptional();
this.addParameter("position", new DoubleParser("x", DefType.X, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE), new DoubleParser("y", DefType.Y, 0.0, (double)World.HEIGHT), new DoubleParser("z", DefType.Z, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE));
this.addParameter("dim", new WorldParser("dim", false, true));
this.addParameter("noinit", 'n');
this.addParameter("count", 'c', new IntParser("count", false, 1, 1, 1024));
this.addVector("position", true);
this.addWorld("dim", true);
this.addTag("tag");
this.addFlag("noinit");
this.addInt("count", 'c', 1, 1024, 1);
this.addTag("postTag", 'p');
}
public Object exec(ScriptEnvironment env, ScriptArgs args) {
@ -42,14 +38,28 @@ public class CommandSpawn extends ScriptExecutable {
Vec3 pos = args.getVector("position");
int count = args.getInt("count");
boolean init = !args.has("noinit");
NBTTagCompound tag = args.getTag("tag");
NBTTagCompound postTag = args.getTag("postTag");
for(int z = 0; z < count; z++) {
Entity entity = EntityRegistry.createEntityByName(type, world);
if(entity == null)
throw new ScriptException("Objekt konnte nicht erzeugt werden");
entity.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, world.rand.floatv() * 360.0f, 0.0f);
if(tag != null) {
NBTTagCompound ent = new NBTTagCompound();
entity.writeToNBT(ent);
ent.merge(tag);
entity.readFromNBT(ent);
}
if(init && (entity instanceof EntityLiving))
((EntityLiving)entity).onInitialSpawn(null);
world.spawnEntityInWorld(entity);
if(postTag != null) {
NBTTagCompound ent = new NBTTagCompound();
entity.writeToNBT(ent);
ent.merge(postTag);
entity.readFromNBT(ent);
}
}
env.getExecutor().logConsole("%s%s bei %d, %d, %d in %s erschaffen", count == 1 ? "" : (count + "x "), EntityRegistry.getEntityName(type), (int)pos.xCoord, (int)pos.yCoord, (int)pos.zCoord, world.dimension.getFormattedName(false));
return null;