mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
Use optionals in CarbonPlayerBukkit
This commit is contained in:
1 parent
ee084c1221
commit
07da847864
1 file changed
+23
-37
@@ -1,7 +1,8 @@
|
|||||||
package net.draycia.carbon.bukkit.users;
|
package net.draycia.carbon.bukkit.users;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Optional;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.util.InventorySlot;
|
import net.draycia.carbon.api.util.InventorySlot;
|
||||||
import net.draycia.carbon.api.util.InventorySlots;
|
import net.draycia.carbon.api.util.InventorySlots;
|
||||||
@@ -30,8 +31,8 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
this.carbonPlayerCommon = carbonPlayerCommon;
|
this.carbonPlayerCommon = carbonPlayerCommon;
|
||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable Player player() {
|
private Optional<Player> player() {
|
||||||
return Bukkit.getPlayer(this.carbonPlayerCommon.uuid());
|
return Optional.ofNullable(Bukkit.getPlayer(this.carbonPlayerCommon.uuid()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -41,38 +42,31 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Audience audience() {
|
public @NotNull Audience audience() {
|
||||||
final @Nullable Player player = this.player();
|
return this.player().map(player -> (Audience) player).orElse(Audience.empty());
|
||||||
|
|
||||||
if (player == null) {
|
|
||||||
return Audience.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void displayName(final @Nullable Component displayName) {
|
public void displayName(final @Nullable Component displayName) {
|
||||||
this.carbonPlayerCommon.displayName(displayName);
|
this.carbonPlayerCommon.displayName(displayName);
|
||||||
|
|
||||||
final @Nullable Player player = this.player();
|
if (this.hasActiveTemporaryDisplayName()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (player != null) {
|
this.player().ifPresent(player -> {
|
||||||
// TODO: don't run this block when the player has a temporary display name already set
|
|
||||||
// Update player's name in chat
|
// Update player's name in chat
|
||||||
player.displayName(displayName);
|
player.displayName(displayName);
|
||||||
|
|
||||||
// Update player's name in the tab player list
|
// Update player's name in the tab player list
|
||||||
player.playerListName(displayName);
|
player.playerListName(displayName);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void temporaryDisplayName(final @Nullable Component displayName, final long expirationEpoch) {
|
public void temporaryDisplayName(final @Nullable Component displayName, final long expirationEpoch) {
|
||||||
this.carbonPlayerCommon.temporaryDisplayName(displayName, expirationEpoch);
|
this.carbonPlayerCommon.temporaryDisplayName(displayName, expirationEpoch);
|
||||||
|
|
||||||
final @Nullable Player player = this.player();
|
this.player().ifPresent(player -> {
|
||||||
|
|
||||||
if (player != null) {
|
|
||||||
// Update player's name in chat
|
// Update player's name in chat
|
||||||
player.displayName(displayName);
|
player.displayName(displayName);
|
||||||
|
|
||||||
@@ -80,14 +74,14 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
player.playerListName(displayName);
|
player.playerListName(displayName);
|
||||||
|
|
||||||
// TODO: schedule task to unset temporary display name when it expires
|
// TODO: schedule task to unset temporary display name when it expires
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
|
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
|
||||||
final @Nullable Player player = this.player(); // This is temporary (it's not)
|
final Optional<Player> player = this.player(); // This is temporary (it's not)
|
||||||
|
|
||||||
if (player == null) {
|
if (player.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +103,7 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final @Nullable EntityEquipment equipment = player.getEquipment();
|
final @Nullable EntityEquipment equipment = player.get().getEquipment();
|
||||||
|
|
||||||
if (equipment == null) {
|
if (equipment == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -126,25 +120,19 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Locale locale() {
|
public @Nullable Locale locale() {
|
||||||
final @Nullable Player player = this.player();
|
return this.player().map(Player::locale).orElse(null);
|
||||||
|
|
||||||
if (player != null) {
|
|
||||||
return player.locale();
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessageAsPlayer(final String message) {
|
public void sendMessageAsPlayer(final String message) {
|
||||||
// TODO: ensure method is not executed from main thread
|
// TODO: ensure method is not executed from main thread
|
||||||
// bukkit doesn't like that
|
// bukkit doesn't like that
|
||||||
Objects.requireNonNull(this.player()).chat(message);
|
this.player().ifPresent(player -> player.chat(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean online() {
|
public boolean online() {
|
||||||
return this.player() != null;
|
return this.player().isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -154,13 +142,11 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
|||||||
|
|
||||||
// Supported by PremiumVanish, SuperVanish, VanishNoPacket
|
// Supported by PremiumVanish, SuperVanish, VanishNoPacket
|
||||||
private boolean hasVanishMeta() {
|
private boolean hasVanishMeta() {
|
||||||
for (final MetadataValue meta : this.player().getMetadata("vanished")) {
|
return this.player().stream()
|
||||||
if (meta.asBoolean()) {
|
.map(player -> player.getMetadata("vanished"))
|
||||||
return true;
|
.flatMap(Collection::stream)
|
||||||
}
|
.filter(value -> value.value() instanceof Boolean)
|
||||||
}
|
.anyMatch(MetadataValue::asBoolean);
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user