|
7 | 7 | */ |
8 | 8 | package net.wurstclient.hacks; |
9 | 9 |
|
| 10 | +import java.util.LinkedHashSet; |
| 11 | +import java.util.Set; |
| 12 | + |
10 | 13 | import net.wurstclient.Category; |
11 | 14 | import net.wurstclient.DontBlock; |
12 | 15 | import net.wurstclient.SearchTags; |
13 | | -import net.wurstclient.events.UpdateListener; |
14 | 16 | import net.wurstclient.hack.Hack; |
| 17 | +import net.wurstclient.hack.HackList; |
| 18 | +import net.wurstclient.settings.ButtonSetting; |
| 19 | +import net.wurstclient.util.ChatUtils; |
| 20 | +import net.wurstclient.util.text.WText; |
15 | 21 |
|
16 | 22 | @SearchTags({"legit", "disable"}) |
17 | 23 | @DontBlock |
18 | | -public final class PanicHack extends Hack implements UpdateListener |
| 24 | +public final class PanicHack extends Hack |
19 | 25 | { |
| 26 | + private final Set<String> savedHackNames = new LinkedHashSet<>(); |
| 27 | + |
| 28 | + private final ButtonSetting restoreButton = new ButtonSetting( |
| 29 | + "Restore saved hacks", |
| 30 | + WText.literal( |
| 31 | + "Re-enables the hacks that were active when Panic was triggered."), |
| 32 | + this::restoreSavedHacks); |
| 33 | + |
20 | 34 | public PanicHack() |
21 | 35 | { |
22 | 36 | super("Panic"); |
23 | 37 | setCategory(Category.OTHER); |
| 38 | + addSetting(restoreButton); |
| 39 | + addPossibleKeybind("panic restore", "Restore hacks saved by Panic"); |
24 | 40 | } |
25 | 41 |
|
26 | 42 | @Override |
27 | 43 | protected void onEnable() |
28 | 44 | { |
29 | | - EVENTS.add(UpdateListener.class, this); |
| 45 | + int saved = snapshotEnabledHacks(); |
| 46 | + disableOtherHacks(); |
| 47 | + setEnabled(false); |
| 48 | + |
| 49 | + if(saved > 0) |
| 50 | + ChatUtils |
| 51 | + .message("Disabled " + saved + " hack" + (saved == 1 ? "" : "s") |
| 52 | + + ". Use \"Restore saved hacks\" to re-enable them."); |
| 53 | + else |
| 54 | + ChatUtils.message("No other hacks were enabled."); |
30 | 55 | } |
31 | 56 |
|
32 | | - @Override |
33 | | - protected void onDisable() |
| 57 | + private int snapshotEnabledHacks() |
34 | 58 | { |
35 | | - EVENTS.remove(UpdateListener.class, this); |
| 59 | + savedHackNames.clear(); |
| 60 | + |
| 61 | + for(Hack hack : WURST.getHax().getAllHax()) |
| 62 | + if(hack.isEnabled() && hack != this) |
| 63 | + savedHackNames.add(hack.getName()); |
| 64 | + |
| 65 | + return savedHackNames.size(); |
36 | 66 | } |
37 | 67 |
|
38 | | - @Override |
39 | | - public void onUpdate() |
| 68 | + private void disableOtherHacks() |
40 | 69 | { |
41 | 70 | for(Hack hack : WURST.getHax().getAllHax()) |
42 | 71 | if(hack.isEnabled() && hack != this) |
43 | 72 | hack.setEnabled(false); |
| 73 | + } |
| 74 | + |
| 75 | + public void restoreSavedHacks() |
| 76 | + { |
| 77 | + if(savedHackNames.isEmpty()) |
| 78 | + { |
| 79 | + ChatUtils.error("There is no saved Panic state to restore."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + HackList hax = WURST.getHax(); |
| 84 | + Set<String> missing = new LinkedHashSet<>(); |
| 85 | + int restored = 0; |
| 86 | + |
| 87 | + for(String name : savedHackNames) |
| 88 | + { |
| 89 | + Hack hack = hax.getHackByName(name); |
| 90 | + if(hack == null || hack == this) |
| 91 | + { |
| 92 | + missing.add(name); |
| 93 | + continue; |
| 94 | + } |
44 | 95 |
|
45 | | - setEnabled(false); |
| 96 | + boolean wasEnabled = hack.isEnabled(); |
| 97 | + hack.setEnabled(true); |
| 98 | + if(!wasEnabled && hack.isEnabled()) |
| 99 | + restored++; |
| 100 | + } |
| 101 | + |
| 102 | + if(!missing.isEmpty()) |
| 103 | + savedHackNames.removeAll(missing); |
| 104 | + |
| 105 | + if(restored > 0) |
| 106 | + ChatUtils.message("Restored " + restored + " hack" |
| 107 | + + (restored == 1 ? "" : "s") + " from Panic."); |
| 108 | + else |
| 109 | + ChatUtils.message( |
| 110 | + "All saved Panic hacks are already enabled or blocked."); |
46 | 111 | } |
47 | 112 | } |
0 commit comments