Skip to content

Commit

Permalink
fix: Provide Identity pointers for "offline" CarbonPlayers
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Apr 24, 2023
1 parent 4445e02 commit 6607ee2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.util;

import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.pointer.Pointers;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
public final class EmptyAudienceWithPointers implements ForwardingAudience.Single {

private final Pointers pointers;

private EmptyAudienceWithPointers(final Pointers pointers) {
this.pointers = pointers;
}

@Override
public Audience audience() {
return Audience.empty();
}

@Override
public Pointers pointers() {
return this.pointers;
}

public static EmptyAudienceWithPointers forCarbonPlayer(final CarbonPlayer player) {
return new EmptyAudienceWithPointers(Pointers.builder()
.withStatic(Identity.UUID, player.uuid())
.withStatic(Identity.NAME, player.username())
.withDynamic(Identity.DISPLAY_NAME, player::displayName)
.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.common.util.EmptyAudienceWithPointers;
import net.draycia.carbon.fabric.CarbonChatFabric;
import net.draycia.carbon.fabric.MinecraftServerHolder;
import net.kyori.adventure.audience.Audience;
Expand Down Expand Up @@ -59,7 +60,7 @@ public CarbonPlayerFabric(final CarbonPlayerCommon carbonPlayerCommon, final Min

@Override
public @NonNull Audience audience() {
return this.player().orElseThrow();
return this.player().map(p -> (Audience) p).orElseGet(() -> EmptyAudienceWithPointers.forCarbonPlayer(this));
}

public Optional<ServerPlayer> player() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.common.util.EmptyAudienceWithPointers;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.key.Key;
Expand Down Expand Up @@ -72,7 +73,7 @@ public CarbonPlayerCommon carbonPlayerCommon() {

@Override
public @NotNull Audience audience() {
return this.player().map(player -> (Audience) player).orElse(Audience.empty());
return this.player().map(player -> (Audience) player).orElseGet(() -> EmptyAudienceWithPointers.forCarbonPlayer(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.common.util.EmptyAudienceWithPointers;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.key.Key;
Expand All @@ -51,7 +52,7 @@ public CarbonPlayerVelocity(final ProxyServer server, final CarbonPlayerCommon c

@Override
public @NotNull Audience audience() {
return this.player().map(value -> (Audience) value).orElse(Audience.empty());
return this.player().map(value -> (Audience) value).orElseGet(() -> EmptyAudienceWithPointers.forCarbonPlayer(this));
}

@Override
Expand Down

0 comments on commit 6607ee2

Please sign in to comment.