Solved Help with item interaction

Discussion in 'Plugin Development' started by Techno, Jul 4, 2014.

Thread Status:
Not open for further replies.
  1. So, I have a plugin made and what it does is when you right click with a book in your hand it shows you your /money or /balance.

    Code:
    Code:
    package me.Bill4788.Wallet;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
    public void onEnable() {
    if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
            setupChat();
     
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
     
    private static final Logger log = Logger.getLogger("Minecraft");
    public static Economy econ = null;
    public static Permission perms = null;
    public static Chat chat = null;
     
        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;
        }
     
        private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }
     
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = rsp.getProvider();
            return perms != null;
        }
     
     
     
     
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("wallet")) { // If the player typed /basic then do the following...
    Player p = (Player) sender;
    PlayerInventory pi = p.getInventory();
     
    // Inv Stufferino
    ItemStack wallet = new ItemStack(Material.BOOK, 1);
    pi.addItem(wallet);
     
    return true;
    }
     
    return false; 
    }
     
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
     
    if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    if (e.getPlayer().getItemInHand().getType() == Material.BOOK) {
    p.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Wallet" + ChatColor.GOLD + "] You have: " + econ.getBalance(e.getPlayer()));
    }
    }
     
    }
    }
    
    Stack: http://pastebin.com/2kqJfVDc
     
  2. Offline

    simolus3

    It's seems like the exception occurs in your setupChat(). The RegisteredServiveProvider rsp is null, which is why you can't do rsp.getProvider(); Check if (rsp == null) first.
     
  3. Offline

    EgyptianKing

    Here are some changes I've made and now your code works:

    From:
    Code:java
    1. public static Economy econ = null;
    2. public static Permission perms = null;
    3. public static Chat chat = null;

    To:
    Code:java
    1. public static Economy econ;
    2. public static Permission perms;
    3. public static Chat chat;


    From:
    Code:java
    1. private boolean setupChat() {
    2. RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
    3. chat = rsp.getProvider();
    4. return chat != null;
    5. }

    To:
    Code:java
    1. private boolean setupChat() {
    2. RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
    3. if (chatProvider != null) {
    4. chat = chatProvider.getProvider();
    5. }
    6.  
    7. return (chat != null);
    8. }


    From:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    6. if (e.getPlayer().getItemInHand().getType() == Material.BOOK) {
    7. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Wallet" + ChatColor.GOLD + "] You have: " + econ.getBalance(e.getPlayer()));
    8. }
    9. }
    10.  
    11. }

    To:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3.  
    4. Player p = e.getPlayer();
    5. ItemStack i = p.getItemInHand();
    6. ItemStack book = new ItemStack(Material.BOOK);
    7. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    8. if (i.equals(book)) {
    9. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Wallet" + ChatColor.GOLD + "] You have: " + econ.getBalance(p));
    10. }
    11. }
    12.  
    13. }


    Hope this helps.
     
  4. Offline

    hankered

    EgyptianKing
    You're probably going to get hate for spoonfeeding
     
  5. Offline

    EgyptianKing

    hankered

    Yea you might be right, but I did not fix the issue with any knowledge on the error, rather I tried and experimented until it worked. Since I cannot give him a nice explanation which would be useful for him, I pasted the code. But I hope the code I provided him would help him figure out what he did wrong.
     
Thread Status:
Not open for further replies.

Share This Page