Enumerators and Classes

Discussion in 'Plugin Development' started by micrlink, Apr 21, 2014.

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

    micrlink

    If I have a enum class and I want to access a method from a class by setting the enum to have the variable of the class, how can I access the method

    example class:
    Code:
    public class example extends Something {
     
      public String name() {
          return "example";
      }
     
    }
    eample enum class:
    Code:
    public enum Examples {
        Example(example.class);
     
        String name;
     
        private Examples(Class<?> c) {
            this.name = c.name();
        }
     
    }
            
     
  2. Offline

    coasterman10

    That's not quite how classes work. You need to think from the perspective of objects, not classes.

    Is there an existing member of that class that you want to access (have you created an object of it yet)?
     
  3. Offline

    micrlink

    Can you example that differently?
     
  4. Offline

    micrlink

  5. Offline

    1Rogue

    Perhaps something like this:

    Code:java
    1. public enum SomeEmum {
    2.  
    3. ONE(new Example()),
    4. TWO(new OtherExample());
    5.  
    6. private final Something clazz;
    7.  
    8. private <T extends Something> SomeEnum(T clazz) {
    9. this.clazz = clazz;
    10. }
    11.  
    12. public Something getClass() {
    13. return this.clazz;
    14. }
    15.  
    16. }


    Where OtherExample and Example both extend "Something"?

    Haven't had time to test it, but seems pretty much what you want.
     
  6. Offline

    ButterSquidz

    This is a Bukkit forum, not a Java forum. Developers will obviously help you with almost every problem you have, but if you are completely new to the world of coding I highly recommend you learn Java before jumping into plugins
     
  7. Offline

    micrlink

    I am trying to be able to access a method from the class
     
  8. Offline

    1Rogue



    SomeEnum.ONE.getClass().<your method here>
     
  9. Offline

    micrlink

    Okay I got something new. I am going to copy my exact code and error.

    Kit.java
    Code:
    package com.micrlink.kitpvp.kits;
     
    import java.util.List;
    import java.util.logging.Logger;
     
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
     
    public enum Kit {
        Archer(kit_archer.kit()),
        Chef(kit_chef.kit()),
        Enderman(kit_enderman.kit()),
        Fighter(kit_fighter.kit()),
        Fisherman(kit_fisherman.kit()),
        Flowerpicker(kit_flowerpicker.kit()),
        Goblin(kit_goblin.kit()),
        Lumberjack(kit_lumberjack.kit()),
        Mage(kit_mage.kit()),
        Skullcollector(kit_skullcollector.kit()),
        Tnt(kit_tnt.kit()),
        Wolftamer(kit_wolftamer.kit());
     
        static Kit kit;
        PlayerKit pkit;
     
        static Logger log = Logger.getLogger("Minecraft");
     
        String name;
        ItemStack helmet;
        ItemStack chestplate;
        ItemStack leggings;
        ItemStack boots;
        List<ItemStack> inventory;
        List<PotionEffect> potions;
     
        private Kit(PlayerKit kit) {
            this.pkit = kit;
        }
     
        public static PlayerKit fromString(String name){
            for(Kit k : Kit.values()){
                if(k.toString().equalsIgnoreCase(name)){
                    log.info("RETURNING");
                    return k.pkit;
                }
            }
            return null;
        }
     
        public ItemStack getChestplate(){
            return pkit.chestplate();
        }
    }
    
    SignClick.java
    Code:
    package com.micrlink.kitpvp.events;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.block.Sign;
    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 com.micrlink.kitpvp.KPlayer;
    import com.micrlink.kitpvp.pvp;
    import com.micrlink.kitpvp.kits.Kit;
    import com.micrlink.kitpvp.kits.PlayerKit;
     
    public class SignClick implements Listener{
     
        pvp plugin;
     
        public SignClick(pvp plugin){
            this.plugin = plugin;
        }
     
        @EventHandler
        public void onSignClick(PlayerInteractEvent e){
            KPlayer p = new KPlayer(e.getPlayer());
            if(e.getClickedBlock() != null){
                if(e.getClickedBlock().getState() instanceof Sign){
                    if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        Sign sign = (Sign) e.getClickedBlock().getState();
                        if(sign.getLine(0).equalsIgnoreCase(ChatColor.DARK_BLUE + "[Kit]")){
                            p.setGameMode(GameMode.SURVIVAL);
                            p.setHealth(20.0);
                            p.setFoodLevel(20);
                            if(sign.getLine(2).contains("$")){
                                String s = sign.getLine(2);
                                s = s.replaceAll("$", "");
                                int i = Integer.parseInt(s);
                                plugin.economy.withdrawPlayer(p.getName(), i);
                                p.sendMessage(ChatColor.GRAY + "-$"+i+".00");
                            }else{
                                p.sendMessage("SignClick");
                                if(Kit.fromString(sign.getLine(1)) != null){
                                    p.sendMessage("SignClick");
                                    PlayerKit kit = Kit.fromString(sign.getLine(1));
                                    p.setKit(kit);
                                }
                            }
                        }
                    }
                }
            }
        }
     
        public void NoPermissions(Player p){
            p.sendMessage(ChatColor.DARK_RED + "You don't have permission.");
        }
     
    }
    
    PlayerKit.java
    Code:
    package com.micrlink.kitpvp.kits;
     
    import java.util.List;
     
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
     
    public abstract class PlayerKit {
     
        public abstract ItemStack boots();
     
        public abstract ItemStack chestplate();
     
        public abstract ItemStack helmet();
     
        public abstract List<ItemStack> inventory();
     
        public abstract ItemStack leggings();
     
        public abstract String name();
     
        public abstract List<PotionEffect> potions();
    }
    
    KPlayer.java
    Code:
    package com.micrlink.kitpvp;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
     
    import com.micrlink.kitpvp.kits.PlayerKit;
     
    public class KPlayer {
     
        Logger log = Logger.getLogger("Minecraft");
        Player player;
     
        public KPlayer (Player p){
            this.player = p;
        }
     
        public Player getPlayer(){
            return player;
        }
     
        public String getName(){
            return player.getName();
        }
     
        public boolean isOp(){
            return player.isOp();
        }
     
        public File getUserFile(){
            return new File("./plugins/KitPVP/players/" + player.getName() + ".yml");
        }
     
        public FileConfiguration getUserConfig(){
            File file = new File("./plugins/KitPVP/players/" + player.getName() + ".yml");
            return YamlConfiguration.loadConfiguration(file);
        }
     
        public void generateConfig(){
            File file = getUserFile();
            if(file.exists()) {
                return;
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        public int getLevel(){
            return getLevelFromExp(getExp());
        }
     
        public int getExp(){
            return getUserConfig().getInt("Exp");
        }
     
        public int getLevelFromExp(int exp) {
            int base = 30;
            int lvl = 0;
            int i = 0;
            if (exp > base) {
                while (i == 0) {
                    base = base + 20;
                    lvl = lvl + 1;
                    if (exp < (base + 20) || exp == base) {
                        return lvl;
                    }
                }
            }
            return 0;
        }
     
        public String getPrefix(){
            String prefix = "";
            File file = new File("./plugins/KitPVP/config.yml");
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            for(String s : config.getStringList("Prefixes")){
                String[] split = s.split(":");
                String permission = split[0];
                if(player.hasPermission(permission)){
                    prefix = " " + ChatColor.translateAlternateColorCodes('&', prefix);
                }
            }
            return prefix;
        }
     
        public void setGameMode(GameMode gamemode){
            player.setGameMode(gamemode);
        }
     
        public void setFoodLevel(int args0){
            player.setFoodLevel(args0);
        }
     
        public void sendMessage(String message){
            player.sendMessage(message);
        }
     
        public void setHealth(double args0){
            player.setHealth(args0);
        }
     
        public void setKit(PlayerKit kit){
            log.info("Setting kit");
            ItemStack chestplate = kit.chestplate();
            player.getInventory().setChestplate(chestplate);
        }
    }
    
    Error
    Code:
    Caused by: java.lang.NullPointerException
            at com.micrlink.kitpvp.KPlayer.setKit(KPlayer.java:114) ~[?:?]
            at com.micrlink.kitpvp.events.SignClick.onSignClick(SignClick.java:45) ~
    [?:?]
    >
    And what I get in chat is "SignClick" once.
     
Thread Status:
Not open for further replies.

Share This Page