Skip to content

Commit 7cf381c

Browse files
committed
Refactoring
1 parent d5d2087 commit 7cf381c

File tree

13 files changed

+223
-187
lines changed

13 files changed

+223
-187
lines changed

src/main/java/io/luna/game/event/impl/PlayerClickEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io.luna.game.model.Locatable;
55
import io.luna.game.model.Position;
66
import io.luna.game.model.mob.Player;
7-
import io.luna.game.model.mob.PlayerInteraction;
7+
import io.luna.game.model.mob.PlayerContextMenuOption;
88
import io.luna.game.model.mob.bot.Bot;
99
import io.luna.game.model.mob.bot.injection.InjectableEvent;
1010

@@ -62,7 +62,7 @@ public PlayerThirdClickEvent(Player player, Player targetPlr) {
6262
@Override
6363
public int distance() {
6464
// todo better way of doing this
65-
return plr.getInteractions().contains(PlayerInteraction.FOLLOW) ? Position.VIEWING_DISTANCE :
65+
return plr.getContextMenu().contains(PlayerContextMenuOption.FOLLOW) ? Position.VIEWING_DISTANCE :
6666
super.distance();
6767
}
6868
}

src/main/java/io/luna/game/model/mob/Player.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ public class Player extends Mob {
191191
private final Set<Long> ignores = new LinkedHashSet<>();
192192

193193
/**
194-
* The interaction menu state (right-click options, custom actions).
194+
* The context menu state (right-click options, custom actions).
195195
*/
196-
private final PlayerInteractionMenu interactions = new PlayerInteractionMenu(this);
196+
private final PlayerContextMenu contextMenu = new PlayerContextMenu(this);
197197

198198
/**
199199
* The hashed password. May be {@code null} for legacy/plaintext accounts until conversion.
@@ -770,7 +770,7 @@ public void playSound(Sounds sound, int delayTicks) {
770770
*
771771
* @return A {@link JsonArray} representing all active potion timers.
772772
*/
773-
public JsonArray savePotionsToJson() { // todo left off here, needs testing tomorrow
773+
public JsonArray savePotionsToJson() {
774774
JsonArray array = new JsonArray();
775775
for (var timer : actions.getAll(PotionCountdownTimer.class)) {
776776
array.add(timer.saveJson());
@@ -1196,12 +1196,12 @@ public Set<Long> getIgnores() {
11961196
}
11971197

11981198
/**
1199-
* Returns this player's interaction menu.
1199+
* Returns this player's context menu.
12001200
*
1201-
* @return The {@link PlayerInteractionMenu}.
1201+
* @return The {@link PlayerContextMenu}.
12021202
*/
1203-
public PlayerInteractionMenu getInteractions() {
1204-
return interactions;
1203+
public PlayerContextMenu getContextMenu() {
1204+
return contextMenu;
12051205
}
12061206

12071207
/**
@@ -1362,9 +1362,8 @@ public Instant getCreatedAt() {
13621362
* Returns the total amount of time this account has been played.
13631363
*
13641364
* <p>
1365-
* This method updates {@link #timePlayed} by adding the elapsed {@link #timeOnline} duration
1366-
* since the last call, then resets and restarts the stopwatch. Avoid calling this too frequently
1367-
* if you need a stable snapshot of playtime.
1365+
* This method updates {@link #timePlayed} by adding the elapsed {@link #timeOnline} duration since the last call,
1366+
* then resets and restarts the stopwatch. Avoid calling this too frequently if you need a stable snapshot of playtime.
13681367
* </p>
13691368
*
13701369
* @return The accumulated time played.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.luna.game.model.mob;
2+
3+
import com.google.common.collect.Iterators;
4+
import com.google.common.collect.UnmodifiableIterator;
5+
6+
import java.util.Arrays;
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
10+
/**
11+
* Maintains the set of right-click context menu options displayed for a {@link Player}.
12+
*
13+
* @author lare96
14+
*/
15+
public final class PlayerContextMenu implements Iterable<PlayerContextMenuOption> {
16+
17+
/**
18+
* The maximum number of context menu option slots supported by this menu.
19+
*/
20+
private static final int SIZE = 5;
21+
22+
/**
23+
* The menu option slots.
24+
* <p>
25+
* The slot used for an option is determined by {@link PlayerContextMenuOption#getIndex()} (1-based), mapped to
26+
* this array using {@code index - 1}. Slots are {@code null} when no option is displayed.
27+
* </p>
28+
*/
29+
private final PlayerContextMenuOption[] options = new PlayerContextMenuOption[SIZE];
30+
31+
/**
32+
* The player that owns this context menu.
33+
* <p>
34+
* Menu updates (show/hide) are sent by queueing messages onto this player.
35+
* </p>
36+
*/
37+
private final Player player;
38+
39+
/**
40+
* Creates a new {@link PlayerContextMenu} for {@code player}.
41+
*
42+
* @param player The owning player.
43+
*/
44+
public PlayerContextMenu(Player player) {
45+
this.player = player;
46+
}
47+
48+
/**
49+
* Returns an unmodifiable iterator over the backing option slots. This iterator includes {@code null} elements
50+
* for empty slots.
51+
*
52+
* @return An iterator over the raw option slots.
53+
*/
54+
@Override
55+
public UnmodifiableIterator<PlayerContextMenuOption> iterator() {
56+
return Iterators.forArray(options);
57+
}
58+
59+
/**
60+
* Retrieves an option in this menu with the specified {@code name}.
61+
* <p>
62+
* Name matching is exact and case-sensitive, using {@link String#equals(Object)}.
63+
* </p>
64+
*
65+
* @param name The option name to match.
66+
* @return The matching menu option (if present), otherwise {@link Optional#empty()}.
67+
*/
68+
public Optional<PlayerContextMenuOption> findOptionWithName(String name) {
69+
return Arrays.stream(options)
70+
.filter(Objects::nonNull)
71+
.filter(interaction -> interaction.getName().equals(name))
72+
.findAny();
73+
}
74+
75+
/**
76+
* Determines if {@code option} is currently displayed in this menu.
77+
* <p>
78+
* This checks the slot implied by {@link PlayerContextMenuOption#getIndex()} and compares the stored value
79+
* to {@code option} using {@link Objects#equals(Object, Object)}.
80+
* </p>
81+
*
82+
* @param option The option to check.
83+
* @return {@code true} if this menu currently contains {@code option} in its indexed slot.
84+
* @throws NullPointerException if {@code option} is {@code null}.
85+
* @throws ArrayIndexOutOfBoundsException if {@code option.getIndex()} is not in {@code [1, SIZE]}.
86+
*/
87+
public boolean contains(PlayerContextMenuOption option) {
88+
return Objects.equals(options[getIndex(option)], option);
89+
}
90+
91+
/**
92+
* Displays {@code option} in its configured slot and queues the corresponding “show” message to the client.
93+
* <p>
94+
* If the slot already contains an option, it will be overwritten.
95+
* </p>
96+
*
97+
* @param option The option to display.
98+
* @throws NullPointerException if {@code option} is {@code null}.
99+
* @throws ArrayIndexOutOfBoundsException if {@code option.getIndex()} is not in {@code [1, SIZE]}.
100+
*/
101+
public void show(PlayerContextMenuOption option) {
102+
options[getIndex(option)] = option;
103+
player.queue(option.toMessage(false));
104+
}
105+
106+
/**
107+
* Hides {@code option} from its configured slot and queues the corresponding “hide” message to the client.
108+
* <p>
109+
* This method clears the slot implied by {@link PlayerContextMenuOption#getIndex()}, regardless of what option
110+
* is currently in that slot.
111+
* </p>
112+
*
113+
* @param option The option to hide.
114+
* @throws NullPointerException if {@code interaction} is {@code null}.
115+
* @throws ArrayIndexOutOfBoundsException if {@code option.getIndex()} is not in {@code [1, SIZE]}.
116+
*/
117+
public void hide(PlayerContextMenuOption option) {
118+
options[getIndex(option)] = null;
119+
player.queue(option.toMessage(true));
120+
}
121+
122+
/**
123+
* Converts an option's 1-based menu index ({@link PlayerContextMenuOption#getIndex()}) into the 0-based array
124+
* index used by {@link #options}.
125+
*
126+
* @param option The menu option (1-based index).
127+
* @return The internal 0-based array index.
128+
* @throws NullPointerException if {@code option} is {@code null}.
129+
*/
130+
private int getIndex(PlayerContextMenuOption option) {
131+
return option.getIndex() - 1;
132+
}
133+
}

src/main/java/io/luna/game/model/mob/PlayerInteraction.java renamed to src/main/java/io/luna/game/model/mob/PlayerContextMenuOption.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
package io.luna.game.model.mob;
22

3-
import io.luna.net.msg.out.PlayerInteractionMessageWriter;
3+
import io.luna.net.msg.out.ContextMenuOptionMessageWriter;
44

55
import java.util.Objects;
66

77
import static com.google.common.base.Preconditions.checkArgument;
88

9-
/**
10-
* A model representing an interaction option on a {@link PlayerInteractionMenu}. Interactions with
9+
/**
10+
* A model representing a context menu option on a {@link PlayerContextMenu}. Options with
1111
* the name "Attack" are <strong>always</strong> pinned as long as the combat level of the Player interacting
1212
* is higher than the Player being interacted with.
1313
*
1414
* @author lare96
1515
*/
16-
public final class PlayerInteraction {
16+
public final class PlayerContextMenuOption {
1717

1818
/**
19-
* The "Attack" interaction. This interaction will be pinned.
19+
* The "Attack" menu option. This interaction will be pinned.
2020
*/
21-
public static final PlayerInteraction ATTACK = new PlayerInteraction(1, "Attack", true);
21+
public static final PlayerContextMenuOption ATTACK = new PlayerContextMenuOption(1, "Attack", true);
2222

2323
/**
24-
* The "Challenge" interaction. This interaction will be pinned.
24+
* The "Challenge" menu option. This interaction will be pinned.
2525
*/
26-
public static final PlayerInteraction CHALLENGE = new PlayerInteraction(1, "Challenge", true);
26+
public static final PlayerContextMenuOption CHALLENGE = new PlayerContextMenuOption(1, "Challenge", true);
2727

2828
/**
29-
* The "Follow" interaction.
29+
* The "Follow" menu option.
3030
*/
31-
public static final PlayerInteraction FOLLOW = new PlayerInteraction(3, "Follow", false);
31+
public static final PlayerContextMenuOption FOLLOW = new PlayerContextMenuOption(3, "Follow", false);
3232

3333
/**
34-
* The "Trade" interaction.
34+
* The "Trade" menu option.
3535
*/
36-
public static final PlayerInteraction TRADE = new PlayerInteraction(4, "Trade with", false);
36+
public static final PlayerContextMenuOption TRADE = new PlayerContextMenuOption(4, "Trade with", false);
3737

3838
/**
3939
* The index.
@@ -51,13 +51,13 @@ public final class PlayerInteraction {
5151
private final boolean pinned;
5252

5353
/**
54-
* Creates a new {@link PlayerInteraction}.
54+
* Creates a new {@link PlayerContextMenuOption}.
5555
*
5656
* @param index The index.
5757
* @param name The name.
58-
* @param pinned If this should be the topmost interaction.
58+
* @param pinned If this should be the topmost menu option.
5959
*/
60-
public PlayerInteraction(int index, String name, boolean pinned) {
60+
public PlayerContextMenuOption(int index, String name, boolean pinned) {
6161
checkArgument(index >= 1 && index <= 5, "Index must be >= 1 and <= 5.");
6262
checkArgument(name != null, "Name cannot be null. Use \"null\" instead.");
6363
this.index = index;
@@ -75,8 +75,8 @@ public boolean equals(Object obj) {
7575
if (this == obj) {
7676
return true;
7777
}
78-
if (obj instanceof PlayerInteraction) {
79-
PlayerInteraction other = (PlayerInteraction) obj;
78+
if (obj instanceof PlayerContextMenuOption) {
79+
PlayerContextMenuOption other = (PlayerContextMenuOption) obj;
8080
return index == other.index &&
8181
name.equals(other.name) &&
8282
pinned == other.pinned;
@@ -85,14 +85,14 @@ public boolean equals(Object obj) {
8585
}
8686

8787
/**
88-
* Converts this interaction into a {@link PlayerInteractionMessageWriter}.
88+
* Converts this menu option into a {@link ContextMenuOptionMessageWriter}.
8989
*
90-
* @param remove If this interaction is being removed.
90+
* @param remove If this menu option is being removed.
9191
* @return The converted game message.
9292
*/
93-
PlayerInteractionMessageWriter toMessage(boolean remove) {
94-
PlayerInteraction interaction = remove ? new PlayerInteraction(index, "null", pinned) : this;
95-
return new PlayerInteractionMessageWriter(interaction);
93+
ContextMenuOptionMessageWriter toMessage(boolean remove) {
94+
PlayerContextMenuOption interaction = remove ? new PlayerContextMenuOption(index, "null", pinned) : this;
95+
return new ContextMenuOptionMessageWriter(interaction);
9696
}
9797

9898
/**
@@ -110,7 +110,7 @@ public String getName() {
110110
}
111111

112112
/**
113-
* @return If this should be the topmost interaction.
113+
* @return If this should be the topmost menu option.
114114
*/
115115
public boolean isPinned() {
116116
return pinned;

0 commit comments

Comments
 (0)