Skip to content

Commit

Permalink
maintain hologram rotation when using moveHere sub-command (#104)
Browse files Browse the repository at this point in the history
* maintain hologram rotation when using moveHere sub-command
In the future, this will be toggle-able using a presence flag.

* handle world updates

* make `HologramData#getLocation` return cloned location

* handle moveTo sub-command as well
  • Loading branch information
Grabsky authored Jun 18, 2024
1 parent 27d8181 commit 4361554
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public HologramData(String name, HologramType type, Location location) {
}

public @NotNull Location getLocation() {
return location;
return location.clone();
}

public HologramData setLocation(@Nullable Location location) {
Expand Down Expand Up @@ -173,7 +173,7 @@ public void write(ConfigurationSection section, String name) {
}

public HologramData copy(String name) {
return new HologramData(name, type, location)
return new HologramData(name, type, this.getLocation())
.setVisibilityDistance(this.getVisibilityDistance())
.setVisibility(this.getVisibility())
.setPersistent(this.isPersistent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,29 @@

public class MoveHereCMD implements Subcommand {

public static boolean setLocation(Player player, Hologram hologram, Location location) {
public static boolean setLocation(Player player, Hologram hologram, Location location, boolean applyRotation) {
final var copied = hologram.getData().copy(hologram.getName());
copied.setLocation(location);
final Location newLocation = (applyRotation)
? location
: new Location(location.getWorld(), location.x(), location.y(), location.z(), copied.getLocation().getYaw(), copied.getLocation().getPitch());
copied.setLocation(newLocation);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.POSITION)) {
return false;
}

final var updatedLocation = copied.getLocation() == null ? location : copied.getLocation(); // note: maybe should fall back to original location?
hologram.getData().setLocation(updatedLocation);
hologram.getData().setLocation(copied.getLocation());

if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHolograms.get().getHologramStorage().save(hologram);
}

MessageHelper.success(player, "Moved the hologram to %s/%s/%s %s\u00B0 %s\u00B0".formatted(
Constants.COORDINATES_DECIMAL_FORMAT.format(updatedLocation.x()),
Constants.COORDINATES_DECIMAL_FORMAT.format(updatedLocation.y()),
Constants.COORDINATES_DECIMAL_FORMAT.format(updatedLocation.z()),
Constants.COORDINATES_DECIMAL_FORMAT.format((updatedLocation.getYaw() + 180f) % 360f),
Constants.COORDINATES_DECIMAL_FORMAT.format((updatedLocation.getPitch()) % 360f)
Constants.COORDINATES_DECIMAL_FORMAT.format(newLocation.x()),
Constants.COORDINATES_DECIMAL_FORMAT.format(newLocation.y()),
Constants.COORDINATES_DECIMAL_FORMAT.format(newLocation.z()),
Constants.COORDINATES_DECIMAL_FORMAT.format((newLocation.getYaw() + 180f) % 360f),
Constants.COORDINATES_DECIMAL_FORMAT.format((newLocation.getPitch()) % 360f)
));

return true;
Expand Down Expand Up @@ -81,6 +83,6 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @

final var location = player.getLocation();

return setLocation(player, hologram, location);
return setLocation(player, hologram, location, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @
return false;
}

final var location = new Location(player.getWorld(), x, y, z);
final var location = new Location(player.getWorld(), x, y, z, hologram.getData().getLocation().getYaw(), hologram.getData().getLocation().getPitch());

if (args.length > 6) {
final var yaw = MoveHereCMD.calculateCoordinate(args[6], hologram.getData().getLocation(), player.getLocation(), loc -> loc.getYaw() + 180f);
Expand All @@ -63,6 +63,6 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @
location.setPitch(pitch.floatValue());
}

return MoveHereCMD.setLocation(player, hologram, location);
return MoveHereCMD.setLocation(player, hologram, location, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @
Location location = hologram.getData().getLocation().clone();
location.setYaw(yaw.floatValue() - 180f);

return MoveHereCMD.setLocation(player, hologram, location);
return MoveHereCMD.setLocation(player, hologram, location, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @
Location location = hologram.getData().getLocation().clone();
location.setPitch(pitch.floatValue());

return MoveHereCMD.setLocation(player, hologram, location);
return MoveHereCMD.setLocation(player, hologram, location, true);
}
}

0 comments on commit 4361554

Please sign in to comment.