CarbonPlayerFabric#createItemHoverComponent

This commit is contained in:
Jason Penilla authored and Josua Parks committed 2021-11-16 20:46:15 -08:00
1 parent fec648de4b
commit 8c0bf7f7bb
1 file changed
+46 -9
@@ -1,10 +1,12 @@
package net.draycia.carbon.fabric.users;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.api.util.InventorySlots;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.fabric.CarbonChatFabric;
@@ -14,13 +16,13 @@ import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.kyori.adventure.platform.fabric.PlayerLocales;
import net.kyori.adventure.text.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
@DefaultQualifier(NonNull.class)
public class CarbonPlayerFabric extends WrappedCarbonPlayer implements ForwardingAudience.Single {
@@ -34,13 +36,16 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
@Override
public @NotNull Audience audience() {
final ServerPlayer player = this.player().get();
return FabricServerAudiences.of(player.server).audience(player);
return this.player()
.map(p -> FabricServerAudiences.of(p.server).audience(p))
.orElseThrow();
}
private Optional<ServerPlayer> player() {
return Optional.ofNullable(this.carbonChatFabric.minecraftServer().getPlayerList()
.getPlayer(this.carbonPlayerCommon.uuid()));
return Optional.ofNullable(
this.carbonChatFabric.minecraftServer().getPlayerList()
.getPlayer(this.carbonPlayerCommon.uuid())
);
}
@Override
@@ -55,7 +60,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
@Override
public @Nullable Locale locale() {
return PlayerLocales.locale(player().get());
return this.player().map(PlayerLocales::locale).orElse(null);
}
@Override
@@ -80,12 +85,44 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
return null;
final Optional<ServerPlayer> playerOptional = this.player();
if (playerOptional.isEmpty()) {
return null;
}
final ServerPlayer player = playerOptional.get();
final EquipmentSlot equipmentSlot;
if (slot.equals(InventorySlots.MAIN_HAND)) {
equipmentSlot = EquipmentSlot.MAINHAND;
} else if (slot.equals(InventorySlots.OFF_HAND)) {
equipmentSlot = EquipmentSlot.OFFHAND;
} else if (slot.equals(InventorySlots.HELMET)) {
equipmentSlot = EquipmentSlot.HEAD;
} else if (slot.equals(InventorySlots.CHEST)) {
equipmentSlot = EquipmentSlot.CHEST;
} else if (slot.equals(InventorySlots.LEGS)) {
equipmentSlot = EquipmentSlot.LEGS;
} else if (slot.equals(InventorySlots.BOOTS)) {
equipmentSlot = EquipmentSlot.FEET;
} else {
return null;
}
final @Nullable ItemStack item = player.getItemBySlot(equipmentSlot);
if (item == null || item.isEmpty()) {
return null;
}
return FabricServerAudiences.of(player.server).toAdventure(item.getDisplayName());
}
@Override
public boolean hasPermission(final String permission) {
return this.player().map(value -> Permissions.check(value, permission)).orElse(false);
return this.player()
.map(value -> Permissions.check(value, permission, value.server.getOperatorUserPermissionLevel()))
.orElse(false);
}
}