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;
}
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;
}
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());
}
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);
}
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);
}
Aggregations