Solved EZ Question :P

Discussion in 'Plugin Development' started by mehboss, Feb 13, 2017.

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

    mehboss

    How do I get a player variable from another class.. I am trying to set the inventory title to the args[0]'s name.. How would I go about doing so.
     
  2. @mehboss
    Depends. If the other class is invoked directly from where you get the player (event, command), then I'd say just pass it as a parameter, but if it's at an unrelated time, save the player's UUID in a Collection and access said Collection from the other class.
     
  3. Offline

    mehboss

    How would I do this
     
  4. Offline

    Drkmaster83

    Almost an XY problem, exactly what are you doing, and what do you want to? Opening an inventory onCommand, modify current inventory name?
     
  5. Offline

    MaxFireIce

    Create a function in another class that accepts the parameters of your player and the args[0] and the inventory you are trying to edit and have the function return the new inventory with the custom name. @mehboss
     
  6. Offline

    mehboss

    @Drkmaster83
    Opening an inventory and making a head with the players name (args[0]) my oncommand and menu are in different classes so how would I get args[0] from class A and set it in class B?


    Sent from my iPhone using Tapatalk
     
  7. Offline

    ipodtouch0218

    @mehboss
    Store an instance of Class B inside of the main class, use "this" inside the new command executor instance, and create a getter method of Class B.
     
  8. Offline

    Tecno_Wizard

  9. Offline

    mehboss

  10. Offline

    ipodtouch0218

    @mehboss
    Sorry to put you down, but he is serious. Maybe you don't need to learn as much as other people on the forums, but not knowing how to pass a class' instance around is basic java.
     
    mehboss likes this.
  11. Offline

    mehboss

    I think I got it, thanks for help.
    Class:
    Code:
            private static final Main instance = new Main();
                head = headItem(instance.p.getName());
    Main:
    Code:
        public Player p;
     
  12. Offline

    Tecno_Wizard

    @mehboss If you're going to do it without learning the proper way, let me at least give you an example that's right. I'm not trying to be mean. I'm really not. I want you to learn, but using Bukkit as a way to learn Java is just a horrible idea and you should learn properly.

    (pre-spoonfeed shiver)

    Code:java
    1. // inside the main class
    2. private String playerName = "Default Value";
    3.  
    4.  
    5. //when instantiating the service provider class
    6.  
    7. [...] = new ServiceProvider(this);
    8.  
    9. /*
    10. * Setter method in main class
    11. */
    12. public void setInventoryName(String str) {
    13. this.playerName = str;
    14. }
    15.  
    16. // in ServiceProvider
    17. private final MainClass plugin;
    18.  
    19. public ServiceProvider(MainClass mainClass) {
    20. this.plugin = mainClass;
    21. }
    22.  
    23. public boolean onCommand([...]) {
    24. // check for name validity
    25. plugin.setInventoryName(args[0]);
    26. }
    27.  


    If anyone is wondering how I got the java formatting, the formatter is still there. [syntax=java][/syntax]
     
  13. Offline

    JanTuck

    Is main the class extending javaplugin? Because then this wont work.

    Sendt fra min ALE-L21 med Tapatalk
     
    ipodtouch0218 likes this.
  14. Offline

    mehboss

    Yes... it is.. :I

    Would this work?

    Main:
    Code:
    public class Main extends JavaPlugin {
    package me.mehboss.playermanager;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin {
    
        private Menu createItem;
    
        public String pl;
        public Player plr;
    
    //on enable
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           if (sender instanceof Player) {
              Player p = (Player) sender;
    
            plr = p;
            pl = p.getName();
    
      //rest of code
    
    Other Class:
    Code:
        Main plugin;
    
        public Menu(Main plugin) {
            this.plugin = plugin;
        }
    
      //Inventory stuff
            cm.setDisplayName(plugin.pl + "'s head");
     
    Last edited: Feb 15, 2017
  15. Offline

    timtower Administrator Administrator Moderator

    @mehboss That would work.
    But based on what I see: is this a warning plugin? If so: then please use a HashMap<UUID, UUID>
    First one will be the one who has the GUI open, second one who the target is.
     
  16. Offline

    mehboss

    Not a warnings plugin. Just a little view plugin. It will have players skull, if they are flying, inventory see, kick, ban, teleport item, basically a player manager plugin.

    Just a fun plugin

    Haven't been up to date on the plugin requests I have been saying I will do. I am A little slow. I have started them though.

    Sent from my iPhone using Tapatalk
     
    Last edited: Feb 15, 2017
Thread Status:
Not open for further replies.

Share This Page