1.16.5 Public static Player?

Discussion in 'Spigot Plugin Development' started by DeQu, Jan 23, 2021.

  1. Code1:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    Player player = (Player) sender;

    Player target = Bukkit.getPlayer(args[0]);


    So i wont to acces this target in another class with Listeners.
    Is this possible?
     
    • Funny Funny x 3
  2. I don't really know what do you want to achieve, can you describe it better?
     
  3. I have this command. But i nedd this variable of target in another class! When i try to make it publicc static there are somer errors D:
     
  4. I said something related to static abuse in your other topic but it seems like I need to say it again, this time in greater detail. Avoid static abuse at all.

    If you do not know what does it exactly represent, shortly, using the static modifier on a field to make your life easier (which actually makes your life gradually worse) when accessing it is simply called static abuse. There are reasons to make a field static (maybe when you have a constant) but most of the time it is used in a wrong manner.

    What are those errors saying? If you don't know what they mean it would be helpful for you to try to understand them. Self-debugging is one of the core needs in coding.

    Also, we would like to know precisely what you want to achieve (give us some background about those two classes, explain the wanted result, etc.). Accessing a variable in another class is too general and we can't give advice. Maybe you need a HashMap, maybe a Set, maybe you need to pass it in another way, or simply you don't need to access it in another class, because we can tell you a better solution.

    It seems like these two questions (this one and your previous topic) are pretty basic. Bukkit is an API built in Java and if you don't understand Java in the first place, it will get pretty frustrating. If you want to really learn Java while developing Bukkit plugins, you should have at least some basic experience with OOP generally.

    As a small tip, make sure you check if your sender is a player before casting it (console can send commands too) and that the player name is actually valid.
     
    • Agree Agree x 4
    • Like Like x 1
  5. what are you trying to do? what’s the end result going to look like?
     
  6. use methods something(Object object)
     
  7. Ok. So i is simply a chat plugin. When i will write /adminpanel it will open me gui that will show rangs to chose for a person.

    The second class is a class that controls chat prefixes using files.yml:

    Code (Java):
    File f = new File("plugins/CCChat/Rangi.yml");
    YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);


    @EventHandler
    public void onChat(AsyncPlayerChatEvent event){
        Player p = event.getPlayer();
        if(yamlFile.get(p.getName()+".ranga")==null) yamlFile.set(p.getName()+".ranga", "gracz");

        event.setCancelled(true);

        try {
            yamlFile.save(f);
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (yamlFile.get(p.getName()+".ranga").equals("gracz")){
            Bukkit.broadcastMessage("§8[§7GRACZ§8]§7 " + event.getPlayer().getName() + ":§f " + event.getMessage());
        }

        if (yamlFile.get(p.getName()+".ranga").equals("TrialMod")){
            Bukkit.broadcastMessage("§8[§bTRIALMOD§8]§7 " + event.getPlayer().getName() + ":§f " + event.getMessage());
        }

    }
    Also in this class i wrote if exact RawSlot from Gui tha i open is clicked it should set the target rang to chosen one.

    Code:

    Code (Java):
    @EventHandler
        public void onClick(InventoryClickEvent event){

        Player p = (Player) event.getWhoClicked();
        if (event.getView().getTitle().equals("§c§lPANEL ADMINISTRATORSKI")) {
            event.setCancelled(true);
        if(event.getRawSlot()==0){
            yamlFile.set(p.getName()+".ranga", "Gracz");
            try {
                yamlFile.save(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            if(event.getRawSlot()==1){
                yamlFile.set(p.getName()+".ranga", "TrialMod");
                try {
                    yamlFile.save(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }

     
    I think i said everything. So the clue is that i need to get the player i wrote in the command and change his rang in another :D

    I know that where i hav p.getName() in the rang set i should have target but i dont know how to acces him.
     
    • Funny Funny x 1