Solved Skills using abstract classes

Discussion in 'Spigot Plugin Development' started by Kepling, Jan 12, 2020.

  1. Hey I'm copying this vid
    And I'm getting an error on this line
    Code (Text):
    skills.put("Example", new ExampleSkill());
    Says "Cannot instantiate the type Fireballs"

    Full SkillHandler Class
    Code (Text):

    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.metadata.MetadataValue;

    import net.md_5.bungee.api.ChatColor;

    public class SkillHandler implements Listener {

        private Map<String, Skill> skills = new HashMap<>();
       
        private Map<UUID, Long> cooldown = new HashMap<>();
       
        public SkillHandler() {
            skills.put("Example", new ExampleSkill());
        }
       
        @EventHandler
        public void onExecute(PlayerInteractEvent event) {
            Player player = event.getPlayer();
           
            if (!event.getAction().name().contains("RIGHT")) return;
            if (!player.hasMetadata("skill")) return;
           
            MetadataValue metadataValue = player.getMetadata("skill").get(0);
            Skill skill = getSkillFromName(metadataValue.asString());
           
            if (skill == null) return;
           
            if (!skill.getItem().isSimilar(event.getPlayer().getEquipment().getItemInMainHand())) return;
           
            if (cooldown.containsKey(player.getUniqueId()) && System.currentTimeMillis() < cooldown.get(player.getUniqueId())) {
                player.sendMessage(ChatColor.RED + "You are still on cooldown!");
            } else {
                skills.execute(event);
                player.sendMessage(ChatColor.YELLOW + "You have used the " + skill.getName() + " skill");
                cooldown.put(player.getUniqueId(), System.currentTimeMillis() + (1000 * skill.getCooldown()));
            }
        }
       
        public Skill getSkillFromName(String name) {
            return skills.get(name);
        }

        public Map<String, Skill> getSkills() {
            return skills;
        }
    }
    No idea what I would have to do to fix it
    Help would be much appreciated :)
     
  2. Can you also show us the ExampleSkill class and the Fireballs class?
     
  3. Okay, sorry. I just named it fireballs instad of example

    Code (Text):
    package me.kepling.bosses.abilities;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.metadata.MetadataValue;

    import net.md_5.bungee.api.ChatColor;

    public class SkillHandler implements Listener {

        private Map<String, Skill> skills = new HashMap<>();
     
        private Map<UUID, Long> cooldown = new HashMap<>();
     
        public SkillHandler() {
            skills.put("Fireballs", new Fireballs());
        }
     
        @EventHandler
        public void onExecute(PlayerInteractEvent event) {
            Player player = event.getPlayer();
         
            if (!event.getAction().name().contains("RIGHT")) return;
            if (!player.hasMetadata("skill")) return;
         
            MetadataValue metadataValue = player.getMetadata("skill").get(0);
            Skill skill = getSkillFromName(metadataValue.asString());
         
            if (skill == null) return;
         
            if (!skill.getItem().isSimilar(event.getPlayer().getEquipment().getItemInMainHand())) return;
         
            if (cooldown.containsKey(player.getUniqueId()) && System.currentTimeMillis() < cooldown.get(player.getUniqueId())) {
                player.sendMessage(ChatColor.RED + "You are still on cooldown!");
            } else {
                ((Fireballs) skills).execute(event);
                player.sendMessage(ChatColor.YELLOW + "You have used the " + skill.getName() + " skill");
                cooldown.put(player.getUniqueId(), System.currentTimeMillis() + (1000 * skill.getCooldown()));
            }
        }
     
        public Skill getSkillFromName(String name) {
            return skills.get(name);
        }

        public Map<String, Skill> getSkills() {
            return skills;
        }
    }
     
    Code (Text):
    package me.kepling.bosses.abilities;

    import org.bukkit.Material;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    public abstract class Fireballs extends Skill {

        int fireballsAbilityLoop;
     
        public Fireballs() {
         
            setName("Example");
            setCooldown(10);
            setItem(new ItemStack(Material.DIAMOND_SWORD));
        }

        @Override
        public void execute(PlayerInteractEvent event) {
         
            event.getPlayer().sendMessage("you have used Fireballs");
         
        }
     
     
    }
     
     
  4. Your Fireballs class is abstract, remove the abstract keyword from the public abstract class Fireballs extends Skill class declaration and implement all missing methods.
     
    • Winner Winner x 1