package com.alttd.custommobs.commands.subcommands; import com.alttd.custommobs.Main; import com.alttd.custommobs.abilities.EntityModifier; import com.alttd.custommobs.abilities.MobType; import com.alttd.custommobs.abilities.MobTypeApplier; import com.alttd.custommobs.commands.SubCommand; import com.alttd.custommobs.config.Messages; import com.alttd.custommobs.config.MobTypes; import com.alttd.custommobs.utility.SpawnableTargetBlock; import lombok.extern.slf4j.Slf4j; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.*; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @Slf4j public class Spawn extends SubCommand { private final MobTypeApplier mobTypeApplier; public Spawn(Main main) { this.mobTypeApplier = new MobTypeApplier(main); } @Override public boolean onCommand(CommandSender commandSender, String[] args) { if (args.length != 3 && args.length != 7) { log.info("DEBUG: Invalid argument length: {} expected 2 or 6", args.length); return false; } Optional optionalMobType = MobTypes.MOB_TYPES.get(args[1]); if (optionalMobType.isEmpty()) { return false; } MobType mobType = optionalMobType.get(); EntityType entityType; try { entityType = EntityType.valueOf(args[2]); } catch (IllegalArgumentException e) { log.info("DEBUG: Invalid mob type {}", args[2]); return false; } if (!entityType.isSpawnable() || !isMob(entityType)) { return false; } Optional optionalLocation = getLocation(commandSender, args); if (optionalLocation.isEmpty()) { return false; } Location location = optionalLocation.get(); spawnMob(location.getWorld(), location, entityType, mobType); return true; } private Optional getLocation(CommandSender commandSender, String[] args) { if (args.length == 3 && commandSender instanceof Player player) { return new SpawnableTargetBlock().getSpawnableTargetBlock(player, 50); } else if (args.length == 7) { return getLocation(args); } else { log.info("DEBUG: Expected player with argument length of 2"); return Optional.empty(); } } private Optional getLocation(String[] args) { World world = Bukkit.getWorld(args[3]); if (world == null) { return Optional.empty(); } try { double x = Double.parseDouble(args[4]); double y = Double.parseDouble(args[5]); double z = Double.parseDouble(args[6]); return Optional.of(new Location(world, x, y, z)); } catch (NumberFormatException e) { log.info("DEBUG: Invalid coordinates x: {}, y: {}, z: {}", args[4], args[5], args[6]); return Optional.empty(); } } public boolean isMob(EntityType entityType) { if (entityType == null) { return false; } try { Class entityClass = entityType.getEntityClass(); if (entityClass == null) { return false; } Class mobEntityClass = Class.forName("org.bukkit.entity." + entityClass.getSimpleName()); return Mob.class.isAssignableFrom(mobEntityClass); } catch (ClassNotFoundException e) { return false; } } private void spawnMob(World world, Location location, EntityType entityType, MobType mobType) { log.info("one"); log.info(mobType.toString()); log.info(mobType.entityModifiers().stream().map(EntityModifier::getName).collect(Collectors.joining(", "))); if (entityType.getEntityClass() == null) { log.warn("Tried to spawn entity with null entity class {}", entityType.name()); return; } log.info("two"); Entity entity = world.spawn(location, entityType.getEntityClass()); log.info("three"); if (!(entity instanceof LivingEntity livingEntity)) { log.warn("Spawned entity is not a living entity? {}", entityType.name()); return; } log.info("four"); mobTypeApplier.apply(livingEntity, mobType); log.info("five"); } @Override public String getName() { return "spawn"; } @Override public List getTabComplete(CommandSender commandSender, String[] args) { return switch (args.length) { case 2 -> MobTypes.MOB_TYPES.getAllMobTypes(); case 3 -> Arrays.stream(EntityType.values()).map(EntityType::name).toList(); case 4 -> Bukkit.getWorlds().stream().map(World::getName).toList(); case 5 -> commandSender instanceof Player player ? List.of(String.valueOf(player.getX())) : List.of("0"); case 6 -> commandSender instanceof Player player ? List.of(String.valueOf(player.getY())) : List.of("0"); case 7 -> commandSender instanceof Player player ? List.of(String.valueOf(player.getZ())) : List.of("0"); default -> List.of(); }; } @Override public String getHelpMessage() { return Messages.HELP.SPAWN; } }