Use method different class

Discussion in 'Plugin Development' started by HenkDeKipGaming, Jul 22, 2015.

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

    HenkDeKipGaming

    Hey,
    I got 2 really simple questions:

    - how to use a method in an other class?
    - how do you make a random that chooses from different locations (i couldn't find that, only that randomly chooses a location, but i want to choose 1 from a list of locations that i have in my code.)

    Thanks!

    Edit: please scroll down, I put my code there.
     
    Last edited: Jul 22, 2015
  2. Offline

    AcePilot10

    @HenkDeKipGaming if you don't know how to use methods in other classes than i'm assuming you don't have much knowledge of java in general. All i'm gonna say is watch < this tutorial to learn java. You need to know java before you try bukkit.

    To get a random location first you need some sort of array of location. Then use the Random class to get a random int and teleport the player to it.
    Code:
    List<Location> locations = new ArrayList<Location>();
    Random rdm = new Random();
    int i = rdm.nextInt(locations.size());
    player.teleport(locations.get(i));
    These tutorials*

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
  3. Offline

    Tecno_Wizard

    @AcePilot10, please don't double post.


    @HenkDeKipGaming, that's a matter of learning some more Java. ThenewBoston, what ace posted, hosts some of the best java tutorial videos ever. For Bukkit, try using pogostick, but do TheNewBoston first.
     
    AcePilot10 likes this.
  4. Offline

    HenkDeKipGaming

    lol, i did everything you just said xD
    pogostick is indeed very good, but he doesn't have a video about random choosing locations.

    @AcePilot10 you can edit your message huh xD
    I do know java from some sides, but that video does not solve my problem about using a method in an other class...
    i got a scoreboard method that i want to call from a different class.
    the random locations do not have to come from an arraylist or config.
    i just want something like this:
    Code:
    Location loc = new Location(Bukkit.getWorld("Spawn"), -679, 81, 244);
    Location loc1 = new Location(Bukkit.getWorld("Spawn"), -679, 82, 244);
    Location loc2 = new Location(Bukkit.getWorld("Spawn"), -679, 83, 244);
    
    and that it chooses one of them
     
  5. Offline

    Tecno_Wizard

    @HenkDeKipGaming, this is going to sound like gibberish...

    Create a new instance of the class with the method inside of it assuming that you don't need a singleton to keep the same values. If you already instantiated it, store the class instance in a variable and call dot notation on it using the method name.

    That's about a simple as I can say it while keeping it brief unfortunately.
     
  6. Offline

    HenkDeKipGaming

    i'm sorry, that's not clear enough for me haha xD
    i'm not english and not the best in java. so yeah haha
     
  7. Offline

    AcePilot10

    @HenkDeKipGaming nobody here is going to sit and teach you how to code. This is very basic java, please watch through TheNewBoston's whole playlist to learn java. Trust me, that's how I learned. And also

    *Edit* @Tecno_Wizard happy lol
     
  8. Offline

    Tecno_Wizard

  9. Offline

    HenkDeKipGaming

    thanks, random is now fixed!
    now only how to use a method from a different class
     
  10. Offline

    Tecno_Wizard

  11. Offline

    Proffesor_Diggi

    Try importing that specific class

    e.g:
    Code:
    import me.<yourpackage>.<yourclass>;
     
  12. Offline

    HenkDeKipGaming

  13. Offline

    AcePilot10

    @HenkDeKipGaming as tecno said before, to access methods from another class you're gonna need an instance of it. In bukkit usually people initialize it in the onEnable method.
    Code:
    public class MainClass extends JavaPlugin {
    
    //NOT REGISTERED YET
    public static otherClass instance;
    
    @Override
    public void onEnable() {
    //GETS CALLED WHEN PLUGIN ENABLES
    instance = new otherClass();
    //MAKES A NEW INSTANCE OF THE CLASS "otherClass"
    
    }
    
    public static otherClass getInstance() {
    return instance;
    //RETURNS OUR otherClass VARIABLE, THUS GIVING US ACCESS TO THE CLASS
     
  14. Offline

    HenkDeKipGaming

    does not work :(
    it gives an error on
    Code:
     instance = new CompassMenu();
     
  15. Offline

    AcePilot10

  16. Offline

    poepdrolify

    @HenkDeKipGaming
    (Dutch)
    Gebruik dit:
    Code:
    public static Plugin getPlugin() {
            return Bukkit.getServer().getPluginManager().getPlugin("[Plugin name]");
        }
    Stop het in je main class, en roep het op als je je plugin nodig hebt, voor bijv Configs

    (English)
    This code calls your plugin when you need it in another class
     
  17. Offline

    HenkDeKipGaming

    let's make myself clear:
    i got a scoreboard method in my main class.
    when a user clicks on a menu it should teleport the player and use the scoreboard method in it.
    If your code can solve this, can you then explain how to call it from an other class.
    btw, thank you for explaining in dutch, that makes it much simpler :)
     
  18. Offline

    AcePilot10

    @HenkDeKipGaming As I said in my last post, make a static instance to the main class, initialize it in the onEnable method. Bam now if you say your instance variable was Instance, and your class was called MainClass, just say MainClass.Instance.Method

    This is basic java, don't expect people here to teach you java
     
  19. Offline

    HenkDeKipGaming

    yeah, i know that's how it works. but i came here because that didn't work!
    i asked someone else this via skype and sent him all my code. i got some things that make this harder in my code.

    i'll just mark it as solved because i'll just get help from that person on skype

    Thanks for your reply's!

    UNSOLVED, SCROLL DOWN PLEASE :)
     
    Last edited: Jul 22, 2015
  20. Offline

    Tecno_Wizard

    @AcePilot10, I'm not really a advocate of singletons for the main class. Bette to just pass it around as needeed.
     
  21. Offline

    HenkDeKipGaming

    UNSOLVED:

    Hey, so it didn't work out...
    i just can't figure it out.
    so i removed everything that i did to use the method in the other class.
    can someone solve this? here is my code:

    OnEnable in main class:

    Code:
    private Menu menu;
        private CompassMenu menu2;
        private Shop menu3;
         
         public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new Signs(), this);
                 menu = new Menu(this);
                 menu2 = new CompassMenu(this);
                 menu3 = new Shop(this);
                 Bukkit.getServer().getPluginManager().registerEvents(this, this);
               getConfig().addDefault("special.DoublePoints", 1);
                getConfig().options().copyDefaults(true);
                saveConfig();
               
         }
    
    ScoreBoardUpdate method in main class:
    Code:
     public void ScoreBoardUpdate(Player p) {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
           
             Scoreboard scoreboard = manager.getNewScoreboard();
         
             Objective obj = scoreboard.registerNewObjective("Board", "dummy");
             obj.setDisplaySlot(DisplaySlot.SIDEBAR);
             obj.setDisplayName(ChatColor.BOLD + "§fChicken§0Fight");
             int pKills = getConfig().getInt("ChickenFight." + p.getName() + ".kills");
             int pDeaths = getConfig().getInt("ChickenFight." + p.getName() + ".deaths");
             int pPoints = getConfig().getInt("ChickenFight." + p.getName() + ".points");
             Score kills = obj.getScore(ChatColor.GREEN + "Kills:");
             kills.setScore(pKills);
             Score deaths = obj.getScore(ChatColor.RED + "Deaths: ");
             deaths.setScore(pDeaths);
             Score points = obj.getScore(ChatColor.GOLD + "Points: ");
             points.setScore(pPoints);
             p.setScoreboard(scoreboard);
             saveConfig();
             }
    
    CompassMenu class:
    Code:
    public class CompassMenu implements Listener {
        private Inventory inv;
        private ItemStack c, s, a;
       Object bow;
       Main plugin;
       
       public CompassMenu (Main plugin){
               this.plugin = plugin;
       }
       
        public CompassMenu(Plugin pl, Player p) {
                inv = Bukkit.getServer().createInventory(null, 9, "Warping");
               
                c = createItem(DyeColor.GREEN, ChatColor.GREEN + "KitPvp");
                s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "One in the quiver");
                a = createItem(DyeColor.RED, ChatColor.RED + "Spawn");
               
                inv.setItem(2, c);
                inv.setItem(4, s);
                inv.setItem(6, a);
               
                Bukkit.getServer().getPluginManager().registerEvents(this, pl);
        }
       
        private ItemStack createItem(DyeColor dc, String name) {
                ItemStack i = new Wool(dc).toItemStack(1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("Warp to: " + name.toLowerCase()));
                i.setItemMeta(im);
                return i;
        }
       
        public void show(Player p) {
                p.openInventory(inv);
        }
       
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("KitPvp")) {
                        e.setCancelled(true);
                        Player p = (Player) e.getWhoClicked();
                        e.getWhoClicked().getInventory().clear();  
                        e.getWhoClicked().getInventory().addItem(new ItemStack(Material.WATCH));
                        e.getWhoClicked().getInventory().addItem(new ItemStack(Material.NETHER_STAR));
                        e.getWhoClicked().getInventory().addItem(new ItemStack(Material.IRON_INGOT));
                        Location loc = new Location(Bukkit.getWorld("world"), 281, 157, 28);
                         e.getWhoClicked().teleport(loc);
                         ScoreBoardUpdate(p);
                       
                        e.getWhoClicked().closeInventory();
                }
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("One in the quiver")) {
                       e.setCancelled(true);
                       e.getWhoClicked().getInventory().clear();  
                        e.getWhoClicked().getInventory().addItem(new ItemStack(Material.WATCH));
                       Location loc = new Location(Bukkit.getWorld("OITQ"), -99, 204, -73);
                         e.getWhoClicked().teleport(loc);
                       
                        e.getWhoClicked().closeInventory();
                }
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Spawn")) {
                   e.setCancelled(true);
                   e.getWhoClicked().getInventory().clear();  
                   e.getWhoClicked().getInventory().addItem(new ItemStack(Material.WATCH));
                   e.getWhoClicked().getInventory().addItem(new ItemStack(Material.IRON_INGOT));
                   Location loc = new Location(Bukkit.getWorld("Spawn"), -679, 81, 244);
                     e.getWhoClicked().teleport(loc);                
                                 
                    e.getWhoClicked().closeInventory();
            }
               
        }
       
    }
    
    
    maybe someone can help now i showed all the code?

    thanks!
     
  22. Offline

    AcePilot10

  23. Offline

    teej107

    Tecno_Wizard likes this.
  24. Offline

    Tecno_Wizard

    @teej107, what's it with static recently? BcBroz?
    I keep seeing more and more people making singletons out of javaplugin and it is horrifying me.
     
  25. Offline

    teej107

    Sorry developers don't quite get the concept of OOP/don't know how to pass-by-reference. Then there are those who just have no idea what static is and still take advantage of what is allows (like BCBroz) :p
     
  26. Offline

    HenkDeKipGaming

    how is it possible that he has more then 10k subscribers?!? haha

    i am not english, so maybe i am reading past it, but i can't seem to find how to use a method in another class...

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Jul 23, 2015
Thread Status:
Not open for further replies.

Share This Page