mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Add basic player abstractions
This commit is contained in:
1 parent
28ffa3d3e9
commit
0b3245ef0c
4 files changed
+150
-2
No files matched your search
@@ -4,7 +4,18 @@ import net.kyori.adventure.audience.Audience;
|
|||||||
import net.kyori.adventure.identity.Identified;
|
import net.kyori.adventure.identity.Identified;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface CarbonPlayer extends Audience, Identified {
|
||||||
|
|
||||||
|
@NonNull String username();
|
||||||
|
|
||||||
|
@NonNull Component displayName();
|
||||||
|
|
||||||
|
void displayName(final @Nullable Component displayName);
|
||||||
|
|
||||||
|
@NonNull UUID uuid();
|
||||||
|
|
||||||
public abstract class CarbonPlayer implements Audience, Identified {
|
|
||||||
public abstract @NonNull Component displayName();
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package net.draycia.carbon.bukkit.users;
|
||||||
|
|
||||||
|
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||||
|
import net.kyori.adventure.audience.Audience;
|
||||||
|
import net.kyori.adventure.identity.Identity;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||||
|
|
||||||
|
public CarbonPlayerBukkit(
|
||||||
|
final @NonNull String username,
|
||||||
|
final @NonNull Component displayName,
|
||||||
|
final @NonNull UUID uuid
|
||||||
|
) {
|
||||||
|
super(username, displayName, uuid, Identity.identity(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Audience audience() {
|
||||||
|
final Player player = this.player();
|
||||||
|
|
||||||
|
if (player == null) {
|
||||||
|
return Audience.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable Player player() {
|
||||||
|
return Bukkit.getPlayer(this.uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package net.draycia.carbon.common.users;
|
||||||
|
|
||||||
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
|
import net.kyori.adventure.audience.ForwardingAudience;
|
||||||
|
import net.kyori.adventure.identity.Identity;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static java.util.Objects.requireNonNullElseGet;
|
||||||
|
import static net.kyori.adventure.text.Component.text;
|
||||||
|
|
||||||
|
public abstract class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single {
|
||||||
|
|
||||||
|
protected final @NonNull String username;
|
||||||
|
protected @NonNull Component displayName;
|
||||||
|
protected final @NonNull UUID uuid;
|
||||||
|
protected final @NonNull Identity identity;
|
||||||
|
|
||||||
|
protected CarbonPlayerCommon(
|
||||||
|
final @NonNull String username,
|
||||||
|
final @NonNull Component displayName,
|
||||||
|
final @NonNull UUID uuid,
|
||||||
|
final @NonNull Identity identity
|
||||||
|
) {
|
||||||
|
this.username = username;
|
||||||
|
this.displayName = displayName;
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.identity = identity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull String username() {
|
||||||
|
return this.username;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Component displayName() {
|
||||||
|
return this.displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayName(final @Nullable Component displayName) {
|
||||||
|
this.displayName = requireNonNullElseGet(displayName, () -> text(this.username));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Identity identity() {
|
||||||
|
return this.identity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull UUID uuid() {
|
||||||
|
return this.uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package net.draycia.carbon.sponge.users;
|
||||||
|
|
||||||
|
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||||
|
import net.kyori.adventure.audience.Audience;
|
||||||
|
import net.kyori.adventure.identity.Identity;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.spongepowered.api.Sponge;
|
||||||
|
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||||
|
|
||||||
|
public CarbonPlayerSponge(
|
||||||
|
final @NonNull String username,
|
||||||
|
final @NonNull Component displayName,
|
||||||
|
final @NonNull UUID uuid
|
||||||
|
) {
|
||||||
|
super(username, displayName, uuid, Identity.identity(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull Audience audience() {
|
||||||
|
final Optional<ServerPlayer> player = this.player();
|
||||||
|
|
||||||
|
if (player.isEmpty()) {
|
||||||
|
return Audience.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
return player.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NonNull Optional<ServerPlayer> player() {
|
||||||
|
return Sponge.server().player(this.uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user