Skip to content

Commit

Permalink
feat(bukkit): add dynamic pointer to chatter location
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed May 13, 2022
1 parent 6074eb9 commit 58efe20
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.platform.chatter.AbstractChatterFactory;
import net.silthus.schat.pointer.Pointers;
import net.silthus.schat.util.Location;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static net.silthus.schat.bukkit.adapter.BukkitIdentityAdapter.identity;
import static net.silthus.schat.bukkit.protocollib.ChatPacketListener.MESSAGE_MARKER;
import static net.silthus.schat.util.Location.LOCATION;
import static org.bukkit.Bukkit.getOfflinePlayer;

public final class BukkitChatterFactory extends AbstractChatterFactory {
Expand All @@ -52,7 +58,7 @@ protected Identity createIdentity(UUID id) {

@Override
protected Chatter.PermissionHandler createPermissionHandler(UUID id) {
return permission -> Optional.ofNullable(Bukkit.getPlayer(id))
return permission -> player(id)
.map(player -> player.hasPermission(permission))
.orElse(false);
}
Expand All @@ -62,4 +68,41 @@ protected Chatter.MessageHandler createMessageHandler(UUID id) {
return msg -> audiences.player(id).sendMessage(msg.append(MESSAGE_MARKER));
}

@Override
protected void buildPointers(UUID id, Pointers.Builder pointers) {
pointers.withDynamic(LOCATION, () -> player(id).map(this::getLocation).orElse(null));
}

@NotNull
private Location getLocation(Player player) {
return fromBukkitLocation(player.getLocation());
}

@NotNull
private Location fromBukkitLocation(org.bukkit.Location l) {
return new Location(
fromBukkitWorld(l.getWorld()),
l.getX(),
l.getY(),
l.getZ(),
l.getYaw(),
l.getPitch()
);
}

@Nullable
private Location.World fromBukkitWorld(@Nullable World world) {
if (world == null)
return null;
else
return new Location.World(
world.getUID(),
world.getName()
);
}

@NotNull
private Optional<Player> player(UUID id) {
return Optional.ofNullable(Bukkit.getPlayer(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.commands.SendMessageCommand;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.util.Location;
import org.bukkit.OfflinePlayer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.schat.util.Location.LOCATION;
import static org.assertj.core.api.Assertions.assertThat;

class BukkitChatterFactoryTests extends BukkitTests {
Expand Down Expand Up @@ -63,6 +65,28 @@ private Chatter createChatter() {
return create(player.getUniqueId());
}

private void assertLocationIs(org.bukkit.Location l) {
assertThat(createChatter().get(LOCATION))
.isPresent().get()
.extracting(
Location::x,
Location::y,
Location::z,
Location::pitch,
Location::yaw,
location -> location.world().id(),
location -> location.world().name()
).contains(
l.getX(),
l.getY(),
l.getZ(),
l.getPitch(),
l.getYaw(),
l.getWorld().getUID(),
l.getWorld().getName()
);
}

@Test
void then_chatter_name_is_player_name() {
assertThat(createChatter().name()).isEqualTo(player.getName());
Expand All @@ -74,6 +98,18 @@ void then_chatter_display_name_is_player_display_name() {
assertThat(createChatter().displayName()).isEqualTo(text("Bob"));
}

@Test
void then_chatter_location_is_player_location() {
assertLocationIs(player.getLocation());
}

@Test
void when_location_changes_then_chatter_location_changes() {
final org.bukkit.Location l = new org.bukkit.Location(player.getWorld(), 12, 34, 56);
player.teleport(l);
assertLocationIs(l);
}

@Test
void given_player_changes_display_name_then_chatter_name_changes() {
final Chatter chatter = createChatter();
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/java/net/silthus/schat/util/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.silthus.schat.util;

import java.util.UUID;
import net.silthus.schat.pointer.Pointer;

/**
* Represents a typical Minecraft location object.
*
* <p>Use the helper inside the implementing platform to wrap and unwrap native location objects.</p>
*
* @param world the world of the location
* @param x x-axis
* @param y y-axis
* @param z z-axis
* @param yaw horizontal rotation
* @param pitch vertical rotation
* @since next
*/
public record Location(World world, double x, double y, double z, float yaw, float pitch) {

public static final Pointer<Location> LOCATION = Pointer.pointer(Location.class, "location");

public Location(World world, double x, double y, double z) {
this(world, x, y, z, 0f, 90f);
}

/**
* Represents a typical Minecraft world identified by an id and name.
*
* @param id the unique id of the world
* @param name the name of the world
* @since next
*/
public record World(UUID id, String name) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.silthus.schat.chatter.ChatterFactory;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.pointer.Pointers;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import static net.silthus.schat.chatter.Chatter.chatterBuilder;
Expand All @@ -50,6 +51,14 @@ public final Chatter createChatter(UUID id) {

protected abstract Chatter.MessageHandler createMessageHandler(UUID id);

/**
* Add platform specific pointers by overriding this method.
*
* @param id the id of the chatter being created
* @param pointers the additional pointers to add to the chatter
* @since next
*/
@ApiStatus.OverrideOnly
protected void buildPointers(UUID id, Pointers.Builder pointers) {
}
}

0 comments on commit 58efe20

Please sign in to comment.