feat: BlackKnife DeltaMine v1.0.0 - Paper 1.21.1 SWOON/Stars/Sword/Diamond/Lunge/Snipe/Tri/Overhead + Magic 4commons Parry chat 400words +12h updates
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package me.sashegdev.blackknife;
|
||||
|
||||
import net.kyori.adventure.bossbar.BossBar;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TensionManager implements Listener {
|
||||
private final Map<UUID, Float> tp = new HashMap<>();
|
||||
final Map<UUID, BossBar> bars = new HashMap<>();
|
||||
private final Map<UUID, Long> blockStart = new HashMap<>();
|
||||
private final Map<UUID, Long> lastCombat = new HashMap<>();
|
||||
private final Map<UUID, java.util.Set<UUID>> battleEnemies = new HashMap<>();
|
||||
|
||||
public float getTP(Player p) { return tp.getOrDefault(p.getUniqueId(), 0f); }
|
||||
|
||||
public void setTP(Player p, float v) {
|
||||
float clamped = Math.max(0, Math.min(250, v));
|
||||
tp.put(p.getUniqueId(), clamped);
|
||||
updateBar(p, clamped);
|
||||
}
|
||||
|
||||
public void addTP(Player p, float amount, String reason) {
|
||||
float cur = Math.min(250, getTP(p) + amount);
|
||||
tp.put(p.getUniqueId(), cur);
|
||||
lastCombat.put(p.getUniqueId(), System.currentTimeMillis());
|
||||
updateBar(p, cur);
|
||||
if (amount > 0) p.sendActionBar(Component.text(String.format("+%.0f%% TP %s", amount / 2.5f, reason)).color(NamedTextColor.YELLOW));
|
||||
}
|
||||
|
||||
public boolean isOwner(Player p) {
|
||||
return BlackKnifeItem.is(p.getInventory().getItemInMainHand()) || BlackKnifeItem.is(p.getInventory().getItemInOffHand());
|
||||
}
|
||||
|
||||
private void updateBar(Player p, float tpVal) {
|
||||
BossBar bar = bars.get(p.getUniqueId());
|
||||
if (bar == null) {
|
||||
bar = BossBar.bossBar(Component.text("TP 0%"), 0f, BossBar.Color.WHITE, BossBar.Overlay.NOTCHED_10);
|
||||
bars.put(p.getUniqueId(), bar);
|
||||
}
|
||||
float pct = tpVal / 250f;
|
||||
bar.progress(Math.max(0, Math.min(1, pct)));
|
||||
bar.name(Component.text(String.format("TP %.0f%%", pct * 100)).color(NamedTextColor.WHITE));
|
||||
bar.color(BossBar.Color.WHITE);
|
||||
if (tpVal > 0) bar.addViewer(p); else bar.removeViewer(p);
|
||||
}
|
||||
|
||||
public void showFor(Player p) {
|
||||
BossBar bar = bars.get(p.getUniqueId());
|
||||
if (bar == null) {
|
||||
updateBar(p, getTP(p));
|
||||
bar = bars.get(p.getUniqueId());
|
||||
}
|
||||
if (getTP(p) > 0) bar.addViewer(p);
|
||||
}
|
||||
|
||||
public boolean isInBattle(Player p) {
|
||||
java.util.Set<UUID> set = battleEnemies.get(p.getUniqueId());
|
||||
return set != null && !set.isEmpty();
|
||||
}
|
||||
|
||||
public void startBattle(Player attacker, org.bukkit.entity.LivingEntity defender) {
|
||||
battleEnemies.computeIfAbsent(attacker.getUniqueId(), k -> new java.util.HashSet<>()).add(defender.getUniqueId());
|
||||
lastCombat.put(attacker.getUniqueId(), System.currentTimeMillis());
|
||||
if (getTP(attacker) > 0) showFor(attacker);
|
||||
if (defender instanceof Player dp) {
|
||||
battleEnemies.computeIfAbsent(dp.getUniqueId(), k -> new java.util.HashSet<>()).add(attacker.getUniqueId());
|
||||
lastCombat.put(dp.getUniqueId(), System.currentTimeMillis());
|
||||
if (getTP(dp) > 0) showFor(dp);
|
||||
}
|
||||
}
|
||||
|
||||
public void tickBattle() {
|
||||
long now = System.currentTimeMillis();
|
||||
for (var e : new java.util.HashMap<>(battleEnemies).entrySet()) {
|
||||
UUID pid = e.getKey();
|
||||
java.util.Set<UUID> set = e.getValue();
|
||||
set.removeIf(uuid -> {
|
||||
var ent = org.bukkit.Bukkit.getEntity(uuid);
|
||||
return ent == null || !ent.isValid() || (ent instanceof org.bukkit.entity.LivingEntity le && le.isDead());
|
||||
});
|
||||
Long last = lastCombat.get(pid);
|
||||
if (set.isEmpty() || (last != null && now - last > 180000)) {
|
||||
battleEnemies.remove(pid);
|
||||
lastCombat.remove(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasHeldShield(Player p) {
|
||||
return p.isBlocking() && (p.getInventory().getItemInMainHand().getType() == org.bukkit.Material.SHIELD || p.getInventory().getItemInOffHand().getType() == org.bukkit.Material.SHIELD);
|
||||
}
|
||||
|
||||
public void tickShield() {
|
||||
long now = System.currentTimeMillis();
|
||||
for (Player p : org.bukkit.Bukkit.getOnlinePlayers()) {
|
||||
if (hasHeldShield(p)) {
|
||||
long start = blockStart.getOrDefault(p.getUniqueId(), now);
|
||||
if (!blockStart.containsKey(p.getUniqueId())) blockStart.put(p.getUniqueId(), now);
|
||||
if (now - start >= 5000) {
|
||||
addTP(p, 25f, "SHIELD");
|
||||
blockStart.put(p.getUniqueId(), now);
|
||||
lastCombat.put(p.getUniqueId(), now);
|
||||
}
|
||||
} else if (!hasHeldShield(p)) {
|
||||
blockStart.remove(p.getUniqueId());
|
||||
}
|
||||
if (isOwner(p)) {
|
||||
float cur = getTP(p);
|
||||
if (cur < 250) {
|
||||
float next = Math.min(250, cur + 2f);
|
||||
tp.put(p.getUniqueId(), next);
|
||||
updateBar(p, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
tickBattle();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent e) { updateBar(e.getPlayer(), getTP(e.getPlayer())); }
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent e) { bars.remove(e.getPlayer().getUniqueId()); }
|
||||
|
||||
public void hideAll() {
|
||||
for (var e : new HashMap<>(bars).entrySet()) {
|
||||
var bar = e.getValue();
|
||||
var p = org.bukkit.Bukkit.getPlayer(e.getKey());
|
||||
if (p != null) bar.removeViewer(p);
|
||||
else {
|
||||
for (var pl : org.bukkit.Bukkit.getOnlinePlayers()) bar.removeViewer(pl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerDeathEvent e) {
|
||||
Player p = e.getEntity();
|
||||
tp.put(p.getUniqueId(), 0f);
|
||||
BossBar bar = bars.get(p.getUniqueId());
|
||||
if (bar != null) { bar.progress(0); bar.name(Component.text("TP 0%")); bar.removeViewer(p); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user