Skip to content

Commit d4eecc9

Browse files
committed
Anchor Entities are now automatically created when implementing the MenuBuilder to ensure non null values.
1 parent 913f366 commit d4eecc9

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed
Binary file not shown.
Binary file not shown.

src/main/java/dev/arctic/interactivemenuapi/builders/MenuBuilder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.arctic.interactivemenuapi.builders;
22

3-
import dev.arctic.interactivemenuapi.interfaces.IMenu;
43
import dev.arctic.interactivemenuapi.objects.Division;
54
import dev.arctic.interactivemenuapi.objects.Menu;
65
import org.bukkit.Location;
@@ -74,7 +73,22 @@ public MenuBuilder setDoCleanup(boolean doCleanup) {
7473
return this;
7574
}
7675

76+
// Method to create the anchor entity if not set explicitly
77+
private Interaction createAnchor(Vector spawnOffset) {
78+
if (rootLocation.getWorld() == null) return null;
79+
return rootLocation.getWorld().spawn(rootLocation.add(spawnOffset), Interaction.class, i -> {
80+
i.setInteractionWidth(0.1f);
81+
i.setInteractionHeight(0.1f);
82+
i.setResponsive(false);
83+
});
84+
}
85+
7786
public Menu build() {
87+
// Automatically create the anchor entity if it hasn't been set
88+
if (this.anchorEntity == null && this.rootLocation != null) {
89+
this.anchorEntity = createAnchor(new Vector(0, 0, 0));
90+
}
91+
7892
Menu menu = new Menu(rootLocation, timeoutSeconds, plugin);
7993
menu.setOwner(owner);
8094
menu.setAnchorEntity(anchorEntity);

src/main/java/dev/arctic/interactivemenuapi/interfaces/IMenu.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ public interface IMenu {
3131
*/
3232
void initializeMenu();
3333

34-
/**
35-
* Creates an anchor entity at the specified spawn offset.
36-
*
37-
* @param spawnOffset The vector offset for spawning the anchor entity.
38-
*/
39-
void createAnchor(Vector spawnOffset);
40-
4134
/**
4235
* Updates the locations of all divisions within this menu.
4336
*/

src/main/java/dev/arctic/interactivemenuapi/objects/Menu.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,15 @@ public Menu (Location rootLocation, int timeoutSeconds, Plugin plugin) {
4242
this.lastInteractionTime = System.currentTimeMillis() / 1000;
4343
this.doCleanup = true;
4444
this.plugin = plugin;
45+
this.anchorEntity = createAnchor(new Vector(0, 0, 0));
4546
initializeMenu();
4647
}
4748

4849
public void initializeMenu() {
49-
createAnchor(new Vector(0, 0, 0));
5050
startRunnableUpdateGUI();
5151
startCleanupTask();
5252
}
5353

54-
public void createAnchor(Vector spawnOffset) {
55-
if (rootLocation.getWorld() == null) return;
56-
this.anchorEntity = rootLocation.getWorld().spawn(rootLocation.add(spawnOffset), Interaction.class, interaction -> {
57-
interaction.setInteractionWidth(0.1f);
58-
interaction.setInteractionHeight(0.1f);
59-
interaction.setResponsive(false);
60-
});
61-
}
6254

6355
private boolean isTimeoutExceeded() {
6456
return (System.currentTimeMillis() / 1000 - lastInteractionTime) >= timeoutSeconds;
@@ -76,7 +68,7 @@ public void startRunnableUpdateGUI() {
7668
public void run() {
7769
updateMenuLocation();
7870
}
79-
}.runTaskTimer(plugin, 0L, 5L); // Update every 5 ticks
71+
}.runTaskTimerAsynchronously(plugin, 2L, 5L); // Update every 5 ticks
8072
}
8173

8274
public void startCleanupTask() {
@@ -89,7 +81,7 @@ public void run() {
8981
this.cancel();
9082
}
9183
}
92-
}.runTaskTimer(plugin, 0L, 20L); // Check every second
84+
}.runTaskTimerAsynchronously(plugin, 2L, 20L); // Check every second
9385
}
9486

9587
public void clearMenu() {

0 commit comments

Comments
 (0)