114 lines
3.9 KiB
Java
114 lines
3.9 KiB
Java
package ca.thetechrobo.fbl;
|
|
|
|
import java.time.Instant;
|
|
import java.util.HashMap;
|
|
import java.util.UUID;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.OfflinePlayer;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.entity.EntityType;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
import org.bukkit.persistence.PersistentDataContainer;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.format.TextColor;
|
|
|
|
public class PlayerTookDamage implements Listener {
|
|
private JavaPlugin pl;
|
|
private HashMap<Player, CountdownTimer> map;
|
|
|
|
public PlayerTookDamage(JavaPlugin pl) {
|
|
this.pl = pl;
|
|
this.map = new HashMap<Player, CountdownTimer>();
|
|
}
|
|
|
|
private long load_internal_long(Player p, NamespacedKey key, long def) {
|
|
PersistentDataContainer c = p.getPersistentDataContainer();
|
|
if (c.has(key, PersistentDataType.LONG)) {
|
|
long i = c.get(key, PersistentDataType.LONG);
|
|
return i;
|
|
}
|
|
return def;
|
|
}
|
|
|
|
private String load_internal_str(Player p, NamespacedKey key) {
|
|
PersistentDataContainer c = p.getPersistentDataContainer();
|
|
if (c.has(key, PersistentDataType.STRING)) {
|
|
String i = c.get(key, PersistentDataType.STRING);
|
|
return i;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@EventHandler
|
|
public void playerTookDamage(EntityDamageByEntityEvent e) {
|
|
if (!(e.getEntity().getType() == EntityType.PLAYER))
|
|
return;
|
|
Entity damager = e.getDamager();
|
|
if (!(damager.getType() == EntityType.PLAYER))
|
|
return;
|
|
Player p2 = (Player) damager;
|
|
Player p = (Player) e.getEntity();
|
|
if (this.map.containsKey(p)) {
|
|
this.map.get(p).cancel();
|
|
}
|
|
if (this.map.containsKey(p2)) {
|
|
this.map.get(p2).cancel();
|
|
}
|
|
long unixTimestamp = Instant.now().getEpochSecond();
|
|
NamespacedKey key = new NamespacedKey(this.pl, "AntiBL_LastHit");
|
|
NamespacedKey key2 = new NamespacedKey(this.pl, "AntiBL_HitOn");
|
|
p.getPersistentDataContainer().set(key, PersistentDataType.LONG, unixTimestamp);
|
|
p2.getPersistentDataContainer().set(key, PersistentDataType.LONG, unixTimestamp);
|
|
p.getPersistentDataContainer().set(key2, PersistentDataType.STRING, p2.getUniqueId().toString());
|
|
p2.getPersistentDataContainer().set(key2, PersistentDataType.STRING, p.getUniqueId().toString());
|
|
CountdownTimer timerP = new CountdownTimer(11, p, this);
|
|
CountdownTimer timerP2 = new CountdownTimer(11, p2, this);
|
|
timerP.runTaskTimer(this.pl, 0, 20);
|
|
timerP2.runTaskTimer(this.pl, 0, 20);
|
|
this.map.put(p, timerP);
|
|
this.map.put(p2, timerP2);
|
|
new WriteLastHit(p, this.pl).runTaskLater(pl, 11 * 20);
|
|
new WriteLastHit(p2, this.pl).runTaskLater(pl, 11 * 20);
|
|
}
|
|
|
|
@EventHandler
|
|
public void playerLeftGame(PlayerQuitEvent e) {
|
|
Player p = e.getPlayer();
|
|
NamespacedKey key = new NamespacedKey(this.pl, "AntiBL_LastHit");
|
|
long lastOldHit = load_internal_long(p, key, 0);
|
|
long unixTimestamp = Instant.now().getEpochSecond();
|
|
long difference = unixTimestamp - lastOldHit;
|
|
if (difference < 11) {
|
|
Component pDisplayName = p.displayName().color(TextColor.color(100, 100, 255));
|
|
String uu = load_internal_str(p, new NamespacedKey(this.pl, "AntiBL_HitOn"));
|
|
Component p2d = Component.text("someone");
|
|
if (uu == null) {
|
|
} else {
|
|
UUID p2u = UUID.fromString(uu);
|
|
OfflinePlayer p2 = Bukkit.getOfflinePlayer(p2u);
|
|
p2d = Component.text(p2.getName()).color(TextColor.color(200, 100, 150));
|
|
}
|
|
Component message = pDisplayName.append(Component.text(" quit during battle with ").append(p2d))
|
|
.append(Component.text("."));
|
|
Bukkit.broadcast(message);
|
|
p.damage(unixTimestamp); // THAT'S A LOTTA DAMAGE!
|
|
p.kick(Component.text("You logged out during PvP and died!"));
|
|
}
|
|
this.map.remove(p);
|
|
}
|
|
|
|
public void removeMe(CountdownTimer timer) {
|
|
Player p = timer.getPlayer();
|
|
this.map.remove(p, timer);
|
|
}
|
|
}
|