Solved On armor stand spawn give them arms

Discussion in 'Plugin Development' started by phil14052, May 15, 2015.

Thread Status:
Not open for further replies.
  1. Hey, bukkit forums!

    The problem is solved!
    Thanks to @FisheyLP
    I am making a plugin that gives a armor stand arms each time, A player spawns a armor stand.
    The plugin works but it gives me a error.


    My code:
    Code:
    package me.phil14052.ArmedArmorStands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ArmedArmorStands extends JavaPlugin implements Listener{
    
    
        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this,this);
        
        }
    
        @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent  e){
            Entity as = (ArmorStand) e.getEntity();
            if(as == null){
                return;
            } else {
                e.getEntityType();
                if(as.getType() != EntityType.ARMOR_STAND){
                    return;
                } else if(e.isCancelled() != true){
                    Location loc = as.getLocation();
                    loc.setDirection(loc.getDirection());
                    ArmorStand stand = as.getLocation().getWorld().spawn(loc, ArmorStand.class);
                    stand.setArms(true);
                    e.setCancelled(true);
                }
            }
        
        
        
        }
    }
    
    My error:
    Its a long error so i put it on hastebin
    http://hastebin.com/eyamipuyoc.avrasm

    I now the problem is on line 34 but i just cant fix it

    - Phil14052
     
    Last edited: May 15, 2015
  2. Don't cast without knowing that the entity MUST be the thing you want to cast it.
    Example:
    Wrong:
    Code:
    ArmorStand as = (ArmorStand) entity;
    if (entity instanceof ArmorStand) {
    
    as.doStuff();
    
    }
    Right:
    Code:
    if (entity instanceof ArmorStand) {
    ArmorStand as = (ArmorStand) entity;
    
    as.doStuff();
    
    }
    Remove this. Your code is messed up.
    This is how you would do it the proper way:
    1. Check if the event is cancelled and return: if (e.isCancelled()) return;
    2. Check if the entity is an instance of ArmorStand
    3. Cast to ArmorStand
    4. Use the method .setArms(true); on the ArmorStand you casted before.
     
  3. Offline

    Agentleader1

    Part of the error says WorldGuard. Try and go without it as it apparently influences your plugin negatively with errors.
     
Thread Status:
Not open for further replies.

Share This Page