fixes, entity info command

This commit is contained in:
Bixilon 2020-12-31 18:19:08 +01:00
parent 102a0f4862
commit 0206d7fb7f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 55 additions and 18 deletions

View File

@ -248,28 +248,35 @@ public abstract class Entity {
public TreeMap<String, Object> getEntityMetaDataFormatted() {
// scan all methods of current class for EntityMetaDataFunction annotation and write it into a list
Class<? extends Entity> clazz = this.getClass();
TreeMap<String, Object> values = new TreeMap<>();
for (Method method : clazz.getMethods()) {
if (!method.isAnnotationPresent(EntityMetaDataFunction.class)) {
continue;
}
if (method.getParameterCount() > 0) {
continue;
}
try {
String identifier = method.getAnnotation(EntityMetaDataFunction.class).identifier();
if (values.containsKey(identifier)) {
if (this.metaData == null) {
return values;
}
Class<?> clazz = this.getClass();
while (clazz != Object.class) {
for (Method method : clazz.getDeclaredMethods()) {
if (!method.isAnnotationPresent(EntityMetaDataFunction.class)) {
continue;
}
Object methodRetValue = method.invoke(this);
if (methodRetValue == null) {
if (method.getParameterCount() > 0) {
continue;
}
values.put(identifier, methodRetValue);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
method.setAccessible(true);
try {
String identifier = method.getAnnotation(EntityMetaDataFunction.class).identifier();
if (values.containsKey(identifier)) {
continue;
}
Object methodRetValue = method.invoke(this);
if (methodRetValue == null) {
continue;
}
values.put(identifier, methodRetValue);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
clazz = clazz.getSuperclass();
}
return values;
}

View File

@ -14,8 +14,12 @@
package de.bixilon.minosoft.terminal.commands.commands;
import com.github.freva.asciitable.AsciiTable;
import de.bixilon.minosoft.data.commands.CommandArgumentNode;
import de.bixilon.minosoft.data.commands.CommandLiteralNode;
import de.bixilon.minosoft.data.commands.CommandNode;
import de.bixilon.minosoft.data.commands.parser.IntegerParser;
import de.bixilon.minosoft.data.commands.parser.properties.IntegerParserProperties;
import de.bixilon.minosoft.data.entities.entities.Entity;
import java.util.ArrayList;
@ -24,7 +28,7 @@ public class CommandEntities extends Command {
@Override
public CommandNode build(CommandNode parent) {
parent.addChildren(
new CommandLiteralNode("entities",
new CommandLiteralNode("entity",
new CommandLiteralNode("list", (connection, stack) -> {
ArrayList<Object[]> tableData = new ArrayList<>();
@ -33,7 +37,33 @@ public class CommandEntities extends Command {
}
print(AsciiTable.getTable(new String[]{"ID", "UUID", "TYPE", "EQUIPMENT", "LOCATION", "ROTATION"}, tableData.toArray(new Object[0][0])));
})));
}),
new CommandLiteralNode("info", new CommandArgumentNode("entityId", IntegerParser.INTEGER_PARSER, new IntegerParserProperties(0, Integer.MAX_VALUE), (connection, stack) -> {
// ToDo: entity uuids
Entity entity = connection.getPlayer().getWorld().getEntity(stack.getInt(0));
if (entity == null) {
printError("Entity %d not found!", stack.getInt(0));
return;
}
ArrayList<Object[]> tableData = new ArrayList<>();
tableData.add(new Object[]{"entity id", entity.getEntityId()});
tableData.add(new Object[]{"uuid", entity.getUUID()});
tableData.add(new Object[]{"type", entity.getEntityInformation()});
tableData.add(new Object[]{"class", entity.getClass().getName()});
tableData.add(new Object[]{"location", entity.getLocation()});
tableData.add(new Object[]{"rotation", entity.getRotation()});
tableData.add(new Object[]{"equipment", entity.getEquipment()});
tableData.add(new Object[]{"effects", entity.getEffectList()});
tableData.add(new Object[]{"attached to", entity.getAttachedEntity() == -1 ? "" : entity.getAttachedEntity()});
for (var entry : entity.getEntityMetaDataFormatted().entrySet()) {
tableData.add(new Object[]{entry.getKey(), entry.getValue()});
}
print(AsciiTable.getTable(new String[]{"PROPERTY", "VALUE"}, tableData.toArray(new Object[0][0])));
}))));
return parent;
}
}