Search in sources :

Example 1 with SpongeUserData

use of org.spongepowered.common.entity.player.SpongeUserData in project SpongeCommon by SpongePowered.

the class InventoryUtil method getPluginContainer.

public static PluginContainer getPluginContainer(Object inventory) {
    // TODO maybe caching?
    final PluginContainer container;
    if (inventory instanceof CustomInventory) {
        return ((CustomInventory) inventory).getPlugin();
    }
    if (inventory instanceof CarriedInventory) {
        final Optional<?> carrier = ((CarriedInventory<?>) inventory).carrier();
        if (carrier.isPresent()) {
            inventory = carrier.get();
        }
    }
    final Object base = inventory;
    if (base instanceof BlockEntity) {
        final ResourceKey key = Sponge.game().registry(RegistryTypes.BLOCK_ENTITY_TYPE).valueKey(((BlockEntity) base).type());
        final String pluginId = key.namespace();
        container = Sponge.pluginManager().plugin(pluginId).orElseThrow(() -> new AssertionError("Missing plugin " + pluginId + " for block " + key.namespace() + ":" + key.value()));
    } else if (base instanceof Entity) {
        final ResourceKey key = (ResourceKey) (Object) EntityType.getKey((EntityType<?>) ((Entity) base).type());
        final String pluginId = key.namespace();
        container = Sponge.pluginManager().plugin(pluginId).orElseGet(() -> {
            SpongeCommon.logger().debug("Unknown plugin for [{}]", base);
            return Launch.instance().minecraftPlugin();
        });
    } else if (base instanceof SpongeUserData) {
        container = Launch.instance().minecraftPlugin();
    } else {
        container = Sponge.pluginManager().plugin(PlatformHooks.INSTANCE.getInventoryHooks().getModIdFromInventory(base.getClass())).orElseGet(() -> {
            SpongeCommon.logger().debug("Unknown plugin for [{}]", base);
            return Launch.instance().minecraftPlugin();
        });
    }
    return container;
}
Also used : EntityType(net.minecraft.world.entity.EntityType) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) Entity(org.spongepowered.api.entity.Entity) PluginContainer(org.spongepowered.plugin.PluginContainer) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) SpongeUserData(org.spongepowered.common.entity.player.SpongeUserData) CustomInventory(org.spongepowered.common.inventory.custom.CustomInventory) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) ResourceKey(org.spongepowered.api.ResourceKey)

Example 2 with SpongeUserData

use of org.spongepowered.common.entity.player.SpongeUserData in project SpongeCommon by SpongePowered.

the class SpongeUserManager method removeFromCache.

@Override
public boolean removeFromCache(final UUID uuid) {
    @Nullable final SpongeUserData data = this.userCache.getIfPresent(uuid);
    if (data != null) {
        this.dirtyUsers.remove(data);
        this.userCache.invalidate(uuid);
        return true;
    }
    return false;
}
Also used : SpongeUserData(org.spongepowered.common.entity.player.SpongeUserData) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with SpongeUserData

use of org.spongepowered.common.entity.player.SpongeUserData in project SpongeCommon by SpongePowered.

the class SpongeUserManager method createUser.

private void createUser(final com.mojang.authlib.GameProfile profile) throws IOException {
    this.pollFilesystemWatcher();
    @Nullable final SpongeUserData user = SpongeUserData.create(profile);
    this.userCache.put(profile.getId(), user);
    this.knownUUIDs.add(profile.getId());
}
Also used : SpongeUserData(org.spongepowered.common.entity.player.SpongeUserData) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 4 with SpongeUserData

use of org.spongepowered.common.entity.player.SpongeUserData in project SpongeCommon by SpongePowered.

the class SpongeUserManager method fetchUser.

private CompletableFuture<@Nullable User> fetchUser(final UUID uniqueId, final boolean always) {
    final UUID uuidToUse = this.ensureNonEmptyUUID(uniqueId);
    if (this.server.getPlayerList().getPlayer(uniqueId) != null) {
        return CompletableFuture.completedFuture(SpongeUserView.create(uniqueId));
    }
    @Nullable final SpongeUserData currentUser = this.userCache.getIfPresent(uuidToUse);
    if (currentUser != null) {
        return CompletableFuture.completedFuture(SpongeUserView.create(uuidToUse));
    }
    return CompletableFuture.supplyAsync(() -> {
        if (always || this.knownUUIDs.contains(uuidToUse)) {
            final com.mojang.authlib.@Nullable GameProfile profile = this.server.getProfileCache().get(uuidToUse);
            try {
                this.createUser(profile == null ? new com.mojang.authlib.GameProfile(uuidToUse, null) : profile);
            } catch (final IOException e) {
                throw new CompletionException(e);
            }
            return SpongeUserView.create(uuidToUse);
        }
        return null;
    }, this.executorService);
}
Also used : SpongeGameProfile(org.spongepowered.common.profile.SpongeGameProfile) GameProfile(org.spongepowered.api.profile.GameProfile) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) UUID(java.util.UUID) SpongeUserData(org.spongepowered.common.entity.player.SpongeUserData) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 5 with SpongeUserData

use of org.spongepowered.common.entity.player.SpongeUserData in project SpongeCommon by SpongePowered.

the class SpongeUserManager method delete.

@Override
public CompletableFuture<Boolean> delete(final UUID uuid) {
    if (SpongeCommon.server().getPlayerList().getPlayer(Objects.requireNonNull(uuid, "uuid")) != null) {
        // cannot delete live player.
        return CompletableFuture.completedFuture(false);
    }
    return CompletableFuture.supplyAsync(() -> {
        @Nullable final Path dataFile = this.getPlayerDataFile(uuid);
        if (dataFile != null) {
            try {
                if (Files.deleteIfExists(dataFile)) {
                    @Nullable final SpongeUserData data = this.userCache.getIfPresent(uuid);
                    if (data != null) {
                        this.dirtyUsers.remove(data);
                    }
                    this.userCache.invalidate(uuid);
                }
            } catch (final SecurityException | IOException e) {
                SpongeCommon.logger().warn("Unable to delete file {}", dataFile, e);
                return false;
            }
        }
        return true;
    }, this.executorService);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) SpongeUserData(org.spongepowered.common.entity.player.SpongeUserData) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

SpongeUserData (org.spongepowered.common.entity.player.SpongeUserData)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)4 IOException (java.io.IOException)2 Path (java.nio.file.Path)1 UUID (java.util.UUID)1 CompletionException (java.util.concurrent.CompletionException)1 EntityType (net.minecraft.world.entity.EntityType)1 ChestBlockEntity (net.minecraft.world.level.block.entity.ChestBlockEntity)1 ResourceKey (org.spongepowered.api.ResourceKey)1 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)1 Entity (org.spongepowered.api.entity.Entity)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1 GameProfile (org.spongepowered.api.profile.GameProfile)1 CustomInventory (org.spongepowered.common.inventory.custom.CustomInventory)1 SpongeGameProfile (org.spongepowered.common.profile.SpongeGameProfile)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1