How to summon an entity with command argument (MCreator 2022.2 Snapshot b25609)

Started by Le lance Flamer on

Topic category: Help with modding (Java Edition)

Last seen on 19:04, 16. Jul 2022
Joined Apr 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to summon an entity with command argument (MCreator 2022.2 Snapshot b25609)
Thu, 06/30/2022 - 20:15 (edited)

Hi, I need to know the method to spawn an entity with a command argument. I tried adding a variable and then in the code making it act to spawner the entity but it tells me that an unexpected error occurred. The code for the procedure:

Entity entityThatWillBeSpawned = null;
		entityThatWillBeSpawned = new Object() {
			public Entity getEntity() {
				try {
					return EntityArgument.getEntity(arguments, "entity");
				} catch (CommandSyntaxException e) {
					e.printStackTrace();
					return null;
				}
			}
		}.getEntity();
Entity entityToSpawn = entityThatWillBeSpawned;
			entityToSpawn.moveTo((new Object() {
				public double getX() {
					try {
						return BlockPosArgument.getLoadedBlockPos(arguments, "pos").getX();
					} catch (CommandSyntaxException e) {
						e.printStackTrace();
						return 0;
					}
				}
			}.getX())

Don't tell me that you can't see more after .getX()) because it is useless.

Normally, you don't need the code below but if you want it.

if (entityToSpawn instanceof Mob _mobToSpawn)
				_mobToSpawn.finalizeSpawn(_level, world.getCurrentDifficultyAt(entityToSpawn.blockPosition()), MobSpawnType.MOB_SUMMONED, null, null);
			world.addFreshEntity(entityToSpawn);

Thanks for your help because for time I am hard thinking to remove the error.

Edited by Le lance Flamer on Thu, 06/30/2022 - 20:15
Last seen on 19:04, 16. Jul 2022
Joined Apr 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Oh, I found how to do it: 1…
Fri, 07/01/2022 - 09:15

Oh, I found how to do it:

1. In the command's procedure, open its code and put under the "execute" method (If you copy & paste, check each classes if they are imported. If not, re-enter the name of the class):

private static int spawnEntity(CommandSourceStack p_138821_, ResourceLocation p_138822_, Vec3 p_138823_, CompoundTag p_138824_, boolean p_138825_) throws CommandSyntaxException {
      BlockPos blockpos = new BlockPos(p_138823_);
      if (!Level.isInSpawnableBounds(blockpos)) {
         throw INVALID_POSITION.create();
      } else {
         CompoundTag compoundtag = p_138824_.copy();
         compoundtag.putString("id", p_138822_.toString());
         ServerLevel serverlevel = p_138821_.getLevel();
         Entity entity = EntityType.loadEntityRecursive(compoundtag, serverlevel, (p_138828_) -> {
            p_138828_.moveTo(p_138823_.x, p_138823_.y, p_138823_.z, p_138828_.getYRot(), p_138828_.getXRot());
            return p_138828_;
         });
         if (entity == null) {
            throw ERROR_FAILED.create();
         } else {
            if (p_138825_ && entity instanceof Mob) {
               ((Mob)entity).finalizeSpawn(p_138821_.getLevel(), p_138821_.getLevel().getCurrentDifficultyAt(entity.blockPosition()), MobSpawnType.COMMAND, (SpawnGroupData)null, (CompoundTag)null);
            }

            if (!serverlevel.tryAddFreshEntityWithPassengers(entity)) {
               throw ERROR_DUPLICATE_UUID.create();
            } else {
               p_138821_.sendSuccess(new TranslatableComponent("commands.summon.success", entity.getDisplayName()), true);
               return 1;
            }
         }
      }
   }

2. In the "execute" method, remove all code except the "execute" method and its parameters and then put a try-catch with the private method you created (spawnEntity) (see code below):

		try{
			spawnEntity(arguments.getSource(), EntitySummonArgument.getSummonableEntity(arguments, "nameOfTheEntityArgument"), Vec3Argument.getVec3(arguments, "vec3Argument"), new CompoundTag(), true);
		}
		catch (CommandSyntaxException e){
			e.printStackTrace();
		}

Tell me if any errors occur, I can fix them for you. AND ALSO DON'T FORGET TO SAVE AND LOCK THE CODE FOR MCREATOR OR THE CODE WON'T BE CHANGED!!!!