Spawn Entity on Player

Discussion in 'Plugin Development' started by JavaNoob, Sep 3, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    JavaNoob

    Hi! I figured out the potion effect problem, and now I am messing with zombies once again. The armor and giving them materials and everything works, but I made a command for it. I made a command that when run, will spawn 20 zombies on top of the player to battle. The command registers, but nothing happens. I am pretty sure I did something wrong with the spawnCreature. Can you help me fix the spawnCreature or maybe tell me a different way to spawn entities on top of players from a command? Thanks!
    Code:
    package me.nay.block;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.EntitySpawnEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    
    
    public class Main extends JavaPlugin implements Listener {
    
    	@Override
    	public void onEnable() {
    		System.out.println("PLUGIN ENABLED");
    		Bukkit.getPluginManager().registerEvents(this, this);
    	}
    	@Override
    	public void onDisable() {
    		System.out.println("PLUGIN DISABLED");
    	}
    
    	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    		if (cmd.getName().equals("speed1")) {
    			if (sender instanceof Player) {
    				Player player = (Player) sender;
    				player.setFlySpeed((float) .20);
    				player.setAllowFlight(true);
    			}
    			
    		} else if (cmd.getName().equals("speed2")) {
    			if (sender instanceof Player) {
    				
    			
    				Player player = (Player) sender;
    				player.setFlySpeed((float) .50);
    			}
    		} else if (cmd.getName().equals("speed3")) {
    			if (sender instanceof Player) {
    				Player player = (Player) sender;
    				player.setFlySpeed((float) 1.0);
    			}
    		} else if (cmd.getName().equals("heal")) {
    			Player player = (Player) sender;
    			player.setHealth(20.0);
    			
    		} else if (cmd.getName().equals("echest")) {
    			if (sender instanceof Player) {
    				Player player = (Player) sender;
    				Inventory ender = player.getEnderChest();
    				ItemStack hand = player.getItemInHand();
    				ender.addItem(hand);
    				player.sendMessage(ChatColor.BLUE + "Item added to ender chest!");
    			}
    		
    		} else if (cmd.getName().equals("zombieraid")) {
    			Player player = (Player) sender;
    			int i = 0;
    			while (i != 2) {
    				spawnCreature(player.getLocation(), EntityType.ZOMBIE);
    				i = i + 1;
    			}
    			return true;
    		}
    		
    		return false;
    	}
    	
    
    	private void spawnCreature(Location location, EntityType zombie) {
    		
    	}
    
    	@EventHandler
    	public void onSpawn(EntitySpawnEvent event) {
    		if (event.getEntityType() == EntityType.CREEPER) {
    			Creeper creeper = (Creeper) event.getEntity();
    			creeper.setPowered(true);
    		
    		} else if (event.getEntityType() == EntityType.ZOMBIE) {
    			Zombie zombie = (Zombie) event.getEntity();
    			zombie.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    			zombie.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    			zombie.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    			zombie.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    			ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    			sword.addEnchantment(Enchantment.KNOCKBACK, 2);
    			zombie.getEquipment().setItemInHand(sword);
    			zombie.getEquipment().setItemInHandDropChance((float) 50.0);
    			
    		} else if (event.getEntityType() == EntityType.SKELETON) {
    			Skeleton skeleton = (Skeleton) event.getEntity();
    			skeleton.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    			skeleton.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    			skeleton.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    			skeleton.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    			ItemStack bow = new ItemStack(Material.BOW);
    			bow.addEnchantment(Enchantment.ARROW_DAMAGE, 5);
    			skeleton.getEquipment().setItemInHand(bow);
    		}
    	}
    
    
    
    }
    Any help is much appreciated!
     
    Last edited by a moderator: Sep 3, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @JavaNoob And how are you sure that the command registers?
    Could you post your plugin.yml?
     
  3. Offline

    JavaNoob

    Yes, here it is. Also, until I changed the loop to only run twice, I had it at 30. I thought it was working because whenever I ran the command, it would crash the server, so I dont know what's wrong.
    Code:
    main: me.nay.block.Main
    
    name: Zombies
    
    description: zombies
    
    version: 1.1
    
    commands:
    
      speed1:
    
      speed2:
    
      speed3:
    
      heal:
    
      echest:
    
      zombieraid:
     
    Last edited: Sep 4, 2020
  4. Offline

    Strahan

    Code:
    System.out.println("PLUGIN ENABLED");
    This is unnecessary; the server already sends enable/disable messages. Also even if you do want to clutter the log, use the logger not println.

    Code:
    if (sender instanceof Player) {
      Player player = (Player) sender;
    Instead of doing this over and over and over, do it once at the beginning of the method. All your commands require a player, so may as well. Also don't forget to check if it is a Player before casting it thus.

    Code:
    int i = 0;
    while (i != 2) {
      spawnCreature(player.getLocation(), EntityType.ZOMBIE);
      i = i + 1;
    }
    That is one seriously awkward way to loop. Don't use a while loop for something like this, use a regular FOR loop:
    Code:
    for (int i=0; i<2; i++) spawnCreature(player.getLocation(), EntityType.ZOMBIE);
    Which brings us to your spawnCreature method. It's empty; of course it isn't going to work lol

    PS, you could also streamline that spawn event a bit. Monster exposes the equipment methods, I'd just switch on type and for creeper do the powered but then on skeleton/zombie use Monster so you only have to do it with one instance. Example:
    Code:
    @EventHandler
    public void onSpawn(EntitySpawnEvent event) {
      switch (event.getEntityType()) {
      case CREEPER:
        ((Creeper) event.getEntity()).setPowered(true);
        break;
    
      case ZOMBIE:
      case SKELETON:
        Monster m = (Monster)event.getEntity();
        m.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
        m.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
        m.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
        m.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
       
        ItemStack item;
        if (event.getEntityType() == EntityType.ZOMBIE) {
          item = new ItemStack(Material.DIAMOND_SWORD);
          item.addEnchantment(Enchantment.KNOCKBACK, 2);
        } else {
          item = new ItemStack(Material.BOW);
          item.addEnchantment(Enchantment.ARROW_DAMAGE, 5);
        }
       
        m.getEquipment().setItemInHand(item);
        m.getEquipment().setItemInHandDropChance(50F);
        break;
       
      default:
      }
    }
    That said, I always test code before I post it and in my testing it works perfect except for the enchanted bow. I can't figure out how to override the default bow being put into the skelly's hands. Even tried just taking it away so it spawns with no bow at all, still spawns with a bow.

    No idea about that I'm afraid. Have to play with it more, but I have a 2:30 meeting so that'll have to be later heh.
     
    Last edited: Sep 4, 2020
  5. Offline

    JavaNoob

    Thank you so much! This is all great. I never thought of doing that, but that will help me streamline my code a lot more. I also didn't think of a for loop, tbh I was just excited to try it. I will try all this, and I will try to code the spawnCreature method. Thank you!

    Edit: Do you think this would suffice for the spawnCreature method?
    private void spawnCreature(Location location, EntityType entity) {

    Bukkit.getWorld("world").spawnEntity(location, entity)
    }
     
    Last edited: Sep 6, 2020
  6. Offline

    JavaNoob

    I put this in as the spawnCreature code, but it still wouldn't work. Is this right, or what am I doing wrong?
    private void spawnCreature(Location location, EntityType entity) {
    Bukkit.getWorld("World").spawnEntity(location,entity);
    }
    The two parameters that I am entering for this are player.getLocation(); for location, and EntityType.ZOMBIE; for the entity.
     
Thread Status:
Not open for further replies.

Share This Page