Skip to content

Commit

Permalink
Add tracker hotbar message
Browse files Browse the repository at this point in the history
  • Loading branch information
MetallicGoat committed Dec 29, 2024
1 parent 3e66eda commit a319e33
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void registerEvents() {
manager.registerEvents(new PlayerLimitBypass(), plugin);
manager.registerEvents(new RemoveInvisOnDamage(), plugin);
manager.registerEvents(new SpecialItemCooldown(), plugin);
manager.registerEvents(new TrackerDistance(), plugin);
//manager.registerEvents(new TieBreaker(), plugin);

// Spawners
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/me/metallicgoat/tweaksaddon/config/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,22 @@ private static void overrideMBedwarsConfigs() {
Console.printWarn("Failed to apply personal ender chests. Try updating MBedwars, or disabling \"personal-ender-chests-enabled\"");
}
}

if (MainConfig.tracker_hotbar_message_enabled) {
try {
final boolean mbedwarsActionbarEnabled = (boolean) ConfigurationAPI.get().getValue("actionbar-enabled");

if (mbedwarsActionbarEnabled) {
ConfigurationAPI.get().setValue("actionbar-enabled", false);
Console.printWarn(
"NOTE: You have \"tracker-hotbar-message-enabled\" enabled. This setting will interfere with the MBedwars's \"actionbar-enabled\" config.",
"We have automatically disabled the MBedwars actionbar, however you may want to disable it yourself in the MBedwars config.yml."
);
}

} catch (IllegalArgumentException e) {
Console.printWarn("Failed to apply tracker hotbar message. Try updating MBedwars, or disabling \"tracker-hotbar-message-enabled\"");
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ public class MainConfig {
@Config public static String tnt_ignite_timer_title = "&c{seconds}";


// ===== SPECIAL ITEMS
@SectionTitle(title = "SPECIAL ITEMS")

@Config(
description = {
"If enabled, trackers will display a message over the Hotbar when a player is being tracked",
}
)
public static boolean tracker_hotbar_message_enabled = true;
@Config public static String tracker_hotbar_message_no_enemies = "";
@Config public static String tracker_hotbar_message = "{team-color}{team}: &a{distance}m";


// ===== MESSAGES
@SectionTitle(title = "MESSAGES")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package me.metallicgoat.tweaksaddon.tweaks.misc;

import de.marcely.bedwars.api.GameAPI;
import de.marcely.bedwars.api.arena.Arena;
import de.marcely.bedwars.api.arena.ArenaStatus;
import de.marcely.bedwars.api.arena.Team;
import de.marcely.bedwars.api.event.arena.ArenaStatusChangeEvent;
import de.marcely.bedwars.api.event.arena.RoundStartEvent;
import de.marcely.bedwars.api.game.specialitem.SpecialItem;
import de.marcely.bedwars.api.game.specialitem.SpecialItemType;
import de.marcely.bedwars.api.message.Message;
import de.marcely.bedwars.tools.NMSHelper;
import de.marcely.bedwars.tools.Pair;
import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import me.metallicgoat.tweaksaddon.MBedwarsTweaksPlugin;
import me.metallicgoat.tweaksaddon.config.MainConfig;
import me.metallicgoat.tweaksaddon.utils.Util;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.Nullable;

public class TrackerDistance implements Listener {

private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.#");

private final List<Arena> tickingArenas = new CopyOnWriteArrayList<>();
private BukkitTask task = null;

@EventHandler
public void onRoundStart(RoundStartEvent event) {
if (!MainConfig.tracker_hotbar_message_enabled)
return;

tickingArenas.add(event.getArena());

if (task == null)
startTask();

}

// Stop checkin' when no ticking arenas
@EventHandler(priority = EventPriority.MONITOR)
public void onArenaStatusChange(ArenaStatusChangeEvent event) {
if (event.getArena().getStatus() == ArenaStatus.RUNNING)
return;

tickingArenas.remove(event.getArena());

if (tickingArenas.isEmpty() && task != null) {
task.cancel();
task = null;
}
}

private final void startTask() {
if (task != null)
task.cancel();

task = new BukkitRunnable() {
@Override
public void run() {
for (Arena arena : tickingArenas) {
for (Player player : arena.getPlayers()) {
final boolean holdingTracker = isTracker(player.getItemInHand()) || isTracker(Util.getItemInOffHand(player));

if (!holdingTracker)
continue;

final Team playerTeam = arena.getPlayerTeam(player);
final Pair<Player, Double> nearestEnemy = getNearestEnemy(arena, player, playerTeam);

if (nearestEnemy.getKey() == null) {
// No enemies found message
sendHotbarMessage(player, Message.build(MainConfig.tracker_hotbar_message_no_enemies));
return;
}

final Team enemyTeam = arena.getPlayerTeam(nearestEnemy.getKey());

if (enemyTeam == null) {
throw new RuntimeException("Player Tracker Error: Enemy team is null");
}

// Send distance message
sendHotbarMessage(player, Message.build(MainConfig.tracker_hotbar_message)
.placeholder("distance", DECIMAL_FORMAT.format(nearestEnemy.getValue()))
.placeholder("team-color", enemyTeam.getBukkitColor()) // TODO Bungee?
.placeholder("team", enemyTeam.getDisplayName(player)));
}
}
}
}.runTaskTimer(MBedwarsTweaksPlugin.getInstance(), 20L, 20L);
}

private void sendHotbarMessage(Player player, Message message) {
final String string = message.done(player);

if (string.isEmpty())
return;

NMSHelper.get().showActionbar(player, string);
}

private Pair<Player, Double> getNearestEnemy(Arena arena, Player player, Team playerTeam) {
Player nearest = null;
double nearestDistance = Double.MAX_VALUE;

for (Player possibleEnemy : arena.getPlayers()) {
if (playerTeam == arena.getPlayerTeam(possibleEnemy) || possibleEnemy.getGameMode() == GameMode.SPECTATOR)
continue;

final double distance = possibleEnemy.getLocation().distance(player.getLocation());

if (distance < nearestDistance) {
nearest = possibleEnemy;
nearestDistance = distance;
}
}

return new Pair<>(nearest, nearestDistance);
}

private boolean isTracker(@Nullable ItemStack itemStack) {
if (itemStack == null)
return false;

final SpecialItem item = GameAPI.get().getSpecialItem(itemStack);

return item != null && item.getType() == SpecialItemType.TRACKER;
}
}

0 comments on commit a319e33

Please sign in to comment.