use of org.spongepowered.api.ResourceKey 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.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeItemStackSnapshot method toContainer.
@Override
public DataContainer toContainer() {
final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.ITEM_TYPE).valueKey(this.itemType);
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, resourceKey).set(Constants.ItemStack.COUNT, this.quantity).set(Constants.ItemStack.DAMAGE_VALUE, this.damageValue);
if (!this.manipulators.isEmpty()) {
// TODO container.set(Constants.Sponge.DATA_MANIPULATORS, DataUtil.getSerializedImmutableManipulatorList(this.manipulators));
}
if (this.compound != null) {
container.set(Constants.Sponge.UNSAFE_NBT, NBTTranslator.INSTANCE.translateFrom(this.compound));
}
return container;
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeItemStackSnapshot method asHoverEvent.
@Override
public HoverEvent<HoverEvent.ShowItem> asHoverEvent(final UnaryOperator<HoverEvent.ShowItem> op) {
final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.ITEM_TYPE).valueKey(this.itemType);
final CompoundTag tag = this.getCompound().orElse(null);
return HoverEvent.showItem(op.apply(HoverEvent.ShowItem.of(resourceKey, this.quantity(), SpongeAdventure.asBinaryTagHolder(tag))));
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method worldKeys.
@Override
public List<ResourceKey> worldKeys() {
final List<ResourceKey> worldKeys = new ArrayList<>();
worldKeys.add((ResourceKey) (Object) Level.OVERWORLD.location());
if (Files.exists(this.defaultWorldDirectory.resolve(this.getDirectoryName((ResourceKey) (Object) Level.NETHER.location())))) {
worldKeys.add((ResourceKey) (Object) Level.NETHER.location());
}
if (Files.exists(this.defaultWorldDirectory.resolve(this.getDirectoryName((ResourceKey) (Object) Level.END.location())))) {
worldKeys.add((ResourceKey) (Object) Level.END.location());
}
try {
for (final Path namespacedDirectory : Files.walk(this.customWorldsDirectory, 1).collect(Collectors.toList())) {
if (this.customWorldsDirectory.equals(namespacedDirectory)) {
continue;
}
for (final Path valueDirectory : Files.walk(namespacedDirectory, 1).collect(Collectors.toList())) {
if (namespacedDirectory.equals(valueDirectory)) {
continue;
}
worldKeys.add(ResourceKey.of(namespacedDirectory.getFileName().toString(), valueDirectory.getFileName().toString()));
}
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
return Collections.unmodifiableList(worldKeys);
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method templateKeys.
@Override
public List<ResourceKey> templateKeys() {
final List<ResourceKey> templateKeys = new ArrayList<>();
// Treat Vanilla ones as template keys
templateKeys.add(WorldTypes.OVERWORLD.location());
templateKeys.add(WorldTypes.THE_NETHER.location());
templateKeys.add(WorldTypes.THE_END.location());
try (final Stream<Path> pluginDirectories = Files.walk(this.getDimensionDataPackDirectory(), 1)) {
pluginDirectories.filter(Files::isDirectory).forEach(pluginDirectory -> {
final Path dimensionPath = pluginDirectory.resolve("dimension");
if (Files.isDirectory(dimensionPath)) {
try (final Stream<Path> pluginTemplates = Files.walk(dimensionPath, 1)) {
pluginTemplates.filter(template -> template.toString().endsWith(".json")).forEach(template -> templateKeys.add((ResourceKey) (Object) new ResourceLocation(pluginDirectory.getFileName().toString(), FilenameUtils.removeExtension(template.getFileName().toString()))));
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
});
} catch (final IOException e) {
throw new RuntimeException(e);
}
return templateKeys;
}
Aggregations