Skip to content

Commit

Permalink
fix plugin breaking when replaying invalid worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumper251 committed Sep 24, 2024
1 parent 1ef4519 commit 4190053
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {

allprojects {
group = 'me.jumper251.replay'
version = '1.8.9'
version = '1.8.10'

apply plugin: 'java'
apply plugin: 'maven-publish'
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/me/jumper251/replay/replaysystem/Replay.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void play(Player watcher) {

private void startReplay(Player watcher) {
this.replayer = new Replayer(this, watcher);
this.replayer.start();
this.isPlaying = true;
this.isPlaying = this.replayer.start();
}

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public static Location toLocation(LocationData locationData) {
return new Location(Bukkit.getWorld(locationData.getWorld()), locationData.getX(), locationData.getY(), locationData.getZ());
}

public boolean isValidWorld() {
return Bukkit.getWorld(this.world) != null;
}


@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public ReplayPacketListener(Replayer replayer) {
this.spectating = new HashMap<Player, Integer>();
this.previous = -1;

if (!isRegistered()) register();
}

@Override
public void register() {
if (isRegistered()) return;

this.packetAdapter = new PacketAdapter(ReplaySystem.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY, PacketType.Play.Server.ENTITY_DESTROY) {

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,36 @@ public Replayer(Replay replay, Player watcher) {
this.utils = new ReplayingUtils(this);
this.session = new ReplaySession(this);
this.paused = false;

ReplayHelper.replaySessions.put(watcher.getName(), this);
}


public void start() {
public boolean start() {
ReplayData data = this.replay.getData();
int duration = data.getDuration();
this.session.setStart(watcher.getLocation());
SpawnData spawnData = null;
if (data.getActions().containsKey(0)) {
for (ActionData startData : data.getActions().get(0)) {
if (startData.getPacketData() instanceof SpawnData) {
SpawnData spawnData = (SpawnData) startData.getPacketData();
watcher.teleport(LocationData.toLocation(spawnData.getLocation()));
spawnData = (SpawnData) startData.getPacketData();
break;
}
}
} else {
Optional<SpawnData> spawnData = findFirstSpawn(data);
if (spawnData.isPresent()) watcher.teleport(LocationData.toLocation(spawnData.get().getLocation()));
spawnData = findFirstSpawn(data).orElse(null);
}

if (spawnData != null && !spawnData.getLocation().isValidWorld()) {
sendMessage("§cThe world for this Replay does not exist or is not loaded. (" + spawnData.getLocation().getWorld() + ")");
return false;
}

ReplayHelper.replaySessions.put(watcher.getName(), this);

if (spawnData != null) {
watcher.teleport(LocationData.toLocation(spawnData.getLocation()));
}


this.session.startSession();

this.speed = 1;
Expand Down Expand Up @@ -128,7 +135,8 @@ public void run() {
};

this.run.runTaskTimerAsynchronously(ReplaySystem.getInstance(), 1, 1);


return true;
}

public void executeTick(int tick, ReplayingMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import me.jumper251.replay.replaysystem.utils.MetadataBuilder;
import me.jumper251.replay.replaysystem.utils.NPCManager;
import me.jumper251.replay.replaysystem.utils.entities.*;
import me.jumper251.replay.utils.LogUtils;
import me.jumper251.replay.utils.version.MaterialBridge;
import me.jumper251.replay.utils.MathUtils;
import me.jumper251.replay.utils.VersionUtil;
Expand Down Expand Up @@ -312,14 +313,18 @@ public void handleAction(ActionData action, ReplayData data, ReplayingMode mode)

if (action.getPacketData() instanceof WorldChangeData) {
WorldChangeData worldChange = (WorldChangeData) action.getPacketData();
if (!worldChange.getLocation().isValidWorld()) {
LogUtils.log("Skipping invalid world: " + worldChange.getLocation().getWorld());
return;
}
Location loc = LocationData.toLocation(worldChange.getLocation());

npc.despawn();
npc.setOrigin(loc);
npc.setLocation(loc);

npc.respawn(replayer.getWatchingPlayer());

}

if (action.getPacketData() instanceof FishingData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public ReplaySession(Replayer replayer) {
}

public void startSession() {
this.packetListener.register();

this.content = this.player.getInventory().getContents();
if (this.start == null) {
this.start = this.player.getLocation();
Expand Down

0 comments on commit 4190053

Please sign in to comment.