Making player variables public for my plugin

Discussion in 'Spigot Plugin Development' started by FreakingChickenB, Jun 1, 2017.

  1. So I need help with this code. I am trying to code a plugin that hooks on to the advanced bans api. This plugin will open a gui when I do /tempban <Player's Name>. The problem is I need this to read target and uuid from the api. I need it to get the uuid and target name in order to give out the ban. So far the command does nothing but I am working on it. I am new to java. My code

    Code (Text):
    package gsc.bans.command1;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.leoko.advancedban.manager.UUIDManager;

    public class Menu implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command command, String label, String[] name) {
           
           
        if (command.getName().equalsIgnoreCase("tempban")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("tempban commands can't be ran from the server");
                return false;

            }

            Player player = (Player) sender;
            Player target = Bukkit.getPlayer(name[0]);
           
            String pid = UUIDManager.get().getInitialUUID(name[0]);

           
           
            if (!(player.hasPermission("gsc.bans"))) {
                player.sendMessage(ChatColor.RED + "Error: You can not use this command");
                return false;
            }

            Inventory inv = Bukkit.createInventory(null, 9, "Ban " + target);
            ItemStack xray = nameItem(Material.DIAMOND, ChatColor.AQUA + "Xray");
            inv.setItem(0, xray);

            player.openInventory(inv);
            return false;

        }
       
        return false;
       
    }
       

        private ItemStack nameItem(ItemStack item, String itemname) {
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(itemname);

            item.setItemMeta(meta);
            return item;
        }

        private ItemStack nameItem(Material item, String itemname) {
            return nameItem(new ItemStack(item), itemname);
        }

    }
     
    The other listener

    Code (Text):
    package gsc.bans.command1;

    import org.bukkit.Material;

    import java.io.FileReader;

    import gsc.bans.command1.Menu;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import me.leoko.advancedban.manager.TimeManager;
    import me.leoko.advancedban.utils.Punishment;
    import me.leoko.advancedban.utils.PunishmentType;

    public class InventoryClick implements Listener {
       
       
       
       
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Inventory inv = event.getInventory();
            if(inv.getTitle().equals("Ban " + target))
                return;
               
            if (!(event.getWhoClicked() instanceof Player))
                return;
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
           
            if(item.getType() == Material.DIAMOND) {
               
               
            player.sendMessage("This is a test for now");
               
            event.setCancelled(true);
            player.closeInventory();
           
               
               
            }
           
           
           
           
           
           
           
        }

        private void get() {
            // TODO Auto-generated method stub
           
        }
    }
     
     
  2. Have you tried it at all yet?
     
  3. Yes I have tested it. Yes I have tried searching for fixes. I am new to this.

    One more thing I will change this is just a test to all the info for the ban
     
    #3 FreakingChickenB, Jun 1, 2017
    Last edited by a moderator: Jun 1, 2017
  4. Alright, well we should start by addressing multiple issues with your code. For starters, your onCommand method's args[] variable should not be called name, because that's not what it is, it's an argument (meaning it could be a name, but isn't necessarily one). Also, doing
    Inventory inv = Bukkit.createInventory(null, 9, "Ban " + target); doesn't work. You can't have a Player (target) in your inventory name. Make sure you change it to Inventory inv = Bukkit.createInventory(null, 9, "Ban " + target.getName());. That's a String now.

    If you're trying to save the player that you're banning and the player who's banning them, use a HashMap to store two Players.

    Edit: let me know if it works at all!
     
    #4 _diam, Jun 1, 2017
    Last edited: Jun 1, 2017
    • Agree Agree x 2
  5. I can't do it right now. I will get back tomorrow
     
    • Like Like x 1
  6. Okay, feel free to let me know if it works or if you need more help with it tomorrow! :)
     
  7. tfw u actually attempt to help op but post gets removed.

    make a hashmap with both UUIDs
     
  8. Post got removed? What for?
     
  9. Technically, it'll call #toString on CraftPlayer, ending up in such:
    Code (Text):
    CraftPlayer{name="_diam"}
    If you're new, you should first learn how to use java. Pick up how to use Override too, and pretty much everything which is in the official Oracle tutorial:
    https://docs.oracle.com/javase/tutorial/
     
    • Agree Agree x 1
  10. "spam" bc spongemock
     
    • Funny Funny x 1
  11. So I did what you said to do with the args and getName. The command still does nothing
    [​IMG]
     
  12. Should probably also point out that the command.getName() is pretty much irrelevant when not having it in your main class.
     
    • Agree Agree x 2
  13. So should I transfer everything to my main class?
     
  14. No, you need to register your command in your onEnable().

    Code (Text):
    Bukkit.getCommand("Command").setExecutor(new YourClassName());

    EDIT:
    What all these people say here is mostly unnecessary with Hashmaps and such, it's just inefficient.

    Create a simple data.yml with Name: UUID

    For the name in your GUI, use Inventory.getTitle().split(" ")[1]

    You can check the name if the player is online,


    If he is offline, check if his name is in the database and get the UUID, if not, write a method that will get read the Mojang API and parse it with JSON.
     
    #14 DizMizzer, Jun 1, 2017
    Last edited: Jun 1, 2017
    • Agree Agree x 1
  15. I did
    Code (Text):
    Bukkit.getCommand("Command").setExecutor(new YourClassName());
    and changed YourClassName to my menu class. Do I need to change command as well?. I get this error
    Code (Text):
    Error occurred while enabling GlowStoneCityBans v0.1 (Is it up to date?)
    java.lang.NullPointerException
            at gsc.bans.glowstonecitybans.Main.onEnable(Main.java:11) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:362) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:322) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.t(MinecraftServer.java:416) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.l(MinecraftServer.java:381) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.a(MinecraftServer.java:336) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.DedicatedServer.init(DedicatedServer.java:270) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:539) [spigot-1.10-R0.1-SNAPSHOT-latest.jar:git-Spigot-6016ac7-10c10b3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    EDIT: I also changed getCommand to getPluginCommand because it told me to do that
     
  16. Change Bukkit.getPluginCommand to this.getCommand

    Also what is on Line 11
     
  17. Actually, this is just more inefficient.

    They can have the file for persistence, of course. But constantly reading from the file will be a waste of CPU power. However, when keeping it in a map, you can just grab it straight from memory.

    It will just take ages to load the GUI if you have to read from every data file belonging to players tempbanned.

    Instead, read the files on startup, and deserialize them and put them in the map. On stop and/or intervals, serialize the map and put them in he file.

    EDIT: I was at fault. It does not read from the file constantly.
     
  18. You aren't reading constantly from the file, you are saving it as a FileConfiguration
     
  19. Ahh, yeah, that makes sense.

    I've just worked so much with JSON and deserialization and serialization of it. I completely forgot that the FileConfiguration is an object representing the values in the file, without having to constantly read.
     
  20. My entire main
    Code (Text):
    package gsc.bans.glowstonecitybans;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    import gsc.bans.command1.Menu;

    public class Main extends JavaPlugin {

        public void onEnable() {
            this.getCommand("Command").setExecutor(new Menu());
            System.out.println("GlowStoneCity Bans Enabled");
        }

        public void onDisable() {
            System.out.println("GlowStoneCity Bans Enabled");
        }
    }
     
    EDIT: Changing "Command" to "tempban" fixed the command not doing anything but it now makes tons of errors.
    EDIT 2: Line 11 has
    Code (Text):
    this.getCommand("tempban").setExecutor(new Menu());
     
    #20 FreakingChickenB, Jun 1, 2017
    Last edited: Jun 2, 2017