Skip to content

Commit

Permalink
Improve fishing hook replaying
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumper251 committed Jun 24, 2024
1 parent ea0bcf1 commit 3b0ffe0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public class FishingData extends PacketData {
private double x, y, z;

private int id;

private String owner;

public FishingData(int id, LocationData location, double x, double y, double z) {
public FishingData(int id, LocationData location, double x, double y, double z, String owner) {
this.location = location;
this.x = x;
this.y = y;
this.z = z;
this.id = id;
this.owner = owner;
}

public LocationData getLocation() {
Expand All @@ -42,4 +45,7 @@ public int getId() {
return id;
}

public String getOwner() {
return owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,18 @@ public void onPacketSending(PacketEvent event) {

}
if ((type == 90 || (VersionUtil.isAbove(VersionEnum.V1_14) && event.getPacket().getEntityTypeModifier().read(0) == EntityType.FISHING_HOOK)) && !spawnedHooks.contains(packet.getEntityID())) {
int throwerId = VersionUtil.isCompatible(VersionEnum.V1_8) ? oldPacket.getObjectData() : packet.getObjectData();

String ownerName = Bukkit.getOnlinePlayers().stream()
.filter(player -> (player.getWorld().getName().equals(p.getWorld().getName()) && player.getEntityId() == throwerId))
.findFirst()
.map(Player::getName)
.orElse(null);

if (VersionUtil.isCompatible(VersionEnum.V1_8)) {
addData(p.getName(), new FishingData(oldPacket.getEntityID(), location, oldPacket.getOptionalSpeedX(), oldPacket.getOptionalSpeedY(), oldPacket.getOptionalSpeedZ()));
addData(p.getName(), new FishingData(oldPacket.getEntityID(), location, oldPacket.getOptionalSpeedX(), oldPacket.getOptionalSpeedY(), oldPacket.getOptionalSpeedZ(), ownerName));
} else {
addData(p.getName(), new FishingData(packet.getEntityID(), location, packet.getOptionalSpeedX(), packet.getOptionalSpeedY(), packet.getOptionalSpeedZ()));
addData(p.getName(), new FishingData(packet.getEntityID(), location, packet.getOptionalSpeedX(), packet.getOptionalSpeedY(), packet.getOptionalSpeedZ(), ownerName));
}
spawnedHooks.add(packet.getEntityID());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void start() {

this.speed = 1;

executeTick(0, false);
executeTick(0, ReplayingMode.PLAYING);

this.run = new BukkitRunnable() {

Expand All @@ -112,10 +112,10 @@ public void run() {

if (currentTicks < duration) {

executeTick(currentTicks++, false);
executeTick(currentTicks++, ReplayingMode.PLAYING);

if ((currentTicks + 2) < duration && speed == 2) {
executeTick(currentTicks++, false);
executeTick(currentTicks++, ReplayingMode.PLAYING);

}

Expand All @@ -130,8 +130,8 @@ public void run() {
this.run.runTaskTimerAsynchronously(ReplaySystem.getInstance(), 1, 1);

}
public void executeTick(int tick, boolean reversed) {

public void executeTick(int tick, ReplayingMode mode) {
ReplayData data = this.replay.getData();
if (!data.getActions().isEmpty() && data.getActions().containsKey(tick)) {

Expand All @@ -141,7 +141,7 @@ public void executeTick(int tick, boolean reversed) {
List<ActionData> list = data.getActions().get(tick);
for (ActionData action : list) {

utils.handleAction(action, data, reversed);
utils.handleAction(action, data, mode);

if (action.getType() == ActionType.CUSTOM) {
if (ReplayAPI.getInstance().getHookManager().isRegistered()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.jumper251.replay.replaysystem.replaying;

public enum ReplayingMode {
PLAYING,
FORWARD,
REVERSED
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public ReplayingUtils(Replayer replayer) {
this.signatures = new HashMap<>();
}

public void handleAction(ActionData action, ReplayData data, boolean reversed) {
public void handleAction(ActionData action, ReplayData data, ReplayingMode mode) {
boolean reversed = mode == ReplayingMode.REVERSED;

if (action.getType() == ActionType.SPAWN) {
if (!reversed) {
spawnNPC(action);
Expand Down Expand Up @@ -313,8 +315,17 @@ public void handleAction(ActionData action, ReplayData data, boolean reversed) {

if (action.getPacketData() instanceof FishingData) {
FishingData fishing = (FishingData) action.getPacketData();
spawnProjectile(null, fishing, replayer.getWatchingPlayer().getWorld(), npc.getId());

int ownerId = replayer.getNPCList().getOrDefault(fishing.getOwner(), npc).getId();

if (mode == ReplayingMode.PLAYING) {
spawnProjectile(null, fishing, replayer.getWatchingPlayer().getWorld(), ownerId);
}

if (reversed && hooks.containsKey(fishing.getId())) {
despawn(null, new int[] { hooks.get(fishing.getId()) });
hooks.remove(fishing.getId());

}
}

if (action.getPacketData() instanceof VelocityData) {
Expand Down Expand Up @@ -381,7 +392,7 @@ public void forward() {
}

for (int i = currentTick; i < forwardTicks; i++) {
this.replayer.executeTick(i, false);
this.replayer.executeTick(i, ReplayingMode.FORWARD);
}
this.replayer.setCurrentTicks(forwardTicks);
this.replayer.setPaused(paused);
Expand All @@ -399,7 +410,7 @@ public void backward() {
}

for (int i = currentTick; i > backwardTicks; i--) {
this.replayer.executeTick(i, true);
this.replayer.executeTick(i, ReplayingMode.REVERSED);
}
this.replayer.setCurrentTicks(backwardTicks);
this.replayer.setPaused(paused);
Expand All @@ -413,7 +424,7 @@ public void jumpTo(Integer seconds) {

if ((targetTicks - 2) > 0) {
for (int i = currentTick; i > targetTicks; i--) {
this.replayer.executeTick(i, true);
this.replayer.executeTick(i, ReplayingMode.REVERSED);
}

this.replayer.setCurrentTicks(targetTicks);
Expand All @@ -425,7 +436,7 @@ public void jumpTo(Integer seconds) {

if ((targetTicks + 2) < duration) {
for (int i = currentTick; i < targetTicks; i++) {
this.replayer.executeTick(i, false);
this.replayer.executeTick(i, ReplayingMode.FORWARD);
}
this.replayer.setCurrentTicks(targetTicks);
this.replayer.setPaused(false);
Expand Down

0 comments on commit 3b0ffe0

Please sign in to comment.