Need help with creating a flying object!

Discussion in 'Plugin Development' started by PoseidonGraphics, Mar 28, 2015.

Thread Status:
Not open for further replies.
  1. Hello! I have been working on a flying feather plugin and I can't seem to get it to work.

    I would like it when Player's are holding the feather they are able to fly.
    Here's my code.

    Pastebin : http://pastebin.com/tGF3wt2V
     
  2. Were do I put this? Thanks for replying :)
     
  3. Offline

    nverdier

    @PoseidonGraphics Those are called methods, and you use them after a dot operator, which goes after an instance of a class, a class (if the method is static), or a method returning something.
     
  4. What? I didn't understand any of that..

    @nverdier @bwfcwalshy

    <Edit by mrCookieSlime: Merged posts.>
     
  5. Offline

    nverdier

  6. @nverdier Thanks .-. Could've just showed me how to do it so I know how..
     
  7. Offline

    nverdier

    @PoseidonGraphics Well I don't spoonfeed, sorry. Trust me, you'll thank me in the long run.
     
  8. Still having trouble with this. Can anyone help?
     
  9. Offline

    ferrago

    @PoseidonGraphics

    They are saying that you need to ensure the item has item-meta as well as a display name, otherwise you will get some of those darn Null Pointer Errors that are oh so pesky.

    So you need to ensure the item you are checking has item-meta and a display name before you use .getItemMeta().getDisplayName(). I recommend changing line 4 from "if(player.getItemInHand()!=null)" to
    Code:
    if (player.getItemInHand() != null && player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().hasDisplayName())
    The way the && works in an if statement is as soon as one of those is violated it returns false. for example if (true && false && true) will never reach the second true because the false statement automatically ensures the if statement will be false.

    As to why this doesn't work. You have are only providing a snipet. Did you ensure that you are registering events to this class so that it will receive the messages when the player switches their item? Also did you ensure your plugin is loading correctly? Does /plugins show your plugin?
     
  10. @ferrago thanks for the help, I am reciving no errors in the console and the plugin in showing green aka working, But It's still not sender through anything. I have tried with check if the Item name is "Feather of the gods" and just doing what you have said below.

    I will paste the entire code.


    Code:
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Flying extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            ItemStack Flight = new ItemStack(Material.FEATHER);
            ItemMeta meta = Flight.getItemMeta();
            meta.setDisplayName(ChatColor.GOLD + "Feather of the gods");
            meta.setLore(Arrays.asList("The Golden Feather", "Forged by the gods themselves","Wield ALL the power!"));
            Flight.setItemMeta(meta);
            ShapedRecipe Flying = new ShapedRecipe(Flight);
           
            Flying.shape("III","IDI","III");
           
            Flying.setIngredient('I', Material.DIAMOND);
            Flying.setIngredient('D', Material.FEATHER);
           
            getServer().addRecipe(Flying);
        }
       
        @Override
        public void onDisable() {
            Bukkit.getServer().clearRecipes();
        }
        @EventHandler
        public void hand(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (player.getItemInHand() != null && player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().hasDisplayName()) {
                                    player.sendMessage(ChatColor.GOLD + "You feel the gods sending you power.");
                                    player.setAllowFlight(true);
                                    player.setFlying(true);
                                    player.getWorld().playSound(player.getLocation(), Sound.AMBIENCE_THUNDER, 1, 1);
                            } else {
                                    player.setAllowFlight(false);
                                    player.setFlying(false);
                            }
                    }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page