Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions api/src/main/java/xyz/bitsquidd/bits/log/BasicLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ public void exceptionInternal(final String msg, final Throwable throwable) {
return;
}

StackTraceElement[] stackTrace = throwable.getStackTrace();
Throwable real = throwable;
while (real.getCause() != null && real.getCause() != real) real = real.getCause();

StackTraceElement[] stackTrace = real.getStackTrace();
String header;
if (stackTrace.length == 0) {
header = String.format("%s |-> Exception (no stack trace) - %s", msg, throwable.getMessage());
header = String.format("%s |-> Exception (no stack trace) - %s", msg, real.getMessage());
} else {
StackTraceElement origin = stackTrace[0];
header = String.format(
Expand All @@ -101,13 +104,13 @@ public void exceptionInternal(final String msg, final Throwable throwable) {
origin.getMethodName(),
origin.getFileName(),
origin.getLineNumber(),
throwable.getMessage()
real.getMessage()
);
}
System.out.println(LogType.ERROR.format(header));

if (flags.logExtendedError()) {
for (StackTraceElement element : throwable.getStackTrace()) {
for (StackTraceElement element : real.getStackTrace()) {
System.out.println(PrettyLogLevel.RED.formatMessage("\tat " + element));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,26 @@
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.Nullable;

import xyz.bitsquidd.bits.mc.animation.impl.Animation;
import xyz.bitsquidd.bits.paper.util.bukkit.runnable.Runnables;

import java.util.function.Consumer;


// TODO
// Refine animation system.
public abstract class AnimationPlayer<D> {
public final class AnimationPlayer<A extends Animatable> {
private final Animation animation;

private int currentTick = 0;
private @Nullable Consumer<AnimationPlayer<D>> onComplete;
private long currentTick = 0;
private @Nullable Consumer<AnimationPlayer<A>> onComplete;
private @Nullable BukkitTask ticker;

public AnimationPlayer(Animation animation) {
this.animation = animation;
}

public final void play(D display) {
ticker = Runnables.cleanup(ticker);
ticker = Runnables.timer(() -> tick(display, currentTick++), 0, 1);
}

public final void pause() {
public final void play(A animatable) {
ticker = Runnables.cleanup(ticker);
currentTick = 0;
ticker = Runnables.timer(() -> tick(animatable, currentTick++), 0, 1);
}

public final void stop() {
Expand All @@ -44,21 +38,23 @@ public final void stop() {
if (onComplete != null) onComplete.accept(this);
}

public final AnimationPlayer<D> onComplete(Consumer<AnimationPlayer<D>> callback) {
public final AnimationPlayer<A> onComplete(Consumer<AnimationPlayer<A>> callback) {
this.onComplete = callback;
return this;
}

public final void tick(D display, int tick) {
if (animation.isFinished(tick)) {
public final void tick(A animatable, long tick) {
AnimationData data = new AnimationData(tick);

if (animation.isFinished(data)) {
stop();
return;
}

AnimationPose pose = animation.evaluate(tick);
applyPose(display, pose);
// Fresh identity every tick: animation.mutate() computes this tick's complete snapshot,
AnimationPoseNew pose = AnimationPoseNew.identity();
animation.mutate(pose, data);
animatable.applyPose(pose);
}

protected abstract void applyPose(D display, AnimationPose pose);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of a Bit libraries package.
* Licensed under the GNU Lesser General Public License v3.0.
*
* Copyright (c) 2023-2026 ImBit
*/

package xyz.bitsquidd.bits.mc.animation;

import org.bukkit.entity.Display;
import org.bukkit.util.Transformation;
import org.joml.Quaternionf;


public class PaperDisplay implements Animatable {
private final Display display;

private PaperDisplay(Display display) {
this.display = display;
}

public static PaperDisplay of(Display display) {
return new PaperDisplay(display);
}


@Override
public void applyPose(AnimationPoseNew pose) {
display.setTransformation(
new Transformation(
pose.translation(),
pose.rotation(),
pose.scale(),
new Quaternionf() // No right rotation, very few situations require this.
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.Nullable;

import xyz.bitsquidd.bits.paper.location.wrapper.BlockPos;
import xyz.bitsquidd.bits.paper.location.wrapper.Locatable;

import java.util.Collection;


/**
* Suite of location-related utility methods.
*
Expand All @@ -23,23 +25,68 @@
public final class Locations {
private Locations() {}


//region Utils
public static boolean isSameWorld(Location... locations) {
if (locations.length == 0) return false;
World world = locations[0].getWorld();

for (Location location : locations) {
if (!location.getWorld().equals(world)) return false;
}
return true;
}

//endregion


//region Distance

/**
* Safe distance check between two locations.
* Returns false if either location is null or they are in different worlds.
*
* @since 0.0.11
*/
public static boolean isWithinDistance(Location loc1, Location loc2, double distance) {
public static boolean isWithinDistance(@Nullable Location loc1, @Nullable Location loc2, double distance) {
return isWithinDistanceSq(loc1, loc2, distance * distance);
}

public static boolean isWithinDistanceSq(@Nullable Location loc1, @Nullable Location loc2, double distanceSq) {
if (loc1 == null || loc2 == null) return false;
return getDistanceSq(loc1, loc2) <= distanceSq;
}

World world1 = loc1.getWorld();
World world2 = loc2.getWorld();
public static double getDistance(Location location1, Location location2) {
if (!isSameWorld(location1, location2)) return Double.MAX_VALUE;
return Math.sqrt(getDistanceSq(location1, location2));
}

if (world1 == null || !world1.equals(world2)) return false;
public static double getDistanceSq(Location location1, Location location2) {
if (!isSameWorld(location1, location2)) return Double.MAX_VALUE;

return loc1.distance(loc2) <= distance;
double dx = location1.getX() - location2.getX();
double dy = location1.getY() - location2.getY();
double dz = location1.getZ() - location2.getZ();
return dx * dx + dy * dy + dz * dz;
}

public static double getHorizontalDistance(Location location1, Location location2) {
return Math.sqrt(getHorizontalDistanceSq(location1, location2));
}

public static double getHorizontalDistanceSq(Location location1, Location location2) {
if (!isSameWorld(location1, location2)) return Double.MAX_VALUE;

double dx = location1.getX() - location2.getX();
double dz = location1.getZ() - location2.getZ();
return dx * dx + dz * dz;
}
//endregion


//region Collection Operations

/**
* Calculates the midpoint BlockPos coordinates from a collection of {@link Locatable}s.
*
Expand Down Expand Up @@ -85,5 +132,6 @@ public static BlockPos getMaxLocation(Collection<? extends Locatable> locatables
locatables.stream().mapToDouble(l -> l.asVector().getZ()).max().orElseThrow(() -> new IllegalArgumentException("Error computing max z"))
);
}
//endregion

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This file is part of a Bit libraries package.
* Licensed under the GNU Lesser General Public License v3.0.
*
* Copyright (c) 2023-2026 ImBit
*/

package xyz.bitsquidd.bits.mc.animation;


public interface Animatable {
void applyPose(AnimationPoseNew pose);

}
Loading
Loading