Issue when spawning projectile

Discussion in 'Plugin Development' started by TheBoo, Jun 7, 2013.

Thread Status:
Not open for further replies.
  1. I'm trying to make a special plugin that allows certain tools to fire snowballs that have different amount of damage on impact, but when I try to right click my tools (currently only a stone tool) it does not do anything. Here is my current code.

    Code:
    package com.enjin.zamoscraft.minez;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class MineZ extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            this.getLogger().info("======ZamosCraft MineZ ENABLED======");
            Bukkit.getPluginManager().registerEvents(new MineZListener(this), this);
        }
       
        @Override
        public void onDisable()
        {
            this.getLogger().info("======ZamosCraft MineZ DISABLED======");
        }
    }
    
    Code:
    package com.enjin.zamoscraft.minez;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.metadata.FixedMetadataValue;
     
    public class MineZListener implements Listener
    {
        private MineZ instance;
     
        public MineZListener(MineZ instance)
        {
            this.instance = instance;
        }
       
        @EventHandler
        public void onPlayerRightClick(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
           
            if(!(player.getItemInHand() == new ItemStack(Material.STONE_HOE))) return;
           
            Snowball snow = (Snowball) player.launchProjectile(Snowball.class);
            snow.setMetadata("MineZ", new FixedMetadataValue(instance, "STONE"));         
           
        }
       
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent event)
        {   
            if(!(event.getEntity() instanceof Player)) return;
           
            Entity entity = event.getDamager();
            if(entity instanceof Snowball && entity.hasMetadata("MineZ")){
                if(entity.getMetadata("MineZ").toString().equals("STONE")){
                    event.setDamage(8);
                }
                if(entity.getMetadata("MineZ").toString().equals("DIAMOND")){
                    //WIP
                }
            }   
        }
     
    }
    
    I've been looking for a fix for a long time, and I would be grateful if someone could help me find the issue.
     
  2. Offline

    _Waffles_

    if(!(player.getItemInHand() == new ItemStack(Material.STONE_HOE))) return;
    I think this is your problem here, you should do something more like
    Material.STONE_HOE.equals(player.getItemInHand().getType())
    or maybe
    Material.STONE_HOE.getId() == player.getItemInHand().getTypeId()
     
  3. That actually works, thanks, but I forgot to mention the fact that the snowball is supposed to damage the target a bit more than usual, so that is my other problem.
     
  4. Offline

    _Waffles_

    You should print entity.getMetadata("MineZ").toString() out to console to see whats happening ;)
     
  5. Code:
    [org.bukkit.metadata.FixedMetadataValue@436d4b50]
    
    This is what I get when I print it to the console, I have no idea what it means.
     
  6. Offline

    sheigutn

    The getMetadata only returns the FixedMetaDataValue, so you have to get the value of this FixedMeadataValue.
    entity.getMetadata(...).get(0).asString().equals(...);
     
  7. Thanks everyone, it works perfect now.
     
Thread Status:
Not open for further replies.

Share This Page