RuneHive-Game
Loading...
Searching...
No Matches
PresetManager.java
Go to the documentation of this file.
1package com.runehive.content.preset;
2
3import com.runehive.Config;
4import com.runehive.content.achievement.AchievementHandler;
5import com.runehive.content.achievement.AchievementKey;
6import com.runehive.content.dialogue.DialogueFactory;
7import com.runehive.game.world.entity.combat.magic.Autocast;
8import com.runehive.game.world.entity.mob.UpdateFlag;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.game.world.entity.mob.player.PlayerRight;
11import com.runehive.game.world.entity.mob.prayer.Prayer;
12import com.runehive.game.world.entity.mob.prayer.PrayerBook;
13import com.runehive.game.world.entity.skill.Skill;
14import com.runehive.game.world.items.Item;
15import com.runehive.net.packet.out.SendItemOnInterface;
16import com.runehive.net.packet.out.SendMessage;
17import com.runehive.net.packet.out.SendString;
18
19import java.util.ArrayList;
20import java.util.List;
21
22/**
23 * Handles managing the preset system.
24 *
25 * @author Daniel
26 */
27public class PresetManager {
28
29 /** The total preset size. */
30 public static final int SIZE = 10;
31
32 /** The player instance. */
33 private final Player player;
34
35 /** The presets. */
36 public Preset[] preset = new Preset[SIZE];
37
38 /** The preset opening on death flag. */
39 public boolean deathOpen;
40
41 /** The automatically deposit items on activate flag. */
42 public boolean autoDeposit;
43
44 /** If the player is allowed all presets. */
45 private boolean permitted;
46
47 /** Constructs a new <code>PresetManager<code>. */
49 this.player = player;
50 }
51
52 /** Opens the preset to the last viewed slot. */
53 public void open() {
54 open(getSlot());
55 }
56
57 /** Opens the preset to a specific slot. */
58 public void open(int slot) {
59 if (valid(true)) {
61
62 if (!permitted) {
63 player.dialogueFactory.sendStatement("You need to be a donator for more preset slots!").execute();
64 return;
65 }
66
67 player.attributes.set("PRELOADING_SLOT_KEY", slot);
68 refresh();
69 player.interfaceManager.open(57_000);
70 }
71 }
72
73 /** Refreshes all the components on the interface. */
74 public void refresh() {
75 int slot = getSlot();
76 int slotId = 57049;
77 for (int i = 0; i < SIZE; i++) {
78 boolean locked = i >= PlayerRight.getPresetAmount(player);
79 String defaultName = locked ? "<col=F23030>Locked" : (preset[i] == null ? "---" : preset[i].getName());
80 String name = (i == slot ? "<col=ffffff>" : "<col=39BF5B>") + defaultName;
81 player.send(new SendString(name, slotId));
82 slotId += 4;
83 }
84
85 int attack = preset[slot] == null ? 1 : player.skills.get(Skill.ATTACK).getMaxLevel();
86 int strength = preset[slot] == null ? 1 : player.skills.get(Skill.STRENGTH).getMaxLevel();
87 int defence = preset[slot] == null ? 1 : player.skills.get(Skill.DEFENCE).getMaxLevel();
88 int hitpoints = preset[slot] == null ? 10 : player.skills.get(Skill.HITPOINTS).getMaxLevel();
89 int prayer = preset[slot] == null ? 1 : player.skills.get(Skill.PRAYER).getMaxLevel();
90 int ranged = preset[slot] == null ? 1 : player.skills.get(Skill.RANGED).getMaxLevel();
91 int magic = preset[slot] == null ? 1 : player.skills.get(Skill.MAGIC).getMaxLevel();
92 player.send(new SendString(attack, 57014));
93 player.send(new SendString(strength, 57015));
94 player.send(new SendString(defence, 57016));
95 player.send(new SendString(hitpoints, 57017));
96 player.send(new SendString(prayer, 57018));
97 player.send(new SendString(ranged, 57019));
98 player.send(new SendString(magic, 57020));
99 player.send(new SendString("Cmb Lvl: " + player.skills.getCombatLevel(), 57041));
100
101 boolean prayers = preset[slot] == null || preset[slot].getPrayer() == null;
102 player.send(new SendString("<col=99E823>" + (prayers ? "Not " : "") + "Set!", 57013));
103 player.send(new SendString("<col=BD23E8>" + (preset[slot] == null ? "Not Set!" : preset[slot].getSpellbook().getName()), 57011));
104 player.send(new SendString("Available " + getTaken() + "/" + PlayerRight.getPresetAmount(player), 57024));
105 player.send(new SendItemOnInterface(57023, preset[slot] == null ? new Item[0] : preset[slot].getInventory()));
106 player.send(new SendItemOnInterface(57022, preset[slot] == null ? new Item[0] : preset[slot].getEquipment()));
107 }
108
109 /** Handles naming the preset. */
110 public void name(String name) {
111 int slot = getSlot();
112
113 if (!permitted && slot >= 5) {
114 player.dialogueFactory.sendStatement("You need to be a donator for more preset slots!").execute();
115 return;
116 }
117
118 if (preset[slot] == null) {
119 preset[slot] = new Preset(name);
120 } else {
121 preset[slot].setName(name);
122 }
123 refresh();
124 player.send(new SendMessage("You have successfully titles preset #" + (slot + 1) + " to " + name + "."));
125 }
126
127 /** Handles uploading the preset content. */
128 public void upload() {
129 if (!valid(false))
130 return;
131
132 int slot = getSlot();
133
134 if (!permitted && slot >= 5) {
135 player.dialogueFactory.sendStatement("You need to be a donator for more preset slots!").execute();
136 return;
137 }
138
139 if (preset[slot] == null) {
140 player.dialogueFactory.sendStatement("Please name your preset before uploading your gear!").execute();
141 return;
142 }
143
144 player.dialogueFactory.sendOption("Upload preset", () -> {
145 PrayerBook prayerBook = new PrayerBook();
146 if (player.quickPrayers != null) {
147 prayerBook.only(player.quickPrayers.getEnabled().toArray(new Prayer[0]));
148 }
149 preset[slot] = new Preset(preset[slot].getName(), player.inventory.toArray(), player.equipment.getEquipment(), prayerBook, player.spellbook);
150 player.send(new SendMessage("You have successfully uploaded your preset."));
151 refresh();
153 player.dialogueFactory.clear();
154 }, "Nevermind", () -> player.dialogueFactory.clear()).execute();
155 }
156
157 /** Activates the preset. */
158 public void activate() {
159 if (!valid(false))
160 return;
161
162 int slot = getSlot();
163
164 if (preset[slot] == null) {
165 player.send(new SendMessage("You have nothing assigned to this preset!"));
166 return;
167 }
168
169 if (autoDeposit) {
170 player.bank.depositeInventory(false);
171 player.bank.depositeEquipment(false);
172 }
173
174 if (!player.inventory.isEmpty() || !player.equipment.isEmpty()) {
175 player.send(new SendMessage("Please deposit all items from your inventory and equipment."));
176 return;
177 }
178
179 player.updateFlags.add(UpdateFlag.APPEARANCE);
180
183
185 player.prayer.reset();
186 player.equipment.login();
187 player.inventory.refresh();
188 player.quickPrayers.only(preset[slot].getPrayer().getEnabled().toArray(new Prayer[0]));
189 player.spellbook = preset[slot].getSpellbook();
190 player.interfaceManager.setSidebar(Config.MAGIC_TAB, player.spellbook.getInterfaceId());
191 player.send(new SendMessage("<col=ff0000>" + preset[slot].getName() + "</col> has been activated."));
192
193 if (player.pvpInstance) {
194 player.playerAssistant.setValueIcon();
195 }
196 }
197
198 private void equipment(Preset preset) {
199 List<String> requirements = new ArrayList<>();
200 for (Item item : preset.getEquipment()) {
201 if (item == null)
202 continue;
203
204 if (!hasItemRequirements(player, item.getRequirements())) {
205 requirements.add(item.getName());
206 continue;
207 }
208
209 int tabSlot = player.bank.computeIndexForId(item.getId());
210 if (tabSlot <= -1)
211 continue;
212
213 int tab = player.bank.tabForSlot(tabSlot);
214 if (tab <= -1)
215 continue;
216
217 int amount = player.bank.get(tabSlot).getAmount();
218 if (amount <= 0)
219 continue;
220
221 if (item.getAmount() > amount) {
222 item = item.copy();
223 item.setAmount(amount);
224 }
225
226 if (player.bank.remove(item.unnoted(), tabSlot, false)) {
227 if (!player.bank.indexOccupied(tabSlot)) {
228 player.bank.changeTabAmount(tab, -1);
229 player.bank.shift();
230 }
231
232 player.equipment.manualWear(item.copy());
233 }
234 }
235
236 if (!requirements.isEmpty()) {
237 player.send(new SendMessage("<col=ff0000>Some items could not be equipped due to not having the skill requirements."));
238 }
239 }
240
241 private void inventory(Preset preset) {
242 Item[] inventory = preset.getInventory();
243 for (int index = 0; index < inventory.length; index++) {
244 Item item = inventory[index];
245
246 if (item == null)
247 continue;
248
249 int tabSlot = player.bank.computeIndexForId(item.getId());
250 if (tabSlot <= -1)
251 continue;
252
253 int tab = player.bank.tabForSlot(tabSlot);
254 if (tab <= -1)
255 continue;
256
257 int amount = player.bank.get(tabSlot).getAmount();
258 if (amount <= 0)
259 continue;
260
261 if (item.getAmount() > amount) {
262 item = item.copy();
263 item.setAmount(amount);
264 }
265
266 if (player.bank.remove(item.unnoted(), tabSlot, false)) {
267 if (!player.bank.indexOccupied(tabSlot)) {
268 player.bank.changeTabAmount(tab, -1);
269 player.bank.shift();
270 }
271
272 player.inventory.set(index, item.copy(), false);
273 }
274 }
275 }
276
277 /** Handles deleting the preset. */
278 public void delete() {
279 if (!valid(false))
280 return;
281 int slot = getSlot();
282 if (preset[slot] == null)
283 return;
284 player.dialogueFactory.sendOption("Delete " + preset[slot].getName() + " (<col=f44259>CANT BE UNDONE</col>)", () -> {
285 preset[slot] = null;
286 refresh();
287 player.send(new SendMessage("You have successfully cleared preset #" + (slot + 1) + "."));
288 player.dialogueFactory.clear();
289 }, "Nevermind", () -> player.dialogueFactory.clear()).execute();
290 }
291
292 /** Handles opening the settings menu for the preset. */
293 public void openSettings() {
294 DialogueFactory factory = player.dialogueFactory;
295 factory.sendOption("View current settings", () -> {
296 factory.onAction(() -> {
297 factory.sendInformationBox("<col=3E74B8>Preset Settings", "</col>Open on death: " + (deathOpen ? "<col=0E8716>Enabled" : "<col=F01818>Disabled"), "</col>Automatic deposit: " + (autoDeposit ? "<col=0E8716>Enabled" : "<col=F01818>Disabled")).execute();
298 });
299 }, "Toggle open on death", () -> factory.onAction(() -> {
301 factory.sendStatement("The preset interface will now " + (deathOpen ? "" : "<col=F01818>not</col> ") + "open on death", "automatically.").execute();
302 }), "Toggle automatic deposit", () -> {
303 factory.onAction(() -> {
305 factory.sendStatement("Your items will now " + (autoDeposit ? "" : "<col=F01818>not</col> ") + "automatically deposit on activation.").execute();
306 });
307 }, "Nevermind", () -> factory.onAction(factory::clear)).execute();
308 }
309
310 /** Checks if the preset is valid. */
311 private boolean valid(boolean open) {
312 if (!open && !player.interfaceManager.isInterfaceOpen(57_000))
313 return false;
314 if (player.getCombat().inCombat()) {
315 player.send(new SendMessage("You can not do this while in combat!"));
316 return false;
317 }
318
319 return !player.positionChange;
320 }
321
322 /** Gets the taken preset amount. */
323 private int getTaken() {
324 int taken = SIZE;
325 for (int i = 0; i < SIZE; i++) {
326 if (preset[i] != null || i >= PlayerRight.getPresetAmount(player)) {
327 taken--;
328 }
329 }
330 return taken;
331 }
332
333 /** Gets the current slot of the preset. */
334 private int getSlot() {
335 return player.attributes.get("PRELOADING_SLOT_KEY", Integer.class);
336 }
337
338 private boolean hasItemRequirements(Player player, int[] requirements) {
339 for (int i = 0; i < requirements.length; i++) {
340 int level = player.skills.getMaxLevel(i);
341 int required = requirements[i];
342
343 if (level < required) {
344 return false;
345 }
346 }
347 return true;
348 }
349}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int MAGIC_TAB
Definition Config.java:194
static void activate(Player player, AchievementKey achievement)
Activates the achievement for the individual player.
Represents a factory class that contains important functions for building dialogues.
final DialogueFactory execute()
Retrieves the next dialogue in the chain and executes it.
final DialogueFactory onAction(Runnable action)
Sets an action so this action can be executed after dialogues are done.
final DialogueFactory sendInformationBox(String title, String...lines)
final DialogueFactory sendStatement(String... lines)
Appends a StatementDialogue to the current dialogue chain.
final DialogueFactory sendOption(String option1, Runnable action1, String option2, Runnable action2)
Appends the OptionDialogue onto the current dialogue chain.
void refresh()
Refreshes all the components on the interface.
void delete()
Handles deleting the preset.
PresetManager(Player player)
Constructs a new PresetManager.
void openSettings()
Handles opening the settings menu for the preset.
boolean permitted
If the player is allowed all presets.
void open(int slot)
Opens the preset to a specific slot.
final Player player
The player instance.
void upload()
Handles uploading the preset content.
int getSlot()
Gets the current slot of the preset.
boolean autoDeposit
The automatically deposit items on activate flag.
boolean deathOpen
The preset opening on death flag.
void name(String name)
Handles naming the preset.
boolean hasItemRequirements(Player player, int[] requirements)
static final int SIZE
The total preset size.
void open()
Opens the preset to the last viewed slot.
boolean valid(boolean open)
Checks if the preset is valid.
int getTaken()
Gets the taken preset amount.
This class represents a character controlled by a player.
Definition Player.java:125
void only(Prayer... prayers)
Activates only the prayers provided.
Represents a trainable and usable skill.
Definition Skill.java:18
static final int PRAYER
The prayer skill id.
Definition Skill.java:36
static final int RANGED
The ranged skill id.
Definition Skill.java:33
static final int DEFENCE
The defence skill id.
Definition Skill.java:24
static final int MAGIC
The magic skill id.
Definition Skill.java:39
static final int ATTACK
The attack skill id.
Definition Skill.java:21
static final int STRENGTH
The strength skill id.
Definition Skill.java:27
static final int HITPOINTS
The hitpoints skill id.
Definition Skill.java:30
The container class that represents an item that can be interacted with.
Definition Item.java:21
final void setAmount(int amount)
Sets the quantity of this item.
Definition Item.java:351
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
Item copy()
A substitute for Object#clone() that creates another 'copy' of this instance.
Definition Item.java:270
Item unnoted()
Gets the unnoted item.
Definition Item.java:78
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.