Advertisement
Guest User

HitListener

a guest
May 16th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. package me.simmi.MobsterHits;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.entity.EntityType;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  9. import org.bukkit.persistence.PersistentDataType;
  10.  
  11. import net.milkbowl.vault.economy.EconomyResponse;
  12.  
  13. public class HitListener implements Listener
  14. {
  15.     // A class-level variable that stores a reference to the main plugin.
  16.     // This has no value until it's assigned in the constructor below.
  17.     // The `final` modifier is here for a couple reasons, most notably
  18.     // that the code won't compile if you don't set it to something
  19.     // within the constructor.
  20.     private final MobsterHits plugin;
  21.     // This means that whenever a HitListener is created,
  22.     // it must be provided an instance of a MobsterHits,
  23.     // i.e. the plugin.
  24.     public HitListener(MobsterHits plugin)
  25.     {
  26.         // We use `this` here to differentiate between the parameter named `plugin`
  27.         // and the class-level variable that we're storing the parameter in, also named `plugin`.
  28.         // In other words, this stores the value of the parameter `plugin`
  29.         // into the class variable `plugin`.
  30.         this.plugin = plugin;
  31.     }
  32.    
  33.     // The `@EventHandler` tag lets Bukkit know that it should pass
  34.     // some kind of event to this method. It doesn't matter what
  35.     // the method is actually named; if it's tagged with `@EventHandler`,
  36.     // Bukkit will know to find it when there's an event.
  37.     // Note that Bukkit will only be able to find this method if
  38.     // we register the whole class as an event "listener", which
  39.     // we have done in onEnable().
  40.     @EventHandler
  41.     public void onDamage(EntityDamageByEntityEvent event)
  42.     {
  43.         // If the entity damaged by this event was NOT a Player,
  44.         // return from the function (which stops execution of this code.)
  45.         if (event.getEntity().getType() != EntityType.PLAYER)
  46.         {
  47.             return;
  48.         }
  49.         // Similarly, if the entity that caused the damage
  50.         // was NOT a wither skeleton, we can stop here because
  51.         // the only "hit mobs" we spawn are wither skeletons.
  52.         if (event.getDamager().getType() != EntityType.WITHER_SKELETON)
  53.         {
  54.             return;
  55.         }
  56.         // Here we're declaring a variable `target` that holds a player,
  57.         // and we're assigning it the value provided by the event as the
  58.         // entity that was damaged.
  59.         // `(Player)` here means that even though `event.getEntity()`
  60.         // returns a base Entity, we are sure that the underlying class
  61.         // here is a Player, and to store it as one.
  62.         Player target = (Player) event.getEntity();
  63.         // This attempts to get the NBT value we stored on the skeleton earlier
  64.         // using ownerKey from the plugin class. We can reference it through the
  65.         // class-level variable `plugin` we created earlier.
  66.         // We're using PersistentDataType.STRING because that's the data
  67.         // type we stored in it earlier (a string.)
  68.         String hitterName = event.getDamager().getPersistentDataContainer().get(plugin.ownerKey, PersistentDataType.STRING);
  69.         // If we didn't find any NBT on the skeleton, stop here,
  70.         // because it must not be a skeleton we spawned.
  71.         if (hitterName == null) {
  72.             return;
  73.         }
  74.         // Try to get the player by name.
  75.         Player hitter = Bukkit.getPlayer(hitterName);
  76.         // If we couldn't find the player, they probably
  77.         // went offline since they called the hit.
  78.         // So, no need to give them money.
  79.         if (hitter == null) {
  80.             return;
  81.         }
  82.         // If we got this far, we need to take money from the player
  83.         // that got damaged, and send money to the player who called
  84.         // the hit.
  85.         // It's stored in a `double` so it can store decimal places.
  86.         // This gets the amount set in the config for "Cash per strike".
  87.         // In the event there's no entry in the config for "Cash per strike",
  88.         // it will instead return the default value we set in onEnable().
  89.         double amount = plugin.getConfig().getDouble("Cash per strike");
  90.         // The withdrawPlayer method returns some information about
  91.         // whether or not the transaction was successful, so we store
  92.         // that so we know how much money to award the player who
  93.         // called the hit.
  94.         EconomyResponse er = plugin.econ.withdrawPlayer(target, amount);
  95.         // Then, we deposit however much money we got from the target
  96.         // into the account of the player who called the hit.
  97.         // No need to check whether or not it succeeded, there's
  98.         // nothing we can do about a failure anyway.
  99.         plugin.econ.depositPlayer(hitter, er.amount);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement