Files
Project-CrankedPvP/Factions/src/main/java/com/massivecraft/factions/cmd/CmdRally.java
T
2026-10-12 13:57:05 -03:00

106 lines
4.3 KiB
Java

package com.massivecraft.factions.cmd;
import com.lunarclient.bukkitapi.LunarClientAPI;
import com.lunarclient.bukkitapi.object.LCWaypoint;
import com.massivecraft.factions.struct.RallyWaypoint;
import com.massivecraft.factions.util.CC;
import com.massivecraft.factions.util.TL;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
public class CmdRally extends FCommand {
private final Map<Player, Long> cooldownMap;
private final Map<Player, RallyWaypoint> rallyWaypointMap;
public CmdRally() {
super();
this.aliases.add("rally");
this.cooldownMap = new HashMap<>();
this.rallyWaypointMap = new HashMap<>();
}
private boolean isOnCooldown(Player player) {
if(!this.cooldownMap.containsKey(player)) return false;
return (cooldownMap.get(player) + 7000) > System.currentTimeMillis();
}
private String getFormattedCooldown(Player player) {
if(!isOnCooldown(player)) return "error";
return (((cooldownMap.get(player) + 7000) - System.currentTimeMillis()) / 1000) + "s";
}
@Override
public void perform(CommandContext context) {
CommandSender commandSender = context.sender;
if(!(commandSender instanceof Player)) return;
Player player = (Player)commandSender;
if(!context.faction.isNormal()) {
commandSender.sendMessage(CC.translate("&cYou must be in a faction to use /f rally."));
player.playSound(player.getLocation(), "ANVIL_LAND", 2.0f, 2.0f);
return;
}
/*
if(!LunarClientAPI.getInstance().isRunningLunarClient(player)) {
player.sendMessage(CC.translate("&cYou must be running lunar client to use f rally!"));
player.playSound(player.getLocation(), "ANVIL_LAND", 2.0f, 2.0f);
return;
} */
if(isOnCooldown(player)) {
player.sendMessage(CC.translate("&cYou cannot use /f rally for " + getFormattedCooldown(player)));
player.playSound(player.getLocation(), "ANVIL_LAND", 2.0f, 2.0f);
return;
}
if(this.rallyWaypointMap.containsKey(player)) {
RallyWaypoint rallyWaypoint = this.rallyWaypointMap.get(player);
for(Player whoSee : rallyWaypoint.getPlayersSentTo()) {
if(!whoSee.isOnline()) continue;
if(!LunarClientAPI.getInstance().isRunningLunarClient(whoSee)) continue;
LunarClientAPI.getInstance().removeWaypoint(whoSee, rallyWaypoint.getLcWaypoint());
}
}
this.cooldownMap.put(player, System.currentTimeMillis());
LCWaypoint waypoint = new LCWaypoint(player.getName() + "'s Rally", player.getLocation(), 20, true);
RallyWaypoint rallyWaypoint = new RallyWaypoint(waypoint, context.faction.getOnlinePlayers());
this.removeAfterThirtySeconds(rallyWaypoint);
this.rallyWaypointMap.put(player, rallyWaypoint);
for(Player loopPlayer : context.faction.getOnlinePlayers()) {
String message = (TL.RALLY_COORDS_MESSAGE.toString()).replace("%player%", player.getName())
.replace("%x%", String.valueOf(waypoint.getX()))
.replace("%y%", String.valueOf(waypoint.getY()))
.replace("%z%", String.valueOf(waypoint.getZ()))
.replace("%world%", String.valueOf(waypoint.getWorld()))
;
loopPlayer.sendMessage(CC.translate(message));
if(!LunarClientAPI.getInstance().isRunningLunarClient(loopPlayer)) continue;
loopPlayer.playSound(loopPlayer.getLocation(), "CHICKEN_EGG_POP", 2.0f, 2.0f);
System.out.println("send waypoint to " + loopPlayer);
LunarClientAPI.getInstance().sendWaypoint(loopPlayer, waypoint);
}
}
private void removeAfterThirtySeconds(RallyWaypoint rallyWaypoint) {
super.plugin.getServer().getScheduler().runTaskLater(super.plugin, () -> {
for(Player playersSentTo : rallyWaypoint.getPlayersSentTo()) {
LunarClientAPI.getInstance().removeWaypoint(playersSentTo, rallyWaypoint.getLcWaypoint());
}
}, 30 * 20L);
}
@Override
public TL getUsageTranslation() {
return TL.RALLY_DESCRIPTION;
}
}