Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Location Comparison #6205

Merged
merged 5 commits into from Dec 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/main/java/ch/njol/skript/classes/data/DefaultComparators.java
Expand Up @@ -24,27 +24,26 @@
import ch.njol.skript.aliases.ItemData;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.ClassInfo;
import org.skriptlang.skript.lang.comparator.Comparator;
import ch.njol.skript.entity.BoatChestData;
import ch.njol.skript.entity.BoatData;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.entity.RabbitData;
import org.skriptlang.skript.lang.comparator.Comparators;
import ch.njol.skript.util.BlockUtils;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Experience;
import ch.njol.skript.util.WeatherType;
import ch.njol.skript.util.GameruleValue;
import ch.njol.skript.util.StructureType;
import ch.njol.skript.util.Time;
import ch.njol.skript.util.Timeperiod;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.WeatherType;
import ch.njol.skript.util.slot.EquipmentSlot;
import ch.njol.skript.util.slot.Slot;
import ch.njol.skript.util.slot.SlotWithIndex;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
Expand All @@ -61,6 +60,8 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.comparator.Relation;

import java.util.Objects;
Expand Down Expand Up @@ -630,6 +631,28 @@ public boolean supportsOrdering() {
return false;
}
});

// Location - Location
Comparators.registerComparator(Location.class, Location.class, new Comparator<Location, Location>() {
@Override
public Relation compare(Location first, Location second) {
return Relation.get(
// compare worlds
Objects.equals(first.getWorld(), second.getWorld()) &&
// compare xyz coords
first.toVector().equals(second.toVector()) &&
// normalize yaw and pitch to [-180, 180) and [-90, 90] respectively
// before comparing them
Location.normalizeYaw(first.getYaw()) == Location.normalizeYaw(second.getYaw()) &&
Location.normalizePitch(first.getPitch()) == Location.normalizePitch(second.getPitch())
);
}

@Override
public boolean supportsOrdering() {
return false;
}
});
}

}
9 changes: 9 additions & 0 deletions src/test/skript/tests/regressions/6205-location comparison.sk
@@ -0,0 +1,9 @@
test "compare similar locations":
set {_world} to world "world"
assert location(1, 2, 3, {_world}, 4, 5) = location(1, 2, 3, {_world}, 4, 5) with "basic location comparison failed"

assert location(1, 2, 3, {_world}, 270, 0) = location(1, 2, 3, {_world}, -90, 0) with "yaw normalization failed when comparing locations"
assert location(1, 2, 3, {_world}, 0, 270) = location(1, 2, 3, {_world}, 0, 90) with "pitch normalization failed when comparing locations"
assert location(1, 2, 3, {_world}, 270, 270) = location(1, 2, 3, {_world}, -90, 90) with "yaw and pitch normalization failed when comparing locations"

assert location(1, (-1/infinity value), 3, {_world}, (-1/infinity value), (-1/infinity value)) = location(1, 0, 3, {_world}, 0, 0) with "0 and -0.0 are not equal when comparing locations"