Wont Spawn

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

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

    JavaNoob

    Sorry I have been asking for a lot of help lately, but I have been busy with school so I'm braindead.
    Anyway, I am still working on the Raid code. The first wave spawns fine, but after they all die, the second wave does not spawn. I think there is an issue I can't see in the EntityDeathEvent counter. Can anyone see the problem?

    Code:
        public Player player = null;
        World world = null;
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equals("raidstart")) {
                if (sender instanceof Player) {
                    player = (Player) sender;
                    world = player.getWorld();
                    raid(1);
                }
            }
            return false;
        }
       
        public void raid(int wave) {
             if (wave == 1) {   
                player.getInventory().addItem(new ItemStack(Material.STONE_SWORD));
                int x = 0;
                int line = -6;
                while (x != 10) {
                    Zombie zomb = (Zombie) world.spawnEntity(player.getLocation().add(6, 0, line), EntityType.ZOMBIE);
                    zomb.setCustomName("Wave 1");
                    x++;
                    line++;
                }
                wave = 0;
             } else if (wave == 2) {
                 player.getInventory().clear();
                 player.getInventory().addItem(new ItemStack(Material.STONE_SWORD));
                 player.getInventory().setItemInOffHand(new ItemStack(Material.SHIELD));
                 int x = 0;
                 int line = -6;
                 while (x != 10) {
                     Zombie zomb = (Zombie) world.spawnEntity(player.getLocation().add(10, 0, line), EntityType.ZOMBIE);
                     Skeleton skele = (Skeleton) world.spawnEntity(player.getLocation().add(12, 0, line), EntityType.SKELETON);
                     line++;
                     x++;
                 }
                 wave = 0;
             }
         }
        public void onDeath(EntityDeathEvent event) {
            Entity ent = (Entity) event.getEntity();
            int x = 0;
            if (ent.getCustomName().equals("Wave 1")) {
                x++;
                if (x == 10) {
                    raid(2);
                }
            } 
     
  2. Offline

    Shqep

    Code:Java
    1. public Player player = null;

    player is now a global variable, that means only ONE person who last executes the command will get the items. Even if your intention is for only one player, pass the player variable on through parameters.
    Code:Java
    1. public final void raid(final int wave, final Player player) { /*Ya yeet.*/ }


    Code:Java
    1. public void onDeath(EntityDeathEvent event) {}

    You forgot the annotation EventHandler. Make sure to register the class as a listener too, if you haven't.

    I don't know much myself, but counting general entity names seems inefficient and inaccurate to start raids, even if you're just testing. LivingEntity(s) implements the Metadatable interface, so it would be better if you assign the mobs that are spawned on raids a fixed MetadataValue, then count deaths from mobs that have that metadata value.

    Code:Java
    1. int x = 0;
    2. while (x != 10) { x++; }
    3.  
    4. for(int x = 0; i < 10; i++) {
    5. //Eyyy
    6. }
    7.  
    8. // Or
    9. IntStream.range(0, 10).forEach(x -> { /*Same thing as for loop's body*/ });

    for loops exist for this purpose, or you could use IntStream.

    Code:Java
    1. wave = 0;

    Fill me in if I'm dense as frick but what is this for? You're not going to reuse the passed variables anyway, judging from your snippet.

    Corrections are very much welcomed.
     
    Xp10d3 likes this.
  3. Offline

    JavaNoob

    Thank you, I didn't notice that I forgot the Event Handler. Also, I was originally going to set a metadata, but I don't know how metadata's work yet. Are they a string or an integer?
     
  4. Offline

    Shqep

    @JavaNoob
    It is neither a string nor an integer. It is a metadata value, bound to a Plugin with a specified name. Basically, it's just an annotation but for entities in this case.
     
  5. Offline

    JavaNoob

    Sorry, I'm pretty dumb. Could you give an example?
     
  6. Offline

    Xp10d3

    Code:java
    1.  
    2. ItemStack item = new ItemStack(Material.STONE);
    3. ItemMeta itemMeta = item.getItemMeta();
    4. itemMeta.setEnchantment(Enchantment.DAMAGE_ALL);
    5.  

    Gah, I haven't worked with ItemStack's in a while. ItemMeta#setEnchantment is probably wrong lmao. Anyways, Shqep is sayin' that itemMeta is neither a string or an integer, but rather an annotation that MC read's as setting lore, item/entity names, enchantments, etc. You can read it by logging it or whatever like this:
    Code:java
    1.  
    2. ItemStack item = new ItemStack(Material.STONE);
    3. ItemMeta itemMeta = item.getItemMeta();
    4. itemMeta.setEnchantment(Enchantment.DAMAGE_ALL);
    5. ...
    6. public void sendMessage(Player player) {
    7. player.sendMessage("Item data: " + itemMeta
    8. }
    9.  

    It will print something like:
    PHP:
    enchantmentsDAMAGE_ALLid}
    Or whatever idk. But all that to say, ItemMeta is not a string, more like a list or whatever. Mess around with it; hope that helped.

    (corrections or welcome; i prob botched it but ik enough about itemmeta to help out xD)
     
    JavaNoob likes this.
  7. Offline

    KarimAKL

    He said "metadata", not "itemmeta". :p

    @Shqep already linked the Javadocs. I do not think there is anything else we can do except spoonfeed.
     
    Xp10d3 likes this.
  8. Offline

    JavaNoob

    lmao okay. Thank you both for your help, I will just troubleshoot until I figure it out XD.
     
  9. Offline

    Xp10d3

    Mb lol didn't read correctly.
     
Thread Status:
Not open for further replies.

Share This Page