| 1 | /*
|
| 2 | * This file is part of the Combatant Client distribution.
|
| 3 | * Copyright (c) 2026 pivosos2007.
|
| 4 | *
|
| 5 | * Licensed under the GNU General Public License v3.0.
|
| 6 | */
|
| 7 |
|
| 8 | package combatant.client.features.module.modules.combat;
|
| 9 |
|
| 10 | import combatant.client.config.values.*;
|
| 11 | import combatant.client.features.relations.PlayerRelations;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.world.item.Item;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import combatant.client.config.SettingDef;
|
| 18 | import combatant.client.config.values.*;
|
| 19 | import combatant.client.features.gui.clickgui.settings.TextListSetting;
|
| 20 | import combatant.client.features.module.Module;
|
| 21 | import combatant.client.features.module.ModuleCategory;
|
| 22 | import combatant.client.features.module.ModuleInfo;
|
| 23 | import combatant.client.util.pvp.ItemCooldownSnapshot;
|
| 24 | import combatant.client.util.pvp.PvpChatParser;
|
| 25 | import combatant.client.util.pvp.PvpOverlayParser;
|
| 26 | import combatant.client.util.pvp.PvpState;
|
| 27 | import combatant.client.util.pvp.client.CooldownsState;
|
| 28 |
|
| 29 | import java.util.*;
|
| 30 |
|
| 31 | /**
|
| 32 | * Handles PvP cooldown rendering and optional local rule synthesis.
|
| 33 | */
|
| 34 | //todo Description
|
| 35 | @ModuleInfo(
|
| 36 | id = "pvpcooldowns",
|
| 37 | displayName = "PvpCooldowns",
|
| 38 | category = ModuleCategory.MISC
|
| 39 | )
|
| 40 | public class PvpCooldowns extends Module {
|
| 41 |
|
| 42 | private static final long LOCAL_RULE_GRACE_MS = 10_000L;
|
| 43 |
|
| 44 | private final BooleanValue renderSlots = bool("pvpcd_render_slots", "render_slots", true);
|
| 45 | private final BooleanValue renderWidget = bool("pvpcd_widget", "show_in_hud", true);
|
| 46 | private final NumberValue<Integer> targetGlowTtlMs =
|
| 47 | num("pvpcd_target_glow_ttl_ms", "target_glow_ttl_ms", 1500, 0, 10000);
|
| 48 | private final BooleanValue hideTargetGlow = bool("pvpcd_hide_target_glow", "hide_target_glow", false);
|
| 49 | private final BooleanValue dynamicEnemyEnabled = bool("pvpcd_dynamic_enemy", "dynamic_enemy", false);
|
| 50 | private final NumberValue<Integer> dynamicEnemyTime = visibleWhen(
|
| 51 | num("pvpcd_dynamic_enemy_time", "dynamic_enemy_time", 60, 0, 600),
|
| 52 | dynamicEnemyEnabled::get
|
| 53 | );
|
| 54 | private final BooleanValue blockDisconnect = bool("pvpcd_block_disconnect", "block_disconnect", true);
|
| 55 |
|
| 56 | private final BooleanMapValue stateSources = group(
|
| 57 | "pvpcd_state_sources",
|
| 58 | "state_sources",
|
| 59 | defaultStateSources()
|
| 60 | );
|
| 61 |
|
| 62 | private final SetValue chatGivePatterns = visibleWhen(textList(
|
| 63 | "pvpcd_chat_give_patterns",
|
| 64 | "chat_give_patterns",
|
| 65 | TextListSetting.PickerMode.TEXT,
|
| 66 | PvpChatParser.defaultGivePatterns()
|
| 67 | ), () -> sourceEnabled("chat") || sourceEnabled("game_message"));
|
| 68 |
|
| 69 | private final SetValue chatReceivePatterns = visibleWhen(textList(
|
| 70 | "pvpcd_chat_receive_patterns",
|
| 71 | "chat_receive_patterns",
|
| 72 | TextListSetting.PickerMode.TEXT,
|
| 73 | PvpChatParser.defaultReceivePatterns()
|
| 74 | ), () -> sourceEnabled("chat") || sourceEnabled("game_message"));
|
| 75 |
|
| 76 | private final SetValue chatActivePatterns = visibleWhen(textList(
|
| 77 | "pvpcd_chat_active_patterns",
|
| 78 | "chat_active_patterns",
|
| 79 | TextListSetting.PickerMode.TEXT,
|
| 80 | PvpChatParser.defaultActivePatterns()
|
| 81 | ), () -> sourceEnabled("chat") || sourceEnabled("game_message"));
|
| 82 |
|
| 83 | private final SetValue chatExitPatterns = visibleWhen(textList(
|
| 84 | "pvpcd_chat_exit_patterns",
|
| 85 | "chat_exit_patterns",
|
| 86 | TextListSetting.PickerMode.TEXT,
|
| 87 | PvpChatParser.defaultExitPatterns()
|
| 88 | ), () -> sourceEnabled("chat") || sourceEnabled("game_message"));
|
| 89 |
|
| 90 | private final SetValue overlayActivePatterns = visibleWhen(textList(
|
| 91 | "pvpcd_overlay_active_patterns",
|
| 92 | "overlay_active_patterns",
|
| 93 | TextListSetting.PickerMode.TEXT,
|
| 94 | PvpOverlayParser.defaultActivePatterns()
|
| 95 | ), () -> sourceEnabled("overlay"));
|
| 96 |
|
| 97 | private final SetValue overlayExitPatterns = visibleWhen(textList(
|
| 98 | "pvpcd_overlay_exit_patterns",
|
| 99 | "overlay_exit_patterns",
|
| 100 | TextListSetting.PickerMode.TEXT,
|
| 101 | PvpOverlayParser.defaultExitPatterns()
|
| 102 | ), () -> sourceEnabled("overlay"));
|
| 103 |
|
| 104 | private final SetValue tabActivePatterns = visibleWhen(textList(
|
| 105 | "pvpcd_tab_active_patterns",
|
| 106 | "tab_active_patterns",
|
| 107 | TextListSetting.PickerMode.TEXT,
|
| 108 | PvpOverlayParser.defaultActivePatterns()
|
| 109 | ), () -> sourceEnabled("tab"));
|
| 110 |
|
| 111 | private final SetValue tabExitPatterns = visibleWhen(textList(
|
| 112 | "pvpcd_tab_exit_patterns",
|
| 113 | "tab_exit_patterns",
|
| 114 | TextListSetting.PickerMode.TEXT,
|
| 115 | PvpOverlayParser.defaultExitPatterns()
|
| 116 | ), () -> sourceEnabled("tab"));
|
| 117 |
|
| 118 | /**
|
| 119 | * Off by default. Server-provided vanilla item cooldown packets must remain the primary flow.
|
| 120 | * Enable only for servers that lock items without sending cooldown data.
|
| 121 | */
|
| 122 | private final BooleanValue localRulesEnabled = bool("pvpcd_local_rules", "local_rules", false);
|
| 123 |
|
| 124 | /**
|
| 125 | * Object-shaped per-item local cooldown rules. Rendered by CooldownRulesSetting.
|
| 126 | */
|
| 127 | private final ItemCooldownRulesValue itemRules = new ItemCooldownRulesValue(
|
| 128 | "pvpcd_item_rules",
|
| 129 | defaultItemRules()
|
| 130 | );
|
| 131 |
|
| 132 | {
|
| 133 | setting(SettingDef.cooldownRules("cooldown_rules", itemRules).visibleWhen(localRulesEnabled::get));
|
| 134 | }
|
| 135 |
|
| 136 | private static Map<String, Boolean> defaultStateSources() {
|
| 137 | Map<String, Boolean> defaults = new LinkedHashMap<>();
|
| 138 | defaults.put("chat", true);
|
| 139 | defaults.put("game_message", true);
|
| 140 | defaults.put("overlay", true);
|
| 141 | defaults.put("tab", true);
|
| 142 | return defaults;
|
| 143 | }
|
| 144 |
|
| 145 | private static Map<String, ItemCooldownRulesValue.Rule> defaultItemRules() {
|
| 146 | Map<String, ItemCooldownRulesValue.Rule> rules = new LinkedHashMap<>();
|
| 147 | rules.put("minecraft:golden_apple", new ItemCooldownRulesValue.Rule(
|
| 148 | false,
|
| 149 | 30,
|
| 150 | 2,
|
| 151 | 10,
|
| 152 | ItemCooldownRulesValue.Scope.PVP_GRACE,
|
| 153 | ItemCooldownRulesValue.Trigger.CONSUME_FINISH,
|
| 154 | false
|
| 155 | ));
|
| 156 | rules.put("minecraft:enchanted_golden_apple", new ItemCooldownRulesValue.Rule(
|
| 157 | false,
|
| 158 | 60,
|
| 159 | 2,
|
| 160 | 10,
|
| 161 | ItemCooldownRulesValue.Scope.PVP_GRACE,
|
| 162 | ItemCooldownRulesValue.Trigger.CONSUME_FINISH,
|
| 163 | false
|
| 164 | ));
|
| 165 | rules.put("minecraft:totem_of_undying", new ItemCooldownRulesValue.Rule(
|
| 166 | false,
|
| 167 | 0,
|
| 168 | 1,
|
| 169 | 0,
|
| 170 | ItemCooldownRulesValue.Scope.PVP_GRACE,
|
| 171 | ItemCooldownRulesValue.Trigger.TOTEM_POP,
|
| 172 | false
|
| 173 | ));
|
| 174 | return rules;
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public void onTick() {
|
| 179 | if (!isEnabled()) {
|
| 180 | CooldownsState.MANAGER.clear();
|
| 181 | }
|
| 182 | syncDynamicEnemySettings();
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void onDisable() {
|
| 187 | CooldownsState.MANAGER.clear();
|
| 188 | CooldownsState.PENDING.clear();
|
| 189 | syncDynamicEnemySettings();
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public void onEnable() {
|
| 194 | syncDynamicEnemySettings();
|
| 195 | }
|
| 196 |
|
| 197 | public boolean isSystemEnabled() {
|
| 198 | return isEnabled();
|
| 199 | }
|
| 200 |
|
| 201 | public boolean shouldRenderSlots() {
|
| 202 | return renderSlots.get();
|
| 203 | }
|
| 204 |
|
| 205 | /** Compatibility API for callers compiled against the previous signature. */
|
| 206 | public boolean shouldBlockUsage() {
|
| 207 | return false;
|
| 208 | }
|
| 209 |
|
| 210 | public boolean shouldRenderWidget() {
|
| 211 | return renderWidget.get();
|
| 212 | }
|
| 213 |
|
| 214 | public int getTargetGlowTtlMs() {
|
| 215 | return targetGlowTtlMs.get();
|
| 216 | }
|
| 217 |
|
| 218 | public boolean shouldHideTargetGlow() {
|
| 219 | return isEnabled() && hideTargetGlow.get();
|
| 220 | }
|
| 221 |
|
| 222 | public boolean isDynamicEnemyEnabled() {
|
| 223 | return dynamicEnemyEnabled.get();
|
| 224 | }
|
| 225 |
|
| 226 | public int getDynamicEnemySeconds() {
|
| 227 | return dynamicEnemyTime.get();
|
| 228 | }
|
| 229 |
|
| 230 | public boolean shouldBlockDisconnect() {
|
| 231 | return isEnabled() && blockDisconnect.get() && CooldownsState.MANAGER.isInPvp();
|
| 232 | }
|
| 233 |
|
| 234 | public long getDisconnectBlockRemainingMs() {
|
| 235 | if (!shouldBlockDisconnect()) {
|
| 236 | return 0L;
|
| 237 | }
|
| 238 | return PvpState.getPredictedRemainingMs();
|
| 239 | }
|
| 240 |
|
| 241 | public Component getDisconnectBlockTooltip() {
|
| 242 | long remainingMs = getDisconnectBlockRemainingMs();
|
| 243 | if (remainingMs <= 0L) {
|
| 244 | return Component.literal("PvP cooldown active");
|
| 245 | }
|
| 246 | double seconds = Math.ceil(remainingMs / 100.0) / 10.0;
|
| 247 | return Component.literal(String.format(Locale.ROOT, "PvP cooldown: %.1fs", seconds));
|
| 248 | }
|
| 249 |
|
| 250 | public boolean shouldBlockItemUse(Item item) {
|
| 251 | if (!isEnabled()) return false;
|
| 252 | if (item == null || item == Items.AIR) return false;
|
| 253 |
|
| 254 | Identifier id = BuiltInRegistries.ITEM.getKey(item);
|
| 255 | if (id == null) return false;
|
| 256 |
|
| 257 | if (!localRulesEnabled.get()) return false;
|
| 258 |
|
| 259 | ItemCooldownRulesValue.Rule rule = itemRules.getRule(id.toString());
|
| 260 | if (rule == null || !rule.enabled() || !scopeAllows(rule.scope())) return false;
|
| 261 |
|
| 262 | ItemCooldownRulesValue.UseBlockMode blockMode = rule.blockMode() != null
|
| 263 | ? rule.blockMode()
|
| 264 | : ItemCooldownRulesValue.UseBlockMode.NONE;
|
| 265 | if (blockMode == ItemCooldownRulesValue.UseBlockMode.NONE) return false;
|
| 266 |
|
| 267 | ItemCooldownSnapshot snapshot = CooldownsState.MANAGER.snapshot(item);
|
| 268 | return switch (blockMode) {
|
| 269 | case NONE -> false;
|
| 270 | case COOLDOWN -> snapshot.cooling();
|
| 271 | case WINDOW -> snapshot.useWindowActive();
|
| 272 | case ANY -> snapshot.visible();
|
| 273 | };
|
| 274 | }
|
| 275 |
|
| 276 | public boolean shouldReadChatMessages() {
|
| 277 | return isEnabled() && sourceEnabled("chat");
|
| 278 | }
|
| 279 |
|
| 280 | public boolean shouldReadGameMessages() {
|
| 281 | return isEnabled() && sourceEnabled("game_message");
|
| 282 | }
|
| 283 |
|
| 284 | public boolean shouldReadOverlayMessages() {
|
| 285 | return isEnabled() && sourceEnabled("overlay");
|
| 286 | }
|
| 287 |
|
| 288 | public boolean shouldReadTabText() {
|
| 289 | return isEnabled() && sourceEnabled("tab");
|
| 290 | }
|
| 291 |
|
| 292 | public Set<String> getChatGivePatterns() {
|
| 293 | return chatGivePatterns.get();
|
| 294 | }
|
| 295 |
|
| 296 | public Set<String> getChatReceivePatterns() {
|
| 297 | return chatReceivePatterns.get();
|
| 298 | }
|
| 299 |
|
| 300 | public Set<String> getChatActivePatterns() {
|
| 301 | return chatActivePatterns.get();
|
| 302 | }
|
| 303 |
|
| 304 | public Set<String> getChatExitPatterns() {
|
| 305 | return chatExitPatterns.get();
|
| 306 | }
|
| 307 |
|
| 308 | public Set<String> getOverlayActivePatterns() {
|
| 309 | return overlayActivePatterns.get();
|
| 310 | }
|
| 311 |
|
| 312 | public Set<String> getOverlayExitPatterns() {
|
| 313 | return overlayExitPatterns.get();
|
| 314 | }
|
| 315 |
|
| 316 | public Set<String> getTabActivePatterns() {
|
| 317 | return tabActivePatterns.get();
|
| 318 | }
|
| 319 |
|
| 320 | public Set<String> getTabExitPatterns() {
|
| 321 | return tabExitPatterns.get();
|
| 322 | }
|
| 323 |
|
| 324 | public int getLocalCooldownSeconds(Item item) {
|
| 325 | ItemCooldownRulesValue.Rule rule = configuredRuleFor(item);
|
| 326 | return rule != null ? rule.seconds() : 0;
|
| 327 | }
|
| 328 |
|
| 329 | public int getLocalRuleUses(Item item) {
|
| 330 | ItemCooldownRulesValue.Rule rule = configuredRuleFor(item);
|
| 331 | return rule != null ? rule.uses() : 1;
|
| 332 | }
|
| 333 |
|
| 334 | public int getLocalRuleWindowSeconds(Item item) {
|
| 335 | ItemCooldownRulesValue.Rule rule = configuredRuleFor(item);
|
| 336 | return rule != null ? rule.windowSeconds() : 0;
|
| 337 | }
|
| 338 |
|
| 339 | public ItemCooldownRulesValue.Rule getLocalRule(Item item) {
|
| 340 | return configuredRuleFor(item);
|
| 341 | }
|
| 342 |
|
| 343 | public Set<Item> getLocalCooldownItems() {
|
| 344 | Set<Item> out = new LinkedHashSet<>();
|
| 345 | if (!isEnabled() || !localRulesEnabled.get()) return out;
|
| 346 |
|
| 347 | for (var entry : itemRules.getRules().entrySet()) {
|
| 348 | ItemCooldownRulesValue.Rule rule = entry.getValue();
|
| 349 | if (rule == null || !rule.enabled()) continue;
|
| 350 | if (rule.seconds() <= 0 && rule.uses() <= 1) continue;
|
| 351 | try {
|
| 352 | Item item = BuiltInRegistries.ITEM.getValue(Identifier.parse(entry.getKey()));
|
| 353 | if (item != null && item != Items.AIR) {
|
| 354 | out.add(item);
|
| 355 | }
|
| 356 | } catch (RuntimeException ignored) {
|
| 357 | // Invalid custom item id in config: ignore this rule instead of breaking the module.
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | return out;
|
| 362 | }
|
| 363 |
|
| 364 | public boolean tryStartLocalCooldown(Item item, ItemCooldownRulesValue.Trigger trigger) {
|
| 365 | ItemCooldownRulesValue.Rule rule = configuredRuleFor(item);
|
| 366 | if (rule == null || rule.trigger() != trigger) return false;
|
| 367 | if (!scopeAllows(rule.scope())) return false;
|
| 368 | CooldownsState.MANAGER.recordRuleUse(item, rule);
|
| 369 | return true;
|
| 370 | }
|
| 371 |
|
| 372 | private ItemCooldownRulesValue.Rule configuredRuleFor(Item item) {
|
| 373 | if (!isEnabled() || !localRulesEnabled.get()) return null;
|
| 374 | if (item == null || item == Items.AIR) return null;
|
| 375 |
|
| 376 | Identifier id = BuiltInRegistries.ITEM.getKey(item);
|
| 377 | if (id == null) return null;
|
| 378 |
|
| 379 | ItemCooldownRulesValue.Rule rule = itemRules.getRule(id.toString());
|
| 380 | if (rule == null || !rule.enabled()) return null;
|
| 381 | if (rule.seconds() <= 0 && rule.uses() <= 1) return null;
|
| 382 | return rule;
|
| 383 | }
|
| 384 |
|
| 385 | private boolean scopeAllows(ItemCooldownRulesValue.Scope scope) {
|
| 386 | return switch (scope != null ? scope : ItemCooldownRulesValue.Scope.PVP_GRACE) {
|
| 387 | case ALWAYS -> true;
|
| 388 | case PVP_ONLY -> CooldownsState.MANAGER.isInPvp();
|
| 389 | case PVP_GRACE -> CooldownsState.MANAGER.isPvpGraceActive(LOCAL_RULE_GRACE_MS);
|
| 390 | };
|
| 391 | }
|
| 392 |
|
| 393 | private void syncDynamicEnemySettings() {
|
| 394 | PlayerRelations.setDynamicEnemyEnabled(
|
| 395 | isEnabled() && dynamicEnemyEnabled.get()
|
| 396 | );
|
| 397 | PlayerRelations.setDynamicEnemyDurationSeconds(
|
| 398 | dynamicEnemyTime.get()
|
| 399 | );
|
| 400 | }
|
| 401 |
|
| 402 | private boolean sourceEnabled(String key) {
|
| 403 | return stateSources.get(key);
|
| 404 | }
|
| 405 | }
|
| 406 |
|