Error when trying to tax players for using ChestShop

Discussion in 'Spigot Plugin Development' started by Viariouz, Jun 4, 2015.

  1. Hello,
    I'm trying to place a "tax" on player's shop transactions on my server and deposit the money to the current town the player is standing in. The script works how it is supposed to, but I get this pesky error when the player is standing in the Towny wild.

    [​IMG]

    The code for this is right here
    Code (Text):
    package me.andrewyunt.dynamiceconomy;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;

    import me.ksafin.dynamiceconomy.DynamicEconomy;

    import com.Acrobot.ChestShop.Events.TransactionEvent;
    import com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
    import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
    import com.palmergames.bukkit.towny.object.WorldCoord;

    public class ChestShopTaxManager implements Listener {
       
        DynamicEconomy plugin;
       
        public ChestShopTaxManager(DynamicEconomy plugin) {
            this.plugin = plugin;
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void postMoneyTransaction(TransactionEvent event) throws NotRegisteredException {
            double price = event.getPrice();
            double purchasetaxamt = price * .07;
            double salestaxamt = price * .14;
            Player player = event.getClient();
           
            if(WorldCoord.parseWorldCoord(player).getTownBlock().hasTown() == false) {} else {
                String townAccount;
                townAccount = "town_" + WorldCoord.parseWorldCoord(player).getTownBlock().getTown().getName().toLowerCase();
               
                if(event.getTransactionType() == TransactionType.SELL) {
                    DynamicEconomy.economy.withdrawPlayer(event.getClient(), salestaxamt);
                    DynamicEconomy.economy.depositPlayer(townAccount, salestaxamt);
                    player.sendMessage(ChatColor.GRAY + "You were charged " + ChatColor.BLUE + "14% tax " + ChatColor.DARK_GRAY + "(" + ChatColor.BLUE + salestaxamt + " pyx" +
                    ChatColor.DARK_GRAY + ")" + ChatColor.GRAY + " on your sale.");
                } else if (event.getTransactionType() == TransactionType.BUY) {
                    DynamicEconomy.economy.withdrawPlayer(event.getClient(), purchasetaxamt);
                    DynamicEconomy.economy.depositPlayer(townAccount, purchasetaxamt);
                    player.sendMessage(ChatColor.GRAY + "You were charged " + ChatColor.BLUE + "7% tax " + ChatColor.DARK_GRAY + "(" + ChatColor.BLUE + purchasetaxamt + " pyx" +
                    ChatColor.DARK_GRAY + ")" + ChatColor.GRAY + " on your purchase.");
                }
            }
        }
    }
    Any ideas on how to fix this? Thanks.
     
  2. This is a basic problem that can be solved with logic. Also, You should get the location of the chest/shop. Rather than the player.

    In regards to the problem though, I see two solutions. Either create a "wild" bank account or, don't do anything if "wild" is detected.
     
  3. Towny is annoying, End off
     
  4. Well it already checks if the player is standing on a town block by using

    if(WorldCoord.parseWorldCoord(player).getTownBlock().hasTown())

    I already removed the else statement and just made it simple so it only acts if the player is in a town. Not exactly sure why it's still giving me the same error.
     
  5. Quick note: There's a huge difference in checking chest is in town, or player is in town. Example: Town A and Town B are neighboring cities with only 3 blocks of wilderness between them. If bob stand in the wilderness, either chest he tries to open will come up as being in the wilderness, too. It's better to check the chest location, because it doesnt really matter where the player is. ;)
     
    • Agree Agree x 1
    • Useful Useful x 1
  6. Thanks, I changed it to signs to make it get the proper location. The issue I have here is that the code works properly and does it's job but throws errors in the console anyways which are just annoying and spam it. It also adds fear to me that there could be worse optimization in the plugin. The Towny API and ChestShop API are both shady areas to me so this is somewhat hard for me right now. My code is now updated to this but producing the same error as before.

    Code (Text):
    package me.andrewyunt.dynamiceconomy;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;

    import me.ksafin.dynamiceconomy.DynamicEconomy;

    import com.Acrobot.ChestShop.Events.TransactionEvent;
    import com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
    import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
    import com.palmergames.bukkit.towny.object.WorldCoord;

    public class ChestShopTaxManager implements Listener {
       
        DynamicEconomy plugin;
       
        public ChestShopTaxManager(DynamicEconomy plugin) {
            this.plugin = plugin;
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void postMoneyTransaction(TransactionEvent event) throws NotRegisteredException {
            double price = event.getPrice();
            double purchasetaxamt = price * .07;
            double salestaxamt = price * .14;
            Player player = event.getClient();
           
            if(WorldCoord.parseWorldCoord(event.getSign().getLocation()).getTownBlock().hasTown()) {
                String townAccount;
                townAccount = "town_" + WorldCoord.parseWorldCoord(event.getSign().getLocation()).getTownBlock().getTown().getName().toLowerCase();
               
                if(event.getTransactionType() == TransactionType.SELL) {
                    DynamicEconomy.economy.withdrawPlayer(event.getClient(), salestaxamt);
                    DynamicEconomy.economy.depositPlayer(townAccount, salestaxamt);
                    player.sendMessage(ChatColor.GRAY + "You were charged " + ChatColor.BLUE + "14% tax " + ChatColor.DARK_GRAY + "(" + ChatColor.BLUE + salestaxamt + " pyx" +
                    ChatColor.DARK_GRAY + ")" + ChatColor.GRAY + " on your sale.");
                } else if (event.getTransactionType() == TransactionType.BUY) {
                    DynamicEconomy.economy.withdrawPlayer(event.getClient(), purchasetaxamt);
                    DynamicEconomy.economy.depositPlayer(townAccount, purchasetaxamt);
                    player.sendMessage(ChatColor.GRAY + "You were charged " + ChatColor.BLUE + "7% tax " + ChatColor.DARK_GRAY + "(" + ChatColor.BLUE + purchasetaxamt + " pyx" +
                    ChatColor.DARK_GRAY + ")" + ChatColor.GRAY + " on your purchase.");
                }
            }
        }
    }
     
  7. You ought to use

    Code (Text):
    if (!TownyUniverse.isWilderness(signLocation))
     
    • Winner Winner x 1
  8. Well that fixed the issue, I wasn't sure what to use in the Towny API. Anyways thanks, because I wasn't expecting anybody to reply to my thread then one of the Towny devs comes on to help out.
     
    • Creative Creative x 1