Solved Vault API doesn't work in PlayerInteract event

Discussion in 'Plugin Development' started by darkgreetingsnl, Apr 27, 2015.

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

    darkgreetingsnl

    Hello, I try to withdraw an amount of money from the player's saldo but it doesn't work.
    If I try it with an command it works, but when I try to withdraw money in an event it doesn't.
    Here is the code:
    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType() == Material.STONE_BUTTON) {
                    if(p.isInsideVehicle()) {
                        p.sendMessage(ChatColor.GREEN + "[GTA Parking Garage] You paid $75,- for an parking garage ticket");
                        plugin.withdrawMoney(e.getPlayer(), 75.0);
                    } else {
                        p.sendMessage("You are not in an vehicle right now.");
                    }
                }
            }
        }
     
  2. Offline

    darkgreetingsnl

    @Mali_Plavi
    Yes, here is the Main class
    Code:
    package me.Remco;
    
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        public Economy econ = null;
        public static Permission perms = null;
        public static Chat chat = null;
       
        public final Events ev = new Events();
       
        public void onEnable() {
            getLogger().info("Parking Garage activated");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.ev, this);
           
            if (!setupEconomy() ) {
                getLogger().info("Parking Garage Enabled");
                return;
            }
           setupPermissions();
           setupChat();
          
        }
       
        public void onDisable() {
            getLogger().info("Parking Garage Deactivated");
        }
       
          // VAULT SECTION
          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("hallo")) {
                      Player p = (Player) sender; // Selecteert de speler die de command uitvoerdes
                      econ.withdrawPlayer(p, 350);
                   }
                 return false;
            }
           
            public void withdrawMoney(Player p, double money) {
                econ.withdrawPlayer(p, money);
            }
    }
    
     
  3. @darkgreetingsnl in which class do you have
    Code:
      @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType() == Material.STONE_BUTTON) {
                    if(p.isInsideVehicle()) {
                        p.sendMessage(ChatColor.GREEN + "[GTA Parking Garage] You paid $75,- for an parking garage ticket");
                        plugin.withdrawMoney(e.getPlayer(), 75.0);
                    } else {
                        p.sendMessage("You are not in an vehicle right now.");
                    }
                }
            }
        }
    this
     
    Last edited: Apr 27, 2015
  4. Offline

    darkgreetingsnl

    @Mali_Plavi
    I have that in my Events class named "Events".
     
  5. @darkgreetingsnl i think that you should to this
    Code:
    this.getServer().getPluginManager().registerEvents(new Events(), this)
    @darkgreetingsnl and, does it trigger the event

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    darkgreetingsnl

    @Mali_Plavi
    It's still not working :/ And what do you exactly mean with "does it trigger the event?" Thnx for your fast reply's btw. I really need help with this.
     
  7. @darkgreetingsnl try to add something to your event to see does it go trought it
     
  8. Offline

    darkgreetingsnl

    @Mali_Plavi
    I added a sendMessage after the withdraw but it doesn't send a message so it will not go through it.
    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType() == Material.STONE_BUTTON) {
                    if(p.isInsideVehicle()) {
                        p.sendMessage(ChatColor.GREEN + "[GTA Parking Garage] You paid $75,- for an parking garage ticket");
                        plugin.withdrawMoney(e.getPlayer(), 75.0);
                        p.sendMessage("Hi"); // To see if it goes through it
                    } else {
                        p.sendMessage("You are not in an vehicle right now.");
                    }
                }
            }
        }
     
  9. Offline

    darkgreetingsnl

    @Mali_Plavi
    Like this?
    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType() == Material.STONE_BUTTON) {
                    if(p.isInsideVehicle()) {
                        p.sendMessage(ChatColor.GREEN + "[GTA Parking Garage] You paid $75,- for an parking garage ticket");
                        plugin.withdrawMoney(e.getPlayer(), 75.0);
                        p.sendMessage("Hi");
                    } else {
                        p.sendMessage("You are not in an vehicle right now.");
                    }
                }
            }
            p.sendMessage("test"); // To see if it goes through it
        }
     
  10. Offline

    darkgreetingsnl

  11. and did you do this:
    Code:
    public Main plugin;
      
        public Events(Main plugin){
            this.plugin = plugin;
        }
     
  12. Offline

    darkgreetingsnl

    @Mali_Plavi
    No but when I try that it will give me a warning "The static field Events.plugin should be accessed in a static way.".
    And I get a few errors in my Main class.

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class Events implements Listener {
       
        public static Main plugin;
       
        public Events(Main plugin){
            this.plugin = plugin; // WARNING
        }
    
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType() == Material.STONE_BUTTON) {
                    if(p.isInsideVehicle()) {
                        p.sendMessage(ChatColor.GREEN + "[GTA Parking Garage] You paid $75,- for an parking garage ticket");
                        plugin.withdrawMoney(e.getPlayer(), 75.0);
                        p.sendMessage("Hi");
                    } else {
                        p.sendMessage("You are not in an vehicle right now.");
                    }
                }
            }
            p.sendMessage("test");
        }
       
    }
    
    Guys, I fixed the problem. I used the old Vault API implent code. Thanks for helping @Mali_Plavi.

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

Share This Page