Skip to content

Commit

Permalink
Improve teleport on replay behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Jan 15, 2025
1 parent b5896ef commit 3c3ecbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import dev.pgm.community.moderation.feature.loggers.BlockGlitchLogger;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.lib.org.incendo.cloud.annotations.Argument;
import tc.oc.pgm.lib.org.incendo.cloud.annotations.Command;
import tc.oc.pgm.lib.org.incendo.cloud.annotations.Permission;

public class BlockGlitchCommand {
private static final int TP_DISTANCE = 10;
private static final int MAX_NO_OBS_DISTANCE = 40;
private static final int OBS_ROTATE_DISTANCE = 15;
private static final int PLAY_ROTATE_DISTANCE = 40;

private final BlockGlitchLogger blockGlitch;

Expand All @@ -39,13 +40,15 @@ public void listIncidents(MatchPlayer player) {
public void replayIncident(MatchPlayer player, @Argument("id") int id) {
var incident = blockGlitch.getIncident(id);
if (incident == null) throw exception("Sorry, the block glitch happened too long ago to view");
Location curr = player.getLocation();
int distance =
(int) Math.min(curr.distance(incident.getStart()), curr.distance(incident.getEnd()));

if (player.isObserving()) {
if (distance > TP_DISTANCE) player.getBukkit().teleport(incident.getStart());
} else if (distance > MAX_NO_OBS_DISTANCE) {
Location curr = player.getLocation(), start = incident.getStart();
int distance = (int) Math.min(curr.distance(start), curr.distance(incident.getEnd()));

if (distance < (player.isObserving() ? OBS_ROTATE_DISTANCE : PLAY_ROTATE_DISTANCE)) {
player.getBukkit().teleport(setFacing(curr, start));
} else if (player.isObserving()) {
player.getBukkit().teleport(moveAway(start));
} else {
throw exception("Join observers or get closer to watch this replay");
}

Expand All @@ -55,4 +58,29 @@ public void replayIncident(MatchPlayer player, @Argument("id") int id) {
.append(incident.getWhen().color(NamedTextColor.YELLOW)));
incident.play(player.getBukkit());
}

private static Location setFacing(Location base, Location target) {
double dx = target.getX() - base.getX();
double dy = target.getY() - base.getY();
double dz = target.getZ() - base.getZ();
double horizDist = Math.sqrt(dx * dx + dz * dz);

base.setPitch((float) Math.toDegrees(Math.atan2(-dy, horizDist)));
base.setYaw((float) Math.toDegrees(Math.atan2(-dx, dz)));
return base;
}

private static Location moveAway(Location base) {
Vector direction = base.getDirection();
base.setY(base.getY() + 1.63); // Add eye height
for (int i = 0; i < 3; i++) {
base.subtract(direction);
if (!base.getBlock().isEmpty()) {
base.add(direction);
break;
}
}
base.setY(base.getY() - 1.63);
return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void onBlockTransform(ParticipantBlockTransformEvent event) {

// Block has to be under you or in your head
Block block = event.getBlock();
Location loc = block.getLocation();
Location loc = playerState.getLocation();
double heightDiff = block.getY() - loc.getY();
boolean isBelow = !pl.isOnGround() && heightDiff < -1;
boolean isAbove = pl.isSprinting() && heightDiff > 1.8 && heightDiff < 2.2;
Expand Down Expand Up @@ -207,11 +207,11 @@ private boolean isAboveVoid() {
}

public Location getStart() {
return ((MoveAction) queue.getFirst()).to;
return ((MoveAction) queue.getFirst()).to.clone();
}

public Location getEnd() {
return ((MoveAction) queue.getLast()).to;
return ((MoveAction) queue.getLast()).to.clone();
}

public Component getPlayerName() {
Expand Down

0 comments on commit 3c3ecbd

Please sign in to comment.