Anvils

Discussion in 'Plugin Development' started by plisov, Jan 13, 2017.

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

    plisov

    Hey, so I'm trying to open an anvil and have a player be able to input text and have that text be stored in a config. I have the anvil opening and I am able to rename the item. However when I go to press the item. It cancels it like it should but instead of saving what the player typed, it saves the display name of the previous item I clicked on to get into the anvil gui. Here are some images to better explain the issue.
    [​IMG]
    [​IMG]
    I had the display name print out at the bottom in chat
    [​IMG]
    Made rename now say renamed
    [​IMG]
    When I go to click on it, it cancels and pops up like nothing happened
    [​IMG]
    And in the config,
    [​IMG]

    My anvil opening class
    Code:
    package me.plisov.titles.inventories.anvil;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.plisov.titles.Enable;
    
    public class Anvil1 implements Listener {
        private static ItemStack np, c1, c2, c3;
    
        Enable plugin;
        public static void createInv(Player player) throws IOException {
    
            Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL);
    
            c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
            //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
            //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");
    
            np = noPermission(ChatColor.RED + "No permission");
    
            if(!player.hasPermission("colors.chat.prefix")) {
                inv.setItem(0, np);
            } else {
                inv.setItem(0, c1);
            }
    
            player.openInventory(inv);
        }
    
        private static ItemStack c1(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        private static ItemStack noPermission(String name) {
            ItemStack i = new ItemStack(Material.BEDROCK, 1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        String message = ChatColor.GOLD + "Your Title has been set to ";
    
        public static final int SLOT_INPUT_LEFT = 0;
        public static final int SLOT_INPUT_RIGHT = 1;
        public static final int SLOT_OUTPUT = 2;
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) throws IOException {
            Player player = (Player) e.getWhoClicked();
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
            config = YamlConfiguration.loadConfiguration(file);
          
            ItemStack item = e.getCurrentItem();
            ItemMeta meta = item.getItemMeta();
          
            if(e.equals(null)) {
                e.setCancelled(true);
                player.sendMessage("Event equals Null");
            }
      
            if(e.getSlot() == SLOT_OUTPUT) {
                config.set("Player.Custom.Prefix.1", meta.getDisplayName());
                config.save(file);
                player.sendMessage("Working");
                player.sendMessage(meta.getDisplayName());
            } else {
                player.sendMessage("Not working");
            }
          
        }
    }
    Part of other class that opens the anvil

    Code:
                        } else if(meta.getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 1")) {
                            e.setCancelled(true);
                            Anvil1.createInv(player);
    Any help is much appreciated.
    Thanks,
    plisov
     
  2. Offline

    Zombie_Striker

    Don't load the config each and every time you click any inventory. Check if the inventory is an anvil first, and if the anvil inventory is equal to your custom inventory, and that the player actaully clicked one of the buttons before loading the config.

    The event will never be null. If it ever was, a ton of things would break (including bukkit, where the whole server would crash). There is not need for this null check.

    Not only that, but you are doing it wrong. Use == for nulls, since .equals references the null object, meaning you are dereferencing a null pointer.

    Null check before you get the item. If the player clicks anywhere that is not a slot, or if the slot is empty, this will return a null value.

    Main thing: What does this print out? Is it equal to "Renamed"?
     
  3. Offline

    plisov

    player.sendMessage(meta.getDisplayName()); prints out Custom Prefix 1, which isn't what I want. I need it to print out whatever the player inputted in the input section of the anvil.
     
  4. Offline

    Zombie_Striker

    @plisov
    How is that possible? If the first item is "Renamed", and the result is "Renamed", then how does it get Custom Prefix 1?

    [Edit] You're not checking if the inventory is the anvil. What is happening is this gets applied to all inventories on the entire server. Even if you open your inventory and click the third item in the hotbar, this will be triggered. To fix this, check if the inventory is an anvil and if the title of the anvil is equal to some custom name (choose the custom name you can the inventory)
     
  5. Offline

    plisov

    I thought that was the case. I was trying to figure out how to check if the inventory type is an anvil however I couldn't find how to do it.
     
  6. Offline

    Zombie_Striker

  7. Offline

    plisov

    When I do if(Inventory.getType() == InventoryType.ANVIL) {

    } It says that Inventory cannot be resolved to a variable
     
  8. Offline

    Zombie_Striker

    @plisov
    That is because you have to replace "Inventory" with your inventory instance. The # signifies that the Inventory is just the class-type, and needs to be replaced with an instance.
     
  9. Offline

    plisov

    So you mean I need to replace Inventory with inv since I'm doing
    Code:
            Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL);
    
    If so, it doesn't seem to work since I'm initiating it in a method and trying to call it in another method.
     
  10. Offline

    Zombie_Striker

    @plisov
    Then you will need to create a new HashMap field. The keys will be the player's UUID, and the value will be the Inventory instance. Do this so we know which inventory the player has open.

    Then, inside your other method, check if the inventory instance from the event is equal to the inventory from the hashmap. If so, you know that inv is that custom inventory.
     
  11. Offline

    plisov

    This is what I have currently
    Code (open)

    package me.plisov.titles.inventories.anvil;

    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.plisov.titles.Enable;

    public class Anvil1 implements Listener {
    private static ItemStack np, c1, c2, c3;

    public static HashMap<UUID, Inventory> map = new HashMap<UUID, Inventory>();

    Enable plugin;
    public static void createInv(Player player) throws IOException {

    Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL);

    map.put(player.getUniqueId(), inv);

    c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
    //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
    //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");

    np = noPermission(ChatColor.RED + "No permission");

    if(!player.hasPermission("colors.chat.prefix")) {
    inv.setItem(0, np);
    } else {
    inv.setItem(0, c1);
    }

    player.openInventory(inv);
    }

    private static ItemStack c1(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack noPermission(String name) {
    ItemStack i = new ItemStack(Material.BEDROCK, 1);
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    String message = ChatColor.GOLD + "Your Title has been set to ";

    public static final int SLOT_INPUT_LEFT = 0;
    public static final int SLOT_INPUT_RIGHT = 1;
    public static final int SLOT_OUTPUT = 2;

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();
    FileConfiguration config = null;
    File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
    config = YamlConfiguration.loadConfiguration(file);

    ItemStack item = e.getCurrentItem();
    ItemMeta meta = item.getItemMeta();

    if(e.getSlot() == SLOT_OUTPUT) {
    config.set("Player.Custom.Prefix.1", meta.getDisplayName());
    config.save(file);
    player.sendMessage("Working");
    player.sendMessage(meta.getDisplayName());
    } else {
    player.sendMessage("Not working");
    }

    }
    }


    However I don't know how to get the inventory instance from the event
     
  12. Offline

    Zombie_Striker

    @plisov
    Code:
    map.get(e.getWhoClicked().getUniqueID())
     
  13. Offline

    plisov

    Code:
            if(e.getWhoClicked().getUniqueId() == map.get(e.getWhoClicked().getUniqueId()))
    
    Is red. It's saying Incompatible operand types UUID and Inventory. How do I check if the inventory instance from the event is equal to the inventory from the hashmap? I'm certain that's not how I should be doing it.
     
  14. Offline

    Zombie_Striker

    @plisov
    Replace the UUID (The "e.getWhoClicked().getUniqueId()" ) with the inventory instance (inv)
     
  15. Offline

    plisov

    It doesn't know what inv is because it's in another method. I'm not initializing it in the event

    Could I do this instead without having to do the hashmap thing?
    Code:
                if(e.getInventory().getType() == InventoryType.ANVIL)
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 14, 2017
  16. Offline

    Zombie_Striker

    @plisov
    My mistake, I meant

    if(e.getInventory ==map.get(e.getWhoClicked().getUniqueID()))
     
  17. Offline

    plisov

    Ah. Ok so I've done as you said but it's still not working. What it's doing is it's saving the config as I'm telling it to do however nothing is being saved.
    Code (open)

    package me.plisov.titles.inventories.anvil;

    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.plisov.titles.Enable;

    public class Anvil1 implements Listener {
    private static ItemStack np, c1, c2, c3;

    public static HashMap<UUID, Inventory> map = new HashMap<UUID, Inventory>();

    Enable plugin;
    public static void createInv(Player player) throws IOException {

    Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL);

    map.put(player.getUniqueId(), inv);

    c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
    //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
    //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");

    np = noPermission(ChatColor.RED + "No permission");

    if(!player.hasPermission("colors.chat.prefix")) {
    inv.setItem(0, np);
    } else {
    inv.setItem(0, c1);
    }

    player.openInventory(inv);
    }

    private static ItemStack c1(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack noPermission(String name) {
    ItemStack i = new ItemStack(Material.BEDROCK, 1);
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    String message = ChatColor.GOLD + "Your Title has been set to ";

    public static final int SLOT_INPUT_LEFT = 0;
    public static final int SLOT_INPUT_RIGHT = 1;
    public static final int SLOT_OUTPUT = 2;

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();
    FileConfiguration config = null;
    File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
    config = YamlConfiguration.loadConfiguration(file);

    ItemStack item = e.getCurrentItem();
    ItemMeta meta = item.getItemMeta();

    if(e.getInventory() == map.get(e.getWhoClicked().getUniqueId())) {

    if(e.getSlot() == SLOT_OUTPUT) {
    config.set("Player.Custom.Prefix.1", meta.getDisplayName());
    config.save(file);
    player.sendMessage("Working");
    player.sendMessage(meta.getDisplayName());
    } else {
    player.sendMessage("Not working");
    }
    }
    }
    }


    EDIT: Nevermind. It's not even saving. It only saves when I open it through the other gui as seen in the first image.
     
    Last edited: Jan 14, 2017
  18. Offline

    Zombie_Striker

    @plisov
    Isn't that how you should be getting to that inventory? What other way are you getting to that inventory?
     
  19. Offline

    JanTuck

    @plisov
    You are checking if the inventory is equal to the other inventory, but if he changes something in it, it isnt equal is it?


    Reformatted:
    Code:
    package me.plisov.titles.inventories.anvil;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.plisov.titles.Enable;
    
    public class Anvil1 implements Listener {
        private static ItemStack np, c1, c2, c3;
    
        public static HashMap<UUID, Inventory> map = new HashMap<UUID, Inventory>();
    
        Enable plugin;
        public static void createInv(Player player) throws IOException {
    
            Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL);
    
            map.put(player.getUniqueId(), inv);
    
            c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
    //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
    //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");
    
            np = noPermission(ChatColor.RED + "No permission");
    
            if(!player.hasPermission("colors.chat.prefix")) {
                inv.setItem(0, np);
            } else {
                inv.setItem(0, c1);
            }
    
            player.openInventory(inv);
        }
    
        private static ItemStack c1(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        private static ItemStack noPermission(String name) {
            ItemStack i = new ItemStack(Material.BEDROCK, 1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        String message = ChatColor.GOLD + "Your Title has been set to ";
    
        public static final int SLOT_INPUT_LEFT = 0;
        public static final int SLOT_INPUT_RIGHT = 1;
        public static final int SLOT_OUTPUT = 2;
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) throws IOException {
            Player player = (Player) e.getWhoClicked();
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
            config = YamlConfiguration.loadConfiguration(file);
    
            ItemStack item = e.getCurrentItem();
            ItemMeta meta = item.getItemMeta();
    
            if(e.getInventory() == map.get(e.getWhoClicked().getUniqueId())) {
    
                if(e.getSlot() == SLOT_OUTPUT) {
                    config.set("Player.Custom.Prefix.1", meta.getDisplayName());
                    config.save(file);
                    player.sendMessage("Working");
                    player.sendMessage(meta.getDisplayName());
                } else {
                    player.sendMessage("Not working");
                }
            }
        }
    }
     
    Zombie_Striker likes this.
  20. Offline

    plisov

    Looks the same as I had it
     
  21. Offline

    JanTuck

    @plisov
    It is. But its nicer to read on the website.


    -Redacted- Said something wrong.
     
    Last edited: Jan 14, 2017
  22. Offline

    plisov

    Ooh. That makes a lot of sense. How would I check it then to make sure it's an anvil?
     
  23. Offline

    JanTuck

    You would still need to know if this anvil you are in is one of your unique anvils.

    Which you can do the way @Zombie_Striker described.

    Or you can set the anvil title to an unique id. Like Bukkit.createInventory(InventoryHolder, Size, Title)
    You could generate a random uuid like UUID.randomUUID().toString()

    And then add it to a List<String> on inventoryclickevent just check if List contains the title. I would recommend listen to the inventory close event too and remove the uuid from the List so it wont make memory leaks.
     
  24. Offline

    plisov

    I tried setting a title to the anvil however I'm pretty sure you can't do that.
     
  25. Offline

    JanTuck

    It's possible but not visible xD.

    Bukkit.createInventory(InventoryHolder, InventoryType, Title)
    You could prob just use the player uuid and not use any list.

    Like

    Bukkit.createInventory(InventoryHolder, InventoryType, playerUUID.tostring)

    and then just checking in the event if the InventoryTitle is equal to the Players UUID. You might as well add something like Anvil: PlayerUUID.
    Remeber to check still if it is a anvil.

    [​IMG]
     
  26. Offline

    Zombie_Striker

    @JanTuck @plisov
    If you want to make the UUID invisible, add ChatColor.GRAY before the UUID. After that, you will have to use the .contains() method instead of .equals when checking the title.
     
  27. Offline

    plisov

    Ah. So something like this?
    Code (open)

    package me.plisov.titles.inventories.anvil;

    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.plisov.titles.Enable;

    public class Anvil1 implements Listener {
    private static ItemStack np, c1, c2, c3;

    public static HashMap<UUID, Inventory> map = new HashMap<UUID, Inventory>();

    Enable plugin;
    public static void createInv(Player player) throws IOException {

    Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL, "Anvil: " + player.getUniqueId());

    map.put(player.getUniqueId(), inv);

    c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
    //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
    //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");

    np = noPermission(ChatColor.RED + "No permission");

    if(!player.hasPermission("colors.chat.prefix")) {
    inv.setItem(0, np);
    } else {
    inv.setItem(0, c1);
    }

    player.openInventory(inv);
    }

    private static ItemStack c1(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack noPermission(String name) {
    ItemStack i = new ItemStack(Material.BEDROCK, 1);
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    String message = ChatColor.GOLD + "Your Title has been set to ";

    public static final int SLOT_INPUT_LEFT = 0;
    public static final int SLOT_INPUT_RIGHT = 1;
    public static final int SLOT_OUTPUT = 2;

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();
    FileConfiguration config = null;
    File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
    config = YamlConfiguration.loadConfiguration(file);

    ItemStack item = e.getCurrentItem();
    ItemMeta meta = item.getItemMeta();

    if(e.getInventory().getType().equals(InventoryType.ANVIL)) {
    if(e.getInventory().getTitle().equalsIgnoreCase("Anvil: " + player.getUniqueId())) {

    if(e.getSlot() == SLOT_OUTPUT) {
    config.set("Player.Custom.Prefix.1", meta.getDisplayName());
    config.save(file);
    player.sendMessage("Working");
    player.sendMessage(meta.getDisplayName());
    } else {
    player.sendMessage("Not working");
    }
    }
    }
    }
    }

    If so, it doesn't seem to work.
     
  28. Offline

    JanTuck

    @plisov

    This seems odd? What slot is it printing out?
    player.sendMessage(e.getSlot() + "")

    @Zombie_Striker
    Ik, i just thought of a alternative way to your suggestion, but it still does not seem to work.
     
  29. Offline

    plisov

    I had to change that to

    player.sendMessage(e.getSlot() + ""); and even then it prints out nothing. Even those messages that I set earlier don't print out unless I click on slot 0 (the one all the way to the left)
    Code (open)

    package me.plisov.titles.inventories.anvil;

    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.plisov.titles.Enable;

    public class Anvil1 implements Listener {
    private static ItemStack np, c1, c2, c3;

    public static HashMap<UUID, Inventory> map = new HashMap<UUID, Inventory>();

    Enable plugin;
    public static void createInv(Player player) throws IOException {

    Inventory inv = Bukkit.getServer().createInventory(player, InventoryType.ANVIL, "Anvil: " + player.getUniqueId());

    map.put(player.getUniqueId(), inv);

    c1 = c1(ChatColor.GREEN + "" + ChatColor.BOLD + "Rename");
    //c2 = c2(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 2");
    //c3 = c3(ChatColor.GREEN + "" + ChatColor.BOLD + "Custom Prefix 3");

    np = noPermission(ChatColor.RED + "No permission");

    if(!player.hasPermission("colors.chat.prefix")) {
    inv.setItem(0, np);
    } else {
    inv.setItem(0, c1);
    }

    player.openInventory(inv);
    }

    private static ItemStack c1(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack noPermission(String name) {
    ItemStack i = new ItemStack(Material.BEDROCK, 1);
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    String message = ChatColor.GOLD + "Your Title has been set to ";

    public static final int SLOT_INPUT_LEFT = 0;
    public static final int SLOT_INPUT_RIGHT = 1;
    public static final int SLOT_OUTPUT = 2;

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();
    FileConfiguration config = null;
    File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
    config = YamlConfiguration.loadConfiguration(file);

    ItemStack item = e.getCurrentItem();
    ItemMeta meta = item.getItemMeta();

    if(e.getInventory().getType().equals(InventoryType.ANVIL)) {

    player.sendMessage(e.getSlot() + "");
    if(e.getInventory().getTitle().contains("Anvil: " + player.getUniqueId())) {

    if(e.getSlot() == SLOT_OUTPUT) {

    player.sendMessage(e.getSlot() + "");
    config.set("Player.Custom.Prefix.1", meta.getDisplayName());
    config.save(file);
    player.sendMessage("Working");
    player.sendMessage(meta.getDisplayName());
    } else {
    player.sendMessage("Not working");
    }
    }
    }
    }
    }
     
  30. Offline

    JanTuck

    Seems its the wrong event.

    Please look at your console goddamnit it prints an NullPointerException all of the time ;/
     
Thread Status:
Not open for further replies.

Share This Page