From 6ac0bc831a97b8b863cf81780709b16990aa0fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 1 May 2021 02:55:46 +0200 Subject: [PATCH 1/2] Added entity class argument --- .../java/demo/commands/SummonCommand.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/test/java/demo/commands/SummonCommand.java b/src/test/java/demo/commands/SummonCommand.java index 0e03ef215..7474d541b 100644 --- a/src/test/java/demo/commands/SummonCommand.java +++ b/src/test/java/demo/commands/SummonCommand.java @@ -3,17 +3,22 @@ package demo.commands; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.arguments.ArgumentEnum; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType; import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec3; import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.entity.Entity; +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.EntityType; +import net.minestom.server.entity.LivingEntity; import org.jetbrains.annotations.NotNull; public class SummonCommand extends Command { private final ArgumentEntityType entity; private final ArgumentRelativeVec3 pos; + private final ArgumentEnum entityClass; public SummonCommand() { super("summon"); @@ -21,12 +26,35 @@ public class SummonCommand extends Command { entity = ArgumentType.EntityType("entity type"); pos = ArgumentType.RelativeVec3("pos"); - addSyntax(this::execute, entity, pos); + entityClass = ArgumentType.Enum("class", EntityClass.class); + entityClass.setFormat(ArgumentEnum.Format.LOWER_CASED); + addSyntax(this::execute, entity, pos, entityClass); + setDefaultExecutor((sender, context) -> sender.sendMessage("Usage: /summon ")); } private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { - final Entity entity = new Entity(commandContext.get(this.entity)); + final Entity entity = commandContext.get(entityClass).instantiate(commandContext.get(this.entity)); //noinspection ConstantConditions - One couldn't possibly execute a command without being in an instance entity.setInstance(commandSender.asPlayer().getInstance(), commandContext.get(pos).from(commandSender.asPlayer()).toPosition()); } + + @SuppressWarnings("unused") + enum EntityClass { + BASE(Entity::new), + LIVING(LivingEntity::new), + CREATURE(EntityCreature::new); + private final EntityFactory factory; + + EntityClass(EntityFactory factory) { + this.factory = factory; + } + + public Entity instantiate(EntityType type) { + return factory.newInstance(type); + } + } + + interface EntityFactory { + Entity newInstance(EntityType type); + } } From 7494c5ac58a3dd22012453400fb8bd344355690e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 4 May 2021 02:20:24 +0200 Subject: [PATCH 2/2] Added default entity type --- src/test/java/demo/commands/SummonCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/demo/commands/SummonCommand.java b/src/test/java/demo/commands/SummonCommand.java index 7474d541b..e47a49c0f 100644 --- a/src/test/java/demo/commands/SummonCommand.java +++ b/src/test/java/demo/commands/SummonCommand.java @@ -28,6 +28,7 @@ public class SummonCommand extends Command { pos = ArgumentType.RelativeVec3("pos"); entityClass = ArgumentType.Enum("class", EntityClass.class); entityClass.setFormat(ArgumentEnum.Format.LOWER_CASED); + entityClass.setDefaultValue(EntityClass.CREATURE); addSyntax(this::execute, entity, pos, entityClass); setDefaultExecutor((sender, context) -> sender.sendMessage("Usage: /summon ")); }