Skip to content

Commit 9c8ed09

Browse files
authored
Lately fetch update job initiator (#163)
1 parent e8cd2b4 commit 9c8ed09

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

bukkit-api/src/main/java/me/saiintbrisson/minecraft/BukkitViewComponentFactory.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.bukkit.inventory.Inventory;
1515
import org.bukkit.inventory.InventoryHolder;
1616
import org.bukkit.inventory.ItemStack;
17-
import org.bukkit.plugin.Plugin;
1817
import org.jetbrains.annotations.NotNull;
1918
import org.jetbrains.annotations.Nullable;
2019

@@ -155,15 +154,8 @@ public Object createItem(@Nullable Object stack) {
155154
updateJob.cancel();
156155
}
157156

158-
final PlatformViewFrame<?, ?, ?> vf = findViewFrame(view);
159-
if (vf == null)
160-
throw new IllegalStateException(
161-
"Cannot schedule view update because there's no initiator to do this");
162-
163157
view.setUpdateJob(
164-
(updateJob =
165-
new BukkitViewUpdateJobImpl(
166-
(Plugin) vf.getOwner(), view, delayInTicks, intervalInTicks)));
158+
(updateJob = new BukkitViewUpdateJobImpl(view, delayInTicks, intervalInTicks)));
167159
return updateJob;
168160
}
169161

bukkit-api/src/main/java/me/saiintbrisson/minecraft/BukkitViewUpdateJobImpl.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/** Object used to store information about the state of a View's update interval. */
99
final class BukkitViewUpdateJobImpl implements ViewUpdateJob, Runnable {
1010

11-
private final Plugin plugin;
1211
private final VirtualView view;
1312

1413
@Getter private final long delay;
@@ -18,9 +17,7 @@ final class BukkitViewUpdateJobImpl implements ViewUpdateJob, Runnable {
1817
private boolean interrupted;
1918
private boolean started;
2019

21-
public BukkitViewUpdateJobImpl(
22-
@NotNull Plugin plugin, @NotNull VirtualView view, long delay, long interval) {
23-
this.plugin = plugin;
20+
public BukkitViewUpdateJobImpl(@NotNull VirtualView view, long delay, long interval) {
2421
this.view = view;
2522
this.delay = delay;
2623
this.interval = interval;
@@ -39,7 +36,14 @@ public void start() {
3936
throw new IllegalStateException("Cannot start a interrupted view update job");
4037
if (task != null) throw new IllegalStateException("View update job already started");
4138

39+
final PlatformViewFrame<?, ?, ?> initiator = findViewFrame();
40+
if (initiator == null)
41+
throw new IllegalStateException(
42+
"Cannot schedule view update because there's no initiator to do this");
43+
44+
final Plugin plugin = (Plugin) initiator.getOwner();
4245
task = plugin.getServer().getScheduler().runTaskTimer(plugin, this, delay, interval);
46+
4347
started = true;
4448
}
4549

@@ -76,7 +80,11 @@ public void cancel() {
7680
started = false;
7781
}
7882

79-
void interrupt() {
80-
this.interrupted = !interrupted;
83+
private PlatformViewFrame<?, ?, ?> findViewFrame() {
84+
if (view instanceof AbstractView) return ((AbstractView) view).getViewFrame();
85+
if (view instanceof ViewContext) return ((ViewContext) view).getRoot().getViewFrame();
86+
87+
throw new IllegalArgumentException(
88+
"Unable to find view frame on: " + view.getClass().getName());
8189
}
8290
}

0 commit comments

Comments
 (0)