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