DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamCamPath.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamCam;
25
26import com.dreamfirestudios.dreamcore.DreamCore;
27import com.dreamfirestudios.dreamcore.DreamJava.DreamClassID;
28import org.bukkit.Bukkit;
29import org.bukkit.GameMode;
30import org.bukkit.Location;
31import org.bukkit.entity.Player;
32import org.bukkit.scheduler.BukkitRunnable;
33
34import java.util.*;
35
54public class DreamCamPath extends DreamClassID {
55 private final List<UUID> players = new ArrayList<>();
56 private List<DreamCamSet> camSets = new ArrayList<>();
57
58 private final Map<UUID, GameMode> playersGamemodesBefore = new HashMap<>();
59 private final Map<UUID, Location> playersLocationsBefore = new HashMap<>();
60 private final Map<UUID, Boolean> playersFlyingBefore = new HashMap<>();
61 private BukkitRunnable bukkitRunnable;
62
64 public List<DreamCamSet> getCamSets() { return camSets; }
65
67 public List<UUID> getPlayers() { return players; }
68
73 public void addPlayer(Player... players) {
74 for (var player : players) addPlayer(player);
75 }
76
82 public void addPlayer(Player player) {
83 if (new CamPathPlayerAddedEvent(this, player).isCancelled()) return;
84 players.add(player.getUniqueId());
85 }
86
91 public void removeAllPlayers(boolean hasFinished) {
92 for (var playerUUID : new ArrayList<>(players)) {
93 var player = Bukkit.getPlayer(playerUUID);
94 if (player != null) removePlayerFromPath(player, hasFinished);
95 }
96 }
97
104 public void removePlayerFromPath(Player player, boolean hasFinished) {
105 if (!players.contains(player.getUniqueId())) return;
106 new CamPathPlayerLeaveEvent(this, player, hasFinished);
107 player.setGameMode(playersGamemodesBefore.getOrDefault(player.getUniqueId(), player.getGameMode()));
108 player.teleport(playersLocationsBefore.getOrDefault(player.getUniqueId(), player.getLocation()));
109 player.setFlying(playersFlyingBefore.getOrDefault(player.getUniqueId(), player.isFlying()));
110 players.remove(player.getUniqueId());
111 }
112
117 public void startCamPath() {
118 if (bukkitRunnable != null) return;
119 for (var playerUUID : players) {
120 var player = Bukkit.getPlayer(playerUUID);
121 if (player == null) continue;
122 playersGamemodesBefore.put(playerUUID, player.getGameMode());
123 playersLocationsBefore.put(playerUUID, player.getLocation());
124 playersFlyingBefore.put(playerUUID, player.isFlying());
125 player.setGameMode(GameMode.SPECTATOR);
126 }
127 new CamPathStartEvent(this);
128 bukkitRunnable = new DreamCamPathRunnable(this);
129 bukkitRunnable.runTaskTimer(DreamCore.DreamCore, 0L, 1L);
130 }
131
137 public void endCamPath(boolean hasFinished) {
138 for (var playerUUID : new ArrayList<>(players)) {
139 var player = Bukkit.getPlayer(playerUUID);
140 if (player != null) removePlayerFromPath(player, hasFinished);
141 }
142 new CamPathStopEvent(this);
143 if (bukkitRunnable != null) {
144 bukkitRunnable.cancel();
145 bukkitRunnable = null;
146 }
147 }
148
152 public void onDisable() {
153 removeAllPlayers(false);
154 }
155
160 public void onPlayerLeave(Player player) {
161 removePlayerFromPath(player, false);
162 }
163
164 // ---- Builder ----
165
169 public static class CamPathBuilder {
170 private final List<DreamCamSet> camSets = new ArrayList<>();
171 private final List<Player> players = new ArrayList<>();
172
183 public CamPathBuilder addTravelPath(Location start, Location end, LookAtType lookAtType, Object data, int durationInTicks) {
184 if (durationInTicks <= 0) throw new IllegalArgumentException("durationInTicks must be > 0");
185
186 var pathLocations = new ArrayList<Location>();
187 pathLocations.add(start.clone());
188
189 var stepX = (end.getX() - start.getX()) / durationInTicks;
190 var stepY = (end.getY() - start.getY()) / durationInTicks;
191 var stepZ = (end.getZ() - start.getZ()) / durationInTicks;
192 var stepYaw = (end.getYaw() - start.getYaw()) / durationInTicks;
193 var stepPitch = (end.getPitch() - start.getPitch()) / durationInTicks;
194
195 var step = new Location(start.getWorld(), stepX, stepY, stepZ, (float) stepYaw, (float) stepPitch);
196
197 for (int i = 1; i <= durationInTicks; i++) {
198 var prevLocation = pathLocations.get(i - 1).clone();
199 var next = prevLocation.add(step);
200 next.setYaw(next.getYaw() + stepYaw);
201 next.setPitch(next.getPitch() + stepPitch);
202 pathLocations.add(next);
203 }
204
205 camSets.add(new DreamCamSet(pathLocations, lookAtType, data));
206 return this;
207 }
208
214 public CamPathBuilder players(Player... players) {
215 this.players.addAll(Arrays.asList(players));
216 return this;
217 }
218
228 var path = new DreamCamPath();
229 path.camSets = camSets;
230 players.forEach(path::addPlayer);
231 return DreamCore.DreamCamPaths.put(path.getClassID(), path);
232 }
233 }
234}
Fired when a player leaves a DreamCamPath (either manually or at the end).
Fired when a DreamCamPath stops (after restoring player states).
Bukkit runnable that steps through a DreamCamPath point-by-point, teleporting players and dispatching...
Builder class for creating DreamCamPath instances.
CamPathBuilder players(Player... players)
Adds players to this path.
CamPathBuilder addTravelPath(Location start, Location end, LookAtType lookAtType, Object data, int durationInTicks)
Adds a linear travel path segment.
DreamCamPath create()
Builds or retrieves the path instance.
Represents a cinematic camera path composed of multiple camera sets (segments).
void onPlayerLeave(Player player)
Called when a player leaves unexpectedly.
List< DreamCamSet > getCamSets()
Gets all camera sets in this path.
void onDisable()
Called when the plugin is disabled to safely remove players.
List< UUID > getPlayers()
Gets the players currently on this path (by UUID).
void endCamPath(boolean hasFinished)
Stops the path and restores all player states.
void addPlayer(Player... players)
Adds multiple players to the camera path.
void startCamPath()
Starts playback of the camera path for all players.
void addPlayer(Player player)
Adds a single player to the camera path.
void removeAllPlayers(boolean hasFinished)
Removes all players from the path.
void removePlayerFromPath(Player player, boolean hasFinished)
Removes a single player from the path and restores their state.
static final LinkedHashMap< UUID, DreamCamPath > DreamCamPaths
Defines how the camera should orient itself during path playback.
record DreamCamSet(List< Location > points, LookAtType lookAtType, Object object)
Represents a segment of a camera path containing ordered points and rotation rules (look-at behavior)...