Search in sources :

Example 1 with CustomInventory

use of org.spongepowered.common.inventory.custom.CustomInventory 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)

Aggregations

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 SpongeUserData (org.spongepowered.common.entity.player.SpongeUserData)1 CustomInventory (org.spongepowered.common.inventory.custom.CustomInventory)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1