SHARE
TWEET

Untitled

a guest Sep 17th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.util.Properties;
  3. import java.util.logging.*;
  4. import org.bukkit.Server;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.plugin.RegisteredServiceProvider;
  7. import com.vexsoftware.votifier.Votifier;
  8. import com.vexsoftware.votifier.model.*;
  9. import net.milkbowl.vault.economy.Economy;
  10. import net.milkbowl.vault.economy.EconomyResponse;
  11. import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
  12.  
  13. public class VaultListener implements VoteListener {
  14.     private static String   version     = "1.1.5";
  15.     private static Logger   logger      = Logger.getLogger( "VaultListener" );
  16.     private static String   PROP_FILE   = "VaultListener.properties";
  17.     private static String   VL_ID       = "[Votifier][VaultListener " + version + "]";
  18.  
  19.     // Reward amount
  20.     private static String   PK_AMT      = "reward_amount";
  21.     private static String   DEF_AMT     = "30.00";
  22.     private double          amount      = 30.0;
  23.     private double          paid        = amount;
  24.  
  25.     // Reward adornment
  26.     private static String   PK_PREFIX   = "reward_prefix";
  27.     private String          prefix      = "";
  28.  
  29.     private static String   PK_SUFFIX   = "reward_suffix";
  30.     private String          suffix      = "";
  31.  
  32.     // Reward type
  33.     private static String   PK_TYPE     = "reward_type";
  34.     private static String   TYPE_FIXED  = "fixed";
  35.     private static String   TYPE_RATE   = "rate";
  36.     private boolean isRate              = false;
  37.  
  38.     // Reward rate
  39.     private static String   PK_RATE     = "reward_rate";
  40.     private static String   DEF_RATE    = "0.01";
  41.     private double          rate        = 0.01;
  42.  
  43.     // Vote confirmation message
  44.     private static String   PK_VMSG     = "confirm_msg";
  45.     private String          confirmMsg  = "Thanks {IGN}, for voting on {SERVICE}!";
  46.  
  47.     // Payment confirmation message
  48.     private static String   PK_PMSG     = "payment_msg";
  49.     private String          paymentMsg  = "{AMOUNT} has been added to your {ECONOMY} balance.";
  50.  
  51.     // Debug Flag
  52.     private static String   PK_DEBUG    = "debug";
  53.     private boolean         debug       = false;
  54.  
  55.     // Broadcast message
  56.     private static String   PK_BCAST    = "broadcast";
  57.     private boolean         bCastFlag   = true;
  58.     private static String   PK_BCASTMSG = "broadcast_msg";
  59.     private String          bCastMsg    = "The server was voted for by {IGN}!";
  60.  
  61.     private Economy         econ        = null;
  62.     private Votifier        plugin      = null;
  63.  
  64.  
  65.     /**
  66.      * Constructor - Initialize properties and economy API
  67.      */
  68.     public VaultListener() {
  69.         plugin = Votifier.getInstance();
  70.         if ( plugin != null ) {
  71.             initializeProperties();
  72.             initializeEconomyAPI();
  73.         }
  74.         else
  75.             log( Level.SEVERE, "Cannot find reference to Votifier plugin!" );
  76.     }
  77.  
  78.  
  79.     /**
  80.      * Initialize VaultListener properties. Property file is expected to reside in Votifier's data directory. If not found, a
  81.      * default property file is generated.
  82.      */
  83.     private void initializeProperties() {
  84.         Properties props = new Properties();
  85.         File propFile = new File( plugin.getDataFolder(), PROP_FILE );
  86.  
  87.         if ( propFile.exists() ) {
  88.             /*
  89.              * Read property file if found.
  90.              */
  91.             try {
  92.                 FileReader freader = new FileReader( propFile );
  93.                 props.load( freader );
  94.                 freader.close();
  95.             }
  96.             catch ( IOException ex ) {
  97.                 log( Level.WARNING,
  98.                         "Error loading VaultListener properties. Using default messages and reward of "
  99.                                 + DEF_AMT );
  100.             }
  101.         }
  102.         else {
  103.             /*
  104.              * Create property file if it wasn't found.
  105.              */
  106.             logInfo( "No VaultListener properties file found, creating default file." );
  107.             try {
  108.                 propFile.createNewFile();
  109.                 props.setProperty( PK_AMT, Double.toString( amount ) );
  110.                 props.setProperty( PK_TYPE, TYPE_FIXED );
  111.                 props.setProperty( PK_RATE, Double.toString( rate ) );
  112.                 props.setProperty( PK_VMSG, confirmMsg );
  113.                 props.setProperty( PK_PMSG, paymentMsg );
  114.                 props.setProperty( PK_BCAST, "" + bCastFlag );
  115.                 props.setProperty( PK_BCASTMSG, bCastMsg );
  116.                 props.setProperty( PK_PREFIX, prefix );
  117.                 props.setProperty( PK_SUFFIX, suffix );
  118.                 props.setProperty( PK_DEBUG, "" + debug );
  119.  
  120.                 FileWriter fwriter = new FileWriter( propFile );
  121.                 props.store( fwriter, "Vault Listener Properties" );
  122.                 fwriter.close();
  123.  
  124.             }
  125.             catch ( IOException ex ) {
  126.                 log( Level.WARNING, "Error creating VaultListener properties." );
  127.             }
  128.         }
  129.  
  130.         // Read reward amount. Use default amount if illegal number.
  131.         try {
  132.             amount = Double.parseDouble( props.getProperty( PK_AMT, DEF_AMT ) );
  133.         }
  134.         catch ( NumberFormatException ex ) {
  135.             amount = Double.parseDouble( DEF_AMT );
  136.             log( Level.WARNING, "Illegal reward_amount! Using default reward of " + DEF_AMT );
  137.         }
  138.  
  139.         isRate = props.getProperty( PK_TYPE, TYPE_FIXED ).toLowerCase().equals( TYPE_RATE );
  140.  
  141.         // Read reward rate. Use default rate if illegal number.
  142.         if ( isRate ) {
  143.             try {
  144.                 rate = Double.parseDouble( props.getProperty( PK_RATE, DEF_RATE ) );
  145.             }
  146.             catch ( NumberFormatException ex ) {
  147.                 rate = Double.parseDouble( DEF_RATE );
  148.                 log( Level.WARNING, "Illegal reward_rate! Using default rate of " + DEF_RATE );
  149.             }
  150.         }
  151.  
  152.         prefix = props.getProperty( PK_PREFIX, prefix );
  153.         suffix = props.getProperty( PK_SUFFIX, suffix );
  154.         confirmMsg = props.getProperty( PK_VMSG, confirmMsg );
  155.         paymentMsg = props.getProperty( PK_PMSG, paymentMsg );
  156.         debug = Boolean.parseBoolean( props.getProperty( PK_DEBUG, "false" ) );
  157.         bCastFlag = Boolean.parseBoolean( props.getProperty( PK_BCAST, "true" ) );
  158.         bCastMsg = props.getProperty( PK_BCASTMSG, bCastMsg );
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Initialize economy API. Note: Because this listener is just a simple class and has no control over how Votifier loads,
  164.      * it is impossible to specify a soft/hard dependency to ensure that Vault loads before Votifier. Fortunately, Bukkit's
  165.      * two pass class loading approach should take care of this.
  166.      */
  167.     private void initializeEconomyAPI() {
  168.         RegisteredServiceProvider<Economy> economyProvider = null;
  169.         try {
  170.             economyProvider = plugin.getServer().getServicesManager()
  171.                     .getRegistration( net.milkbowl.vault.economy.Economy.class );
  172.  
  173.             if ( economyProvider != null ) {
  174.                 econ = economyProvider.getProvider();
  175.                 logInfo( "Using economy plugin: " + econ.getName() );
  176.             }
  177.             else {
  178.                 econ = null;
  179.                 log( Level.WARNING,
  180.                         "Vault cannot detect a valid economy plugin. No payments will be made!" );
  181.             }
  182.         }
  183.         catch ( NoClassDefFoundError ex ) {
  184.             log( Level.SEVERE,
  185.                     "Could not find Vault API. Please make sure Vault is installed and enabled!" );
  186.         }
  187.     }
  188.  
  189.  
  190.     @Override
  191.     public void voteMade( Vote vote ) {
  192.         String ign = vote.getUsername();
  193.         logInfo( ign );
  194.  
  195.         if ( debug ) {
  196.             voteRecordDump( vote );
  197.             configDump();
  198.         }
  199.  
  200.         // Try to pay vote IGN
  201.         if ( econ != null ) {
  202.             logDebug( "Using " + econ.getName() + " to pay IGN -> " + ign );
  203.  
  204.             /*
  205.              * If reward_type is 'rate' calculate percentage of player's balance. If it is less than the fixed amount, pay
  206.              * the fixed amount instead.
  207.              */
  208.             if ( isRate ) {
  209.                 double balance = econ.getBalance( ign );
  210.                 logDebug( "IGN balance (if 0.00, player may not have economy account): " + balance );
  211.  
  212.                 paid = balance * rate;
  213.                 logDebug( "Calculated reward: " + paid );
  214.  
  215.                 if ( paid < amount ) {
  216.                     paid = amount;
  217.                     logDebug( "Calculated reward less than fixed amount. Paying fixed amount: " + paid );
  218.                 }
  219.             }
  220.             else {
  221.                 paid = amount;
  222.                 logDebug( "Paying fixed amount: " + paid );
  223.             }
  224.  
  225.             paid = Math.round( 100.0 * paid ) / 100.0;
  226.             EconomyResponse eres = econ.depositPlayer( ign, paid );
  227.             if ( eres.type == ResponseType.FAILURE )
  228.                 logInfo( eres.errorMessage );
  229.         }
  230.         else {
  231.             paid = 0;
  232.             logDebug( "No economy plugin found" );
  233.         }
  234.  
  235.         Player player = plugin.getServer().getPlayerExact( ign );
  236.  
  237.         // Thank player, if online
  238.         if ( player != null ) {
  239.             sendMessage( player, vote, confirmMsg );
  240.             logDebug( "Found online player -> " + player.getName() );
  241.             for ( String s : insertTokenData( vote, confirmMsg ) )
  242.                 logDebug( "Confirmation message -> " + s );
  243.             if ( econ != null ) {
  244.                 sendMessage( player, vote, paymentMsg );
  245.                 for ( String s : insertTokenData( vote, paymentMsg ) )
  246.                     logDebug( "Payment message -> " + s );
  247.             }
  248.             else
  249.                 logDebug( "No economy plugin found. No payment message sent." );
  250.         }
  251.         else
  252.             logDebug( "No online player found for -> " + ign );
  253.  
  254.         if ( bCastFlag ) {
  255.             broadcastMessage( plugin.getServer(), vote, bCastMsg );
  256.             for ( String s : insertTokenData( vote, bCastMsg ) )
  257.                 logDebug( "Broadcast message -> " + s );
  258.         }
  259.         else
  260.             logDebug( "Broadcast disabled. No broadcast message sent." );
  261.     }
  262.  
  263.  
  264.     private void log( Level lvl, String msg ) {
  265.         logger.log( lvl, VL_ID + " " + msg );
  266.     }
  267.  
  268.  
  269.     private void logInfo( String msg ) {
  270.         log( Level.INFO, msg );
  271.     }
  272.  
  273.  
  274.     private void logDebug( String msg ) {
  275.         if ( debug )
  276.             logInfo( msg );
  277.     }
  278.  
  279.    
  280.     private void sendMessage( Player player, Vote vote, String msg ) {
  281.         for ( String s : insertTokenData( vote, msg ) )
  282.             player.sendMessage( s );
  283.     }
  284.    
  285.     private void broadcastMessage( Server server, Vote vote, String msg ) {
  286.         for ( String s : insertTokenData( vote, msg ) )
  287.             server.broadcastMessage( s );
  288.     }
  289.  
  290.     /*
  291.      * Replace token values in given string with actual data and split resulting string in
  292.      * multi-lines.
  293.      */
  294.     private String[] insertTokenData( Vote vote, String str ) {
  295.         String msg = str.replace( "{SERVICE}", vote.getServiceName() );
  296.         msg = msg.replace( "{IGN}", vote.getUsername() );
  297.         msg = msg.replace( "{AMOUNT}", prefix + Double.toString( paid ) + suffix );
  298.         msg = msg.replace( "{ECONOMY}", (econ != null) ? econ.getName()
  299.                 : "UNKNOWN" );
  300.         msg = msg.replaceAll( "(?i)&([0-9A-FK-OR])", "\u00A7$1" );
  301.         return msg.split( "\n" );
  302.     }
  303.  
  304.  
  305.     private void voteRecordDump( Vote vote ) {
  306.         logInfo( "Vote notification received. Vote record dump..." );
  307.         logInfo( "Voting service name -> " + vote.getServiceName() );
  308.         logInfo( "Voting service address -> " + vote.getAddress() );
  309.         logInfo( "Voting IGN -> " + vote.getUsername() );
  310.         logInfo( "Voting timestamp -> " + vote.getTimeStamp() );
  311.     }
  312.  
  313.  
  314.     private void configDump() {
  315.         logInfo( PK_AMT + " -> " + Double.toString( amount ) );
  316.         logInfo( PK_TYPE + " -> " + (isRate ? TYPE_RATE : TYPE_FIXED) );
  317.         logInfo( PK_RATE + " -> " + Double.toString( rate ) );
  318.         logInfo( PK_VMSG + " -> " + confirmMsg );
  319.         logInfo( PK_PMSG + " -> " + paymentMsg );
  320.         logInfo( PK_BCAST + " -> " + bCastFlag );
  321.         logInfo( PK_BCASTMSG + " -> " + bCastMsg );
  322.         logInfo( PK_PREFIX + " -> " + prefix );
  323.         logInfo( PK_SUFFIX + " -> " + suffix );
  324.         logInfo( PK_DEBUG + " -> " + debug );
  325.     }
  326.  
  327. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top