NamespacedKey wont import

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

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

    JavaNoob

    I don't know if it has to import, but I was trying to mess around with custom recipes, just to make a knockback stick. When it came time to make the key, the NamespacedKey would not import. What am I doing wrong in this code? (Even though it doesn't show it here, the NamespacedKey is underlined red and has the error message "Cannot resolve to type"). What am I doing wrong?
    Code:
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.world.ChunkLoadEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        @Override
        public void onEnable() {
            System.out.println("PLUGIN ENABLED");
        }
        @Override
        public void onDisable() {
            System.out.println("PLUGIN DISABLED");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equals("fly")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    player.setAllowFlight(true);
                    player.setFlySpeed(1);
                }
            }
            if (cmd.getName().equals("flydeny")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    player.setFlying(false);
                    player.setAllowFlight(false);
                }
            }
            if (cmd.getName().equals("heal")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    player.setHealth(20.0);
                }
            }
           
            return false;
        }
        public ShapedRecipe getKnockbackRecipe() {
            ItemStack item = new ItemStack(Material.STICK);
           
            NamespacedKey key = new NamespacedKey(this, "STICK");
           
            ShapedRecipe recipe = new ShapedRecipe(key, item);
           
            recipe.shape("  S", " S ", "S  ");
            recipe.setIngredient('S', Material.STICK);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("Knockback Stick");
            meta.addEnchant(Enchantment.KNOCKBACK, 10, true);
            return null;
        }
     
    Last edited by a moderator: Sep 1, 2020
  2. Offline

    KarimAKL

  3. Offline

    JavaNoob

    Looks like it still doesn't work. The error when I try to do that it says "
    import org.bukkit.NamespacedKey; cannot be resolved"
    What other things could I try? Did I set something up wrong? Is some other code clashing with it?
    Edit: Oh, that would make sense. I am running version 1.8.9. So that must be it. Thank you!
     
    Last edited: Sep 2, 2020
  4. Offline

    KarimAKL

    @JavaNoob Which version are you building on? NamespacedKey was added in version 1.12 and ShapedRecipe don't require anything other than the ItemStack (result) in versions below that.
     
Thread Status:
Not open for further replies.

Share This Page