Advertisement
Guest User

MobsterHits

a guest
May 16th, 2022
24
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.simmi.MobsterHits;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.NamespacedKey;
  6. import org.bukkit.World;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Entity;
  10. import org.bukkit.entity.EntityType;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.persistence.PersistentDataType;
  13. import org.bukkit.plugin.RegisteredServiceProvider;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. import net.milkbowl.vault.economy.Economy;
  17.  
  18. public class MobsterHits extends JavaPlugin
  19. {
  20.     // This is a variable that holds the `key` we use to get who owns
  21.     // a 'hit' using NBT data.
  22.     // We're supplying `this` here, referring to this class, MobsterHits,
  23.     // so that Bukkit will be able to distinguish our "HitOwner" data from
  24.     // another plugin's "HitOwner" data (if another plugin coincidentally
  25.     // uses the same key of "HitOwner".)
  26.     public final NamespacedKey ownerKey = new NamespacedKey(this, "HitOwner");
  27.     // This will store the economy provider that we get through Vault.
  28.     // For how we get this, see setupEconomy()
  29.     public Economy econ;
  30.     @Override
  31.     public void onEnable()
  32.     {
  33.         // This sets a "default config value" so that
  34.         // if there is no entry for "Cash per strike",
  35.         // we use this value instead.
  36.         getConfig().addDefault("Cash per strike", 5);
  37.         // This sets it to write any default values back
  38.         // to the config if they aren't already present.
  39.         // In this case, that's just "Cash per strike: 5"
  40.         getConfig().options().copyDefaults(true);
  41.         // This actually writes the config, now that all
  42.         // the settings are correct.
  43.         saveConfig();
  44.         // Now we need to attempt to hook into Vault.
  45.         // If we failed to get an instance of the economy,
  46.         // (which we know by the return value of setupEconomy() )
  47.         // print an error message.
  48.         if (!setupEconomy())
  49.         {
  50.             getLogger().severe("Couldn't register with Vault. Economy features will be disabled.");
  51.             // Stop here.
  52.             return;
  53.         }
  54.         // If we got to this point, the economy was registered successfully
  55.         // and we can register the damage listener that pays out money.
  56.         getServer().getPluginManager().registerEvents(new HitListener(this), this);
  57.     }
  58.    
  59.     @Override
  60.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
  61.     {
  62.         String prefix = ChatColor.BLUE.toString() + ChatColor.BOLD.toString()
  63.          + "Hits" + ChatColor.DARK_GRAY.toString() + ChatColor.BOLD.toString() + "> ";
  64.        
  65.         if (args.length == 0) {
  66.             sender.sendMessage(prefix + ChatColor.RED + "/hit <player>");
  67.         }
  68.         else if (args.length == 1 && sender.hasPermission("hits.hit"))
  69.         {
  70.             String targetName = args[0];
  71.             Player target = Bukkit.getServer().getPlayer(targetName);
  72.             if (target != null)
  73.             {
  74.                 // Make sure to only get the target's world
  75.                 // after we've confirmed the target isn't null.
  76.                 World world = target.getWorld();
  77.                 // Loops only while `i` is less than 3, and starting with 0.
  78.                 // This means it loops 3 times, with the values 0, 1, and 2.
  79.                 // We don't actually use the `i` variable except in the loop,
  80.                 // it just helps us loop exactly 3 times.
  81.                 for (int i = 0; i < 3; i++)
  82.                 {
  83.                     // This just stores a reference to the spawned wither skeleton into a variable.
  84.                     // We could store it as a WitherSkeleton type, but we don't need to, so just skip it.
  85.                     Entity witherSkeleton = world.spawnEntity(target.getLocation(), EntityType.WITHER_SKELETON);
  86.                    
  87.                     // Only store owner data if the command was run by a Player,
  88.                     // because if the console or a command block ran the command,
  89.                     // we don't have a Vault account to give money to.
  90.                     if (sender instanceof Player)
  91.                     {
  92.                         // This sets a piece of NBT data on the wither skeleton that we can read later in HitListener.
  93.                         // `ownerKey` is the variable at the top of this file that holds the name we're storing
  94.                         // the NBT data under.
  95.                         // PersistentDataType.STRING says we're storing, well, a string.
  96.                         // It would probably be better to store the sender's account ID (UUID) instead
  97.                         // of their account name, but storing their account name is easier.
  98.                         witherSkeleton.getPersistentDataContainer().set(ownerKey, PersistentDataType.STRING, sender.getName());
  99.                     }
  100.                 }
  101.                 target.sendMessage(prefix + ChatColor.WHITE + "You've been hit by " + sender.getName());
  102.             }
  103.             else
  104.             {
  105.                 sender.sendMessage(prefix + ChatColor.RED + "Player not online");          
  106.             }
  107.            
  108.         }
  109.        
  110.         return true;
  111.     }
  112.    
  113.     // This code is taken from the Vault API example at:
  114.     // https://github.com/MilkBowl/VaultAPI
  115.     // with additional comments added.
  116.     private boolean setupEconomy()
  117.     {
  118.         // If Bukkit says there is no plugin named "Vault" loaded,
  119.         // then we can't set get the economy.
  120.         if (getServer().getPluginManager().getPlugin("Vault") == null)
  121.         {
  122.             // If we couldn't find Vault, stop code execution.
  123.             // We specify `false` because that's how we're telling
  124.             // the caller of the function that we weren't successful.
  125.             return false;
  126.         }
  127.         // This attempts to get a RegisteredServiceProvider for the Vault economy
  128.         // through Bukkit's services API.
  129.         RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
  130.         // If the Bukkit API didn't find a provider for Vault's economy,
  131.         // stop here, and return `false` because we couldn't find an economy.
  132.         if (rsp == null)
  133.         {
  134.             return false;
  135.         }
  136.         // This "unwraps" the Economy instance Bukkit found for us
  137.         // and stores it in the class-level variable `econ` that
  138.         // we declared earlier.
  139.         econ = rsp.getProvider();
  140.         // This is shorthand for returning true if `econ` is not null,
  141.         // or returning false if it is null, because if `econ` is null,
  142.         // we didn't succeed on our quest to find Vault's Economy.
  143.         return econ != null;
  144.     }
  145. }
Advertisement
RAW Paste Data Copied
Advertisement