Villager Inventories (Vault Intergeration)

Discussion in 'Plugin Help/Development/Requests' started by SkyLarkPvP, Dec 31, 2014.

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

    SkyLarkPvP

    - FIXED RIGHT CLICKING VILLAGER -
    Ok, I've made a custom inventory that allows you to purchase items, the currency is being hooked by vault. For some reason, when i click on one of the items, i don't recieve the item, it also generates an error.
    Code:
    Code:
    package me.Jacksparrow3007.sExtras.core;
    
    import java.util.Arrays;
    import java.util.logging.Logger;
    
    import me.Jacksparrow3007.sExtras.commands.Clearname;
    import me.Jacksparrow3007.sExtras.listeners.Deaths;
    import me.Jacksparrow3007.sExtras.listeners.Names;
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin implements Listener, CommandExecutor {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Core plugin;
        public static Economy econ = null;
        Inventory drinks;
    
        @Override
        public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        getServer().getPluginManager().registerEvents(this, this);
        drinks = Bukkit.createInventory(null, 9, "§dMerchant's Selection of Drinks");
        drinks.setItem(0, createItem(Material.POTION, 1, (short) 0, "§3Root Beer", "§fPrice §a$200"));
        if(this.getServer().getPluginManager().getPlugin("Vault") == null){
        this.getServer().getPluginManager().disablePlugin(this);
        this.getLogger().info("Plugin disabled due to Vault not being found!");
        }}
        @Override
        public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
      
        private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
        return false;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
        public ItemStack createItem(Material material, int amount, int shrt, String displayname, String lore) {
        ItemStack item = new ItemStack(material, amount, (short) shrt);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(displayname);
        meta.setLore(Arrays.asList(lore));
        item.setItemMeta(meta);
        return item;
    }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onClick(InventoryClickEvent e) {
        Player p = (Player) e.getWhoClicked();
        ItemStack item = e.getCurrentItem();  
        if (e.getInventory().getName().equals("§dMerchant's Selection of Drinks")) {
        e.setCancelled(true);
        if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
        if(item.getItemMeta().getDisplayName().equals("§3Root Beer")){
        if (econ.withdrawPlayer(p.getName(), 1000).transactionSuccess()) {
        p.sendMessage("Here you go, an icy cold root beer for you!");
        }
        else{
        p.sendMessage("§cYou don't have enough money!");
        }
        }
    }}}}
    Error:
    Code:
    [22:15:32 ERROR]: Could not pass event InventoryClickEvent to sExtras v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:299) ~[spigot.jar:git-Spigot-1571]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1571]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-1571]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :1428) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.a(SourceFile:32)
    [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.handle(SourceFil
    e:10) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184
    ) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    31) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1571]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1571]
    Caused by: java.lang.NullPointerException
            at me.Jacksparrow3007.sExtras.core.Core.onClick(Core.java:120) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    ]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    ]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:296) ~[spigot.jar:git-Spigot-1571]
            ... 13 more
     
    Last edited: Jan 3, 2015
  2. Offline

    Monkey_Swag

    You are only registering your events if Vault is null. Clean up your brackets.
     
  3. Offline

    SkyLarkPvP

    @Monkey_Swag
    This isn't all of the code, i just posted all of the necessary code for the GUI.
     
  4. Offline

    Monkey_Swag

    @SkyLarkPvP look at your onEnable. You're registering your events only if Valt is null.
     
  5. Offline

    Fuzzybear04

    When I'm doing it, I'll close the players inventory after the events been called, then open my own inventory.
     
  6. Offline

    SkyLarkPvP

    Thanks @Monkey_Swag misread what you said.

    Ok, now when i check if the villager has a custom name the villager inventory doesn't disappear, blocking the custom inventory. It also gives an error
    Heres the new piece of code:
    Code:
        @EventHandler
        public void PlayerInteractEntityEvent(PlayerInteractEntityEvent e1){
        Player player = e1.getPlayer();
        if(e1.getRightClicked() instanceof Villager && ((LivingEntity) e1.getRightClicked()).getCustomName().equals(Color.GREEN + "Merchant"))
        e1.setCancelled(true);
        player.openInventory(drinks);
        return;
    Error:
    Code:
    01:33:13] [Server thread/ERROR]: Could not pass event PlayerInteractEntityEvent to sExtras v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:299) ~[spigot.jar:git-Spigot-1571]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1571]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-1571]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:1136) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.PacketPlayInUseEntity.a(SourceFile:55) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.PacketPlayInUseEntity.handle(SourceFile:10) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:731) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1571]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1571]
    Caused by: java.lang.NullPointerException
        at me.Jacksparrow3007.sExtras.core.Core.PlayerInteractEntityEvent(Core.java:87) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:296) ~[spigot.jar:git-Spigot-1571]
        ... 13 more
    
    @Monkey_Swag Any suggestions?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  7. Offline

    Monkey_Swag

    @SkyLarkPvP
     
  8. Offline

    SkyLarkPvP

    @Monkey_Swag
    e1.setCancelled(true)
    player.openInventory(drinks);
    I've already done that.
     
  9. Offline

    Monkey_Swag

    @SkyLarkPvP
    e1.setCancelled(true);
    p.closeInventory();
    player.openInventory(drinks);
     
  10. Offline

    SkyLarkPvP

    I tried it, but still nothing, the villager inventory overlaps the custom inventory and it still gives an error.
    @Monkey_Swag
     
  11. Offline

    Monkey_Swag

  12. Offline

    Fuzzybear04

    You could always run the close inventory, then run a for loop to open the inventory multiple times as a fail safe.
     
  13. Offline

    SkyLarkPvP

    @Monkey_Swag
     
    Last edited: Jan 2, 2015
  14. Offline

    MCMatters

    Core.java line 87 is...
    @SkyLarkPvP

    also made a correction
    Code:
        @EventHandler
        public void PlayerInteractEntityEvent(PlayerInteractEntityEvent e1){
        Player player = e1.getPlayer();
        if(e1.getRightClicked() instanceof Villager && ((LivingEntity) e1.getRightClicked()).getCustomName().equals(Color.GREEN + "Merchant")){
        e1.setCancelled(true);
       player.closeInventory();
        player.openInventory(drinks);
        return;
    }}
     
    Last edited by a moderator: Jan 3, 2015
  15. Offline

    nj2miami

    ->> NullPointerException at me.Jacksparrow3007.sExtras.core.Core.PlayerInteractEntityEvent(Core.java:87)

    What is in your interact event at line 87?
     
  16. Offline

    SkyLarkPvP

    Line 87 in Core.java is
    if(e1.getRightClicked() instanceof Villager && ((LivingEntity) e1.getRightClicked()).getCustomName().equals(Color.GREEN + "Merchant")){
    I think i have to check if the villager's name is null first, i'm not sure how to do that though.
    @nj2miami @MCMatters
     
    Last edited: Jan 3, 2015
  17. Moved to Bukkit Alternates.
     
  18. Offline

    SkyLarkPvP

    Ok, I've got it working, however when i click on the item it doesnt give me the item and gives an error, post has been updated.
    For those who are wondering, the line thats causing it to make an error is this:
    if (econ.withdrawPlayer(p.getName(), 1000).transactionSuccess()) {

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
Thread Status:
Not open for further replies.

Share This Page